Exemple #1
0
        public bool[,] PossibleMoves(ChessPosition sourcePosition)
        {
            Position source = sourcePosition.ToPosition();

            ValidadeSourcePosition(source);
            return(_board.Piece(source).PossibleMoves());
        }
Exemple #2
0
        private void executePlay(ChessPosition init, ChessPosition dest)
        {
            Piece Piece = bor.piece(init.ToPosition());

            bool[,] possiblemvmnts = Piece.possibleMovements();
            Piece p = executeMovement(possiblemvmnts, init.ToPosition(), dest.ToPosition());

            checkPromotion();
            Piece.increasemvmtAmount();
            if (isincheck(adversary(currentPlayer)))
            {
                undoMovement(p, dest.ToPosition(), init.ToPosition());
                throw new BoardException("You can't put yourself in check");
            }
            else if (isincheck(currentPlayer))
            {
                checkColor = adversary(currentPlayer);
            }
            else
            {
                checkColor = null;
            }
            if (checkColor != null)
            {
                gameIsFinished = testcheckmate((Color)checkColor);
                if (gameIsFinished)
                {
                    throw new BoardException("Game was finished, the winner  was: " + adversary((Color)checkColor));
                }
            }
            if (bor.potentialEmPassant != null)
            {
                bor.emPassant = bor.potentialEmPassant;
            }
            else
            {
                bor.emPassant = null;
                bor.emPassant = null;
            }
            turn++;
            passTurn();
        }
Exemple #3
0
        public ChessPiece PerformChessMove(ChessPosition sourcePosition, ChessPosition targetPosition)
        {
            Position source = sourcePosition.ToPosition();
            Position target = targetPosition.ToPosition();

            ValidadeSourcePosition(source);
            ValidateTargetPosition(source, target);

            Piece capturedPiece = MakeMove(source, target);

            if (TestCheck(CurrentPlayer))
            {
                UndoMove(source, target, capturedPiece);
                throw new ChessException("You can't put yourself in check");
            }

            ChessPiece movedPiece = (ChessPiece)_board.Piece(target);

            // #specialmove promotion
            Promoted = null;
            if (movedPiece is Pawn)
            {
                if ((movedPiece.Color == Color.White && target.Row == 0) || (movedPiece.Color == Color.Black && target.Row == 7))
                {
                    Promoted = (ChessPiece)_board.Piece(target);
                    Promoted = ReplacePromotedPiece("Q");
                }
            }

            Check = (TestCheck(Opponent(CurrentPlayer))) ? true : false;

            if (TestCheckmate(Opponent(CurrentPlayer)))
            {
                Checkmate = true;
            }
            else
            {
                NextTurn();
            }

            // #specialmove en passant
            if (movedPiece is Pawn && (target.Row == source.Row - 2 || target.Row == source.Row + 2))
            {
                EnPassantVulnerable = movedPiece;
            }
            else
            {
                EnPassantVulnerable = null;
            }

            return((ChessPiece)capturedPiece);
        }
Exemple #4
0
 public ChessPosition getDest(ChessPosition init)
 {
     if (bor.validPosition(init.ToPosition()) && bor.pieceExists(init.ToPosition()))
     {
         if (bor.piece(init.ToPosition()).color == currentPlayer)
         {
             Piece p1 = bor.piece(init.ToPosition());
             bool[,] possiblemvmnts = p1.possibleMovements();
             Screen.possiblePosition(bor, possiblemvmnts);
             Console.Write("Destiny: ");
             return(Screen.readChessPosition());
         }
         else
         {
             throw new BoardException("Invalid Play: it's not your turn");
         }
     }
     else
     {
         throw new BoardException("Invalid Output:Try again");
     }
 }
Exemple #5
0
        static void Main(string[] args)
        {
            try
            {
                ChessGame game = new ChessGame();

                while (!game.Finished)
                {
                    try
                    {
                        Screen.PrintPlay(game, new bool[game.Board.Lines, game.Board.Columns]);

                        Console.Write("Initial position: ");
                        ChessPosition initialChessPosition = Screen.ReadPosition();
                        Position      initialPosition      = initialChessPosition.ToPosition();

                        game.CheckInitialPosition(initialPosition);
                        bool[,] possibleMoves = game.Board.GetPiece(initialPosition).PossibleMoves();

                        Screen.PrintPlay(game, possibleMoves);
                        Console.Write("Initial position: ");
                        Screen.PrintPosition(initialPosition);

                        Console.Write("Final position: ");
                        Position finalPosition = Screen.ReadPosition().ToPosition();
                        game.CheckFinalPosition(initialPosition, finalPosition);

                        game.PerformPlay(initialPosition, finalPosition);

                        Screen.EndGame(game);
                    }
                    catch (BoardException e)
                    {
                        Console.WriteLine();
                        Console.WriteLine(e.Message);
                        Console.Write("Press ENTER to re-do this play. ");
                        Console.ReadLine();
                    }
                }
            }
            catch (BoardException e)
            {
                Console.WriteLine(e.Message);
            }
        }