using System; using System.Numerics; using System.Reflection.Metadata.Ecma335; namespace…
- User
using System; using System.Numerics; using System.Reflection.Metadata.Ecma335;
namespace TicTacToe { class Program { private static char[,] _board = new char[3, 3]; private static char player;
private static void Main(string[] args) { InitBoard(); DisplayBoard(); var counter = 0; while (BoardHasAnyEmptyCell() || HasWinner()) { var player = counter % 2 == 0 ? 'x' : 'o'; Console.WriteLine("Ir " + player + " kārta gājienam"); Console.WriteLine("Ievadi kooridinātes formātā X Y."); var input = Console.ReadLine(); var coords = input.Split(' '); var x = int.Parse(coords[0]); var y = int.Parse(coords[1]); if (x <= 2 && y <= 2 && x >= 0 && y >= 0 && _board[x, y] == ' ') { _board[x, y] = player; } counter++; DisplayBoard(); } } private static bool HasWinner() { for (int row = 0; row < 3; row++) { if (_board[row, 0] == player && _board[row, 1] == player && _board[row, 2] == player) { return true; } } for (int col = 0; col < 3; col++) { if (_board[0, col] == player && _board[1, col] == player && _board[2, col] == player) { return true; } } if (_board[0, 0] == player && _board[1, 1] == player && _board[2, 2] == player) { return true; } if (_board[0, 2] == player && _board[1, 1] == player && _board[2, 0] == player) { return true; } return true; } private static bool BoardHasAnyEmptyCell() { for (var r = 0; r < 3; r++) { for (var c = 0; c < 3; c++) { if (_board[r, c] == ' ') { Console.WriteLine(_board[r, c]); return true; } } } return false; } private static void InitBoard() { // fills up the board with blanks for (var r = 0; r < 3; r++) { for (var c = 0; c < 3; c++) _board[r, c] = ' '; } } private static void DisplayBoard() { Console.WriteLine(" 0 " + _board[0, 0] + "|" + _board[0, 1] + "|" + _board[0, 2]); Console.WriteLine(" --+-+--"); Console.WriteLine(" 1 " + _board[1, 0] + "|" + _board[1, 1] + "|" + _board[1, 2]); Console.WriteLine(" --+-+--"); Console.WriteLine(" 2 " + _board[2, 0] + "|" + _board[2, 1] + "|" + _board[2, 2]); Console.WriteLine(" --+-+--"); } }} Please analyze the classic tic tac toe game code and show fixies.
- Assistant
Here are some fixes and improvements for the given Tic Tac Toe game code:
-
Remove the unnecessary line
using System.Numerics;as it is not used in the code. -
Change the initialization of the
playervariable inside theMainmethod tovar player = counter % 2 == 0 ? 'x' : 'o';. This will ensure that the correct player is assigned for each turn.
-