i want to now how i can have exemple c# code game my_tic_tac_toe console.
Loading
i want to now how i can have exemple c# code game my_tic_tac_toe console.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Deepak RawatRank #646 · 3.7K reputationPosted Apr 18, 2023, 2:46 PM EDT
using System; class Program { static char[,] board = new char[3, 3]; // 2D array to represent the Tic-Tac-Toe board static void Main() { InitializeBoard(); PrintBoard(); PlayGame(); } static void InitializeBoard() { // Fill the board with empty spaces for (int row = 0; row < 3; row++) { for (int col = 0; col < 3; col++) { board[row, col] = ' '; } } } static void PrintBoard() { // Print the current state of the board to the console for (int row = 0; row < 3; row++) { Console.WriteLine(" {0} | {1} | {2} ", board[row, 0], board[row, 1], board[row, 2]); if (row < 2) { Console.WriteLine("-----------"); } } } static void PlayGame() { char currentPlayer = 'X'; // Start with player X while (true) // Keep playing until the game is over { Console.WriteLine("Player {0}'s turn. Enter row and column numbers (e.g. 0 2):", currentPlayer); string[] input = Console.ReadLine().Split(); int row = int.Parse(input[0]); int col = int.Parse(input[1]); if (board[row, col] != ' ') // If the selected cell is already taken, ask the player to choose again { Console.WriteLine("That cell is already taken. Try again."); } else { board[row, col] = currentPlayer; // Set the cell to the player's symbol PrintBoard(); // Check if the game is over if (CheckForWin(currentPlayer)) { Console.WriteLine("Player {0} wins!", currentPlayer); break; } else if (CheckForDraw()) { Console.WriteLine("It's a draw!"); break; } // Switch to the other player if (currentPlayer == 'X') { currentPlayer = 'O'; } else { currentPlayer = 'X'; } } } } static bool CheckForWin(char player) { // Check rows for (int row = 0; row < 3; row++) { if (board[row, 0] == player && board[row, 1] == player && board[row, 2] == player) { return true; } } // Check columns for (int col = 0; col < 3; col++) { if (board[0, col] == player && board[1, col] == player && board[2, col] == player) { return true; } } // Check diagonals 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 false; } static bool CheckForDraw() { // Check if all cells areTuhin PaulRank #7 · 71.9K reputationPosted Apr 18, 2023, 10:16 PM EDT
Tic Tac Toe game in C# console application
using System; namespace TicTacToe { class Program { static char[,] board = new char[3, 3]; static char currentPlayer = 'X'; static void Main(string[] args) { InitializeBoard(); PrintBoard(); while (!IsGameOver()) { Console.WriteLine($"Player {currentPlayer}'s turn:"); Console.Write("Enter row number (0-2): "); int row = int.Parse(Console.ReadLine()); Console.Write("Enter column number (0-2): "); int col = int.Parse(Console.ReadLine()); if (board[row, col] != ' ') { Console.WriteLine("Invalid move. That spot is already taken!"); continue; } board[row, col] = currentPlayer; PrintBoard(); if (HasPlayerWon()) { Console.WriteLine($"Player {currentPlayer} wins!"); return; } SwitchPlayer(); } Console.WriteLine("Game over. It's a draw!"); } static void InitializeBoard() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { board[i, j] = ' '; } } } static void PrintBoard() { Console.WriteLine(" 0 1 2"); for (int i = 0; i < 3; i++) { Console.Write($"{i} "); for (int j = 0; j < 3; j++) { Console.Write($"[{board[i, j]}]"); } Console.WriteLine(); } } static bool IsGameOver() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i, j] == ' ') { return false; } } } return true; } static bool HasPlayerWon() { for (int i = 0; i < 3; i++) { if (board[i, 0] == currentPlayer && board[i, 1] == currentPlayer && board[i, 2] == currentPlayer) { return true; } if (board[0, i] == currentPlayer && board[1, i] == currentPlayer && board[2, i] == currentPlayer) { return true; } } if (board[0, 0] == currentPlayer && board[1, 1] == currentPlayer && board[2, 2] == currentPlayer) { return true; } if (board[0, 2] == currentPlayer && board[1, 1] == currentPlayer && board[2, 0] == currentPlayer) { return true; } return false; } static void SwitchPlayer() { currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; } } }Tuhin PaulRank #7 · 71.9K reputationPosted Apr 18, 2023, 10:16 PM EDT
This is a console-based implementation of the classic game "Tic Tac Toe" using C#. The program allows two players to take turns placing their marks (X or O) on a 3x3 game board until either one of them wins by placing three marks in a row, column or diagonal or the board is full resulting in a tie game. The game board is represented by a 2-dimensional char array of size 3x3, and the current player is represented by a char variable.
different methods used in the program:
1. Main method: This is the entry point of the program where the game loop starts. The game board is initialized, and the board is printed using the PrintBoard() method. It then enters into a while loop that continues until either one of the players wins or the board is full.
2. InitializeBoard() method: This method initializes the game board by setting all the elements in the 2-dimensional array to empty space.
3. PrintBoard() method: This method prints the game board on the console.
4. IsGameOver() method: This method checks if the game is over by iterating through the game board and checking if any cell is empty. If there are any empty cells, the method returns false. Otherwise, it returns true, indicating that the game is over.
5. HasPlayerWon() method: This method checks if the current player has won the game by iterating through the game board and checking for three consecutive marks in a row, column, or diagonal. If the current player has won, the method returns true; otherwise, it returns false.
6. SwitchPlayer() method: This method switches the current player from X to O or vice versa after each turn.
The program takes input from the user by asking for the row and column number to place the mark. It checks if the chosen cell is empty and updates the game board accordingly. If the move is invalid, it prints an error message and prompts the player to choose a valid move. When the game is over, it prints the winner's name or declares a tie if no one won.
Amit MohantyRank #112 · 20K reputationPosted Apr 19, 2023, 12:06 AM EDT
using System; namespace MyConsoleApp { class TicTacToe { static char[,] board = new char[3, 3]; // 2D array to represent the game board static char player = 'X'; // the current player static bool gameover = false; // flag to indicate if the game is over public static void StartGame() { InitializeBoard(); DisplayBoard(); while (!gameover) { Console.WriteLine($"Player {player}'s turn. Enter row and column (e.g. 1 1):"); string input = Console.ReadLine(); if (int.TryParse(input[0].ToString(), out int row) && int.TryParse(input[2].ToString(), out int col)) { if (row >= 1 && row = 1 && col <= 3) { row--; // convert 1-based index to 0-based index col--; if (board[row, col] == '\0') { board[row, col] = player; DisplayBoard(); if (CheckWin()) { Console.WriteLine($"Player {player} wins!"); gameover = true; } else if (CheckTie()) { Console.WriteLine("It's a tie!"); gameover = true; } else { player = player == 'X' ? 'O' : 'X'; // switch to the other player } } else { Console.WriteLine("That spot is already taken. Try again."); } } else { Console.WriteLine("Invalid input. Try again."); } } else { Console.WriteLine("Invalid input. Try again."); } } Console.WriteLine("Press any key to exit."); Console.ReadKey(); } static void InitializeBoard() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { board[i, j] = '\0'; // empty cell } } } static void DisplayBoard() { Console.WriteLine(" 1 2 3"); for (int i = 0; i < 3; i++) { Console.Write($"{i + 1} "); for (int j = 0; j < 3; j++) { Console.Write(board[i, j] == '\0' ? "- " : $"{board[i, j]} "); } Console.WriteLine(); } Console.WriteLine(); } static bool CheckWin() { // check rows for (int i = 0; i < 3; i++) { if (board[i, 0] != '\0' && board[i, 0] == board[i, 1] && board[i, 1] == board[i, 2]) { return true; } } // check columns for (int j = 0; j < 3; j++) { if (board[0, j] != '\0' && board[0, j] == board[1, j] && board[1, j] == board[2, j]) { return true; } } // check diagonals if (board[0, 0] != '\0' && board[0, 0] == board[1, 1] && board[1, 1] == board[2, 2]) { return true; } if (board[0, 2] != '\0' && board[0, 2] == board[1, 1] && board[1, 1] == board[2, 0]) { return true; } return false; } static bool CheckTie() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i, j] == '\0') { return false; // there are still empty cells, not a tie } } } return true; // all cells are taken, it's a tie } } }Raj BhattRank #513 · 4.8K reputationPosted Apr 19, 2023, 3:29 AM EDT
using System;
using System.Threading;
namespace TicTacToeGame
{
class Program
{
//making array and
//by default I am providing 0-9 where no use of zero
static readonly char[] arr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
static int player = 1; //By default player 1 is set
static int choice; //This holds the choice at which position user want to mark
// The flag variable checks who has won if it's value is 1 then someone has won the match
//if -1 then Match has Draw if 0 then match is still running
static int flag = 0;
static void Main(string[] args)
{
do
{
Console.Clear();// whenever loop will be again start then screen will be clear
Console.WriteLine("Player1:X and Player2:O");
Console.WriteLine("\n");
if (player % 2 == 0)//checking the chance of the player
{
Console.WriteLine("Player 2 Chance");
}
else
{
Console.WriteLine("Player 1 Chance");
}
Console.WriteLine("\n");
Board();// calling the board Function
choice = int.Parse(Console.ReadLine());//Taking users choice
// checking that position where user want to run is marked (with X or O) or not
if (arr[choice] != 'X' && arr[choice] != 'O')
{
if (player % 2 == 0) //if chance is of player 2 then mark O else mark X
{
arr[choice] = 'O';
player++;
}
else
{
arr[choice] = 'X';
player++;
}
}
else
//If there is any possition where user want to run
//and that is already marked then show message and load board again
{
Console.WriteLine("Sorry the row {0} is already marked with {1}", choice, arr[choice]);
Console.WriteLine("\n");
Console.WriteLine("Please wait 2 second board is loading again.....");
Thread.Sleep(2000);
}
flag = CheckWin();// calling of check win
}
while (flag != 1 && flag != -1);
// This loop will be run until all cell of the grid is not marked
//with X and O or some player is not win
Console.Clear();// clearing the console
Board();// getting filled board again
if (flag == 1)
// if flag value is 1 then someone has win or
//means who played marked last time which has win
{
Console.WriteLine("Player {0} has won", (player % 2) + 1);
}
else// if flag value is -1 the match will be draw and no one is winner
{
Console.WriteLine("Draw");
}
Console.ReadLine();
}
// Board method which creats board
private static void Board()
{
Console.WriteLine(" | | ");
Console.WriteLine(" {0} | {1} | {2}", arr[1], arr[2], arr[3]);
Console.WriteLine("_____|_____|_____ ");
Console.WriteLine(" | | ");
Console.WriteLine(" {0} | {1} | {2}", arr[4], arr[5], arr[6]);
Console.WriteLine("_____|_____|_____ ");
Console.WriteLine(" | | ");
Console.WriteLine(" {0} | {1} | {2}", arr[7], arr[8], arr[9]);
Console.WriteLine(" | | ");
}
//Checking that any player has won or not
private static int CheckWin()
{
#region Horzontal Winning Condtion
//Winning Condition For First Row
if (arr[1] == arr[2] && arr[2] == arr[3])
{
return 1;
}
//Winning Condition For Second Row
else if (arr[4] == arr[5] && arr[5] == arr[6])
{
return 1;
}
//Winning Condition For Third Row
else if (arr[6] == arr[7] && arr[7] == arr[8])
{
return 1;
}
#endregion
#region vertical Winning Condtion
//Winning Condition For First Column
else if (arr[1] == arr[4] && arr[4] == arr[7])
{
return 1;
}
//Winning Condition For Second Column
else if (arr[2] == arr[5] && arr[5] == arr[8])
{
return 1;
}
//Winning Condition For Third Column
else if (arr[3] == arr[6] && arr[6] == arr[9])
{
return 1;
}
#endregion
#region Diagonal Winning Condition
else if (arr[1] == arr[5] && arr[5] == arr[9])
{
return 1;
}
else if (arr[3] == arr[5] && arr[5] == arr[7])
{
return 1;
}
#endregion
#region Checking For Draw
// If all the cells or values filled with X or O then any player has won the match
else if (arr[1] != '1' && arr[2] != '2' && arr[3] != '3' && arr[4] != '4' && arr[5] != '5' && arr[6] != '6' && arr[7] != '7' && arr[8] != '8' && arr[9] != '9')
{
return -1;
}
#endregion
else
{
return 0;
}
}
}
}