Reputation: 25
I am creating a local multiplayer text based game that could involve up to an unlimited number of players. I have created an array to hold all the players names that are inputed by the user on the launch of the app. However I am having problems trying to work out how to display the array index by index and then repeat on its self such as
player 1 turn
player 2 turn
player 1 turn
player 2 turn
player 1 turn
And so forth until all questions have been asked (think of a quiz game)
So far I have come up with this but its not doing the job
int i;
int count;
for (i = 0, count = [_players count]; i < count; i = i + 1)
{
NSString *element = [_players objectAtIndex:i];
self.turnlabel.text =( i, element);
NSLog(@"The element at index %d in the array is: %@", i, element);
}
Previously when I was working without and array I used a simple if statement that checked the value of a string and counted how many times its been shown and then added to this and and if the value was less than the previous string the app determined it was the other players turn. However now I am working with an array I cant seem to work out how to do this.
Basically how would I go about showing each index in array in turn and then repeating until the game ends?
EDIT The game ends when it runs out of questions that I am generating from a plist.
Each player will take it in turns so player 1 then player 2 then player 3 and then back to player 1 and player 2 and so forth. They will be asked one question at a time.
Sorry in advance if this is a very basic question I'm very very new to Xcode my first week now.
Upvotes: 0
Views: 1840
Reputation: 961
What you probably want to do is hold a reference to the current player. Then loop through the questions. If player gets them correct, continue to next question, if player gets it wrong, then increment the player reference. Do a check when you increment the player to see if it's necessary to go back to player #1.
So, for example:
NSArray *playerArray = @[@"Player 1", @"Player 2", @"Player 3"];
NSUInteger currentPlayerIndex = 1;
Player then has his turn, if he gets it incorrect,
currentPlayerIndex++;
Then check:
if (curentPlayerIndex >= playerArray.count) {
currentPlayerIndex = 0;
}
Hope this helps.
Upvotes: 1