Im doing a tic tac toe game in xcode. this is my code
- (IBAction)c1Button:(id)sender {
if ((status.text = @"X goes now"))
{
c1.text = @"X";
if ([c1.text isEqualToString: @"X"])
{
status.text = @"O goes now";
}
else
{
status.text = @"X goes now";
}
}
else if ((status.text = @"O goes now"))
{
c1.text = @"O";
if ((c1.text = @"O"))
{
status.text = @"X goes now";
}
else
{
status.text = @"O goes now";
}
}
}
When the first cell is clicked a X appears as it should. And the status label changes to O goes now. But the when the cell is clicked it still writes X instead of O. What is wrong?
In the first
ifstatement you assign as string instead of comparing it. This:should be:
And the same goes for the second statement.
Also, it is a better idea to keep the state (as an integer or boolean) and not use the title to resolve the state each time.