Exemplo n.º 1
0
        public Piece ExecuteMove(Position origin, Position destiny)
        {
            Piece p = GameBoard.removePiece(origin);

            p.incrementMoveCount();
            Piece capturedPiece = GameBoard.removePiece(destiny);

            GameBoard.placePiece(p, destiny);
            if (capturedPiece != null)
            {
                captured.Add(capturedPiece);
            }

            // #specialmove short King's Indian Defense
            if (p is King && destiny.Column == origin.Column + 2)
            {
                Position originRook  = new Position(origin.Line, origin.Column + 3);
                Position destinyRook = new Position(origin.Line, origin.Column + 1);
                Piece    rook        = GameBoard.removePiece(originRook);
                rook.incrementMoveCount();
                GameBoard.placePiece(rook, destinyRook);
            }

            // #specialmove long King's Indian Defense
            if (p is King && destiny.Column == origin.Column - 2)
            {
                Position originRook  = new Position(origin.Line, origin.Column - 4);
                Position destinyRook = new Position(origin.Line, origin.Column - 1);
                Piece    rook        = GameBoard.removePiece(originRook);
                rook.incrementMoveCount();
                GameBoard.placePiece(rook, destinyRook);
            }

            // #specialmove En Passant
            if (p is Pawn)
            {
                if (origin.Column != destiny.Column && capturedPiece == null)
                {
                    Position posPawn;
                    if (p.color == Color.White)
                    {
                        posPawn = new Position(destiny.Line + 1, destiny.Column);
                    }
                    else
                    {
                        posPawn = new Position(destiny.Line - 1, destiny.Column);
                    }

                    capturedPiece = GameBoard.removePiece(posPawn);
                    captured.Add(capturedPiece);
                }
            }

            return(capturedPiece);
        }
Exemplo n.º 2
0
        public void MakePlay(Position origin, Position destiny)
        {
            Piece piece = ExecuteMove(origin, destiny);

            if (KingInCheck(CurrentPlayer))
            {
                UndoMove(origin, destiny, piece);
                throw new BoardException("You can't put yourself in check!");
            }

            Piece p = GameBoard.piece(destiny);

            // #specialmove Promote pawn
            if (p is Pawn)
            {
                if ((p.color == Color.White && destiny.Line == 0) || (p.color == Color.Black && destiny.Line == 7))
                {
                    p = GameBoard.removePiece(destiny);
                    gamePieces.Remove(p);
                    Piece queen = new Queen(GameBoard, p.color);
                    GameBoard.placePiece(queen, destiny);
                    gamePieces.Add(queen);
                }
            }

            if (KingInCheck(rival(CurrentPlayer)))
            {
                Check = true;
            }
            else
            {
                Check = false;
            }

            if (testCheckMate(rival(CurrentPlayer)))
            {
                Finished = true;
            }
            else
            {
                Turn++;
                changePlayer();
            }

            // #specialmove En Passant
            if (p is Pawn && (destiny.Line == origin.Line + 2 || destiny.Line == origin.Line - 2))
            {
                enPassantDanger = p;
            }
            else
            {
                enPassantDanger = null;
            }
        }
Exemplo n.º 3
0
        public void UndoMove(Position origin, Position destiny, Piece capturedPiece)
        {
            Piece p = GameBoard.removePiece(destiny);

            p.decrementMoveCount();
            if (capturedPiece != null)
            {
                GameBoard.placePiece(capturedPiece, destiny);
                captured.Remove(capturedPiece);
            }
            GameBoard.placePiece(p, origin);

            // #specialmove short King's Indian Defense
            if (p is King && destiny.Column == origin.Column + 2)
            {
                Position originRook  = new Position(origin.Line, origin.Column + 3);
                Position destinyRook = new Position(origin.Line, origin.Column + 1);
                Piece    rook        = GameBoard.removePiece(destinyRook);
                rook.decrementMoveCount();
                GameBoard.placePiece(rook, originRook);
            }

            // #specialmove long King's Indian Defense
            if (p is King && destiny.Column == origin.Column - 2)
            {
                Position originRook  = new Position(origin.Line, origin.Column - 4);
                Position destinyRook = new Position(origin.Line, origin.Column - 1);
                Piece    rook        = GameBoard.removePiece(destinyRook);
                rook.decrementMoveCount();
                GameBoard.placePiece(rook, originRook);
            }

            // #specialmove En Passant
            if (p is Pawn)
            {
                if (origin.Column != destiny.Column && capturedPiece == enPassantDanger)
                {
                    Piece    pawn = GameBoard.removePiece(destiny);
                    Position posPawn;
                    if (p.color == Color.White)
                    {
                        posPawn = new Position(3, destiny.Column);
                    }
                    else
                    {
                        posPawn = new Position(4, destiny.Column);
                    }
                    GameBoard.placePiece(pawn, posPawn);
                }
            }
        }
Exemplo n.º 4
0
 public void placeNewPiece(char column, int line, Piece piece)
 {
     GameBoard.placePiece(piece, new ChessPosition(column, line).ToPosition());
     gamePieces.Add(piece);
 }