Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
疑問 - 如何使用C寫一個猜英文單字的遊戲?
以前常玩的猜字母遊戲@@
老師會話一個人上吊的圖~~~
然後26個字母 去猜一個單子~~~
不過要寫程一個遊戲 我覺得好難阿
誰可以幫幫我
1.The computer randomly selects an English word from a list of at least 10 English words.
2.The computer randomly exposes some characters of an English word of the lenght n. At least 1 character is exposed and at maximum n - 1 character.
3.The player guesses character by character until the word is solved or the 8 times of wrong guesses is exceeded.
4.Each time a correct character is guessed the character is exposed.
5.They player can decide to continue to play or not.
Hint:
1.To get the string length, use the strlen function.
2.To compare two strings, use the strcmp function.
3.To count the number of strings, refer to the following code:
main() {
char *words[] = {"computer", "information", "engineering", "programming"};
char guess[30];
int i, n = sizeof(words)/sizeof(char *);
printf("List of string: ");
for (i = 0; i < n; i++) printf("%s ", words[i]);
i = rand() % n;
printf("\nEnter your guess : ");
scanf("%s", guess);
printf("Your guess is \"%s\".\n", guess);
printf("Computer selects \"%s\".\n", words[i]);
if (strcmp(guess, words[i])) printf("Your guess is wrong.\n");
else printf("Your guess is correct.\n");
}
A possible session could look like as follows:
The word to guess: -a--c
Your guess => o
The word to guess: -a--c
Your guess => m
The word to guess: ma--c
Your guess => l
The word to guess: ma--c
Your guess => g
The word to guess: mag-c
Your guess => i
Correct! The word is magic.
====================================================
Continue (Y/N)? Y
The word to guess: wo--e-
...
...
You guessed wrong more than 8 times. The word is wonder.
====================================================
Continue (Y/N)? N
Bye!
拜託一下了
2 Answers
- ?Lv 51 decade agoFavorite Answer
示範程式 與 示範執行 差好多喔!
哪位天才老師給的啊!?
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX 32
#define MAXG 8 // Maximun guessing times
int main()
{ char *words[] = {"computer", "information", "engineering", "programming",
"test", "about", "an", "IBM", "Microsoft", "Visual", "Studio" },
word[MAX],
ch;
int g, i, l, m, n = sizeof(words)/sizeof(*words), w;
srand((unsigned) time(NULL));
do
{ w = rand() % n;
strcpy(word, words[w]);
l = (int) strlen(word);
do
{ for (m=i=0; i<l; i++)
if (rand() > 3*(RAND_MAX>>2))
word[i] = '-', m++;
} while (!m); // no character is gone
if (m == l) // all the characters are gone
continue; // choice a new word because this word maybe bad!
for (g=0; word[g]!='-'; g++);
for (i=MAXG; i && m; i--)
{ printf("The word to guess: %s\nEnter your guess: ", word);
printf("%c\n", ch = (char) getch());
if (ch == words[w][g])
{ word[g] = ch, m--;
for (; word[g]!='-'; g++);
} }
if (strcmp(word, words[w]))
printf("You guess more than %d times.\nThe word is %s.", MAXG, words[w]);
else
printf("Your guess is correct!");
printf("\n=================================\nAgain? (Y/N)\b\b\b\b");
} while (toupper(getchar()) != 'N') ;
printf("Bye!\n");
return 0;
}
你試玩看看囉!