Exemplo n.º 1
0
        private static int CheckDiagonalMove(IBoard board, Position figurePosition, int available, IFigure figure, Position position)
        {
            if (Position.CheckIsValid(position))
            {
                IFigure figureAtPosition = board.GetFigureAtPosition(position);

                if (figureAtPosition != null)
                {
                    board.AddFigure(figure, position);
                    board.AddFigure(null, figurePosition);

                    var kingPosition = board.GetFigurePostionByTypeAndColor("King", figure.Color);

                    if (IsFieldAttacked(board, kingPosition, figure.Color) == false)
                    {
                        available++;
                    }

                    board.AddFigure(figureAtPosition, position);
                    board.AddFigure(figure, figurePosition);
                }
            }

            return(available);
        }
        public void AddFigureToBoard(IPlayer firstPlayser, IPlayer secondPlayer, IBoard board, string fen)
        {
            var splitedFen = fen.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            var index = 0;

            for (int row = splitedFen.Length - 1; row >= 0; row--)
            {
                var currentRow = this.MakeRow(splitedFen[row]);

                for (int col = 0; col < currentRow.Length; col++)
                {
                    if (currentRow[col] == Pown)
                    {
                        var pawn = new Pawn(secondPlayer.Color);
                        secondPlayer.AddFigure(pawn);
                        var position = new Position(index + 1, (char)(col + 'a'));
                        board.AddFigure(pawn, position);
                    }
                    else if (currentRow[col] == King)
                    {
                        var figureInstance = new King(firstPlayser.Color);
                        firstPlayser.AddFigure(figureInstance);
                        var position = new Position(index + 1, (char)(col + 'a'));
                        board.AddFigure(figureInstance, position);
                    }
                }

                index++;
            }
        }
Exemplo n.º 3
0
        private static void ChosenFigure(IBoard board, Position to, int chosenNumber, ChessColor color)
        {
            IFigure promotedFigure = null;

            switch (chosenNumber)
            {
            case 1:
                board.RemoveFigure(to);
                promotedFigure = new Queen(color);
                board.AddFigure(promotedFigure, to);
                break;

            case 2:
                board.RemoveFigure(to);
                promotedFigure = new Rook(color);
                board.AddFigure(promotedFigure, to);
                break;

            case 3:
                board.RemoveFigure(to);
                promotedFigure = new Bishop(color);
                board.AddFigure(promotedFigure, to);
                break;

            case 4:
                board.RemoveFigure(to);
                promotedFigure = new Knight(color);
                board.AddFigure(promotedFigure, to);
                break;
            }
        }
        public void Initialize(IList <IPlayer> players, IBoard board)
        {
            this.ValidateStrategy(players, board);
            var firstPlayer  = players[0];
            var secondPlayer = players[1];
            var thirdPlayer  = players[2];
            var fourthPlayer = players[3];

            //first player figure position
            for (int i = 1; i < 3; i++)
            {
                for (int j = 1; j < 3; j++)
                {
                    var figure = new Figure(firstPlayer.Color);
                    firstPlayer.AddFigure(figure);
                    var position = new Position(i, j);
                    board.AddFigure(figure, position);
                }
            }
            //second player
            for (int i = 1; i < 3; i++)
            {
                for (int j = 12; j < 14; j++)
                {
                    var figure = new Figure(secondPlayer.Color);
                    secondPlayer.AddFigure(figure);
                    var position = new Position(i, j);
                    board.AddFigure(figure, position);
                }
            }
            ////third player figure position
            for (int i = 12; i < 14; i++)
            {
                for (int j = 1; j < 3; j++)
                {
                    var figure = new Figure(thirdPlayer.Color);
                    thirdPlayer.AddFigure(figure);
                    var position = new Position(i, j);
                    board.AddFigure(figure, position);
                }
            }
            ////fourth player figure position
            for (int row = 12; row < 14; row++)
            {
                for (int col = 12; col < 14; col++)
                {
                    var figure = new Figure(fourthPlayer.Color);
                    fourthPlayer.AddFigure(figure);
                    var position = new Position(row, col);
                    board.AddFigure(figure, position);
                }
            }
        }
Exemplo n.º 5
0
        public void Start()
        {
            while (true)
            {
                try
                {
                    IPlayer player = this.GetNextPlayer();
                    IFigure figure = new Figure(player.FigureType);
                    var     move   = this.input.GetNextPlayerMove(player);
                    var     to     = move.Position;
                    this.CheckIfPositionIsEmpty(to);

                    board.AddFigure(figure, to);
                    this.renderer.RenderBoard(this.board);

                    try
                    {
                        WinningConditions(this.board);
                    }
                    catch (Exception ex)
                    {
                        this.renderer.PrintErrorMessage(ex.Message);
                        ConsoleHelpers.PrintWinner(player);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    this.currentPlayerIndex--;
                    this.renderer.PrintErrorMessage(ex.Message);
                }
            }
        }
        private void AddPawnsToBoardRow(IPlayer player, IBoard board, int chessRow)
        {
            for (int i = 0; i < GlobalConstants.StandartGameTotalBoardCols - 2; i += 2)
            {
                var pawn = new Pawn(player.Color);
                player.AddFigure(pawn);
                var position = new Position(chessRow, (char)(i + 'a'));
                board.AddFigure(pawn, position);
            }

            // TODO: Remove
            //// For testing only
            //var pawn = new Pawn(player.Color);
            //player.AddFigure(pawn);
            //var position = new Position(8, (char)(0 + 'a'));
            //board.AddFigure(pawn, position);

            //pawn = new Pawn(player.Color);
            //player.AddFigure(pawn);
            //position = new Position(8, (char)(2 + 'a'));
            //board.AddFigure(pawn, position);

            //pawn = new Pawn(player.Color);
            //player.AddFigure(pawn);
            //position = new Position(8, (char)(4 + 'a'));
            //board.AddFigure(pawn, position);

            //pawn = new Pawn(player.Color);
            //player.AddFigure(pawn);
            //position = new Position(6, (char)(4 + 'a'));
            //board.AddFigure(pawn, position);
        }
 private void AddKingToBoardRow(IPlayer player, IBoard board, int chessRow, char chessCol)
 {
     var figureInstance = new King(player.Color);
         player.AddFigure(figureInstance);
         var position = new Position(chessRow, chessCol);
         board.AddFigure(figureInstance, position);
 }
Exemplo n.º 8
0
        private static int CheckIfMoveIsAvailable(IBoard board, int available, Position figurePosition, IFigure figure, Position currentPosition, IFigure currentFigure)
        {
            board.AddFigure(null, figurePosition);
            board.AddFigure(figure, currentPosition);

            var kingPosition = board.GetFigurePostionByTypeAndColor("King", figure.Color);

            if (IsFieldAttacked(board, kingPosition, figure.Color) == false)
            {
                available++;
            }

            board.AddFigure(figure, figurePosition);
            board.AddFigure(currentFigure, currentPosition);

            return(available);
        }
 private void AddPawnsToBoardRow(IPlayer player, IBoard board, int chessRow)
 {
     for (int i = 0; i < BOARD_SIZE; i++)
     {
         var pawn = new Pawn(player.Color);
         player.AddFigure(pawn);
         board.AddFigure(pawn, new Position(chessRow, (char)(i + 'a')));
     }
 }
 private void AddPawnsToBoardRow(IPlayer player, IBoard board, int chessRow)
 {
     for (int i = 0; i < GlobalConstants.StandartGameTotalBoardCols; i++)
     {
         var pawn = new Pawn(player.Color);
         player.AddFigure(pawn);
         var position = new Position(chessRow, (char)(i + 'a'));
         board.AddFigure(pawn, position);
     }
 }
 private void AddPawnsToBoardRow(IPlayer player, IBoard board, int chessRow)
 {
     for (int i = 0; i < BoardTotalRowsAndCols; i++)
     {
         var pawn = new Pawn(player.Color);
         player.AddFigure(pawn);
         var position = new Position(chessRow, (char)(i + 'a'));
         board.AddFigure(pawn, position);
     }
 }
        private void PlacePeice(int pos, Type gamepeice, IPlayer player, IBoard board, int chessRow)
        {
            var figureType     = gamepeice;
            var figureInstance = (IFigure)Activator.CreateInstance(figureType, player.Color);

            player.AddFigure(figureInstance);
            var position = new Position(chessRow, (char)(pos + 'a'));

            board.AddFigure(figureInstance, position);
        }
 private void AddPawnsToBoardRow(IPlayer player, IBoard board, int chessRow)
 {
     for (int i = 0; i < BoardTotalRowsAndCols; i++)
     {
         var pawn = new Pawn(player.Color);
         player.AddFigure(pawn);
         var position = new Position(chessRow, (char)(i + 'a'));
         board.AddFigure(pawn, position);
     }
 }
Exemplo n.º 14
0
 private void AddArmyToBoardRow(IPlayer player, IBoard board, int chessRow)
 {
     for (int i = 0; i < BOARD_SIZE; i++)
     {
         var figureType   = this.figureTypes[i];
         var figureEntity = (IFigure)Activator.CreateInstance(figureType, player.Color);
         player.AddFigure(figureEntity);
         board.AddFigure(figureEntity, new Position(chessRow, (char)(i + 'a')));
     }
 }
 private void AddArmyToBoardRow(IPlayer player, IBoard board, int chessRow)
 {
     for (int i = 0; i < BoardTotalRowsAndCols; i++)
     {
         var figureType     = this.figureTypes[i];
         var figureInstance = (IFigure)Activator.CreateInstance(figureType, player.Color);
         player.AddFigure(figureInstance);
         var position = new Position(chessRow, (char)(i + 'a'));
         board.AddFigure(figureInstance, position);
     }
 }
        private void InitializePawns(IPlayer player, IBoard board, int chessRow)
        {
            for (int i = 0; i < BoradTotalRowsCols; i++)
            {
                Pawn pawn = new Pawn(player.Color);
                player.AddFigure(pawn);

                var position = new Position(chessRow, (char)(i + 'a'));
                board.AddFigure(pawn, position);
            }
        }
Exemplo n.º 17
0
        /// <summary>
        /// Move Figure  method that moves figures
        /// </summary>
        /// <param name="from">From position</param>
        /// <param name="direction">To direction</param>
        /// <param name="board">The board on which we are moving the figure</param>
        public void MoveFigure(IPosition from, int direction, IBoard board)
        {
            var figurePosition = board.GetFigurePosition(this.Figure);
            IPosition to = figurePosition.GenerateNewPosition(direction);

            Validator.CheckIfPositionValid(to);
            Validator.CheckIfFigureOnTheWay(to, board);

            board.RemoveFigure(from);
            board.AddFigure(this.Figure, to);
        }
 private void AddArmyToBoardRow(IPlayer player, IBoard board, int chessRow)
 {
     for (int i = 0; i < GlobalConstants.StandartGameTotalBoardCols; i++)
     {
         var figureType = this.figureTypes[i];
         var figureInstance = (IFigure)Activator.CreateInstance(figureType, player.Color);
         player.AddFigure(figureInstance);
         var position = new Position(chessRow, (char)(i + 'a'));
         board.AddFigure(figureInstance, position);
     }
 }
Exemplo n.º 19
0
 private void AddArmyToBoardRow(IPlayer player, IBoard board, int chessRow)
 {
     for (var i = 0; i < BOARD_TOTAL_ROWS_AND_COLS; i++)
     {
         var figureType     = this._figureTypes[i];
         var figureInstance = (IFigure)Activator.CreateInstance(figureType, player.Color);
         player.AddFigure(figureInstance);
         var chessCol = (char)(i + 'a');
         var position = new Position(chessRow, chessCol);
         board.AddFigure(figureInstance, position);
     }
 }
Exemplo n.º 20
0
        private void AddPawnsToBoardRow(IPlayer player, IBoard board, int chessRow)
        {
            for (var i = 0; i < BOARD_TOTAL_ROWS_AND_COLS; i++)
            {
                var chessCol = (char)(i + 'a');

                var pawn = new Pawn(player.Color);
                player.AddFigure(pawn);
                var position = new Position(chessRow, chessCol);
                board.AddFigure(pawn, position);
            }
        }
        private void UndoMovement(IBoard board, IFigure figure, IFigure figureTaken, Move move)
        {
            var from = move.To;
            var to   = move.From;

            board.MoveFigureAtPosition(figure, from, to);

            if (figureTaken != null)
            {
                board.AddFigure(figureTaken, from);
            }

            throw new InvalidOperationException(ExceptionMessages.YouAreInCheckException);
        }
Exemplo n.º 22
0
        private void AddPawnsToBoardRow(IPlayer player, IBoard board, int chessRow)
        {
            if (chessRow > BoardTotalRowsAndCols || chessRow < 1)
            {
                throw new Exception("Chess row out of bounds!");
            }

            for (int i = 0; i < BoardTotalRowsAndCols; i++)
            {
                var pawn = new Pawn(player.Color);
                player.AddFigure(pawn);
                var position = new Position(chessRow, (char)(i + 'a'));
                board.AddFigure(pawn, position);
            }
        }
Exemplo n.º 23
0
        public bool CheckCastling(IBoard board, IFigure figure, Position from, Position to)
        {
            var color = figure.Color;

            if (color == ChessColor.White)
            {
                if (to.Row == WhiteShortCastlingPosition.Row && to.Col == WhiteShortCastlingPosition.Col &&
                    from.Col == GlobalConstants.StandardGameWhiteKingPosition.Col && from.Row == GlobalConstants.StandardGameWhiteKingPosition.Row)
                {
                    if (!MoveFiguresInformation.isWhiteKingMoved && !MoveFiguresInformation.isWhiteRightRookMoved)
                    {
                        CheckForFiguresBetweenPositions(board, figure, from, to);
                        CheckIfKingIsInCheck(board, color);
                        CheckIfKingCrossesAttackedField(board, GlobalConstants.StandardGameWhiteKingPosition, WhiteShortCastlingPosition, color);

                        board.RemoveFigure(GlobalConstants.StandardGameWhiteKingPosition);
                        board.RemoveFigure(GlobalConstants.StandardGameWhiteRightRookPosition);

                        board.AddFigure(new King(color), WhiteShortCastlingPosition);
                        board.AddFigure(new Rook(color), Position.FromChessCoordinates(1, 'f'));

                        return(true);
                    }
                    else
                    {
                        throw new InvalidOperationException(ExceptionMessages.RookOrKingHaveBeenMovedException);
                    }
                }

                if (to.Row == WhiteLongCastlingPosition.Row && to.Col == WhiteLongCastlingPosition.Col &&
                    from.Col == GlobalConstants.StandardGameWhiteKingPosition.Col && from.Row == GlobalConstants.StandardGameWhiteKingPosition.Row)
                {
                    if (!MoveFiguresInformation.isWhiteKingMoved && !MoveFiguresInformation.isWhiteLeftRookMoved)
                    {
                        CheckForFiguresBetweenPositions(board, figure, from, to);
                        CheckIfKingIsInCheck(board, color);
                        CheckIfKingCrossesAttackedField(board, GlobalConstants.StandardGameWhiteKingPosition, WhiteLongCastlingPosition, color);

                        board.RemoveFigure(GlobalConstants.StandardGameWhiteKingPosition);
                        board.RemoveFigure(GlobalConstants.StandardGameWhiteLeftRookPosition);

                        board.AddFigure(new King(color), WhiteLongCastlingPosition);
                        board.AddFigure(new Rook(color), Position.FromChessCoordinates(1, 'd'));

                        return(true);
                    }
                    else
                    {
                        throw new InvalidOperationException(ExceptionMessages.RookOrKingHaveBeenMovedException);
                    }
                }
            }

            if (color == ChessColor.Black)
            {
                if (to.Row == BlackShortCastlingPosition.Row && to.Col == BlackShortCastlingPosition.Col &&
                    from.Col == GlobalConstants.StandardGameBlackKingPosition.Col && from.Row == GlobalConstants.StandardGameBlackKingPosition.Row)
                {
                    if (!MoveFiguresInformation.isBlackKingMoved && !MoveFiguresInformation.isBlackRightRookMoved)
                    {
                        CheckForFiguresBetweenPositions(board, figure, from, to);
                        CheckIfKingIsInCheck(board, color);
                        CheckIfKingCrossesAttackedField(board, GlobalConstants.StandardGameBlackKingPosition, BlackShortCastlingPosition, color);

                        board.RemoveFigure(GlobalConstants.StandardGameBlackKingPosition);
                        board.RemoveFigure(GlobalConstants.StandardGameBlackRightRookPosition);

                        board.AddFigure(new King(color), BlackShortCastlingPosition);
                        board.AddFigure(new Rook(color), Position.FromChessCoordinates(8, 'f'));

                        return(true);
                    }
                    else
                    {
                        throw new InvalidOperationException(ExceptionMessages.RookOrKingHaveBeenMovedException);
                    }
                }

                if (to.Row == BlackLongCastlingPosition.Row && to.Col == BlackLongCastlingPosition.Col &&
                    from.Col == GlobalConstants.StandardGameBlackKingPosition.Col && from.Row == GlobalConstants.StandardGameBlackKingPosition.Row)
                {
                    if (!MoveFiguresInformation.isBlackKingMoved && !MoveFiguresInformation.isBlackLeftRookMoved)
                    {
                        CheckForFiguresBetweenPositions(board, figure, from, to);
                        CheckIfKingIsInCheck(board, color);
                        CheckIfKingCrossesAttackedField(board, GlobalConstants.StandardGameBlackKingPosition, BlackLongCastlingPosition, color);

                        board.RemoveFigure(GlobalConstants.StandardGameBlackKingPosition);
                        board.RemoveFigure(GlobalConstants.StandardGameBlackLeftRookPosition);

                        board.AddFigure(new King(color), BlackLongCastlingPosition);
                        board.AddFigure(new Rook(color), Position.FromChessCoordinates(8, 'd'));

                        return(true);
                    }
                    else
                    {
                        throw new InvalidOperationException(ExceptionMessages.RookOrKingHaveBeenMovedException);
                    }
                }
            }

            return(false);
        }