2013-10-22 54 views
1

我有一個雙人遊戲猜謎遊戲;當遊戲開始時隨機產生玩家號碼(1 0或2),並提示輸入玩家號碼。如果輸入的玩家號碼與隨機玩家號碼不匹配,則應打印「您必須等待輪到」並返回到輸入的玩家號碼提示。我的程序直接輸入隨機數字,然後由玩家猜出,而不是回頭去詢問玩家號碼,然後轉向要求輸入隨機數字,玩家輪到他/她時會猜出這個隨機數字。在繼續前進之前,如何讓它回到詢問正確的球員號碼。兩個玩家在C編程中猜謎遊戲

此外還輸入了一個正確的玩家號碼;每個玩家可以選擇在遊戲的整個生命週期內連續兩次輸入「PASS」並且三次。我如何使這些條件在遊戲中起作用?在此先感謝所有人。

下面是代碼:

#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; 
} 
+3

歡迎來到Stack Overflow。請儘快閱讀[關於]頁面。你應該每次運行程序只調用一次srand()。通過調用它兩次,你通過調用一次就可以撤銷你所做的任何好事。這兩個調用返回給'rand()',通常會得到相同的數字,這顯然不是你想要的。此外,您在這裏不需要''(並且很少需要它)。 ''頭部已經足夠好了,除非你知道''提供了什麼額外的服務(你可能不會使用這些服務,因爲你沒有使用它們中的任何一個)。 –

回答

1

首先,srand需要進行一次,並在程序的開始調用。

其次,scanf移到第二內側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; 

} 
+0

謝謝Sukhvir。兩次連續的傳球意味着兩次嘗試。任何牌手都不能在整個遊戲過程中連續兩次或三次輸入「合格」。例如,如果玩家2通過輸入「PASS」將遊戲傳遞給玩家1,則當玩家2再次獲得該回合時,他/她必須輸入猜測並且不允許「通過」。然而任一玩家在遊戲的整個生命週期內只允許三次傳球,而不是連續兩次嘗試。對不起,以前沒有說清楚。 –

+0

此外,現在我的編程進入顯示負值,「Player2還有-1個」通過「左!」 - 這是錯誤的。 –

+0

嗯好吧,我明白了..哥們我很忙,接下來的8個小時..我會努力之後,讓你知道 – sukhvir

1

您需要將同時中添加的輸入。換句話說,而不是,:

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); 
}