【发布时间】:2020-05-08 20:54:37
【问题描述】:
我正在为学校的要求制作一个刽子手游戏。运行程序后,猜到一个单词后突然停止工作。它还没有完成,因为它不工作 这就是我想要完成的事情: 刽子手是一个猜词游戏,玩家被告知单词中有多少个字母。玩家必须通过一次猜一个字母来发现这个词。每个正确猜测的字母都会添加到单词中。在每一个错误的猜测中,一个身体部位被添加到一个被绞死的人的照片上。玩家允许犯5个错误,分别对应刽子手的头、身体、左臂、右臂、左腿。在第 6 次失误时,右腿被拉平,比赛结束。如果一个字母重复多次,即使第一次是正确的,重复出现也始终被视为错误。一旦被吊死的人完成,玩家就输掉了游戏。
A hangman will look like this after 0 to 6 errors. There should be no whitespace at the end of lines.
+--+ +--+ +--+ +--+ +--+ +--+ +--+
| | o | o | o | o | o | o
| | | | | /| | /|\ | /|\ | /|\
|\ |\ |\ |\ |\ |\ / |\ / \
Your task is to Implement the game of Hangman. For this problem, you need to have an array holding ten different words for the player to choose from. You will be required to use rand() function under <stdlib.h> to choose between those ten words. The chosen word will then be guessed by the player.
Sample Run.
H A N G M A N
+---+
|
|
|\
===
Missed letters:
Mystery word: _ _ _
Guess a letter.
a
H A N G M A N
+---+
|
|
|\
===
Missed letters:
Mystery word: _ a _
Guess a letter.
o
H A N G M A N
+---+
| o
|
|\
===
Missed letters: o
Mystery word: _ a _
Guess a letter.
r
H A N G M A N
+---+
| o
| |
|\
===
Missed letters: o r
Mystery word: _ a _
Guess a letter.
t
H A N G M A N
+---+
| o
| |
|\
===
Missed letters: o r
Mystery word: _ a t
Guess a letter.
a
H A N G M A N
+---+
| o
| /|
|\
===
You have already guessed that letter. Choose again.
Guess a letter.
c
Yes! The secret word is "cat"! You have won!
Do you want to play again? (yes or no)
no
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <string.h>
char word[3][10] = {"gabb", "mnl", "josh"};
char parts[6] = {" "};
char mwords[6] = {" "};
char blanks[10];
int life = 0;
int len;
char guess;
void body(int);
void generate(int);
int compare(int);
int main(){
int res;
char ans[3];
int gen;
srand(time(0));
gen = rand() % 3;
for(int i = 0; i < strlen(word[gen]); i++){
blanks[i] = '_';
}
do{
while(life < 6){
body(life);
generate(gen);
res = compare(gen);
if(res == 1){
printf("Yes! The secret word is \"%s\"! You have won!");
break;
}
}
printf("Do you want to play again? (yes or no): ");
scanf(" %s", ans);
}while(strcmp (ans, "yes") == 0);
}
这会根据生命的数量打印刽子手的尸体
void body(int n){
char guess;
switch(n){
case 6:
parts[5] = '\\';
case 5:
parts[4] = '/';
case 4:
parts[3] = '\\';
case 3:
parts[2] = '/';
case 2:
parts[1] = '|';
case 1:
parts[0] = 'O';
}
printf("H A N G M A N\n");
printf("\t+---+\n");
printf("\t| %c\n", parts[0]);
printf("\t| %c%c%c\n",parts[2], parts[1], parts[3]);
printf("\t|\\ %c %c\n", parts[4], parts[5]);
printf("\t===\n");
}
这会生成“猜测区域”
void generate(int a){
len = strlen(word[a]);
printf("Missed words: ");
for(int i = 0; i < 6; i++){
if(mwords == '\0')
break;
else
printf("%c",mwords[i]);
}
printf("\n");
printf("Mystery word: ");
for(int i = 0;i < len; i++){
printf("%c ", blanks[i]);
}
printf("\n");
不知道是不是问题出在这里
printf("Guess a letter: ");
scanf("%c", guess);
for (int i = 0; i < len; i++){
if(word[a][i] == guess)
blanks[i] = guess;
}
for(int i = 0; i < len;i++){
if(word[a][i] == guess)
break;
else
life++;
}
}
这个函数比较两个字符串
int compare(int comp){
if(strcmp (word[comp], blanks) == 0)
return 0;
else
return 1;
}
【问题讨论】:
-
char ans[3];应该是char ans[4];如果你要写"yes"给它。 -
阅读this article 了解调试代码的技巧。这些是您在学习编码时需要培养的关键技能。
-
@FiddlingBits 谢谢。程序仍然存在同样的问题
-
希望您通过@Code-Apprentice 和其他建议解决了您的问题
标签: c windows runtime-error dev-c++