Пример #1
0
 public Game(IPlayer whitePlayer, IPlayer blackPlayer,
             Action <Board, PiecesColor> onNewTurn,
             Func <Board, PiecesColor, Move, bool> onMoveConfirm)
 {
     Board         = new Board();
     WhitePlayer   = whitePlayer;
     BlackPlayer   = blackPlayer;
     OnNewTurn     = onNewTurn;
     OnMoveConfirm = onMoveConfirm;
     TurnColor     = PiecesColor.White;
 }
Пример #2
0
        public bool IsPositionUnderCheck(Board board, PiecesColor turnColor, Position pos)
        {
            foreach (var(_, srcPos) in board.PiecesPositions[turnColor])
            {
                var move = new Move(srcPos, pos);
                if (Validate(board, turnColor, move) == MoveValidationResult.Valid)
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #3
0
        public Move?GetMove(UserPlayer player, Board board, PiecesColor turnColor)
        {
            var move = player.OnMoveRequest(board, turnColor);

            if (move != null)
            {
                var validationResult = _moveValidationService.Validate(board, turnColor, move);
                player.OnMoveValidated(move, validationResult);

                return(validationResult switch
                {
                    MoveValidationResult.Valid => move,
                    _ => GetMove(player, board, turnColor)
                });
Пример #4
0
        public List <CpuMove> GetValidCpuMoves(Board board, PiecesColor turnColor)
        {
            var validMoves = new List <CpuMove>();

            foreach (var(piece, srcPos) in board.PiecesPositions[turnColor])
            {
                foreach (var move in GetPotentialDestinations(piece, srcPos)
                         .Select(dstPos => new CpuMove(srcPos, dstPos))
                         .Where(move =>
                                _moveValidationService.Validate(board, turnColor, move)
                                == MoveValidationResult.Valid))
                {
                    move.Value = EvaluateMoveValue(board, move);
                    validMoves.Add(move);
                }
            }

            return(validMoves);
        }
Пример #5
0
 public bool CheckIfCheck(PiecesColor color)
 {
     foreach (ChessPieceBase chessPiece in ChessPieces)
     {
         List <Vector3> moves = chessPiece?.GetMoves();
         if (moves?.Count > 0)
         {
             foreach (Vector3 position in moves)
             {
                 ChessPieceBase piece = CheckIfPositionOccupied(position);
                 if (piece?.Type == PiecesType.King && chessPiece.isActiveAndEnabled && piece?.Color == color)
                 {
                     Debug.Log("CHECK!!!");
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
Пример #6
0
        public MoveValidationResult Validate(Board board, PiecesColor turnColor, Move move)
        {
            var srcPiece = board[move.Src];
            var dstPiece = board[move.Dst];

            if (srcPiece != null && srcPiece.Color == turnColor)
            {
                if (dstPiece == null || dstPiece.Color != turnColor)
                {
                    if (IsMoveValidForPiece(move, srcPiece, dstPiece != null))
                    {
                        if (srcPiece is Knight || IsMoveValidForPath(board, move))
                        {
                            if (!IsKingUnderCheck(board, turnColor, move))
                            {
                                return(MoveValidationResult.Valid);
                            }

                            return(MoveValidationResult.KingUnderCheck);
                        }

                        return(MoveValidationResult.InvalidPath);
                    }

                    if (srcPiece is King king)
                    {
                        return(ValidateCastling(board, turnColor, move, king));
                    }

                    return(MoveValidationResult.InvalidMove);
                }

                return(MoveValidationResult.InvalidDst);
            }

            return(MoveValidationResult.InvalidSrc);
        }
Пример #7
0
 private Move?GetMove(IPlayer player, Board board, PiecesColor turnColor) =>
 player switch
 {
Пример #8
0
 public Pawn(PiecesColor color)
     : base(color, color == PiecesColor.White ? 'P' : 'p', 1)
 {
 }
Пример #9
0
 public NewGameConfig(PiecesColor userColor, int recursionDepth)
 {
     UserColor      = userColor;
     RecursionDepth = recursionDepth;
 }
Пример #10
0
 public void Deconstruct(out PiecesColor userColor, out int recursionDepth)
 {
     userColor      = UserColor;
     recursionDepth = RecursionDepth;
 }
Пример #11
0
        private CpuMove?GetBestMove(CpuPlayer player, Board board, PiecesColor turnColor,
                                    int recursionLevel, int recursionDepth)
        {
            void EvaluateMove(CpuMove move)
            {
                var tempBoard = new Board(board);

                tempBoard.ApplyMove(move);

                if (recursionLevel < recursionDepth)
                {
                    var response = GetBestMove(player, tempBoard, turnColor.Invert(),
                                               recursionLevel + 1, recursionDepth);

                    move.Response = response ?? (ICpuMoveResponse?)new CpuMoveCheckMate();
                }
                else if (_moveValidationService.IsPositionUnderCheck(tempBoard,
                                                                     turnColor.Invert(), move.Dst))
                {
                    move.Response = new CpuMoveCheck(tempBoard[move.Dst]?.Value ?? 0);
                }
                else
                {
                    move.Response = new CpuMoveUnknown();
                }

                move.Value -= move.Response?.Value ?? 0;
                if (recursionLevel == 0)
                {
                    move.Value += EvaluateBoardCoverageValue(tempBoard, turnColor);
                }
            }

            var startTime = DateTime.Now;
            var moves     = _moveEvaluationService.GetValidCpuMoves(board, turnColor);

            if (!moves.Any())
            {
                return(null);
            }

            if (recursionLevel == 0)
            {
                Parallel.ForEach(moves, EvaluateMove);
            }
            else
            {
                foreach (var move in moves)
                {
                    EvaluateMove(move);
                }
            }

            var bestValue = moves.Max(move => move.Value);
            var bestMoves = moves.Where(move => move.Value == bestValue).ToArray();
            var bestMove  = bestMoves[_random.Next(bestMoves.Length)];

            if (recursionLevel <= MaxNotifiableLevel)
            {
                player.OnMoveEvaluated(recursionLevel,
                                       moves, bestMove, DateTime.Now - startTime);
            }

            return(bestMove);
        }
Пример #12
0
 public Move?GetMove(CpuPlayer player, Board board, PiecesColor turnColor) =>
 GetBestMove(player, board, turnColor, 0, player.RecursionDepth);
Пример #13
0
 public void SetColor(PiecesColor color)
 {
     _color = color;
 }
Пример #14
0
 public Queen(PiecesColor color)
     : base(color, color == PiecesColor.White ? 'Q' : 'q', 10)
 {
 }
Пример #15
0
        private decimal EvaluateBoardCoverageValue(Board board, PiecesColor turnColor)
        {
            var coverage = _moveEvaluationService.GetValidCpuMoves(board, turnColor);

            return(Math.Round(0.01m * coverage.Count, 1));
        }
Пример #16
0
 protected Piece(PiecesColor color, char symbol, int value)
 {
     Color  = color;
     Symbol = symbol;
     Value  = value;
 }
Пример #17
0
 public Rook(PiecesColor color)
     : base(color, color == PiecesColor.White ? 'R' : 'r', 5)
 {
 }
Пример #18
0
 public GameSettings(PiecesColor col, Difficulty diff)
 {
     color      = col;
     difficulty = diff;
 }
Пример #19
0
 public Knight(PiecesColor color)
     : base(color, color == PiecesColor.White ? 'N' : 'n', 4)
 {
 }
Пример #20
0
 protected CastlingPiece(PiecesColor color, char symbol, int value)
     : base(color, symbol, value)
 {
 }
Пример #21
0
 public Bishop(PiecesColor color)
     : base(color, color == PiecesColor.White ? 'B' : 'b', 3)
 {
 }
Пример #22
0
 public King(PiecesColor color)
     : base(color, color == PiecesColor.White ? 'K' : 'k', 0)
 {
 }
Пример #23
0
 public static PiecesColor Invert(this PiecesColor color) =>
 color == PiecesColor.White
         ? PiecesColor.Black
         : PiecesColor.White;