예제 #1
0
        public Move MakeMove(PieceColor color)
        {
            try
            {
                var startTime = DateTime.Now;
                var board     = Board.DeepClone();
                switch (color)
                {
                case PieceColor.White:
                    Board.LastMove = Board.MakeMove(WhitePlayerEngine.MakeMove(Board, Variant, GameMoves));
                    break;

                case PieceColor.Black:
                    Board.LastMove = Board.MakeMove(BlackPlayerEngine.MakeMove(Board, Variant, GameMoves));
                    break;
                }
                var endTime = DateTime.Now;
                History.Add(new HistoryBoard(startTime, endTime, board, color));
                if (Board.BoardArray.Where(p => p < 0).Count() == 0)
                {
                    throw new NoAvailablePiecesException(PieceColor.Black, Board.LastMove);
                }
                if (Board.BoardArray.Where(p => p > 0).Count() == 0)
                {
                    throw new NoAvailablePiecesException(PieceColor.White, Board.LastMove);
                }
                GameMoves.Add(Board.LastMove);
                if (IsDraw())
                {
                    throw new DrawException();
                }
                return(Board.LastMove);
            }
            catch (NotAvailableMoveException exception)
            {
                string winner = "";
                winner = exception.Color == PieceColor.Black ? "W" : "B";
                AddGameToDatabase(winner);
                throw;
            }
            catch (NoAvailablePiecesException exception)
            {
                string winner = "";
                switch (exception.Color)
                {
                case PieceColor.White when Variant == GameVariant.Checkers:
                case PieceColor.Black when Variant == GameVariant.Anticheckers:
                    winner = "B";
                    break;

                case PieceColor.Black when Variant == GameVariant.Checkers:
                case PieceColor.White when Variant == GameVariant.Anticheckers:
                    winner = "W";
                    break;
                }
                AddGameToDatabase(winner);
                throw;
            }
            catch (DrawException)
            {
                AddGameToDatabase("D");
                throw;
            }
            catch
            {
                throw;
            }
        }
예제 #2
0
        private void AddGameToDatabase(string winner)
        {
            player_information whitePlayerInformation = new player_information()
            {
                algorithm = new algorithm()
                {
                    algorithm_name = WhitePlayerEngine.Kind.ToString()
                },
                number_of_pieces = Board.NumberOfWhitePiecesAtBeggining,
                player           = WhitePlayerEngine.Kind == EngineKind.Human ? new player()
                {
                    player_name = "syntaximus"
                } : new player()
                {
                    player_name = "CPU"
                }
            };
            player_information blackPlayerInformation = new player_information()
            {
                algorithm = new algorithm()
                {
                    algorithm_name = BlackPlayerEngine.Kind.ToString()
                },
                number_of_pieces = Board.NumberOfBlackPiecesAtBeggining,
                player           = BlackPlayerEngine.Kind == EngineKind.Human ? new player()
                {
                    player_name = "syntaximus"
                } : new player()
                {
                    player_name = "CPU"
                }
            };

            if (WhitePlayerEngine.GetType() == typeof(AlphaBetaEngine))
            {
                var engine = (AlphaBetaEngine)WhitePlayerEngine;
                whitePlayerInformation.tree_depth = engine.AlphaBetaTreeDepth;
            }
            if (BlackPlayerEngine.GetType() == typeof(AlphaBetaEngine))
            {
                var engine = (AlphaBetaEngine)BlackPlayerEngine;
                blackPlayerInformation.tree_depth = engine.AlphaBetaTreeDepth;
            }
            if (WhitePlayerEngine.GetType() == typeof(MctsEngine))
            {
                var engine = (MctsEngine)WhitePlayerEngine;
                whitePlayerInformation.uct_parameter        = engine.UctParameter;
                whitePlayerInformation.number_of_iterations = engine.NumberOfIterations;
            }
            if (BlackPlayerEngine.GetType() == typeof(MctsEngine))
            {
                var engine = (MctsEngine)BlackPlayerEngine;
                blackPlayerInformation.uct_parameter        = engine.UctParameter;
                blackPlayerInformation.number_of_iterations = engine.NumberOfIterations;
            }
            game_type gameType = new game_type()
            {
                game_type_name = Variant.ToString()
            };
            List <game_move> gameMoves = new List <game_move>();

            foreach (var move in History.Skip(1))
            {
                var gameMove = new game_move()
                {
                    player              = move.PieceColor == PieceColor.White ? "W" : "B",
                    start_time          = move.StartTime,
                    end_time            = move.EndTime,
                    from_position       = move.Board.LastMove.OldPiece.Position,
                    to_position         = move.Board.LastMove.NewPiece.Position,
                    beated_pieces_count = move.Board.LastMove.BeatedPieces?.Count ?? 0,
                    beated_pieces       = move.Board.LastMove.GetBeatedPiecesString(),
                    board_after_move    = move.Board.ToString()
                };
                gameMoves.Add(gameMove);
            }
            int moveCount = History.Count;

            _databaseLayer.AddGame(whitePlayerInformation, blackPlayerInformation, gameType, gameMoves, Board.Size, winner, moveCount, StartDate);
        }