0

I am making a board game style game. There is two players, player1 and player 2. Player 1 starts by pressing the player 1 play button. Their sprite moves forward a random number of tiles. Then player 2 starts their turn by pressing the player 1 play button. At the end of each turn I logged whose turn it was when I pressed the player 2 play button after pressing the player 1 play button. It logs saying it is still player 1's turn. Why is this? This is my code:

- (void)player1play {
isPlayer1turn = YES;
CCLOG(@"Beggining of player 1's turn");
NSLog(@"%hhd", isPlayer1turn);
if (_randNumLabel == nil)
{
    CCLOG(@"Nil");
}
if (_randNumLabel !=nil)
{
CCLOG(@"play button pressed!");
int max = 6;
int randNumber = (arc4random() % max) + 1; // Generates a number between 1-6.
CCLOG(@"Random Number %d", randNumber);
_randNumLabel.string = [NSString stringWithFormat:@"Number: %d", randNumber];
}
//Movement code
isPlayer1turn = NO;
CCLOG(@"End of player 1's turn");
NSLog(@"%hhd", isPlayer1turn);

}

- (void)player2play {
if (isPlayer1turn == YES)
{
    isPlayer1turn = NO;
    CCLOG(@"Beggining of player 2's turn");
    NSLog(@"%hhd", isPlayer1turn);
    if (_randNumLabel == nil)
{
    CCLOG(@"Nil");
}
if (_randNumLabel !=nil)
{
    CCLOG(@"play button pressed!");
    int max = 6;
    int randNumber = (arc4random() % max) + 1; // Generates a number between 1-6.
    CCLOG(@"Random Number %d", randNumber);
    _randNumLabel.string = [NSString stringWithFormat:@"Number: %d", randNumber];
}
isPlayer1turn = TRUE;
    CCLOG(@"End of player 2's turn");
NSLog(@"%hhd", isPlayer1turn);
}
}

How can I fix this? Or would it be better to have just one play button?

After pressing the player 1 play button:

014-06-18 20:50:33.450 Sunk[8091:60b] Beggining of player 1's turn
2014-06-18 20:50:33.451 Sunk[8091:60b] 1
2014-06-18 20:50:33.451 Sunk[8091:60b] play button pressed!
2014-06-18 20:50:33.452 Sunk[8091:60b] Random Number 1
2014-06-18 20:50:33.453 Sunk[8091:60b] End of player 1's turn
2014-06-18 20:50:33.454 Sunk[8091:60b] 0

After pressing the player 2 play button:

2014-06-18 20:52:26.666 Sunk[8091:60b] 1
2014-06-18 20:52:26.667 Sunk[8091:60b] play button pressed!
2014-06-18 20:52:26.668 Sunk[8091:60b] Random Number 5
2014-06-18 20:52:26.668 Sunk[8091:60b] End of player 1's turn
2014-06-18 20:52:26.669 Sunk[8091:60b] 0
jakesan700
  • 56
  • 9

1 Answers1

0

Here's what your code is doing in actuality:

  1. player1Play
  2. set isPlayer1Turn = YES
  3. set isPlayer1Turn = NO
  4. player2Play
  5. set isPlayer1Turn = YES
  6. NSLOG isPlayer1Turn

I'm not sure why you are bothering with tracking playerTurn, since you have 2 buttons dedicated to the task, and don't prevent the button use if it's not the right player's turn, but the if statement in -(void)player2Play:

if (isPlayer1turn == YES)

is probably supposed to be isPlayer1Turn == NO for your specific logic. But instead of what you are doing, you might want to do something like using your isPlayer1Turn Boolean to determine what a single Play button might look like and do.

-(void)playButtonPressed{
    if (isPlayer1Turn) {
        //do all the things for player 1
        isPlayer1Turn = NO;
    }else{
        //do all the things for player 2
        isPlayer1Turn = YES;
    }
    [self changeTheOneTruePlayButton];
}

-(void)changeTheOneTruePlayButton{
    if (isPlayer1Turn) {
        //change play button text to PLAYER 1
    }else{
        //change play button text to PLAYER 2
    }
}
Cooper Buckingham
  • 2,503
  • 2
  • 15
  • 23