Hi all! today I would like to show you the game "Tic Tac Toe". this game can be played by two participants. I wrote this code in C#. Well? Go)
class Program
{
static char[,] board = new char[3, 3];
static char currentPlayer = 'X';
static void Main()
{
InitializeBoard();
PlayGame();
}
static void InitializeBoard()
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
board[row, col] = ' ';
}
}
}
static void PlayGame()
{
bool gameEnded = false;
int moves = 0;
while (!gameEnded)
{
PrintBoard();
Console.WriteLine($"\nPlayer {currentPlayer}'s turn. Enter row (1-3) and column (1-3) separated by space:");
string input = Console.ReadLine();
if (input != null)
{
string[] coordinates = input.Split();
if (coordinates.Length == 2 &&
int.TryParse(coordinates[0], out int row) &&
int.TryParse(coordinates[1], out int col) &&
row >= 1 && row <= 3 &&
col >= 1 && col <= 3)
{
row--;
col--;
if (board[row, col] == ' ')
{
board[row, col] = currentPlayer;
moves++;
if (CheckForWin(row, col))
{
gameEnded = true;
Console.WriteLine($"\nPlayer {currentPlayer} wins!");
}
else if (moves == 9)
{
gameEnded = true;
Console.WriteLine("\nIt's a draw!");
}
else
{
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
}
else
{
Console.WriteLine("\nThat cell is already occupied. Please try again.");
}
}
else
{
Console.WriteLine("\nInvalid input. Please enter two numbers from 1 to 3, separated by space.");
}
}
}
PrintBoard();
Console.WriteLine("\nGame over. Press any key to exit.");
Console.ReadKey();
}
static bool CheckForWin(int row, int col)
{
if (board[row, 0] == board[row, 1] && board[row, 1] == board[row, 2])
return true;
if (board[0, col] == board[1, col] && board[1, col] == board[2, col])
return true;
if (row == col && board[0, 0] == board[1, 1] && board[1, 1] == board[2, 2])
return true;
if (row + col == 2 && board[0, 2] == board[1, 1] && board[1, 1] == board[2, 0])
return true;
return false;
}
static void PrintBoard()
{
Console.WriteLine("\n 1 2 3");
for (int row = 0; row < 3; row++)
{
Console.Write($"{row + 1} ");
for (int col = 0; col < 3; col++)
{
Console.Write($"{board[row, col]}");
if (col < 2)
Console.Write("|");
}
Console.WriteLine();
if (row < 2)
Console.WriteLine(" -----");
}
}
}
Now I'll explain the code)
the first thing we do is -->
- Declaration of variables and board array:
static char[,] board = new char[3, 3]; // playing field 3x3
static char currentPlayer = 'X';
board is a two-dimensional char array that is used to store the state of the 3x3 board.
currentPlayer specifies the current player starting the game with the 'X' symbol.
- Main() method:
static void Main()
{
InitializeBoard();
PlayGame();
}
- Main() is the entry point to the program. It calls the InitializeBoard() methods to initialize the board and PlayGame() to start the game.
- InitializeBoard() method:
static void InitializeBoard()
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
board[row, col] = ' ';
}
}
}
- InitializeBoard() fills the board array with space characters ' ', which means empty cells at the beginning of the game.
- PlayGame() method:
static void PlayGame()
{
bool gameEnded = false;
int moves = 0;
while (!gameEnded)
{
PrintBoard();
Console.WriteLine($"\nPlayer {currentPlayer}'s turn. Enter row (1-3) and column (1-3) separated by space:");
string input = Console.ReadLine();
if (input != null)
{
string[] coordinates = input.Split();
if (coordinates.Length == 2 &&
int.TryParse(coordinates[0], out int row) &&
int.TryParse(coordinates[1], out int col) &&
row >= 1 && row <= 3 &&
col >= 1 && col <= 3)
{
row--;
col--;
if (board[row, col] == ' ')
{
board[row, col] = currentPlayer;
moves++;
if (CheckForWin(row, col))
{
gameEnded = true;
Console.WriteLine($"\nPlayer {currentPlayer} wins!");
}
else if (moves == 9)
{
gameEnded = true;
Console.WriteLine("\nIt's a draw!");
}
else
{
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
}
else
{
Console.WriteLine("\nThat cell is already occupied. Please try again.");
}
}
else
{
Console.WriteLine("\nInvalid input. Please enter two numbers from 1 to 3, separated by space.");
}
}
}
PrintBoard();
Console.WriteLine("\nGame over. Press any key to exit.");
Console.ReadKey();
}
gameEnded
is a boolean variable indicating whether the game has ended.moves
is a move counter.The
while (!gameEnded)
loop continues until the game ends.PrintBoard()
displays the current board.Console.ReadLine()
reads the user input.The input is split into row and column coordinates. Their correctness is checked.
If the cell is empty, the current player's move is made.
It checks if the current player has won (CheckForWin
) or if the moves have run out (draw).
If the game is not over, the current player is switched.
- Check for victory
static bool CheckForWin(int row, int col)
{
if (board[row, 0] == board[row, 1] && board[row, 1] == board[row, 2])
return true;
if (board[0, col] == board[1, col] && board[1, col] == board[2, col])
return true;
if (row == col && board[0, 0] == board[1, 1] && board[1, 1] == board[2, 2])
return true;
if (row + col == 2 && board[0, 2] == board[1, 1] && board[1, 1] == board[2, 0])
return true;
return false;
}
- The CheckForWin method checks whether there are three identical characters in a row in a row, column, or diagonal.
- Board output
static void PrintBoard()
{
Console.WriteLine("\n 1 2 3");
for (int row = 0; row < 3; row++)
{
Console.Write($"{row + 1} ");
for (int col = 0; col < 3; col++)
{
Console.Write($"{board[row, col]}");
if (col < 2)
Console.Write("|");
}
Console.WriteLine();
if (row < 2)
Console.WriteLine(" -----");
}
}
- The PrintBoard method displays the current board as a table with row and column delimiters.
after checking the code you can start the game)
Top comments (0)