将 IF 和 SWITCH 一起用于游戏关卡的最佳方式是什么?
What's the best way to use IF and SWITCH together for game levels?
我的游戏项目中有以下代码可以正常工作,其中一个球会随机显示给玩家。
int random = arc4random_uniform(5);
switch (random)
{
case 0:
{
NSLog(@"Case 0 Ball");
}
break;
case 1:
{
NSLog(@"Case 1 Big Ball");
break;
}
case 2:
{
NSLog(@"Case 2 Center Ball");
break;
}
case 3:
{
NSLog(@"Case 3 Red Ball");
break;
}
case 4:
{
NSLog(@"Case 4 Blue Ball");
break;
}
}
我想做的是限制向玩家显示的潜在球的数量,直到玩家达到一定水平。我认为最简单的方法就是改变随机案例的数量。例如...
int random = arc4random_uniform(1);
int random = arc4random_uniform(2);
int random = arc4random_uniform(3);
int random = arc4random_uniform(4);
int random = arc4random_uniform(5);
当然,问题是当我尝试将这些选项包装在下面的 if statement
中时,它将它与 Switch 分开,这将不起作用。
int unlockBallLevel = [_levels getCurrentLevel];
if (unlockBallLevel > 5) {
int random = arc4random_uniform(5);
}
我想知道在达到特定水平之前限制球数的最合乎逻辑和最有效的方法是什么?
也许:
switch (arc4random_uniform([_levels getCurrentLevel] < 5 ?: 5))
{
...
}
我解决了这个问题。我仍然不知道是否有更有效的方法来执行此操作,但以下代码可以工作并在玩家升级到 75 级时解锁更多球。在 75 级之后,所有五个球都已解锁。
int unlockAtLevel = [_levels getCurrentLevel];
if (unlockAtLevel >= 1)
{
ballsForRandom = 1;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
if (unlockAtLevel >= 10)
{
ballsForRandom = 2;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
if (unlockAtLevel >= 30)
{
ballsForRandom = 3;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
if (unlockAtLevel >= 50)
{
ballsForRandom = 4;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
if (unlockAtLevel >= 75)
{
ballsForRandom = 5;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
int random = arc4random_uniform(ballsForRandom);
switch (random)
{
case 0:
{
NSLog(@"Case 0 Ball");
}
break;
case 1:
{
NSLog(@"Case 1 Big Ball");
break;
}
case 2:
{
NSLog(@"Case 2 Center Ball");
break;
}
case 3:
{
NSLog(@"Case 3 Red Ball");
break;
}
case 4:
{
NSLog(@"Case 4 Blue Ball");
break;
}
}
根据@CRD 的评论,我对代码进行了以下更改。与发布的最后一个答案相比,下面的代码似乎更有效、更智能。
int unlockAtLevel = [_levels getCurrentLevel];
if (unlockAtLevel >= 25)
{
ballsForRandom = 6;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 20)
{
ballsForRandom = 5;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 15)
{
ballsForRandom = 4;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 10)
{
ballsForRandom = 3;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 3)
{
ballsForRandom = 2;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 1)
{
ballsForRandom = 1;
[[GCHelper defaultHelper]reportAchievementIdentifier:achievementID1 percentComplete:100];
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
NSLog(@"\n\n###############################################################\n\nCurrently the player is on level %i so there should be %i main balls unlocked!\n\n###############################################################\n\n",unlockAtLevel,ballsForRandom);
int random = arc4random_uniform(ballsForRandom);
switch (random)
我的游戏项目中有以下代码可以正常工作,其中一个球会随机显示给玩家。
int random = arc4random_uniform(5);
switch (random)
{
case 0:
{
NSLog(@"Case 0 Ball");
}
break;
case 1:
{
NSLog(@"Case 1 Big Ball");
break;
}
case 2:
{
NSLog(@"Case 2 Center Ball");
break;
}
case 3:
{
NSLog(@"Case 3 Red Ball");
break;
}
case 4:
{
NSLog(@"Case 4 Blue Ball");
break;
}
}
我想做的是限制向玩家显示的潜在球的数量,直到玩家达到一定水平。我认为最简单的方法就是改变随机案例的数量。例如...
int random = arc4random_uniform(1);
int random = arc4random_uniform(2);
int random = arc4random_uniform(3);
int random = arc4random_uniform(4);
int random = arc4random_uniform(5);
当然,问题是当我尝试将这些选项包装在下面的 if statement
中时,它将它与 Switch 分开,这将不起作用。
int unlockBallLevel = [_levels getCurrentLevel];
if (unlockBallLevel > 5) {
int random = arc4random_uniform(5);
}
我想知道在达到特定水平之前限制球数的最合乎逻辑和最有效的方法是什么?
也许:
switch (arc4random_uniform([_levels getCurrentLevel] < 5 ?: 5))
{
...
}
我解决了这个问题。我仍然不知道是否有更有效的方法来执行此操作,但以下代码可以工作并在玩家升级到 75 级时解锁更多球。在 75 级之后,所有五个球都已解锁。
int unlockAtLevel = [_levels getCurrentLevel];
if (unlockAtLevel >= 1)
{
ballsForRandom = 1;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
if (unlockAtLevel >= 10)
{
ballsForRandom = 2;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
if (unlockAtLevel >= 30)
{
ballsForRandom = 3;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
if (unlockAtLevel >= 50)
{
ballsForRandom = 4;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
if (unlockAtLevel >= 75)
{
ballsForRandom = 5;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
int random = arc4random_uniform(ballsForRandom);
switch (random)
{
case 0:
{
NSLog(@"Case 0 Ball");
}
break;
case 1:
{
NSLog(@"Case 1 Big Ball");
break;
}
case 2:
{
NSLog(@"Case 2 Center Ball");
break;
}
case 3:
{
NSLog(@"Case 3 Red Ball");
break;
}
case 4:
{
NSLog(@"Case 4 Blue Ball");
break;
}
}
根据@CRD 的评论,我对代码进行了以下更改。与发布的最后一个答案相比,下面的代码似乎更有效、更智能。
int unlockAtLevel = [_levels getCurrentLevel];
if (unlockAtLevel >= 25)
{
ballsForRandom = 6;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 20)
{
ballsForRandom = 5;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 15)
{
ballsForRandom = 4;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 10)
{
ballsForRandom = 3;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 3)
{
ballsForRandom = 2;
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 1)
{
ballsForRandom = 1;
[[GCHelper defaultHelper]reportAchievementIdentifier:achievementID1 percentComplete:100];
NSLog(@"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
NSLog(@"\n\n###############################################################\n\nCurrently the player is on level %i so there should be %i main balls unlocked!\n\n###############################################################\n\n",unlockAtLevel,ballsForRandom);
int random = arc4random_uniform(ballsForRandom);
switch (random)