static void Main(string[] args) { bool gameIsOver = false; Mark currentPlayer = Mark.O; OxoBoard board = new OxoBoard(); while (!gameIsOver) { // Print board Console.WriteLine(); board.PrintBoard(); Console.WriteLine("Player turn: {0}", currentPlayer); // Read input Tuple <int, int> square = null; while (square == null) { Console.WriteLine("Enter a square in the form x,y:"); square = InputSquare(); } // Try to play the move if (board.SetSquare(square.Item1, square.Item2, currentPlayer)) { // Success, so switch players currentPlayer = (currentPlayer == Mark.O) ? Mark.X : Mark.O; // Check for a win var winner = board.GetWinner(); if (winner != Mark.None) { board.PrintBoard(); Console.WriteLine("Player {0} wins!", winner); gameIsOver = true; } // Check for board full if (board.IsBoardFull()) { board.PrintBoard(); Console.WriteLine("It's a draw!"); gameIsOver = true; } } else { Console.WriteLine("Invalid move!"); } } Console.WriteLine("Press enter to continue"); Console.ReadLine(); }
static void Main(string[] args) { bool gameIsOver = false; Mark currentPlayer = Mark.O; // Gets the board width and height from the player. Console.WriteLine("Enter the width of the board: "); int boardWidth = Int32.Parse(Console.ReadLine()); Console.WriteLine("Enter the height of the board: "); int boardHeight = Int32.Parse(Console.ReadLine()); // Gets the target number of symbols in a row. Console.WriteLine("Enter the number of symbols in a row to win: "); int symbolsInARow = Int32.Parse(Console.ReadLine()); OxoBoard board = new OxoBoard(boardWidth, boardHeight, symbolsInARow); // Render loop. while (!gameIsOver) { // Print board Console.WriteLine(); board.PrintBoard(); Console.WriteLine("Player turn: {0}", currentPlayer); // Read input Tuple <int, int> square = null; while (square == null) { Console.WriteLine("Enter a square in the form x,y:"); square = InputSquare(); } // Try to play the move if (board.SetSquare(square.Item1, square.Item2, currentPlayer)) { // Success, so switch players currentPlayer = (currentPlayer == Mark.O) ? Mark.X : Mark.O; // Check for a win var winner = board.GetWinner(); if (winner != Mark.None) { board.PrintBoard(); Console.WriteLine("Player {0} wins!", winner); gameIsOver = true; } // Check for board full if (board.IsBoardFull()) { board.PrintBoard(); Console.WriteLine("It's a draw!"); gameIsOver = true; } } else { Console.WriteLine("Invalid move!"); } } Console.WriteLine("Press enter to continue"); Console.ReadLine(); }
static void Main(string[] args) { bool gameIsOver = false; Mark currentPlayer = Mark.O; Tuple <int, int> boardSize = null; while (boardSize == null) { Console.WriteLine("Enter the size of the square in the form x,y:"); boardSize = InputSquare(); } int inARow = 0; while (inARow == 0) { Console.WriteLine("Enter amount of symbols in a row to win:"); string input = Console.ReadLine(); if (!int.TryParse(input, out inARow)) { Console.WriteLine("Invalid input!"); } else if (inARow == 0 || (inARow > boardSize.Item1 && inARow > boardSize.Item2)) { Console.WriteLine("Invalid input!"); inARow = 0; } } OxoBoard board = new OxoBoard(boardSize.Item1, boardSize.Item2, inARow); while (!gameIsOver) { // Print board Console.WriteLine(); board.PrintBoard(); Console.WriteLine("Player turn: {0}", currentPlayer); // Read input Tuple <int, int> square = null; while (square == null) { Console.WriteLine("Enter a square in the form x,y:"); square = InputSquare(); } // Try to play the move if (board.SetSquare(square.Item1, square.Item2, currentPlayer)) { // Success, so switch players currentPlayer = (currentPlayer == Mark.O) ? Mark.X : Mark.O; // Check for a win var winner = board.GetWinner(); if (winner != Mark.None) { board.PrintBoard(); Console.WriteLine("Player {0} wins!", winner); gameIsOver = true; } // Check for board full if (board.IsBoardFull()) { board.PrintBoard(); Console.WriteLine("It's a draw!"); gameIsOver = true; } } else { Console.WriteLine("Invalid move!"); } } Console.WriteLine("Press enter to continue"); Console.ReadLine(); }