Question: I need help with this C# code. Basically, this code is supposed to be a local two player tic tac toe game. I have everything
I need help with this C# code. Basically, this code is supposed to be a local two player tic tac toe game. I have everything setup except the buttons for X and O being disabled after a game is won. I am not sure how to do that. Here is my code. How can I make the button b disable after a game is won? Thanks
namespace WindowsFormsApplication1 { public partial class TicTacToe : Form { bool turn = true; // when its true it's X turn and when false it's Y turn int turn_count = 0; public TicTacToe() { InitializeComponent(); }
private void TicTacToe_Load(object sender, EventArgs e) {
}
// private void exitToolStripMenuItem_Click(object sender, EventArgs e) // { // Application.Exit(); // }
private void button_clicked(object sender, EventArgs e) { Button b = (Button)sender; if (turn) b.Text = "X"; else b.Text = "O"; turn = !turn; b.Enabled = false; turn_count++; WinnerCheck();
}
private void button1_Click(object sender, EventArgs e) { Button d = (Button)sender; d.Enabled = true; turn = true; turn_count = 0;
{ foreach (Control c in Controls) { Button b = (Button)c; b.Enabled = true; b.Text = ""; d.Text = "New Game"; //d.Enabled = true; } } } private void WinnerCheck() { bool win = false; //horizontal if (A1.Text == A2.Text && A2.Text == A3.Text && !A1.Enabled) win = true; else if (B1.Text == B2.Text && B2.Text == B3.Text && !B1.Enabled) win = true; else if (C1.Text == C2.Text && C2.Text == C3.Text && !C1.Enabled) win = true; //vertical else if (A1.Text == B1.Text && B1.Text == C1.Text && !A1.Enabled) win = true; else if (A2.Text == B2.Text && B2.Text == C2.Text && !A2.Enabled) win = true; else if (A3.Text == B3.Text && B3.Text == C3.Text && !A3.Enabled) win = true; //diagonal else if (A1.Text == B2.Text && B2.Text == C3.Text && !A1.Enabled) win = true; else if (A3.Text == B2.Text && B2.Text == C1.Text && !C1.Enabled) win = true;
{ if (win) { String winner = ""; if (turn) winner = "0"; else winner = "X"; MessageBox.Show(winner + " Wins!");
} else {
if (turn_count == 9) MessageBox.Show(" Draw :(");
} } }
// private void Buttonsdisabled()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
