Exemple #1
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);
        }
        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");
        }
 private bool IsDrawn(ITicTacToeBoard ticTacToeBoard)
 {
     var cells = ticTacToeBoard.Cells();
     return !AreCellsAvailable(cells)
            && !IsWon(cells);
 }
 public bool IsGameInPlay(ITicTacToeBoard ticTacToeBoard)
 {
     var cells = ticTacToeBoard.Cells();
     return AreCellsAvailable(cells)
            && !IsWon(cells);
 }