Question: Below is my code for a Tic Tac Toe Application in Visual Studio, using C#. The final product must use random number generator to show
Below is my code for a Tic Tac Toe Application in Visual Studio, using C#. The final product must use random number generator to show game, once "New Game" is clicked. Then a message box showing who one. My code runs the program but nothing appears the random X's and O's do not appear, nor the message. Any ideas on how to get the information to appear on the game board?
public partial class Form1 : Form { int[,] array = new int[3, 3]; int a = 0; int b = 0;
public Form1() { InitializeComponent(); }
private void exitButton_Click(object sender, EventArgs e) { this.Close(); }
private void newButton_Click(object sender, EventArgs e) { //row 1 if (A1.Text != "" && A2.Text != "" && A3.Text != "") { if (A1.Text == A2.Text && A1.Text == A3.Text) { if (A1.Text == "1") { a++; } else { b++; } } } //row 2 if (B1.Text != "" && B2.Text != "" && B3.Text != "") { if (B1.Text == B2.Text && B1.Text == B3.Text) { if (B1.Text == "1") { a++; } else { b++; } } } //row 3 if (C1.Text != "" && C2.Text != "" && C3.Text != "") { if (C1.Text == C2.Text && C2.Text == C3.Text) { if (C1.Text == "1") { a++; } else { b++; } }
//diagonal 1 if (A1.Text != "" && B2.Text != "" && C3.Text != "") { if (A1.Text == "1") { a++; } else { b++; } } } //diagonal 2 if (A3.Text != "" && B2.Text != "" && C1.Text != "") { if (A3.Text == "1") { a++; } else { b++; } }
//column 1 if (A1.Text != "" && B1.Text != "" && C1.Text != "") { if (A1.Text == B1.Text && A1.Text == C1.Text) { if (A1.Text == "1") { a++; } else { b++; }
//column 2 if (A2.Text != "" && B2.Text != "" && C2.Text != "") { if (A2.Text == B2.Text && A2.Text == C2.Text) { if (A2.Text == "1") { a++; } else { b++; } }
//column 3 if (A3.Text != "" && B3.Text != "" && C3.Text != "") { if (A3.Text == B3.Text && A3.Text == C3.Text) { if (A3.Text == "1") { a++; } else { b++; } } if (a > b) { MessageBox.Show("X Wins!"); if (a < b) { MessageBox.Show("O Wins!"); } if (a == b) { MessageBox.Show("Tie!"); }
//create random and array const int ROWS = 3; const int COLUMNS = 3; int[,] array = new int[ROWS, COLUMNS];
Random rand = new Random();
for (int row = 0; row < 3; row++) { for (int col = 0; col < 3; col++) { array[row, col] = rand.Next(0, 2); } //show random A1.Text = array[0, 0].ToString(); A2.Text = array[0, 1].ToString(); A3.Text = array[0, 2].ToString(); B1.Text = array[1, 0].ToString(); B2.Text = array[1, 1].ToString(); B3.Text = array[1, 2].ToString(); C1.Text = array[2, 0].ToString(); C2.Text = array[2, 1].ToString(); C3.Text = array[2, 2].ToString(); } } } } } } }
} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
