2013-10-22 33 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); 
}