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

            ValidadeSourcePosition(source);
            return(_board.Piece(source).PossibleMoves());
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            ChessMatch        chessMatch     = new ChessMatch();
            List <ChessPiece> capturedPieces = new List <ChessPiece>();

            while (!chessMatch.CheckMate)
            {
                try
                {
                    Console.Clear();
                    UI.PrintMatch(chessMatch, capturedPieces);
                    Console.Write("\nSource: ");
                    ChessPosition source = UI.ReadChessPosition();
                    bool[,] possibleMoves = chessMatch.PossibleMoves(source);
                    Console.Clear();
                    UI.PrintBoard(chessMatch.MakeChessPieces(), possibleMoves);
                    Console.Write("\nTarget: ");
                    ChessPosition target = UI.ReadChessPosition();

                    ChessPiece capturedPiece = chessMatch.PerformChessMove(source, target);
                    if (capturedPiece != null)
                    {
                        capturedPieces.Add(capturedPiece);
                    }

                    if (chessMatch.Promoted != null)
                    {
                        Console.Write("Enter piece for promotion (B/N/R/Q): ");
                        string type = Console.ReadLine().ToUpper();
                        while (!type.Equals("B") && !type.Equals("N") && !type.Equals("R") && !type.Equals("Q"))
                        {
                            Console.Write("Invalid value! Enter piece for promotion (B/N/R/Q): ");
                            type = Console.ReadLine().ToUpper();
                        }
                        chessMatch.ReplacePromotedPiece(type);
                    }
                } catch (ChessException e)
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                } catch (ArgumentException e)
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                } catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                }
            }
            Console.Clear();
            UI.PrintMatch(chessMatch, capturedPieces);
        }
Esempio n. 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);
        }
Esempio n. 4
0
 public void GameLoop()
 {
     Board(bor);
     while (!gameIsFinished)
     {
         mountScreen();
         try{
             ChessPosition init = getInit();
             ChessPosition dest = getDest(init);
             executePlay(init, dest);
         }
         catch (BoardException e) {
             Console.WriteLine(e.Message);
             Console.ReadKey();
         }
     }
 }
Esempio n. 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);
            }
        }
Esempio n. 6
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();
        }
Esempio n. 7
0
        public static List <ChessPosition> ToList(this ChessBoardSnapshot boardSnapshot)
        {
            List <ChessPosition> ret = new List <ChessPosition>();

            if (boardSnapshot == null)
            {
                return(ret);
            }

            ChessPieceType[] board    = boardSnapshot.board;
            bool[]           hasMoved = boardSnapshot.hasMoved;

            if (board.Length != ChessSettings.boardSize * ChessSettings.boardSize)
            {
                return(ret);
            }

            for (int i = 0; i < board.Length; i++)
            {
                if (!board[i].IsValid())
                {
                    continue;
                }
                if (board[i].IsEmpty())
                {
                    continue;
                }

                ChessPosition newPos = new ChessPosition
                {
                    coord    = i.ToChessCoord(),
                    type     = board[i],
                    hasMoved = hasMoved[i]
                };

                ret.Add(newPos);
            }

            return(ret);
        }
Esempio n. 8
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");
     }
 }
Esempio n. 9
0
 public ChessPosition(ChessPosition other) : this(other.type, other.coord, other.hasMoved)
 {
 }