Exemplo n.º 1
0
        /*
         * Determines if the last move caused the game to end
         *
         * First, check if the king is in check, and store it in a boolean variable.
         *
         * If the king is in check, check if the attack is preventable. This is done
         * by seeing it the king can either move away safely or can neutralize all
         * attacks (both the primary attack and discoveryCheck if it occured). If the
         * king is in check and the conditions checking if the attempted capture
         * is preventable, checkmate has occured, otherwise the game is not over and the
         * King is simply in check.
         *
         * If the king is not in check, determine if any of the various draw conditions are
         * met, including by repetition, redundancy (x moves without a capture or pawn movement),
         * stalemate (where a player can not make a legal move) and by material (where neither side
         * has enough material to checkmate).
         *
         * If none of those conditions are met, the game can still be played, and is
         * considered to have normal status.
         */
        private void IsGameOver(ChessMove.MoveStatusCodes moveType, BoardSquare directAttacker)
        {
            // Check if king is in check (discovered, normal)
            // Be sure to account for rook when castled, removed pawn in en passant

            // If one or the other, check for blocks, capture, move out of way
            // If both, check move out of way
            // If neither, check other pieces can move (stalemate)
            // Check draw by repetition, inaction (no capture/movement), and material

            // Set gameStatus internal variable as appropriate

            BoardSquare currKingPos = this.kingPos[(int)(whiteTurn ? Sides.white : Sides.black)];
            BitBoard futureMoves = this.board[directAttacker].getAllMoves(this.board, (short)directAttacker.X, (short)directAttacker.Y);
            bool isDirectCheck = futureMoves.Contains(currKingPos);
            bool isDiscoverCheck;

            this.attackingPieces[(int)AttackTypes.discovery] = ExtrapolateAttack((short)this.startCoord.X, (short)this.startCoord.Y, (short)currKingPos.X, (short)currKingPos.Y);
            this.attackingPieces[(int)AttackTypes.regular] = isDirectCheck ? endCoord : BoardSquare.Empty;

            isDiscoverCheck = this.attackingPieces[(int)AttackTypes.discovery] != BoardSquare.Empty;

            if (!isDiscoverCheck)
            {
                BoardSquare lastKingPos = this.kingPos[(int)(whiteTurn ? Sides.black : Sides.white)];

                if (moveType == ChessMove.MoveStatusCodes.queenCastle)
                {
                    isDiscoverCheck = this[lastKingPos.Y, lastKingPos.X + 1].getAllMoves(this.board, (short)(this.endCoord.X + 1), (short)this.endCoord.Y).Contains(currKingPos);
                    if (isDiscoverCheck)
                        this.attackingPieces[(int)AttackTypes.discovery] = new BoardSquare(lastKingPos.X + 1, lastKingPos.Y);
                }
                else if (moveType == ChessMove.MoveStatusCodes.queenCastle)
                {
                    isDiscoverCheck = this[lastKingPos.Y, lastKingPos.X - 1].getAllMoves(this.board, (short)(this.endCoord.X - 1), (short)this.endCoord.Y).Contains(currKingPos);
                    if (isDiscoverCheck)
                        this.attackingPieces[(int)AttackTypes.discovery] = new BoardSquare(lastKingPos.X - 1, lastKingPos.Y);
                }
                else if (moveType == ChessMove.MoveStatusCodes.enPassant)
                {
                    // Check captured square
                    this.attackingPieces[(int)AttackTypes.discovery] = ExtrapolateAttack((short)endCoord.X, (short)startCoord.Y, (short)currKingPos.X, (short)currKingPos.Y);
                    isDiscoverCheck = this.attackingPieces[(int)AttackTypes.discovery] != BoardSquare.Empty;
                }
            }

            if (isDirectCheck || isDiscoverCheck)
            {
                this.gameStatus = GameStatusCodes.check;

                // If king can't move safely, check alternatives
                if (this.board[currKingPos].getAllMoves(this.board, (short)currKingPos.X, (short)currKingPos.Y) == BitBoard.Empty)
                {
                    // Double attack, can only get out of it by moving
                    if (isDirectCheck && isDiscoverCheck)
                        this.gameStatus = GameStatusCodes.checkMate;
                    else
                    {
                        BoardSquare attackSquare = BoardSquare.Empty;

                        bool canBlockCap = false;

                        if (isDirectCheck) attackSquare = this.attackingPieces[(int)AttackTypes.regular];
                        else attackSquare = this.attackingPieces[(int)AttackTypes.discovery];

                        // Check for blocking, capture
                        foreach (BoardSquare sq in this.board)
                        {
                            Piece p = this.board[sq];

                            if (p.Colour == this.board[currKingPos].Colour)
                            {
                                BitBoard bbMoves = p.getAllMoves(this.board, (short)sq.X, (short)sq.Y);
                                foreach (BoardSquare sqMove in bbMoves)
                                {
                                    // Can block or capture
                                    if (sqMove == attackSquare || (!(board[sq] is King) && sqMove.IsBetweenPoints(currKingPos, attackSquare))
                                        || board[sq] is Pawn && ((Pawn)board[sq]).MayEnPassant(this.board, (short)sq.X, (short)sq.Y, (short)sqMove.X, (short)sqMove.Y))
                                    {
                                        canBlockCap = true;
                                        break;
                                    }
                                }

                                if (canBlockCap)
                                    break;
                            }
                        }

                        if (!canBlockCap)
                            this.gameStatus = GameStatusCodes.checkMate;
                    }
                }
            }
            else
            {
                gameStatus = GameStatusCodes.normal;

                //Check for draw conditions (stalemate, repetition, inactivity (no capture/pawn), material
                if (this.positions.isDrawByMoves())
                    this.gameStatus = GameStatusCodes.forcedDraw;
                else if (this.lastMove.IsCapture && IsDrawByMaterial())
                    this.gameStatus = GameStatusCodes.drawByMaterial;
                else
                {
                    bool isStalemate = true;
                    char nextMoveCol = whiteTurn ? Piece.NOTATION_W : Piece.NOTATION_B;

                    foreach (BoardSquare sq in this.board)
                        if (this.board[sq].Colour == nextMoveCol && this.board[sq].getAllMoves(this.board, (short)sq.X, (short)sq.Y) != BitBoard.Empty)
                        {
                            isStalemate = false;
                            break;
                        }

                    if (isStalemate)
                        this.gameStatus = GameStatusCodes.stalemate;
                }
            }
        }
Exemplo n.º 2
0
        /*
         * Standard set of actions that occur when a valid turn takes takes place.
         *
         * The two arguments, currPos and newPos, are character representations
         * of the original and new positions of the moved piece respectively
         *
         * First, the board array is updated.
         * Second, switch internal track of move, and record the last move.
         * Third, update the array of repeatable board positions, clear discoverCheckPiece, and increment the move count for draws
         * Fourth of all, if the piece that moved was the king, update his board position in the array
         * Lastly, check to see if this move causes the game to end
         */
        private void DoTurn(ChessMove.MoveStatusCodes moveType)
        {
            BoardSquare directAttacker = endCoord;

            switch (moveType)
            {
                case ChessMove.MoveStatusCodes.enPassant:
                    board[lastMove.EndSquare.Y, lastMove.EndSquare.X] = null;
                    break;
                case ChessMove.MoveStatusCodes.kingCastle:
                    board.movePiece(Board.NUM_FILES-1, startCoord.Y, startCoord.X + 1, startCoord.Y);
                    directAttacker = new BoardSquare(startCoord.X + 1, startCoord.Y);
                    break;
                case ChessMove.MoveStatusCodes.queenCastle:
                    board.movePiece(0, startCoord.Y, startCoord.X - 1, startCoord.Y);
                    directAttacker = new BoardSquare(startCoord.X - 1, startCoord.Y);
                    break;
            }

            // Update internal board position and move trackers
            lastMove = new ChessMove(startCoord, endCoord, board[startCoord].Notational, board[endCoord] != null);
            allMoves.Add(lastMove);

            board.movePiece(startCoord.X, startCoord.Y, endCoord.X, endCoord.Y);

            // Switch who's move it is and record move
            whiteTurn = !whiteTurn;

            // Update internal move-by-move trackers
            positions.add(lastMove, board);

            // Update position of king if king moved
            if (lastMove.PieceMoved == (char)Piece.PieceNotation.King)
            {
                kingPos[Convert.ToInt32(whiteTurn)] = endCoord;
                ((King)board[endCoord]).HasNotMoved = false;
            }

            if (moveType != ChessMove.MoveStatusCodes.pawnPromote)
                IsGameOver(moveType, directAttacker);
        }