private static void PlayGame() { var board = new UltimateTicTacToeBoard(); ITicTacToePlayer player1 = new HumanConsolePlayer(Player.X); ITicTacToePlayer player2 = new HumanConsolePlayer(Player.O); Console.SetWindowSize(80, 48); Console.WriteLine("Ultimate Tic Tac Toe!!!"); bool correctPlayers = false; while (!correctPlayers) { Console.Write("How Many Players? "); int players; correctPlayers = int.TryParse(Console.ReadLine(), out players); if (correctPlayers) { if (players == 2) { player1 = new HumanConsolePlayer(Player.X); player2 = new HumanConsolePlayer(Player.O); } else if (players == 1) { Console.WriteLine("Do you want to play as X or O?"); var response = Console.ReadLine(); if (response?.StartsWith("x", StringComparison.InvariantCultureIgnoreCase) == true) { player1 = new HumanConsolePlayer(Player.X); player2 = new MinMaxAIPlayer(Player.O); } else if (response?.StartsWith("o", StringComparison.InvariantCultureIgnoreCase) == true) { player1 = new MinMaxAIPlayer(Player.X); player2 = new HumanConsolePlayer(Player.O); } else { correctPlayers = false; } } else if (players == 0) { player1 = new MinMaxAIPlayer(Player.X); player2 = new MinMaxAIPlayer(Player.O); } else { correctPlayers = false; } } if (!correctPlayers) { Console.WriteLine("Sorry kiddums, try again!"); } } var startTime = DateTime.UtcNow; string[] player1LastTaunt = { "" }; Taunt p1Taunt = delegate(string msg) { if (player1LastTaunt[0].Equals(msg)) { return; } player1LastTaunt[0] = msg; Console.WriteLine(msg); }; player1.SetTauntDelegate(p1Taunt); string[] player2LastTaunt = { "" }; Taunt p2Taunt = delegate(string msg) { if (player2LastTaunt[0].Equals(msg)) { return; } player2LastTaunt[0] = msg; Console.WriteLine(msg); }; player2.SetTauntDelegate(p2Taunt); var playerTurn = Player.X; while (board.State == GameState.Open) { player1LastTaunt[0] = ""; player2LastTaunt[0] = ""; var validMove = false; while (!validMove) { var move = playerTurn == Player.X ? player1.TakeTurn(board.Clone(cloneMoves: true)) : player2.TakeTurn(board.Clone(cloneMoves: true)); validMove = board.MakeMove(playerTurn, move.OuterCell, move.InnerCell); if (!validMove) { Console.WriteLine("Sorry kiddums, try again!"); } else { Console.Clear(); Console.WriteLine(GetCurrentGameStats(DateTime.UtcNow.Subtract(startTime))); Console.WriteLine(); Console.WriteLine(board.ToString()); } } playerTurn = playerTurn == Player.X ? Player.O : Player.X; } var gameTime = DateTime.UtcNow.Subtract(startTime); _playtime = _playtime.Add(gameTime); _totalPlaytime = _totalPlaytime.Add(gameTime); string stateStr = null; switch (board.State) { case GameState.Xwin: _xWins++; stateStr = "Player X Wins!"; break; case GameState.Owin: _oWins++; stateStr = "Player O Wins!"; break; case GameState.Open: break; case GameState.Draw: _draws++; stateStr = "DRAW!"; break; default: throw new ArgumentOutOfRangeException(); } Console.Clear(); Console.WriteLine(GetGameStats()); Console.WriteLine(); Console.WriteLine(board.ToString()); Console.WriteLine("Game has Ended: " + stateStr); }
UltimateTicTacToeMove ITicTacToePlayer.TakeTurn(UltimateTicTacToeBoard ticTacToeBoard) { Console.Clear(); Console.WriteLine(ticTacToeBoard.ToString()); Console.WriteLine(); if (ticTacToeBoard.LastMove != null) { Console.WriteLine($"{ticTacToeBoard.LastMove.Player} made the move [{BoardToCell(ticTacToeBoard.LastMove.OuterCell)}:{BoardToCell(ticTacToeBoard.LastMove.InnerCell)}]"); Console.WriteLine(""); } Console.WriteLine($"Player {Player}'s, Please make your move!"); byte outerCell; if (ticTacToeBoard.RequiredOuterCell == null) { Console.WriteLine("You get to play anywhere, choose wisely!"); Console.Write("Outer Cell 1-9: "); byte.TryParse(Console.ReadLine(), out outerCell); outerCell = CellToBoard(outerCell); Console.WriteLine(); } else { outerCell = ticTacToeBoard.RequiredOuterCell.Value; } var msg = "You are now playing in the "; switch (outerCell) { case 0: msg += "upper left cell"; break; case 1: msg += "upper middle cell"; break; case 2: msg += "upper right cell"; break; case 3: msg += "middle left cell"; break; case 4: msg += "center cell"; break; case 5: msg += "middle right cell"; break; case 6: msg += "lower left cell"; break; case 7: msg += "lower center cell"; break; case 8: msg += "lower right cell"; break; default: msg += "DEFAULT ERROR"; break; } Console.WriteLine(); Console.WriteLine(msg); Console.WriteLine(); Console.Write("Inner Cell 1-9: "); byte innerCell; byte.TryParse(Console.ReadLine(), out innerCell); innerCell = CellToBoard(innerCell); ticTacToeBoard.MakeMove(Player, outerCell, innerCell); Console.Clear(); Console.WriteLine(ticTacToeBoard.ToString()); return(new UltimateTicTacToeMove(Player, outerCell, innerCell)); }