visual studio C# 2012 tic tac toe simulator Hi, i am doing a tic tac toe simulat
ID: 3674060 • Letter: V
Question
visual studio C# 2012 tic tac toe simulator
Hi, i am doing a tic tac toe simulator on visual studio, and we are using WPF.
I did mine to a point where I dont understand how to make the label tell or say X win or O win. I show you guys on how far I am, but now I am stuck. I hope someone can show me a demo on the tic tac toe simulator or show an example, but here is mine, it not finish, so I need help.
private void btnNewGame_Click(object sender, RoutedEventArgs e)
{
Random rand = new Random();
const int ROWS = 3;
const int COLS = 9;
int[,] values = new int[ROWS, COLS];
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
values[row, col] = rand.Next(2);
lblA1.Content = (values[row, col] == 0) ? "O" : "X";
values[row, col] = rand.Next(2);
lblA2.Content = (values[row, col] == 0) ? "O" : "X";
values[row, col] = rand.Next(2);
lblA3.Content = (values[row, col] == 0) ? "O" : "X";
values[row, col] = rand.Next(2);
lblB1.Content = (values[row, col] == 0) ? "O" : "X";
values[row, col] = rand.Next(2);
lblB2.Content = (values[row, col] == 0) ? "O" : "X";
values[row, col] = rand.Next(2);
lblB3.Content = (values[row, col] == 0) ? "O" : "X";
values[row, col] = rand.Next(2);
lblC1.Content = (values[row, col] == 0) ? "O" : "X";
values[row, col] = rand.Next(2);
lblC2.Content = (values[row, col] == 0) ? "O" : "X";
values[row, col] = rand.Next(2);
lblC3.Content = (values[row, col] == 0) ? "O" : "X";
}
if ((lblA1.Content == lblA2.Content) && (lblA2.Content == lblA3.Content))
{
lblAnswer.Content = "win";
}
else
lblAnswer.Content = "It a Tie";
if ((lblB1.Content == lblB2.Content) && (lblB2.Content == lblB3.Content))
{
lblAnswer.Content = "win";
}
else lblAnswer.Content = "It a tie";
Explanation / Answer
Ok, you already filled the Tic Tac Toe board with random values. Only thing needed winning cases check..
Comments added and modified code i marked as bold
private void btnNewGame_Click(object sender, RoutedEventArgs e)
{
Random rand = new Random();
const int ROWS = 3;
const int COLS = 9;
int[,] values = new int[ROWS, COLS];
//filling tic tac toe table
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
values[row, col] = rand.Next(2);
lblA1.Content = (values[row, col] == 0) ? "O" : "X";
values[row, col] = rand.Next(2);
lblA2.Content = (values[row, col] == 0) ? "O" : "X";
values[row, col] = rand.Next(2);
lblA3.Content = (values[row, col] == 0) ? "O" : "X";
values[row, col] = rand.Next(2);
lblB1.Content = (values[row, col] == 0) ? "O" : "X";
values[row, col] = rand.Next(2);
lblB2.Content = (values[row, col] == 0) ? "O" : "X";
values[row, col] = rand.Next(2);
lblB3.Content = (values[row, col] == 0) ? "O" : "X";
values[row, col] = rand.Next(2);
lblC1.Content = (values[row, col] == 0) ? "O" : "X";
values[row, col] = rand.Next(2);
lblC2.Content = (values[row, col] == 0) ? "O" : "X";
values[row, col] = rand.Next(2);
lblC3.Content = (values[row, col] == 0) ? "O" : "X";
}
}
//checking player 1 winning chances
if ((lblA1.Content == lblA2.Content == lblA3.Content == "O"))
{
lblAnswer.Content = "winer player 1 ("O")";
}
else if ((lblA1.Content == lblB1.Content == lblC1.Content == "O"))
{
lblAnswer.Content = "winer player 1 ("O")";
}
else if ((lblA3.Content == lblB3.Content == lblC3.Content == "O"))
{
lblAnswer.Content = "winer player 1 ("O")";
}
else if ((lblC1.Content == lblC2.Content == lblC3.Content == "O"))
{
lblAnswer.Content = "winer player 1 ("O")";
}
else if ((lblA1.Content == lblB2.Content == lblC3.Content == "O"))
{
lblAnswer.Content = "winer player 1 ("O")";
}
else if ((lblA3.Content == lblB2.Content == lblC1.Content == "O"))
{
lblAnswer.Content = "winer player 1 ("O")";
}
//checking for player2 winning chances
else if ((lblA1.Content == lblA2.Content == lblA3.Content == "X"))
{
lblAnswer.Content = "winer player 1 ("X")";
}
else if ((lblA1.Content == lblB1.Content == lblC1.Content == "X"))
{
lblAnswer.Content = "winer player 1 ("X")";
}
else if ((lblA3.Content == lblB3.Content == lblC3.Content == "X"))
{
lblAnswer.Content = "winer player 1 ("X")";
}
else if ((lblC1.Content == lblC2.Content == lblC3.Content == "X"))
{
lblAnswer.Content = "winer player 1 ("X")";
}
else if ((lblA1.Content == lblB2.Content == lblC3.Content == "X"))
{
lblAnswer.Content = "winer player 1 ("X")";
}
else if ((lblA3.Content == lblB2.Content == lblC1.Content == "X"))
{
lblAnswer.Content = "winer player 1 ("X")";
}
//finally of not met any condition then it is a tie
else lblAnswer.Content = "It a tie";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.