private static char[,] board =
private static char currentPlayer = 'X';
public static void Main()
} while (!IsGameFinished());
private static void DrawBoard()
Console.WriteLine(" 0 1 2");
for (int row = 0; row < 3; row++)
Console.Write(row + " ");
for (int col = 0; col < 3; col++)
Console.Write(board[row, col]);
Console.WriteLine(" -----");
private static void GetPlayerMove()
Console.WriteLine("Player {currentPlayer}, enter your move (row and column): ");
row = int.Parse(Console.ReadLine());
Console.Write("Column: ");
col = int.Parse(Console.ReadLine());
} while (!IsValidMove(row, col));
board[row, col] = currentPlayer;
private static bool IsValidMove(int row, int col)
if (row < 0 || row >= 3 || col < 0 || col >= 3 || board[row, col] != ' ')
Console.WriteLine("Invalid move. Try again.");
private static bool IsGameFinished()
Console.WriteLine("Player {Player} wins!");
Console.WriteLine("It's a draw!");
private static bool CheckForWinner()
for (int i = 0; i < 3; i++)
if (board[i, 0] == currentPlayer && board[i, 1] == currentPlayer && board[i, 2] == currentPlayer)
if (board[0, i] == currentPlayer && board[1, i] == currentPlayer && board[2, i] == currentPlayer)
if (board[0, 0] == currentPlayer && board[1, 1] == currentPlayer && board[2, 2] == currentPlayer)
if (board[0, 2] == currentPlayer && board[1, 1] == currentPlayer && board[2, 0] == currentPlayer)
private static bool IsBoardFull()
foreach (var cell in board)
private static void SwitchPlayer()
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';