Search code examples
iosxcodemultiplayerequals-operator

Moving from AI to Multiplayer Game


I currently have a single player board game with one human player and three AI players. I would like to change that to four human players. How can I achieve that from the code below? Can I simply use and OR statement for the 1st line "currentplayer ==3" and have that read "currentplayer == 3 | 1"

   if (currentPlayer == 3 && ([currentTokens count])){
    for (int count = 0; count < [currentTokens count]; count++) {
        Token *token = (Token*)[currentTokens objectAtIndex:count];
        [token setUserInteractionEnabled:YES];
    }

}
else if ([currentTokens count])//For NonHuman Players
{
    //        NSLog(@"Break3.3");

    int arrLength = [currentTokens count];
    //        NSLog(@"Break3.4 and %i",arrLength);

    ////////////////// AI for NON HUMAN AT OCCURENCE OF SIX OF DICE////////////
    Token *nonHumanToken;
    int tokenAtHomeCount = 0;
    if (firstDiceValue == 6) {
        for (int count = 0; count < [currentTokens count]; count++) {
            Token *selectedToken = (Token*)[currentTokens objectAtIndex:count];
            if (selectedToken.isAtHome) {
                tokenAtHomeCount++;
                nonHumanToken = (Token*)[currentTokens objectAtIndex:count];
                break;
            }
        }

        if (!tokenAtHomeCount) {
            nonHumanToken = (Token*)[currentTokens objectAtIndex:(arc4random()%arrLength)];
        }

    }
    else{
        nonHumanToken = (Token*)[currentTokens objectAtIndex:(arc4random()%arrLength)];
    }
    ////////////////////////////////
    [self performSelector:@selector(courseOfActionWithDelay:) withObject:nonHumanToken afterDelay:1.5];

    //        [self moveToken:nonHumanToken Till:firstDiceValue];

    if ([currentTokens count]) {
        [self DisplayMessageForMovingTokens:currentPlayer];
    }

}

Solution

  • Ideally if you are replacing your AI with human players, you would completely ditch the AI code. I would look into duplicating the human code for each player.

    Say you have:

    if(player==1) {
         player 1's turn stuff here
    }
    

    you would expand off of that and move on to:

    if(player==2) {
         player 2's turn stuff here
    }
    

    If this is to be a turn based game, instead of tracking players, track turns. Turn one would be player one, turn two is player two, and then just reset once you get to the max number of players for the round.