我有一個雙人遊戲猜謎遊戲;當遊戲開始時隨機產生玩家號碼(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;
}
歡迎來到Stack Overflow。請儘快閱讀[關於]頁面。你應該每次運行程序只調用一次srand()。通過調用它兩次,你通過調用一次就可以撤銷你所做的任何好事。這兩個調用返回給'rand()',通常會得到相同的數字,這顯然不是你想要的。此外,您在這裏不需要''(並且很少需要它)。 ''頭部已經足夠好了,除非你知道''提供了什麼額外的服務(你可能不會使用這些服務,因爲你沒有使用它們中的任何一個)。 –