Пример #1
0
        public void RenderBoard(IBoard board)
        {
            this.PrintHorizontalNumbers();
            for (int row = 0; row < board.NumberOfRows; row++)
            {
                Console.Write(row + "| ");
                for (int column = 0; column < board.NumberOfColumns; column++)
                {
                    Position position = new Position(row, column);
                    var figure = board.GetFigureAtPosition(position);

                    if (figure != null)
                    {
                        Console.Write(figure.DisplayName + " ");
                    }
                    else
                    {
                        this.PrintCell(position);
                    }
                }

                Console.Write("|");
                Console.WriteLine();
            }

            this.PrintHorizontalLines();
        }
Пример #2
0
        public override Move Move(string command,IBoard board)
        {
            int[] deltaRed = { -1, +1, +1, -1 }; //UR, DR, DL, UL

            int[] deltaColona = { +1, +1, -1, -1 };
            int indexOfChange = -1;

            if (command.Length != 3)
            {
                //TODO:change the exception to custom exception
                throw new ArgumentOutOfRangeException("The command should contain three symbols");
            }

            var oldPosition = board.GetFigurePosition(this.Figures[0]);

            switch (command)
            {
                case "kur": { indexOfChange = 0; } break;
                case "kdr": { indexOfChange = 1; } break;
                case "kdl": { indexOfChange = 2; } break;
                case "kul": { indexOfChange = 3; } break;
                default:
                    //TODO:change the exception to custom exception
                    throw new ArgumentOutOfRangeException("The command is not correct");

            }
            int newRow = oldPosition.Row + deltaRed[indexOfChange];
            int newColumn = oldPosition.Col + deltaColona[indexOfChange];
            var newPosition = new Position(newRow, newColumn);
            //Position.CheckIfValid(newPosition, GlobalErrorMessages.PositionNotValidMessage);
              //  this.Figures[0].Position = newPosition;
            return new Move(oldPosition, newPosition);
        }
Пример #3
0
 public void CheckIfTheAddFigureMethodThrowsCorrectlyNullReferenceExeption()
 {
     var board = new Board();
     var position = new Position(-1, -1);
     IFigure figure = new King(FigureSign.K);
     board.AddFigure(figure, position);
 }
Пример #4
0
 public void CheckIfAddNullFigureThrowsException()
 {
     var board = new Board();
     var position = new Position(6, 6);
     IFigure figure = null;
     board.AddFigure(figure, position);
 }
Пример #5
0
 public static void CheckIfFigureOnTheWay(Position position, IBoard board, string message)
 {
     if (board.GetFigureAtPosition(position) != null)
     {
         throw new ArgumentException(message);
     }
 }
Пример #6
0
        public override Move Move(string command,IBoard board)
        {
            int[] deltaRed = { -1, +1, +1, -1 }; //UR, DR, DL, UL

            int[] deltaColona = { +1, +1, -1, -1 };
            int indexOfChange = -1;

            if (command.Length != 3)
            {
                //TODO:Change the exception to custom exception
                throw new ArgumentOutOfRangeException("The command should contain three symbols");
            }

            switch (command)
            {
                case "adr":
                case "bdr":
                case "cdr":
                case "ddr":
                    { indexOfChange = 1; }
                    break;
                case "adl":
                case "bdl":
                case "cdl":
                case "ddl":
                    { indexOfChange = 2; }
                    break;
                default:
                    //TODO:change the exception to custom exception
                    throw new ArgumentOutOfRangeException("The command is not correct");
            }
            int pawnIndex = -1;
            switch (command[0])
            {
                case 'a':
                case 'A':
                    { pawnIndex = 0; }
                    break;
                case 'b':
                case 'B':
                    { pawnIndex = 1; }
                    break;
                case 'c':
                case 'C':
                    { pawnIndex = 2; }
                    break;
                case 'd':
                case 'D':
                    { pawnIndex = 3; }
                    break;
            }
            var fig = this.Figures;
            var oldPosition = board.GetFigurePosition(this.Figures[pawnIndex]);
            int pawnNewRow = oldPosition.Row + deltaRed[indexOfChange];
            int pawnNewColum = oldPosition.Col + deltaColona[indexOfChange];
            var newPosition = new Position(pawnNewRow, pawnNewColum);
            //Position.CheckIfValid(newPosition, GlobalErrorMessages.PositionNotValidMessage);
               // this.Figures[pawnIndex].Position = newPosition;
            return new Move(oldPosition, newPosition);
        }
Пример #7
0
 public void CheckIfMoveConstructorSetsToRowCorrectly()
 {
     Position positionFrom = new Position(1, 3);
     Position positionTo = new Position(2, 4);
     Move move = new Move(positionFrom, positionTo);
     int expectedPositionRow = 2;
     Assert.AreEqual(positionTo.Row, expectedPositionRow);
 }
Пример #8
0
        public void AddFigure(IFigure figure,Position position)
        {
            Validator.CheckIfObjectIsNull(figure, GlobalErrorMessages.NullFigureErrorMessage);
            Validator.CheckIfPositionValid(position,GlobalErrorMessages.PositionNotValidMessage);
            this.board[position.Row, position.Col] = figure;

            this.figurePositionsOnBoard[figure] = position;
        }
Пример #9
0
 public void CheckIfAFigureIsAddedCorrectlyToTheBoard()
 {
     var board = new Board();
     var position = new Position(1, 1);
     var figure = new KingFigure();
     board.AddFigure(figure, position);
     Assert.AreEqual(figure, board.GetFigureAtPosition(position));
 }
Пример #10
0
        public void CheckfAddFigureMethodThrowsWhenPositionIsNotValid()
        {
            var board = new Board();
            var position = new Position(9, 9);
            IFigure king = new KingFigure();

            board.AddFigure(king, position);
        }
Пример #11
0
 public void CheckIfCheckFigureOnTheWayWorksProperlyWhenFigureIsOnTheWay()
 {
     IFigure king = new KingFigure("K");
     var position = new Position(Constants.InitialKingRow, Constants.InitialKingColumn);
     var board = new Board();
     board.AddFigure(king, position);
     Validator.CheckIfFigureOnTheWay(position, board);
 }
Пример #12
0
 public void CheckIfCheckFigureOnTheWayWorksProperlyWhenFigureIsOnTheWay()
 {
     var board = new Board();
     var figure = new KingFigure();
     var position = new Position(1, 1);
     board.AddFigure(figure, position);
     Validator.CheckIfFigureOnTheWay(position, board, "Figure on the way");
 }
Пример #13
0
 public void CheckIfTheAddFigureMethodThrowsCorrectlyIndexOutOfRangeException()
 {
     var board = new Board();
     var position = new Position(9, 9);
     var figureSign = FigureSign.K;
     var figure = new King(figureSign);
     board.AddFigure(figure, position);
 }
Пример #14
0
 public IPosition GenerateNewPosition(int direction)
 {
     int[] deltaRow = { -1, +1, +1, -1 }; ////UR, DR, DL, UL
     int[] deltaCol = { +1, +1, -1, -1 };
     int newRow = this.Row + deltaRow[direction];
     int newColumn = this.Col + deltaCol[direction];
     var newPosition = new Position(newRow, newColumn);
     return newPosition;
 }
Пример #15
0
        public void CheckIfAddFigureWorksCorrectlyWithCorrectFigureAndCorrectPosition()
        {
            var board = new Board();
            var position = new Position(3, 7);
            IFigure king = new KingFigure();

            board.AddFigure(king, position);
            Assert.AreSame(king, board.GetFigureAtPosition(position));
        }
Пример #16
0
 public void CheckIfCheckFigureOnTheWayWorksProperlyWhenNoFigureOnTheWay()
 {
     IFigure king = new KingFigure("K");
     var board = new Board();
     var position = new Position(1, 1);
     board.AddFigure(king, position);
     var expectedPosition = new Position(2, 2);
     Validator.CheckIfFigureOnTheWay(expectedPosition, board);
 }
Пример #17
0
        public void CheckIfGetFigureAtPositionReturnsTheCorrectFigure()
        {
            var board = new Board();
            var position = new Position(1, 1);
            var figure = new KingFigure();
            board.AddFigure(figure, position);

            Assert.AreEqual(figure, board.GetFigureAtPosition(position));
        }
Пример #18
0
 private IPosition GenerateNewPosition(IPosition oldPosition, int direction)
 {
     int[] deltaRow = { -1, +1, +1, -1 }; ////UR, DR, DL, UL
     int[] deltaCol = { +1, +1, -1, -1 };
     int newRow = oldPosition.Row + deltaRow[direction];
     int newColumn = oldPosition.Col + deltaCol[direction];
     var newPosition = new Position(newRow, newColumn);
     return newPosition;
 }
Пример #19
0
 private void PrintCell(Position position)
 {
     if ((position.Row + position.Col) % 2 != 0)
     {
         Console.Write(EmptyWhiteCell + " ");
     }
     else
     {
         Console.Write(EmptyBlackCell + " ");
     }
 }
Пример #20
0
        public void CheckIfRemoveFigureRemovesTheFigure()
        {
            var board = new Board();
            var position = new Position(1, 1);
            var figureSign = FigureSign.K;
            var figure = new King(figureSign);
            board.AddFigure(figure, position);
            board.RemoveFigure(figure, position);

            Assert.AreNotEqual(figure, board.GetFigureAtPosition(position));
        }
Пример #21
0
        public void CheckIfAFigureIsAddedCorrectlyToTheBoard()
        {
            var position = new Position(1, 1);

            Mock<IFigure> mockedFigure = new Mock<IFigure>();
            mockedFigure.Setup(r => r.DisplaySign).Returns("K");

            var board = new Board();
            board.AddFigure(mockedFigure.Object, position);
            Assert.AreSame(mockedFigure.Object, board.GetFigureAtPosition(position));
        }
Пример #22
0
        public void CheckIfAddFigureMethodThrowsWhenPositionIsNotValid()
        {
            var position = new Position(Constants.MaximumRowValueOnBoard + 2, Constants.MaximumRowValueOnBoard + 2);

            Mock<IFigure> mockedFigure = new Mock<IFigure>();
            mockedFigure.Setup(r => r.DisplaySign).Returns("K");

            var king = mockedFigure.Object;

            var board = new Board();
            board.AddFigure(king, position);
        }
Пример #23
0
        private bool CheckIfPositionIsEmpty(Position position, IBoard board)
        {
            if (this.CheckIfInsideTheBoard(position))
            {
                if (board.GetFigureAtPosition(position) == null)
                {
                    return true;
                }
            }

            return false;
        }
Пример #24
0
 private bool CheckIfInsideTheBoard(Position position)
 {
     try
     {
         Validator.CheckIfPositionValid(position, GlobalErrorMessages.PositionNotValidMessage);
         return true;
     }
     catch (IndexOutOfRangeException)
     {
         return false;
     }
 }
Пример #25
0
 private bool CheckIfInsideTheBoard(Position position)
 {
     try
     {
         Validator.CheckIfPositionValid(position);
         return true;
     }
     catch (InvalidPositionException)
     {
         return false;
     }
 }
Пример #26
0
        public void CheckIfAddFigureWorksCorrectlyWithCorrectFigureAndCorrectPosition()
        {
            var board = new Board();
            var position = new Position(Constants.MaximumRowValueOnBoard / 2, Constants.MaximumColumnValueOnBoard / 2);

            Mock<IFigure> mockedFigure = new Mock<IFigure>();
            mockedFigure.Setup(r => r.DisplaySign).Returns("K");

            var king = mockedFigure.Object;

            board.AddFigure(king, position);
            Assert.AreSame(king, board.GetFigureAtPosition(position));
        }
Пример #27
0
        public static void CheckIfPositionValid(Position position, string errorMessage)
        {
            if (position.Row < Constants.MinimumRowValueOnBoard
                || position.Row > Constants.MaximumRowValueOnBoard)
            {
                throw new IndexOutOfRangeException(errorMessage);
            }

            if (position.Col < Constants.MinimumColumnValueOnBoard
                || position.Col > Constants.MaximumColumnValueOnBoard)
            {
                throw new IndexOutOfRangeException(errorMessage);
            }
        }
Пример #28
0
        public void CheckIfGetFigureAtPositionReturnsTheCorrectFigure()
        {
            var position = new Position(1, 1);

            Mock<IFigure> mockedFigure = new Mock<IFigure>();
            mockedFigure.Setup(r => r.DisplaySign).Returns("K");

            var figure = mockedFigure.Object;

            var board = new Board();
            board.AddFigure(figure, position);

            Assert.AreSame(figure, board.GetFigureAtPosition(position));
        }
Пример #29
0
        public void CheckIfAnExceptionIsThrownWhenTheCommandIsThreeSymbolsLongButStillNotCorrect()
        {
            var player = new Player("Serafim");

            var king = new KingFigure();
            player.AddFigure(king);

            var board = new Board();
            var position = new Position(Constants.InitialKingRow, Constants.InitialKingColumn);
            board.AddFigure(king, position);

            var commandFactory = new CommandFactory();
            var commandContext = new CommandContext(new BoardMemory(), board, player);
            player.ExecuteCommand(commandContext, "aaa");
        }
Пример #30
0
        public void CheckIfAnExceptionIsThrownWhenTheCommandIsNotExactlyThreeSymbolsAndThePlayerIsPawn()
        {
            var player = new Player("Serafim");

            var pawn = new PawnAFigure();
            player.AddFigure(pawn);

            var board = new Board();
            var position = new Position(Constants.InitialKingRow, Constants.InitialKingColumn);
            board.AddFigure(pawn, position);

            var commandFactory = new CommandFactory();
            var commandContext = new CommandContext(new BoardMemory(), board, player);
            player.ExecuteCommand(commandContext, "aaaa");
        }