public void MakeMove(ITicTacToeBoard ticTacToeBoard)
        {
            while (true)
            {
                Console.Write($"{Name}: ");
                var      response        = Console.ReadLine().Trim();
                string[] possibleNumbers = response.Split(' ', StringSplitOptions.RemoveEmptyEntries);

                if (possibleNumbers.Length != 2 ||
                    !int.TryParse(possibleNumbers[0], out int row) ||
                    !int.TryParse(possibleNumbers[1], out int cell))
                {
                    LogErrorMessage("Remember, the correct format is: row cell. Example: 1 3");
                    continue;
                }

                if (!ticTacToeBoard.TryMarkCell(row - 1, cell - 1, Sign))
                {
                    LogErrorMessage("Can't mark that cell! Please write a valid one.");
                    continue;
                }

                break;
            }
        }
Пример #2
0
//        public int GetBoardSize()
//        {
//            throw new System.NotImplementedException();
//        }

        public void RenderGameBoard(ITicTacToeBoard board)
        {
            for (var depth = 0; depth < board.GetBoardSize(); depth++)
            {
                RenderOneLayerOfBoard(board, depth);
            }
        }
Пример #3
0
 public void RenderBoard(ITicTacToeBoard ticTacToeBoard)
 {
     var cells = ticTacToeBoard.Cells();
     Console.WriteLine(" {0} | {1} | {2} ", cells[0, 0].ToConsoleString(), cells[0, 1].ToConsoleString(), cells[0, 2].ToConsoleString());
     Console.WriteLine(" {0} | {1} | {2} ", cells[1, 0].ToConsoleString(), cells[1, 1].ToConsoleString(), cells[1, 2].ToConsoleString());
     Console.WriteLine(" {0} | {1} | {2} ", cells[2, 0].ToConsoleString(), cells[2, 1].ToConsoleString(), cells[2, 2].ToConsoleString());
     Console.WriteLine("");
 }
        public void TakeTurn(ITicTacToeBoard ticTacToeBoard)
        {
            TicTacToePiece[,] cells = ticTacToeBoard.Cells();

            Tuple<int,int> cell = PickMove(cells);

            ticTacToeBoard.AddPieceToBoard(_piece, cell.Item1, cell.Item2);
        }
Пример #5
0
 public void RenderStart(ITicTacToePlayer player1, ITicTacToePlayer player2, ITicTacToeBoard ticTacToeBoard)
 {
     Console.WriteLine("");
     Console.WriteLine("New game started");
     Console.WriteLine("Player 1 is {0} and playing {1}", player1.Name, player1.Piece.ToConsoleString());
     Console.WriteLine("Player 2 is {0} and playing {1}", player2.Name, player2.Piece.ToConsoleString());
     Console.WriteLine("");
     RenderBoard(ticTacToeBoard);
 }
Пример #6
0
        /// <summary>
        /// This method finds the best move and makes it on the specified board.
        /// </summary>
        /// <param name="Board">The board to make a move on.</param>
        public static void MakeBestMove(ITicTacToeBoard Board)
        {
            int m = BestMove(Board.GetFieldSymbols(), Board.XsTurn);

            if (m != -1)
            {
                Board.SetMark(m);
            }
        }
Пример #7
0
        public void MakeMove(ITicTacToeBoard ticTacToeBoard)
        {
            if (MadeMoveToWin(ticTacToeBoard))
            {
                return;
            }
            if (MadeMoveToBlockOpponent(ticTacToeBoard))
            {
                return;
            }

            MakeRandomMove(ticTacToeBoard);
        }
Пример #8
0
        public IGameResult GetGameResult(
            ITicTacToeBoard ticTacToeBoard,
            ITicTacToePlayer player1,
            ITicTacToePlayer player2
            )
        {
            var cells = ticTacToeBoard.Cells();
            var diagonal = IsWinOnDiagonal(cells);
            if (diagonal != TicTacToePiece.None)
            {
                return new TicTacToeGameResult()
                {
                    IsDraw = false,
                    Winner = diagonal == player1.Piece ? player1 : player2,
                    Loser = diagonal == player1.Piece ? player2 : player1,
                };
            }

            var horizontal = IsWinOnHorizontal(cells);
            if (horizontal != TicTacToePiece.None)
            {
                return new TicTacToeGameResult()
                {
                    IsDraw = false,
                    Winner = horizontal == player1.Piece ? player1 : player2,
                    Loser = horizontal == player1.Piece ? player2 : player1,
                };
            }

            var vertical = IsWinOnVertical(cells);
            if (vertical != TicTacToePiece.None)
            {
                return new TicTacToeGameResult()
                {
                    IsDraw = false,
                    Winner = vertical == player1.Piece ? player1 : player2,
                    Loser = vertical == player1.Piece ? player2 : player1,
                };
            }

            if (IsDrawn(ticTacToeBoard))
            {
                return new TicTacToeGameResult()
                {
                    IsDraw = true
                };
            }

            throw new Exception("No valid result found");
        }
Пример #9
0
 public TicTacToeGame(
     ITicTacToeBoard ticTacToeBoard,
     ITicTacToePlayer player1,
     ITicTacToePlayer player2,
     IGameJudge gameJudge,
     IGameRenderer gameRenderer,
     IGamePauser gamePauser
     )
 {
     _ticTacToeBoard = ticTacToeBoard;
     _player1 = player1;
     _player2 = player2;
     _gameJudge = gameJudge;
     _gameRenderer = gameRenderer;
     _gamePauser = gamePauser;
 }
Пример #10
0
        public TicTacToeGameTests()
        {
            TicTacToeBoard = Substitute.For<ITicTacToeBoard>();
            Player1 = Substitute.For<ITicTacToePlayer>();
            Player2 = Substitute.For<ITicTacToePlayer>();
            GameJudge = Substitute.For<IGameJudge>();
            GameRenderer = Substitute.For<IGameRenderer>();
            GamePauser = Substitute.For<IGamePauser>();

            Sut = new TicTacToeGame(
                TicTacToeBoard,
                Player1,
                Player2,
                GameJudge,
                GameRenderer,
                GamePauser
                );
        }
Пример #11
0
        public bool HasWinner(ITicTacToeBoard ticTacToeBoard)
        {
            var lastPlayer  = ticTacToeBoard.GetPlayedCells().Last().State;
            var coordinates = ticTacToeBoard.GetPlayedCells().Where(move => move.State == lastPlayer)
                              .Select(move => move.Coordinates)
                              .ToList();
            var hasWinningLine = new List <bool>
            {
                CheckForWinningRowWithSameDepth(coordinates),
                CheckForWinningRowWithDifferentDepth(coordinates),
                CheckForWinningColumnWithSameDepth(coordinates),
                CheckForWinningColumnWithDifferentDepth(coordinates),
                CheckForWinningDepth(coordinates),
                CheckForWinningVerticalPrimaryDiagonalLineWithSameDepth(coordinates),
                CheckForWinningVerticalSecondaryDiagonalLineWithSameDepth(coordinates),
                CheckForWinningVerticalPrimaryDiagonalLineWithDifferentDepth(coordinates),
                CheckForWinningVerticalSecondaryDiagonalLineWithDifferentDepth(coordinates)
            };

            return(hasWinningLine.Contains(true));
        }
Пример #12
0
 private static void RenderOneLayerOfBoard(ITicTacToeBoard board, int depth)
 {
     for (var row = 0; row < board.GetBoardSize(); row++)
     {
         for (var col = 0; col < board.GetBoardSize(); col++)
         {
             var symbolOfCurrentPosition = board.GetPlayedCells()
                                           .Where(cell => cell.Coordinates.Row == row && cell.Coordinates.Column == col && cell.Coordinates.Depth == depth)
                                           .Select(move => move.State);
             if (symbolOfCurrentPosition.Any())
             {
                 Console.Write(" " + symbolOfCurrentPosition.First() + " ");
             }
             else
             {
                 Console.Write(" . ");
             }
         }
         Console.WriteLine(Environment.NewLine);
     }
     Console.Write(Environment.NewLine);
 }
Пример #13
0
        /// <summary>
        /// The get winner.
        /// </summary>
        /// <param name="gameBoard">
        /// The game board.
        /// </param>
        /// <returns>
        /// The <see cref="TicTacToePlayers?"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// When gameBoard is null.
        /// </exception>
        public TicTacToePlayers?GetWinner(ITicTacToeBoard gameBoard)
        {
            if (gameBoard == null)
            {
                throw new ArgumentNullException("gameBoard");
            }

            this.gameBoardInternal = gameBoard;

            TicTacToePlayers?cellOccupiedBy;

            for (int column = 0; column < 3; column++)
            {
                cellOccupiedBy = gameBoard.Values[0][column];

                if (cellOccupiedBy != null)
                {
                    if (this.CheckVerticalPlayersIdentical((TicTacToePlayers)cellOccupiedBy, column) ||
                        (column == 0 && this.CheckDiagonalLeanLeftIdentical((TicTacToePlayers)cellOccupiedBy)) ||
                        (column == 2 && this.CheckDiagonalLeanRightIdentical((TicTacToePlayers)cellOccupiedBy)))
                    {
                        return(cellOccupiedBy);
                    }
                }
            }

            for (int row = 0; row < 3; row++)
            {
                cellOccupiedBy = gameBoard.Values[row][0];

                if (cellOccupiedBy != null && this.CheckHorizontalIdentical((TicTacToePlayers)cellOccupiedBy, row))
                {
                    return(cellOccupiedBy);
                }
            }

            return(null);
        }
Пример #14
0
        private void InitializeGame()
        {
            Console.WriteLine("Please write the name of the P1: ");
            _player1 = new HumanPlayer(Console.ReadLine().Trim(), PLAYER1_SIGN);

            Console.WriteLine("One player mode? (y/n): ");
            var  response = Console.ReadLine().Trim().ToLower();
            bool useBot   = response.Equals("y");

            if (useBot)
            {
                Console.WriteLine("Lonely eh? I got you, let's play a game!");
                _player2 = new BotPlayer("BOT", PLAYER2_SIGN);
            }
            else
            {
                Console.WriteLine("Please write the name of the P2: ");
                _player2 = new HumanPlayer(Console.ReadLine().Trim(), PLAYER2_SIGN);
            }

            _currentPlayer  = DeterminePlayerToStart();
            _ticTacToeBoard = new TicTacToeBoard();
        }
Пример #15
0
        /// <summary>
        /// The get winner.
        /// </summary>
        /// <param name="gameBoard">
        /// The game board.
        /// </param>
        /// <returns>
        /// The <see cref="TicTacToePlayers?"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// If gameBoard is null.
        /// </exception>
        public TicTacToePlayers?GetWinner(ITicTacToeBoard gameBoard)
        {
            if (gameBoard == null)
            {
                throw new ArgumentNullException("gameBoard");
            }

            TicTacToePlayers?possibleDiagonalLeanRightWinner = null;
            TicTacToePlayers?possibleDiagonalLeanLeftWinner  = null;
            var possibleColumnWinners = new List <TicTacToePlayers?>(3)
            {
                null, null, null
            };

            for (int row = 0; row < 3; row++)
            {
                TicTacToePlayers?possibleRowWinner = null;

                for (int column = 0; column < 3; column++)
                {
                    TicTacToePlayers?currentSpotOccupiedBy = gameBoard.Values[row][column];

                    // analyze horizontal
                    if (column == 0 && currentSpotOccupiedBy != null)
                    {
                        possibleRowWinner = currentSpotOccupiedBy;
                    }
                    else if (currentSpotOccupiedBy != possibleRowWinner)
                    {
                        possibleRowWinner = null;
                    }
                    else if (possibleRowWinner != null && column == 2)
                    {
                        return(possibleRowWinner);
                    }

                    // analyze vertical
                    TicTacToePlayers?possibleColumnWinner = possibleColumnWinners[column];
                    if (row == 0)
                    {
                        possibleColumnWinners[column] = currentSpotOccupiedBy;
                    }
                    else if (currentSpotOccupiedBy != possibleColumnWinner)
                    {
                        possibleColumnWinners[column] = null;
                    }
                    else if (possibleColumnWinner != null && row == 2)
                    {
                        return(possibleColumnWinner);
                    }

                    // analyze diagonal lean left
                    if (column == row)
                    {
                        if (column == 0)
                        {
                            possibleDiagonalLeanLeftWinner = currentSpotOccupiedBy;
                        }
                        else if (currentSpotOccupiedBy != possibleDiagonalLeanLeftWinner)
                        {
                            possibleDiagonalLeanLeftWinner = null;
                        }
                        else if (possibleDiagonalLeanLeftWinner != null && column == 2)
                        {
                            return(possibleDiagonalLeanLeftWinner);
                        }
                    }

                    // analyze diagonal lean right
                    if (column == row || Math.Abs(row - column) == 2)
                    {
                        if (column == 2)
                        {
                            possibleDiagonalLeanRightWinner = currentSpotOccupiedBy;
                        }
                        else if (currentSpotOccupiedBy != possibleDiagonalLeanRightWinner)
                        {
                            possibleDiagonalLeanRightWinner = null;
                        }
                        else if (possibleDiagonalLeanRightWinner != null && column == 0)
                        {
                            return(possibleDiagonalLeanRightWinner);
                        }
                    }
                }
            }

            return(null);
        }
Пример #16
0
 public TicTacToeGame()
 {
     _ticTacToeRules = new TicTacToeRules();
     _ticTacToeBoard = new TicTacToeCPUBoard();
     _gamePlayers    = new List <IPlayer>();
 }
Пример #17
0
 public TicTacToeGame(ITicTacToeBoard board)
 {
     Board = board;
 }
Пример #18
0
 public bool IsGameInPlay(ITicTacToeBoard ticTacToeBoard)
 {
     var cells = ticTacToeBoard.Cells();
     return AreCellsAvailable(cells)
            && !IsWon(cells);
 }
Пример #19
0
 private bool IsDrawn(ITicTacToeBoard ticTacToeBoard)
 {
     var cells = ticTacToeBoard.Cells();
     return !AreCellsAvailable(cells)
            && !IsWon(cells);
 }
Пример #20
0
 private bool MadeMoveToWin(ITicTacToeBoard ticTacToeBoard)
 {
     foreach (var pattern in _patterns)
     {
         if (CheckIfPatternIsAlmostComplete(pattern, ticTacToeBoard.BoardState, out int[] moveLocation, TicTacToeCellValue.Cross))
Пример #21
0
 public TicTacToeGame(PlayerEnum currentTurnPlayer, ITicTacToeBoard board)
 {
     CurrentPlayer = currentTurnPlayer;
     Board         = board;
 }