private void ValidateValidBoardToPlay(Position positionToValidate)
        {
            if (!_moves.Any())
            {
                //no moves ==> board is empty ==> all moves valid
                return;
            }

            Move     lastMove             = _moves.Last();
            Position lastMoveTilePosition = lastMove.TilePosition;

            SmallBoardInformation nextBoardFromLastMove =
                _board[lastMoveTilePosition.X][lastMoveTilePosition.Y];

            if (nextBoardFromLastMove.Value == TileValue.Empty)
            {
                // the active board is empty ==> can play here
                if (lastMoveTilePosition.Equals(positionToValidate))
                {
                    // the Position is on this board ==> valid
                    return;
                }

                throw new IllegalPositionException();
            }

            // SmallBoard is full ==> all active | Check if the position is not finished yet
            if (_board[positionToValidate.X][positionToValidate.Y].Value == TileValue.Empty)
            {
                return;
            }

            throw new IllegalPositionException();
        }
 private SmallTileInformation GetTile(SmallBoardInformation board, Position p)
 {
     try
     {
         return(board.Tiles[p.X][p.Y]);
     }
     catch (IndexOutOfRangeException ex)
     {
         throw new InvalidPositionException(ex);
     }
 }
 private void InitializeBoard()
 {
     _board = new SmallBoardInformation[3][];
     for (var x = 0; x < 3; x++)
     {
         _board[x] = new SmallBoardInformation[3];
         for (var y = 0; y < 3; y++)
         {
             _board[x][y] = new SmallBoardInformation
             {
                 Value = TileValue.Empty,
                 Tiles = GenerateTiles(x, y)
             };
         }
     }
 }
        public MoveResult Move(Move m)
        {
            try
            {
                ValidateNoWinner();
                ValidateIsPlayersMove(m.Player);
                ValidateValidBoardToPlay(m.BoardPosition);

                SmallBoardInformation board = GetBoard(m.BoardPosition);
                SmallTileInformation  tile  = GetTile(board, m.TilePosition);

                ValidateTileEmpty(tile);

                tile.Value = m.Player.ToTileValue();

                Winner winner = board.Tiles.GetWinner(m.Player);
                if (winner != Winner.None)
                {
                    board.Value = winner.ToTileValue();
                    _winner     = _board.GetWinner(m.Player);
                }

                // if there is no exception until here the move was valid
                _moves.Add(m);
                ChangeCurrentPlayer();

                return(new MoveResult
                {
                    IsValid = true,
                    Move = m,
                    MoveFinishedBoard = winner != Winner.None,
                    MoveFinishedGame = _winner != Winner.None,
                });
            }
            catch (InvalidMoveException e)
            {
                return(new MoveResult
                {
                    IsValid = false,
                    Move = m,
                    MoveFinishedBoard = false,
                    MoveFinishedGame = false,
                    InvalidReason = e.Message,
                });
            }
        }
Esempio n. 5
0
        public void GetAllLinesOfBoard_ValidSmallBoardInformation_ShouldReturnAllLines()
        {
            // arrange
            var board = new SmallBoardInformation[3][];

            for (var x = 0; x < 3; x++)
            {
                board[x] = new SmallBoardInformation[3];
                for (var y = 0; y < 3; y++)
                {
                    board[x][y] = new SmallBoardInformation
                    {
                        Value = TileValue.Empty,
                    };
                }
            }

            // act
            IEnumerable <IEnumerable <TileInformation> > result =
                BoardExtensions.GetAllLinesOfBoard(board);

            // assert
            result.Should().HaveCount(8);
        }