Jonny123
Member
- Joined
- Oct 2, 2015
- Messages
- 13
- Programming Experience
- Beginner
hello,
I am re-creating a application that was posted on youtube. Its a Tic-Tac-Toe game. Here is the link if you are interested:
https://www.youtube.com/watch?v=p3gYVcggQOU
the problem that I am having that happens around 25.28 in the video.
The problem that I am having is that there are 9 buttons in the application to put an "o" or an "X". If there is a winner,
the rest of the buttons are supposed to de-activate by calling a function called disabledButtons. It dose not seem to be working.
AS the chris creates the funtion he realizes that he throws an exception with the function and has to put a try catch into it
because for some reason the menue he has created conflicts with the button.This is the function code:
it gets called in an if statement
I am re-creating a application that was posted on youtube. Its a Tic-Tac-Toe game. Here is the link if you are interested:
https://www.youtube.com/watch?v=p3gYVcggQOU
the problem that I am having that happens around 25.28 in the video.
The problem that I am having is that there are 9 buttons in the application to put an "o" or an "X". If there is a winner,
the rest of the buttons are supposed to de-activate by calling a function called disabledButtons. It dose not seem to be working.
AS the chris creates the funtion he realizes that he throws an exception with the function and has to put a try catch into it
because for some reason the menue he has created conflicts with the button.This is the function code:
C#:
private void disabledButtons()
{
try//edit
{//edit
foreach (Control c in Controls)
{
Button b = (Button)c;
b.Enabled = false;
}//end for each
}// end try
catch { }
}
C#:
private void CheckForWinner()
{
bool there_is_a_winner = false;
//horizontal checks
if ((A1.Text == A2.Text) && (A2.Text == A3.Text) && (!A1.Enabled))
there_is_a_winner = true;
else if ((B1.Text == B2.Text) && (B2.Text == B3.Text) && (!B1.Enabled))
there_is_a_winner = true;
else if ((C1.Text == C2.Text) && (C2.Text == C3.Text) && (!C1.Enabled))
there_is_a_winner = true;
//vertical checks
else if ((A1.Text == B1.Text) && (B1.Text == C1.Text) && (!A1.Enabled))
there_is_a_winner = true;
else if ((A2.Text == B2.Text) && (B2.Text == C2.Text) && (!A2.Enabled))
there_is_a_winner = true;
else if ((A3.Text == B3.Text) && (B3.Text == C3.Text) && (!A3.Enabled))
there_is_a_winner = true;
//Diagonal checks
else if ((A1.Text == B2.Text) && (B2.Text == C3.Text) && (!A1.Enabled))
there_is_a_winner = true;
else if ((A3.Text == B2.Text) && (B2.Text == C1.Text) && (!C1.Enabled))
there_is_a_winner = true;
if (there_is_a_winner)
{
disabledButtons();
String winner = "";
if (turn)
{
winner = player2;
o_win_count.Text = (Int32.Parse(o_win_count.Text) + 1).ToString();
}
else
{
winner = player1;
x_win_count.Text = (Int32.Parse(x_win_count.Text) + 1).ToString();
}
MessageBox.Show(winner + " wins!", "Yay!");
}//end if
else
{
if (turn_count == 9)
{
draw_count.Text = (Int32.Parse(draw_count.Text) + 1).ToString();
MessageBox.Show("Draw!", "Bummer!");
}
}
}