按顺序循环数组并重复

时间:2022-06-15 21:38:10

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

球员转1圈

player 2 turn

球员转2圈

player 1 turn

球员转1圈

player 2 turn

球员转2圈

player 1 turn

球员转1圈

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.

以前当我没有和数组工作时,我使用了一个简单的if语句,它检查了一个字符串的值,并计算了它显示的次数,然后添加到此,如果该值小于前一个字符串,则应用确定它是其他球员转身。但是现在我正在使用数组我似乎无法解决如何做到这一点。

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.

编辑当游戏用尽我从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.

每个玩家将轮流接受它,然后玩家1然后玩家2然后玩家3然后回到玩家1和玩家2等等。他们将一次被问到一个问题。

Sorry in advance if this is a very basic question I'm very very new to Xcode my first week now.

提前抱歉,如果这是一个非常基本的问题,我现在第一周对Xcode非常新。

1 个解决方案

#1


1  

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.

你可能想要做的是保持对当前玩家的引用。然后循环问题。如果玩家得到正确的话,继续下一个问题,如果玩家弄错了,那么增加玩家参考。在增加玩家时进行检查,看是否有必要回到玩家#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.

希望这可以帮助。

#1


1  

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.

你可能想要做的是保持对当前玩家的引用。然后循环问题。如果玩家得到正确的话,继续下一个问题,如果玩家弄错了,那么增加玩家参考。在增加玩家时进行检查,看是否有必要回到玩家#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.

希望这可以帮助。