Setting values within a method and the value returned from the method are not the same:
if you have a method called
nnPlayersGame
and you write code to call it:
if (nnPlayersGame())
{
...
}
else
{
...
}
then whether the code follows the "True path" or the "False path" depends only on the value specified in the
return
statement of the method:
public bool nnPlayersGame()
{
return numberOfPlayers > 1;
}
Depending on the value of
numberOfPlayers
the code will follow different paths:
0 False path
1 False path
2 True path
3 True path
So you need to make your methods return the correct value, or check the values that they set instead of calling them again:
private void Button_click(object sender, EventArgs e)
{
Button b = (Button)sender;
if (twoPlayersPlayGame())
{
if (playerTwoToolStripMenuItem.Checked && playerTurn == true)
{
b.Text = "X";
playerTurn = false;
b.Enabled = false;
computerPlayer = true;
}
else
{
b.Text = "O";
playerTurn = true;
b.Enabled = false;
}
clicksCount++;
checkWinner();
}
if (computerToolStripMenuItem.Checked)
{
if (!playerTurn && computerPlayer)
{
computerMove();
p2TextBox2.Text = "Computer";
}
}
}
Should get you a little further.