0

我正在构建一个石头剪刀布游戏,并且我正在尝试实现一个永远不会出现平局的系统。我现在的系统是

if (userSelection == 0)
    computerPick =[self computerGenerateResponce];
    if (computerPick == 0)
        while (computerPick == 0)
            computerPick = [self computerGenerateResponce];

有没有更好的方法来实施这个系统?这可行,但似乎有点笨拙。

4

1 回答 1

1

至少,您可以消除 if(computerPick == 0) 级别,因为这是 while(computerPick == 0) 所做的第一件事。这根本不会影响您的算法。然后,您可以将调用合并到条件检查中,此外,您可以使用隐式布尔转换:

if (!userSelection)
    while(! (computerPick = [self computerGenerateResponce]))

这实际上只是继续为 computerPick 分配一个新的选择,直到它不为零。

于 2012-07-14T15:36:36.130 回答