[英]Number guessing game in C
我在用 C 進行猜數字游戲時遇到了問題。在運行程序時,當我將 char 元素放入其中時,它會顯示返回函數。 有人可以幫我弄這個嗎? 另外,如何改進我的代碼以使其可行? 我真的被這個問題困住了。
這是代碼:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
int main()
{
char s, n, q;
int b;
bool (1=true), (0=false);
int secret;
secret = rand();
int guess;
int seed;
srand(seed);
printf("Welcome to the guessing game!\n");
do{
printf("Menu: (s) to start a new game, (n) to set a new range, or (q) to quit:\n");
scanf("%s, %s, %s", s, n, q);
if((s==1))
{
printf("The secret number is Between 0 AND rand(). Guess\n");
scanf("%s", b);
}
else if((n ==1))
{
printf("Enter a new MAXIMUM\n");
scanf("%s", rand());
if(( s ==1))
{
printf("The secret number is Between 0 AND rand(). Guess\n");
scanf("%s", b);
printf("The secret number is between 0 and rand() Guess:");
scanf("%s", b);
if(guess = rand()){
printf("Congratulations you won, You took %d guesses!", b);
break;
}
else if(guess > rand())
{
printf("Too High, Guess again:");
}
else if(guess < rand()){
printf("Too Low, Guess Again:");
}
else{
printf("This number out of the number set!");
}
}
}
else{
printf("Unrecognized command");
}
}while(q == 1);
printf("You quited the game");
return 0;
}
這段代碼有無數的問題需要解決。 我建議以小步驟來處理您的代碼編寫過程。 看起來好像您一口氣編寫了整個程序,運行它,然后發現它不起作用,而不是逐步添加小功能並運行每個功能以驗證它是否有效,然后再繼續下一步。 不這樣做會導致程序難以調試,並且對程序的運行方式缺乏了解。
具體來說,嘗試編寫一個三行或四行程序來收集和打印用戶輸入。 輸出是否按預期工作? 您是否在各種輸入上測試了它的穩健性? 您可以編寫它以使用多種數據類型嗎? 如果某些東西不起作用,您是否在繼續前進之前研究了問題並解決了它?
計划中要調查的一些領域:
bool (1=true), (0=false);
不編譯,也不是程序所必需的。 如果你#include <stdbool.h>
你不需要這樣做(你可以簡單地寫if (something == true)
)。srand()
未正確調用或播種。 使用您包含的標題中的time()
調用為每個程序播種一次,並在每個游戲中調用rand()
一次。 使用%
運算符將函數輸出設置在0
和max
之間。guess
與您之前存儲rand()
的值進行比較,而不是在每次比較期間調用rand()
,這會使游戲邏輯變得隨意。%s
輸入不合適; 讀取char
s %c
和int
s %d
,將適當的變量引用傳遞給scanf
。 scanf("%s, %s, %s", s, n, q);
收集 3 個空格分隔的字符串用於輸入,而不是提示建議的 1 個char
。s
並將您的猜測/響應邏輯移到那里時運行游戲。綜上所述,這是一個可能的工作版本。 它可以更好地利用功能並實現安全的用戶輸入(讀者練習):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
char menu_choice;
int guess;
int guesses;
int secret;
int max = 100;
srand(time(NULL));
printf("Welcome to the guessing game!\n");
for (;;) {
printf("\nMenu: (s) to start a new game, (n) to set a new range, or (q) to quit: ");
scanf(" %c", &menu_choice);
if (menu_choice == 's') {
guesses = 0;
secret = rand() % max;
for (;;) {
printf("\nThe secret number is between 0 and %d. Enter a guess: ", max);
scanf("%d", &guess);
guesses++;
if (guess == secret) {
printf("\nCongratulations, you won! You guessed %d in %d guesses!\n", secret, guesses);
break;
}
else if (guess > secret) {
printf("Too high! Guess again.");
}
else if (guess < secret) {
printf("Too low! Guess again.");
}
else if (guess >= max) {
puts("Out of range");
}
}
}
else if (menu_choice == 'n') {
printf("\nEnter a new maximum: ");
scanf("%d", &max);
}
else if (menu_choice == 'q') {
puts("\nGoodbye!");
break;
}
else {
puts("\nUnrecognized command.");
}
}
return 0;
}
示例運行:
Welcome to the guessing game!
Menu: (s) to start a new game, (n) to set a new range, or (q) to quit: n
Enter a new maximum: 50
Menu: (s) to start a new game, (n) to set a new range, or (q) to quit: s
The secret number is between 0 and 50. Enter a guess: 25
Too low! Guess again.
The secret number is between 0 and 50. Enter a guess: 37
Too low! Guess again.
The secret number is between 0 and 50. Enter a guess: 43
Too low! Guess again.
The secret number is between 0 and 50. Enter a guess: 47
Congratulations, you won! You guessed 47 in 4 guesses!
Menu: (s) to start a new game, (n) to set a new range, or (q) to quit: q
Goodbye!
printf("Menu: (s) to start a new game, (n) to set a new range, or (q) to quit:\n");
scanf("%s, %s, %s", s, n, q);
您不需要使用三個變量 s,n,q。 您應該要求用戶輸入一個選項。 要么開始新游戲,要么退出或其他任何事情。
其次, rand() 每次都返回一個隨機數。 你應該在某處存儲隨機數。 像這樣
rand_num=rand()
還
if(guess = rand())
是一種錯誤的比較方式。 這應該是
if(guess==rand_num)
最后二分搜索是您問題的解決方案。 請在互聯網上參考
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.