コード例 #1
0
 public void ValidateDestinationPosition(Position origin, Position destination)
 {
     if (!Gboard.GetPiece(origin).CanMoveTo(destination))
     {
         throw new GameBoardException("Impossible movement.");
     }
 }
コード例 #2
0
        public void UndoMovement(Position origin, Position destination, Piece capturedPiece)
        {
            //  Lift up the piece at destination and storing it to use after
            Piece pPicked = Gboard.LiftPiece(destination);

            //
            if (capturedPiece != null)
            {
                //  Give captured piece back to previous location (the pPicked destination)
                Gboard.PutPiece(capturedPiece, destination);
                //  Remove it from the captured pieces set
                CapturedPieces.Remove(capturedPiece);
            }
            //  Using the lifted up piece to place it as it was before
            Gboard.PutPiece(pPicked, origin);
            pPicked.Positioning = origin;
            pPicked.DecrementQtdMoves();

            //  #SpecialMovement : Castling (1)
            if (pPicked is King && destination.Collumn == origin.Collumn + 2)
            {
                Position rookOrigin      = new Position(origin.Line, origin.Collumn + 3);
                Position rookDestination = new Position(origin.Line, origin.Collumn + 1);
                //  It takes the pieces (now it is lifted up) in origin and in the destination.
                Piece p = Gboard.LiftPiece(rookDestination);
                Gboard.PutPiece(p, rookOrigin);
                p.Positioning = rookDestination;
                p.DecrementQtdMoves();
            }
            //  #SpecialMovement : Castling (1)
            if (pPicked is King && destination.Collumn == origin.Collumn - 2)
            {
                Position rookOrigin      = new Position(origin.Line, origin.Collumn - 4);
                Position rookDestination = new Position(origin.Line, origin.Collumn - 1);
                //  It takes the pieces (now it is lifted up) in origin and in the destination.
                Piece pos = Gboard.LiftPiece(rookDestination);
                Gboard.PutPiece(pos, rookOrigin);
                pos.Positioning = rookDestination;
                pos.DecrementQtdMoves();
            }
            //  #SpecialMovement : En passant
            if (pPicked is Pawn)
            {
                if (origin.Collumn != destination.Collumn && capturedPiece == VulnerableEnPassant)
                {
                    Piece    pawn = Gboard.LiftPiece(destination);
                    Position posCaptured;
                    if (pPicked.Color == Color.White)
                    {
                        posCaptured = new Position(3, destination.Collumn);
                    }
                    else
                    {
                        posCaptured = new Position(4, destination.Collumn);
                    }
                    Gboard.PutPiece(pawn, posCaptured);
                }
            }
        }
コード例 #3
0
 public void ValidateOriginPosition(Position pos)
 {
     if (Gboard.GetPiece(pos) == null)
     {
         throw new GameBoardException("There is no piece in the specified location.");
     }
     if (Gboard.GetPiece(pos).Color != currentPlayer)
     {
         throw new GameBoardException("That piece is not yours.");
     }
     if (!Gboard.GetPiece(pos).ExistsPossibleMovement())
     {
         throw new GameBoardException("That piece is blocked and there is no possible movements.");
     }
 }
コード例 #4
0
        //  Here we make the tests
        public void DoTheMove(Position origin, Position destination)
        {
            Piece capturedPiece = MovePiece(origin, destination);

            //  Current player getting itself in check
            if (Check == true)
            {
                if (InCheck(currentPlayer))
                {
                    UndoMovement(origin, destination, capturedPiece);
                    throw new GameBoardException("You can not let your own king in check, try to save him!");
                }
            }
            else
            {
                if (InCheck(currentPlayer))
                {
                    UndoMovement(origin, destination, capturedPiece);
                    throw new GameBoardException("You must not put your own king in check!");
                }
            }

            Piece p = Gboard.GetPiece(destination);

            //  #SpecialMovement : Pawn promotion

            if (p is Pawn)
            {
                if (p.Color == Color.White && destination.Line == 0 || p.Color == Color.Black && destination.Line == 7)
                {
                    p = Gboard.LiftPiece(destination);
                    PiecesInGame.Remove(p);
                    Piece queen = new Queen(Gboard, p.Color);
                    Gboard.PutPiece(queen, destination);
                    PiecesInGame.Add(queen);
                }
            }

            //  Current player cheking his adversary
            if (InCheck(AdversaryOf(currentPlayer)))
            {
                Check = true;
            }
            else
            {
                Check = false;
            }

            if (CheckMate(AdversaryOf(currentPlayer)))
            {
                MatchEnded = true;
            }
            else
            {
                turn++;
                ChangePlayer();
            }

            // #SpecialMovement : En passant
            if (p is Pawn && (destination.Line == origin.Line - 2 || destination.Line == origin.Line + 2))
            {
                VulnerableEnPassant = p;
            }
            else
            {
                VulnerableEnPassant = null;
            }
        }
コード例 #5
0
 public void PutNewPiece(char collumn, int line, Piece piece)
 {
     Gboard.PutPiece(piece, new ChessPosition(collumn, line).ToPosition());
     PiecesInGame.Add(piece);
 }
コード例 #6
0
        public Piece MovePiece(Position origin, Position destination)
        {
            //  It takes the pieces (now it is lifted up) in origin and in the destination.
            Piece pPicked = Gboard.LiftPiece(origin);
            //  Storing and adding to the hashset of captured pieces if it isn't null.
            Piece pCaptured = Gboard.LiftPiece(destination);

            if (pCaptured != null)
            {
                CapturedPieces.Add(pCaptured);
            }
            //  Putting piece in game board and changing the position inside the piece; after, increment the piece's move.
            Gboard.PutPiece(pPicked, destination);
            pPicked.Positioning = destination;
            pPicked.IncrementQtdMoves();

            //  #SpecialMovement : Castling (1)
            if (pPicked is King && destination.Collumn == origin.Collumn + 2)
            {
                Position rookOrigin      = new Position(origin.Line, origin.Collumn + 3);
                Position rookDestination = new Position(origin.Line, origin.Collumn + 1);
                //  It takes the pieces (now it is lifted up) in origin and in the destination.
                Piece p = Gboard.LiftPiece(rookOrigin);
                Gboard.PutPiece(p, rookDestination);
                p.Positioning = rookDestination;
                p.IncrementQtdMoves();
            }
            //  #SpecialMovement : Castling (2)
            if (pPicked is King && destination.Collumn == origin.Collumn - 2)
            {
                Position rookOrigin      = new Position(origin.Line, origin.Collumn - 4);
                Position rookDestination = new Position(origin.Line, origin.Collumn - 1);
                //  It takes the pieces (now it is lifted up) in origin and in the destination.
                Piece p = Gboard.LiftPiece(rookOrigin);
                Gboard.PutPiece(p, rookDestination);
                p.Positioning = rookDestination;
                p.IncrementQtdMoves();
            }
            //  #SpecialMovement : En passant
            if (pPicked is Pawn)
            {
                if (origin.Collumn != destination.Collumn && pCaptured == null)
                {
                    Position posPawn;
                    if (pPicked.Color == Color.White)
                    {
                        posPawn = new Position(destination.Line + 1, destination.Collumn);
                    }
                    else
                    {
                        posPawn = new Position(destination.Line - 1, destination.Collumn);
                    }
                    pCaptured = Gboard.LiftPiece(posPawn);
                    if (pCaptured != null)
                    {
                        CapturedPieces.Add(pCaptured);
                    }
                }
            }

            return(pCaptured);
        }