1

私は 2 人で乱数を推測するゲームをしています。プレイヤー番号 (1 0r 2) は、ゲームの開始時にランダムに生成され、プレイヤー番号を入力するよう求められます。入力したプレーヤー番号がランダムなプレーヤー番号と一致しない場合は、「順番を待つ必要があります」と出力され、プレーヤー番号の入力プロンプトに戻ります。私のプログラムは、最初にプレーヤー番号を要求してから、プレーヤーが推測する乱数を自分の番で入力するように要求するのではなく、プレーヤーが推測する乱数に直接移動します。先に進む前に、正しいプレイヤー番号を要求するように戻すにはどうすればよいですか。

また、1 つの正しいプレーヤー番号が入力されます。各プレイヤーは、「PASS」を 2 回連続して入力し、ゲームの生涯を通じて 3 回パスすることを選択できます。これらの条件をゲームで機能させるにはどうすればよいですか。すべてに前もって感謝します。

コードは次のとおりです。

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <malloc.h>

int main(void) { 

    int player_num = 0; int number = 0; int player_input = 0;
    int guess = 0; char input; char str[6] = {0}; int Player_1 = 1;
    int Player_2 = 2; int Pass_1 = 3; int Pass_2 = 3; int i = 1;
    int player_turn = 0; int turn = 0;

    srand(time(NULL));  player_num = 1 + rand() % 2; /* Random number is generated */
    srand(time(NULL));  number = 0 + rand() % 100; /* Random number is generated */

    while(number != guess) {

        printf("\nIt's player's %d turn\n", player_num);
        printf("Player Number?\n");
        scanf("%d", &player_input);

        while (player_num != player_input) {
            printf("You Have to wait your turn.\nPlayer number?\n");
        }

        if (Player_1 != player_num)
            Player_2 = player_num;

        if (i%2 == 1) {
            player_num = Player_1;
        } else {
            player_num = Player_2;
        }

        i = i+1;

        printf("Enter Your Guess, 0 - 100 or Pass: ");

        scanf("%s", str);

        if (strcmp(str, "pass") == 0){
            if (player_num == Player_1){
                Pass_2 = Pass_2 -1;
                printf("Player 2 has %d more 'Pass' left!\n", Pass_2);
            }   
            else {
                Pass_1 = Pass_1 -1;
                printf("Player 1 has %d more 'Pass' left!\n", Pass_1);
            }
        } else {
            guess = atoi(str);
            if(guess < number) /* if the guess is lower, output: the guess is too low */
                printf("Your guess was to low.\n ");

            else if(guess > number) /* if the guess is higher, output: the guess is too high */
                printf("Your guess was to high.\n ");

            else /* if the guess is equal to the random number: Success!! */
                printf("Yes!! you got it!\n");

        }
    }
    return 0;
}
4

2 に答える 2

1

まず、srandプログラムの開始時に一度だけ呼び出す必要があります。

2scanfつ目は、2 番目の while ループ内に移動して、ユーザーに正しいプレイヤー番号を入力させるか、正しくなるまで尋ね続けるように強制しました。

次のコードは、上記のすべてを修正します。コード内のコメントを読んで、変更の理由を確認してください。

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>

int main(void) { 

 int player_num = 0; int number = 0; int player_input = 0;
 int guess = 0; char input; char str[15] = {0}; // size was increased to compensate for pass pass input
 int Player_1 = 1;
 int Player_2 = 2; int Pass_1 = 3; int Pass_2 = 3; int i = 1;
 int player_turn = 0; int turn = 0; int alternate=0;
 int player1Flag=0,player2Flag=0;

 int lastPlayer=0;

 srand(time(NULL));  
 player_num = 1 + rand() % 2; /* Random number is generated */
 lastPlayer = player_num;
 number = 0 + rand() % 100; /* Random number is generated */

  while(number != guess) {

      while (player_num != player_input)    {
           printf("\nIt's player's %d turn\n", player_num);
           printf("Player Number?\n");
           scanf("%d", &player_input);
           getchar();// to get rid of \n after the input

           if(player_input!=player_num){
               printf("You Have to wait your turn.\n");
           }

      }

      if (Player_1 != player_num) Player_2 = player_num;


      printf("Enter Your Guess, 0 - 100 or Pass: ");

      scanf("%s",str);

     if (strcmp(str, "pass") == 0){
        if (player_num == Player_1){
            player1Flag = player1Flag+1; // flag to detect if last input was a pass

            if(player1Flag>1){
                printf("Dude you passed in your last attempt .. dont be a pus*y\nEnter a guess : ");
                scanf("%s",&str);
                guess = atoi(str);
                if(guess < number){ /* if the guess is lower, output: the guess is too low */
                   printf("Your guess was to low.\n ");
                }else if(guess > number){ /* if the guess is higher, output: the guess is too high */
                   printf("Your guess was to high.\n ");
                }else{ /* if the guess is equal to the random number: Success!! */
                   printf("Yes!! you got it!\n");
                }
                player1Flag = 0; // reset the pass flag = 1 as this pass isn't counted
            }else{
                Pass_2 = Pass_2 -1;
                if(Pass_2<0){
                    printf("You have already passed Thrice\nEnter a guess: ");
                    scanf("%s",&str);
                    guess = atoi(str);
                    if(guess < number){ /* if the guess is lower, output: the guess is too low */
                       printf("Your guess was to low.\n ");
                    }else if(guess > number){ /* if the guess is higher, output: the guess is too high */
                       printf("Your guess was to high.\n ");
                    }else{ /* if the guess is equal to the random number: Success!! */
                       printf("Yes!! you got it!\n");
                    }
                }else{
                    printf("Player 1 has %d more 'Pass' left!\n", Pass_2);
                }
            }
        }   
        else{

            player2Flag = player2Flag + 1;

            if(player2Flag>1){
                printf("Dude you passed in your last attempt .. dont be a pus*y\nEnter a guess : ");
                scanf("%s",&str);
                guess = atoi(str);
                if(guess < number){ /* if the guess is lower, output: the guess is too low */
                   printf("Your guess was to low.\n ");
                }else if(guess > number){ /* if the guess is higher, output: the guess is too high */
                   printf("Your guess was to high.\n ");
                }else{ /* if the guess is equal to the random number: Success!! */
                   printf("Yes!! you got it!\n");
                }
                player2Flag=0;// reset the player2Flag = 1 as this pass isn't counted
            }else{
                Pass_1 = Pass_1 -1;
                if(Pass_2<0){
                    printf("You have already passed Thrice\nEnter a guess: ");
                    scanf("%s",&str);
                    guess = atoi(str);
                    if(guess < number){ /* if the guess is lower, output: the guess is too low */
                       printf("Your guess was to low.\n ");
                    }else if(guess > number){ /* if the guess is higher, output: the guess is too high */
                       printf("Your guess was to high.\n ");
                    }else{ /* if the guess is equal to the random number: Success!! */
                       printf("Yes!! you got it!\n");
                    }
                }else{
                    printf("Player 2 has %d more 'Pass' left!\n", Pass_2);
                }
            }
        }   

     }else {

        if (player_num == Player_1){
            player1Flag = 0;//reset pass flag as this player enetered a guess
        }else{
            player2Flag = 0;
        }

        guess = atoi(str);
            if(guess < number){ /* if the guess is lower, output: the guess is too low */
                printf("Your guess was to low.\n ");

            }else if(guess > number){ /* if the guess is higher, output: the guess is too high */
                printf("Your guess was to high.\n ");

            }else{ /* if the guess is equal to the random number: Success!! */
                printf("Yes!! you got it!\n");

             }
      }

      if(lastPlayer==1){
          player_num = 2;
          lastPlayer = 2;
      }else if(lastPlayer==2){
            player_num = 1;
            lastPlayer = 1;
      }

  }   

  return 0;

}
于 2013-10-22T01:20:55.270 に答える
1

while 内に入力を追加する必要があります。つまり、代わりに:

   while (player_num != player_input)    {
     printf("You Have to wait your turn.\nPlayer number?\n");
   }

これを試して:

while (player_num != player_input)    {
   printf("You Have to wait your turn.\nPlayer number?\n");
   scanf("%d", &player_input);
}
于 2013-10-22T00:39:58.117 に答える