如何在C中使用此代码在Rock Paper Scissors中添加更多选项

时间:2014-08-03 09:06:52

标签: c

现在我运行了这个程序,我这里的代码仅适用于Rock Paper和Scissors。 我已经成功地包含了Lizard和Spock的输入,但我不知道如何让CPU(随机)选择蜥蜴和spock以及让程序认为蜥蜴毒害spock等。

以下是代码段:

这是随机选择CPUmove:

int rand_i(int n)
{
    int rand_max = RAND_MAX - (RAND_MAX % n);
    int ret;
    while ((ret = rand()) >= rand_max);
    return ret/(rand_max / n);
}

int weighed_rand(int *tbl, int len)
{
    int i, sum, r;
    for (i = 0, sum = 0; i < len; sum += tbl[i++]);
    if (!sum) return rand_i(len);

    r = rand_i(sum) + 1;
    for (i = 0; i < len && (r -= tbl[i]) > 0; i++);
    return i;
}

这是适当的游戏,包括输入:

int ERPSGame()
{
    system("cls");
    char CPUmove[10], Usermove[10], line[255];
    int user, comp;
    int tbl[]={0,0,0};
    int tbllen=3;
    printf("Hello, Welcome to Enhanced Rock Paper Scissors\nBy Mark Sanchez\n");
    mainloop:
    while(1)
    { // infinite loop :)
        printf("\n\nPlease type in 1 for Rock, 2 For Paper, 3 for Scissors, 4 for Lizard \nand 5 for Spock. 0 to quit\n");
        srand(time(NULL));
        comp = (weighed_rand(tbl, tbllen) + 1) % 5;
        fgets(line, sizeof(line), stdin);   
        while(sscanf(line, "%d", &user) != 1) //1 match of defined specifier on input line
        { 
            printf("You have not entered an integer.\n");
            fgets(line, sizeof(line), stdin);
        }               
        if( (user > 5) || (user < 1) )
        {
            printf("Please enter a valid number!\n");
            continue;
        }
        switch (comp)
        {
            case 1 :
            strcpy(Usermove, "Rock");
            break;
            case 2 :
            strcpy(Usermove, "Paper");
            break;
            case 3 :
            strcpy(Usermove, "Scissors");
            break;
            case 4 :
            strcpy(Usermove, "Lizard");
            break;
            case 5 :
            strcpy(Usermove, "Spock");
            break;
            default :
            printf("Computer Error, set comp=1\n");
            comp=1;
            strcpy(Usermove, "Rock");
            break;
        }
        switch (user)
        {
            case 1 :
            strcpy(CPUmove, "Rock");
            break;
            case 2 :
            strcpy(CPUmove, "Paper");
            break;
            case 3 :
            strcpy(CPUmove, "Scissors");
            break;
            case 4 :
            strcpy(CPUmove, "Lizard");
            break;
            case 5 :
            strcpy(CPUmove, "Spock");
            break;
            case 0 :
            printf("Goodbye! Thanks for playing!\n");
            return 0;
            default :
            printf("Error, your number is not between 1-5 exiting...");
            goto mainloop;
        }
        if( (user+1)%3 == comp )
        {
            printf("Comp Played: %s\nYou Played: %s\nSorry, You Lost!\n", Usermove, CPUmove);
        }   
        else if(comp == user)
        {
            printf("Comp Played: %s\nYou Played: %s\nYou Tied :p\n", Usermove, CPUmove);
        }
        else
        {
            printf("Comp Played: %s\nYou Played: %s\nYay, You Won!\n", Usermove, CPUmove);
        }
        tbl[user-1]++;
    }
}

1 个答案:

答案 0 :(得分:0)

一点建议:尽可能长时间保持所有数字。您可能已经注意到,处理字符串非常繁琐。 所以你有cpu和user的输入。现在这会产生两个数字..然后是时间进行切换或者相似:

switch (user)
case 1: 
  if (cpu==1){
    printf('draw');
  }elseif (cpu==2 || cpu==3 || cpu==4){
    printf('pc won');
  }else{
   printf('congrats, you made it!');
  }
case 2:
...

或者就像你所做的一样,我们已经尝试了一个简单的计算方法来代表胜利和失败(就像你的(user+1)%3 == comp),我将在下一步做它以使它更优雅。为此,写下了所有组合,并想出一个好方法来命令字段,然后做模数或某些分区。

编辑:将数字打印成像你演奏的那样,电脑播放了,我建议为你做一个功能。所以你给它数字,它会直接打印结果,或者给你打印的字符串。