Exemplo n.º 1
0
        public bool ValidateMove(ChessBitBoard bitBoard, ChessMove moveToCheck, ChessColor colorOfPlayerMoving)
        {
            // When playing against an AI opponent, the framework will only call this method
            // if the AI didn't declare a stalemate or go over its time. This means we're
            // guaranteed to have a move and only need to check for check and stalemate.

            // We'll still check for stalemate in case it's a human player though.
            if (moveToCheck.Flag == ChessFlag.Stalemate)
            {
                var allWhiteMoves = new Queue <Move>();
                var allBlackMoves = new Queue <Move>();

                bitBoard.getAllValidMoves(Team.White, allWhiteMoves);
                bitBoard.getAllValidMoves(Team.Black, allBlackMoves);

                return(allWhiteMoves.Count == 0 && allBlackMoves.Count == 0);
            }

            // convert from the frameworks coordinates to cartesian
            var srcy  = 7 - moveToCheck.From.Y;
            var desty = 7 - moveToCheck.To.Y;

            var team       = colorOfPlayerMoving == ChessColor.White ? Team.White : Team.Black;
            var enemyTeam  = team == Team.White ? Team.Black : Team.White;
            var actualTeam = bitBoard.getPieceTeam(moveToCheck.From.X, srcy);

            if (team != actualTeam)
            {
                return(false);
            }

            if (!bitBoard.validateMove(moveToCheck.From.X, srcy, moveToCheck.To.X, desty))
            {
                return(false);
            }

            bitBoard.move(moveToCheck.From.X, srcy, moveToCheck.To.X, desty);

            if (bitBoard.lastMove().causesCheck)
            {
                var allEnemyMoves = new Queue <Move>();
                bitBoard.getAllValidMoves(enemyTeam, allEnemyMoves);

                if (allEnemyMoves.Count == 0)
                {
                    return(moveToCheck.Flag == ChessFlag.Checkmate);
                }
                else
                {
                    return(moveToCheck.Flag == ChessFlag.Check);
                }
            }

            return(moveToCheck.Flag == ChessFlag.NoFlag);
        }
Exemplo n.º 2
0
        public static ChessBoard ToChessBoard(this ChessBitBoard bitBoard)
        {
            var chessBoard = new ChessBoard();

            for (int x = 0; x < 8; ++x)
            {
                for (int y = 0; y < 8; ++y)
                {
                    var pieceTeam = bitBoard.getPieceTeam(x, y);
                    var pieceType = bitBoard.getPieceType(x, y);

                    var y2 = 7 - y;

                    if (pieceTeam == Team.Black)
                    {
                        switch (pieceType)
                        {
                        case Piece.Bishop:
                            chessBoard[x, y2] = ChessPiece.BlackBishop;
                            break;

                        case Piece.King:
                            chessBoard[x, y2] = ChessPiece.BlackKing;
                            break;

                        case Piece.Knight:
                            chessBoard[x, y2] = ChessPiece.BlackKnight;
                            break;

                        case Piece.Pawn:
                            chessBoard[x, y2] = ChessPiece.BlackPawn;
                            break;

                        case Piece.Queen:
                            chessBoard[x, y2] = ChessPiece.BlackQueen;
                            break;

                        case Piece.Rook:
                            chessBoard[x, y2] = ChessPiece.BlackRook;
                            break;

                        default:
                            chessBoard[x, y2] = ChessPiece.Empty;
                            break;
                        }
                    }
                    else
                    {
                        switch (pieceType)
                        {
                        case Piece.Bishop:
                            chessBoard[x, y2] = ChessPiece.WhiteBishop;
                            break;

                        case Piece.King:
                            chessBoard[x, y2] = ChessPiece.WhiteKing;
                            break;

                        case Piece.Knight:
                            chessBoard[x, y2] = ChessPiece.WhiteKnight;
                            break;

                        case Piece.Pawn:
                            chessBoard[x, y2] = ChessPiece.WhitePawn;
                            break;

                        case Piece.Queen:
                            chessBoard[x, y2] = ChessPiece.WhiteQueen;
                            break;

                        case Piece.Rook:
                            chessBoard[x, y2] = ChessPiece.WhiteRook;
                            break;

                        default:
                            chessBoard[x, y2] = ChessPiece.Empty;
                            break;
                        }
                    }
                }
            }

            return(chessBoard);
        }