/// <summary> /// Begins the game loop. This method will block the calling thread /// until the game has been completed. /// </summary> /// <returns>True if the players would like to play another game.</returns> public bool BeginLoop() { // All squares will have been exhausted in 9 turns for (int turn = 0; turn < 9; turn++) { int currentPlayer = 0; while (currentPlayer < 2) { Console.Clear(); Console.WriteLine(GameBoard.CreateArtString(Players)); Console.WriteLine($"It is {Players[currentPlayer].Name}'s turn!"); Console.WriteLine($"Your squares are marked with {Players[currentPlayer].Token}"); Console.WriteLine("Please enter a square to mark " + "corresponding to how it appears above:"); try { GameBoard.MarkSquare( Convert.ToInt32(Console.ReadLine()), currentPlayer); } catch (ArgumentOutOfRangeException e) { Console.WriteLine("Please only enter a number 1-9"); Console.WriteLine("Please press a key to try again..."); Console.ReadKey(true); continue; } catch (ArgumentException e) { Console.WriteLine("That square is already marked!"); Console.WriteLine("Please press a key to try again..."); Console.ReadKey(true); continue; } catch (Exception) { Console.WriteLine("Could not understand your input."); Console.WriteLine("Please try again with a number 1-9"); Console.WriteLine("Press any key to continue..."); Console.ReadKey(true); continue; } // Check for a winner int winner = GameBoard.CheckWinningState(); if (winner >= 0) { Console.Clear(); Console.WriteLine(GameBoard.CreateArtString(Players)); Console.WriteLine("\nCongratulations!"); Console.WriteLine($"{Players[currentPlayer].Name} has won!"); Console.WriteLine("Would you like to play again? (Y/N)"); // Keep reading keys until either Y or N is pressed ConsoleKey response; do { response = Console.ReadKey(true).Key; } while (response != ConsoleKey.Y && response != ConsoleKey.N); return(response == ConsoleKey.Y); } currentPlayer++; } } // If the number of turns have been exhausted, tell the user that // the game has resulted in a tie Console.Clear(); Console.WriteLine(GameBoard.CreateArtString(Players)); Console.WriteLine("\nThe game has resulted in a tie!"); Console.WriteLine("Would you like to play again? (Y/N)"); // Keep reading keys until either Y or N is pressed ConsoleKey tieResponse; do { tieResponse = Console.ReadKey(true).Key; } while (tieResponse != ConsoleKey.Y && tieResponse != ConsoleKey.N); return(tieResponse == ConsoleKey.Y); }