Windows application form C# Tic-Tac-Toe Simulator Create an application that sim
ID: 3850647 • Letter: W
Question
Windows application form C# Tic-Tac-Toe Simulator Create an application that simulates a game of tic-tac-toe. Figure 7-45 shows a example of the application's form. The form shown in the figure uses eight large Label controls to display the Xs and Os. The application should use a two-dimensional int array to simulate the game board in memory. When the user clicks the New Game button, the application should step through the array, storing a random number in the range of 0 through 1 in each element. The number 0 represents the letter O, and the number 1 represents the letter X. The form should then be updated to display the game board. The application should display a message indicating whether player X won, player Y won, or the game was a tie.Explanation / Answer
The required code is as follows for the TicTacToe game in C# using the Windows Form. Hence , the code goes as follows:
TTT.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TTT
{
public partial class Form1 : Form
{
bool isChance = true; // Boolean value to know whose turn is it
int ChanceCnt = 0; // To count the chances played in the game.
public Form1()
{
InitializeComponent();
}
// To make the input in the grid
private void button_click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (isChance)
{
btn.Text = "X";
btn.BackColor = Color.Red;
}
else
{
btn.Text = "O";
btn.BackColor = Color.Black;
}
isChance = !isChance;
btn.Enabled = false;
ChanceCnt++;
Victorious(); // Calling the function to know who wins the game
}
private void Victorious()
{
bool Winners = false; // To initialize with no winners
// Conditions for winning the game
if ((R1.Text == R2.Text) && (R2.Text == R3.Text) && (!R1.Enabled))
Winners = true;
else if ((S1.Text == S2.Text) && (S2.Text == S3.Text) && (!S1.Enabled))
Winners = true;
else if ((T1.Text == T2.Text) && (T2.Text == T3.Text) && (!T1.Enabled))
Winners = true;
else if ((R1.Text == S1.Text) && (S1.Text == T1.Text) && (!R1.Enabled))
Winners = true;
else if ((R2.Text == S2.Text) && (S2.Text == T2.Text) && (!R2.Enabled))
Winners = true;
else if ((R3.Text == S3.Text) && (S3.Text == T3.Text) && (!R3.Enabled))
Winners = true;
else if ((R1.Text == S2.Text) && (S2.Text == T3.Text) && (!R1.Enabled))
Winners = true;
else if ((R3.Text == S2.Text) && (S2.Text == T1.Text) && (!T2.Enabled))
Winners = true;
if (Winners)
{
disBtn(); // To disable the buttons call the function on winning
string wins = "";
if (isChance) //Deciding which side wins and displaying it.
wins = "O";
else
wins = "X";
MessageBox.Show(wins + " You Win!", "TicTacToe Game");
}
else // Condition for the Game Draw
{
if (ChanceCnt == 9)
MessageBox.Show("Game Draws!", "TicTacToe Game");
}
}
private void disBtn() // To select the options on the screen whether to start New Game or Exit.
{
foreach (Control cont in Controls)
{
Button btn = (Button)cont;
if (btn.Text != "New Game" && btn.Text != "Exit")
{
btn.Enabled = false;
}
}
}
private void btnClose_Click(object sender, EventArgs e) // For Exitting the Application
{
Application.Exit();
}
private void btnNG_Click(object sender, EventArgs e) // To start the new game
{
isChance = true;
ChanceCnt = 0;
foreach (Control cont in Controls)
{
Button btn = (Button)cont;
if(btn.Text!= "New Game" && btn.Text != "Exit")
{
btn.Enabled = true;
btn.Text = "";
btn.BackColor = System.Drawing.Color.White;
}
}
}
}
}
Hence, this is the code for the Tic Tac Toe game in C#.
Please rate the answer if it helped.....Thankyou
Hope it helps.....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.