如何改善我的代码(C / Objective C)乒乓球比赛

时间:2018-08-05 08:12:04

标签: objective-c c

我是C和目标C的新手。因此,我正在做小而轻松的任务。 在这一节中,我正在模拟一个乒乓球游戏。我的代码正在做它应该做的所有事情,但我对其结构不满意。

我的代码应涵盖的内容:

  • 随机选择获胜者/失败者
  • 击中21位优胜者将获得荣誉

但是我不满意这一点,即如果双方的得分都为20,那么比赛应该持续到领先两分。 我很确定有更好的方法可以完成该操作。

谢谢

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {

    int player1 = 0;
    int player2 = 0;



    while (player1 < 21 && player2 < 21){

            if(player1 == 20 && player2 == 20){
                do{
                    int pick = arc4random()%2;

                    if (pick == 0) {
                        player1 = player1 + 1;
                    } else if (pick == 1){
                        player2 = player2 + 1;
                    }
                }while (player1 == player2 - 2 || player2 == player1 - 2);
            }

        int pick = arc4random()%2;

        if (pick == 0) {
            player1 = player1 + 1;
        } else if (pick == 1){
            player2 = player2 + 1;
        }

    }

    if (player1 > player2){
       NSLog(@"Player 1 has %i points and Player 2 %i", player1, player2);
    } else if (player2 > player1){
        NSLog(@"Player 2 has %i points and Player1 %i",player2, player1);
    }


  return 0;
}

4 个答案:

答案 0 :(得分:0)

我将通过使用players数组而不是player1player2变量来改进此代码。 players有两个项目,第一个被索引为0(玩家1),第二个被索引为1。

这样,您就可以进行更改

                if (pick == 0) {
                    player1 = player1 + 1;
                } else if (pick == 1){
                    player2 = player2 + 1;
                }

players[pick]++;

另一个问题是,即使得分为20到20,您仍在随机化,这是不必要的。将随机代码移到返回0或1的函数中会很有帮助。

答案 1 :(得分:0)

当两个玩家的平均得分都等于20时,只需将两个玩家的得分减至19。

while (player1 < 21 && player2 < 21){               
    int pick = arc4random()%2;    
    if (pick == 0) {
        player1 = player1 + 1;
    } else if (pick == 1){
        player2 = player2 + 1;
    }        
    if(player1 == 20 && player2 == 20){
        player1 = 19;
        player2 = 19;
    }    
}

答案 2 :(得分:0)

我将提取代码以选择玩家是否赢了一个方法,然后为玩家1和玩家2调用相同的代码。另一个外观上的变化是使用开关盒而不是其他情况。 代码如下所示:

static int player_won( int player_score, int other_player_score )
{
    if ( player_score < 21 )
        return 0;
    //player score is 21 or more.

    if ( other_player_score < 20 )
        return 1;

    //other player score is more than 20, must have at least 2 points more
    int diff = player_score - other_player_score;

    if ( diff >= 2 )
        return 1;

    return 0;
}

int main(int argc, const char * argv[]) {

    int player1 = 0;
    int player2 = 0;

    while ( 1 )
    {
        if ( player_won( player1, player2 ) ||
             player_won( player2, player1 ) )
            break;

        int pick = arc4random()%2;
        switch ( pick )
        {
        case 0:
            player1++;
            break;
        case 1:
            player2++;
            break;
        }
    }

    if (player1 > player2){
       NSLog(@"Player 1 has %i points and Player 2 %i", player1, player2);
    } else if (player2 > player1){
        NSLog(@"Player 2 has %i points and Player1 %i",player2, player1);
    }
  return 0;
}

答案 3 :(得分:0)

如果差异小于两点,不要停止:

int main(int argc, const char * argv[]) {
    int player1 = 0;
    int player2 = 0;

    while ((player1 < 21 && player2 < 21) || abs(player1 - player2) < 2) {
        int pick = arc4random()%2;

        if (pick == 0) {
            player1 = player1 + 1;
        } else if (pick == 1) {
            player2 = player2 + 1;
        }
    }

    if (player1 > player2){
       NSLog(@"Player 1 has %i points and Player 2 %i", player1, player2);
    } else if (player2 > player1){
        NSLog(@"Player 2 has %i points and Player 1 %i", player2, player1);
    }
}