- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在上 C 编程入门类(class),我们被分配编写 Hangman程序。
在游戏中,计算机随机选择一个单词并显示它有多少个字母。用户必须通过输入他们认为可能存在于该单词中的字母来猜测该单词。此外,用户只有六次机会正确说出单词。每猜错一次,绞死的画面就会完成。该程序需要有一个包含三个选项的主菜单(新游戏、得分和退出)。程序还需要具备这三个功能:
当我运行游戏时,问题就出现了,而不是显示虚线,而虚线应该只是显示(空)。但图片仍然显示。
我很困惑到底是什么原因造成的。这是我到目前为止所拥有的:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char selectWord(char []);
int drawMan(int);
void checkWord(char, char [], char [], int, int);
int main()
{
int menuSelect;
int chances = 0;
char word[13];
int length;
int score;
do
{
printf("\n\t\t\tThe Hangman Game v1.0 has booted\n\n");
printf("\t\t[1]\t Create new game\n");
printf("\t\t[2]\t View Score\n");
printf("\t\t[3]\t Exit game\n");
printf("Please enter a number from the menu: ");
scanf("%d", &menuSelect);
switch(menuSelect)
{
case 1:
selectWord(word);
length = strlen(word);
char dash[20]; //Will create dashes considering the length of the selected word
int dashCount;
int letterTry;
int wordMatch = 0;
for(dashCount = 0; dashCount < length; dashCount++)
{
dash[dashCount] = '-';
}
dash[dashCount] = '\0';
while(wordMatch != 1)
{
drawMan(chances);
printf("\n%s", dash[dashCount]);
printf("\n\nPlease enter a letter: ");
fflush(NULL);
while(letterTry != '\n')
{
letterTry = getchar();
}
letterTry = getchar();
if(strcmp(dash, word) == 0)
{
printf("\nThat is correct!\n");
wordMatch = 1;
score++;
}
}
break;
case 2:
printf("The score is: %d", score);
break;
case 3:
printf("Thank you for playing!");
break;
}
}while(menuSelect != 3);
}
char selectWord(char word[])
{
int index;
char list[65][13]={{"sailboat"}, {"school"}, {"rug"}, {"chalk"}, {"tusk"}, {"camera"}, {"pajamas"}, {"cello"}, {"cake"}, {"beehive"}, {"gate"}, {"swing"}, {"lighthouse"}, {"toe"}, {"newspaper"}, {"stingray"}, {"brain"}, {"french"}, {"purse"}, {"soda"}, {"money"}, {"dryer"}, {"scale"}, {"clam"}, {"knee"}, {"artist"}, {"stomach"}, {"ticket"}, {"face"}, {"TV"}, {"can"}, {"blowfish"}, {"popsicle"}, {"beaver"}, {"snowball"}, {"dollar"}, {"sprinkler"}, {"hair"}, {"bathroom"}, {"rain"}, {"skirt"}, {"beach"}, {"suitcase"}, {"garden"}, {"doormat"}, {"pretzel"}, {"watering"}, {"fur"}, {"birthday"}, {"fries"}, {"owl"}, {"chin"}, {"spool"}, {"mushroom"}, {"peach"}, {"pelican"}, {"pineapple"}, {"flagpole"}, {"doghouse"}, {"boot"}, {"flute"}, {"quilt"}, {"hook"}, {"rainbow"}, {"happy"}};
srand(time(NULL));
index = rand()%65;
strcpy(word, list[index]);
return word;
}
int drawMan(int chances)
{
if(chances == 6)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | ");
printf("\n | ");
printf("\n |");
printf("\n |");
printf("\n /|\\\n\n");
}
else if(chances == 5)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | |");
printf("\n |");
printf("\n |");
printf("\n /|\\\n\n");
}
else if(chances == 4)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|");
printf("\n | ");
printf("\n | ");
printf("\n /|\\\n\n");
}
else if(chances == 3)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|\\");
printf("\n | ");
printf("\n | ");
printf("\n /|\\\n\n");
}
else if(chances == 2)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|\\");
printf("\n | ");
printf("\n | ");
printf("\n /|\\\n\n");
}
else if(chances == 1)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|\\");
printf("\n | /");
printf("\n | ");
printf("\n /|\\\n\n");
}
else if(chances == 0)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|\\");
printf("\n | / \\");
printf("\n | ");
printf("\n /|\\\n\n");
printf("\n\n\t You have lost!");
}
}
void checkWord(char ltrTry, char word[], char dash[], int length, int chances)
{
int count;
int correct = 0; // 0 is incorrect 1 is correct
for(count = 0; count < length; count++)
{
if(ltrTry == word[count])
{
dash[count] = word[count];
correct = 1;
}
}
}
更新#1:感谢大家修复破折号字符串。向破折号数组添加空字符修复了破折号问题。我在主函数中的情况 1 中添加了一个名为“wordMatch”的新变量,并将其作为 while 循环的控制变量,因为可以在不耗尽所有机会的情况下获得正确的单词并退出循环。但似乎又出现了一个新的。当选择新游戏时,刽子手会显示两次,输入错误的字母后,机会的数量不会改变,刽子手的图像也不会改变(给我无限次的尝试)。然而,一旦我猜对了这个词,循环就会正确退出。为什么会发生这种情况?
更新#2:我已经更正了代码并使其能够正确执行。现在唯一的问题似乎是 case 1 没有中断,因为它被困在带有 letterTry 的 while 循环中。
最佳答案
实际上有两个问题。分配破折号后,请确保以 \0
终止字符串,如下所示:
for(dashCount = 0; dashCount < length; dashCount++)
{
dash[dashCount] = '-';
}
dash[dashCount] = '\0';
并在 while 循环中打印:
printf("\n%s", dash);
而不是:
printf("\n%s", dash[dashCount]);
此外,您不会在每次尝试后更新机会
值。您可以通过使 checkWord
函数返回正确
并据此更新机会计数来实现此目的,如下所示:
int checkWord(char ltrTry, char word[], char dash[], int length, int trys)
{
...
return correct;
}
在循环中,不只是调用函数,而是执行以下操作:
if(!checkWord(letterTry, word, dash, length, chances))
{
chances++;
}
我看到的另一个问题是在读取 letterTry
值时。当您在之前使用 scanf
函数读取字符(在本例中为 letterTry
)时,\n
字符将存储在变量中。然后程序将不会提示您进行其他输入。在你的情况下,玩家会无缘无故地失去一次机会。此问题最简单的解决方案是执行以下操作:
while(letterTry != '\n')
letterTry = getchar();
并且,一旦玩家得到正确答案,就从主循环中断。
if(strcmp(dash, word) == 0)
{
printf("You Won!");
score++;
break;
}
正确理解上述解决方案后,请执行此固定解决方案:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char selectWord(char[]);
int drawMan(int);
int checkWord(char, char[], char[], int);
int main()
{
int menuSelect;
int chances = 0;
char word[13];
int length;
int score;
do
{
printf("\n\t\t\tThe Hangman Game v1.0 has booted\n\n");
printf("\t\t[1]\t Create new game\n");
printf("\t\t[2]\t View Score\n");
printf("\t\t[3]\t Exit game\n");
printf("Please enter a number from the menu: ");
scanf("%d", &menuSelect);
switch(menuSelect)
{
case 1:
selectWord(word);
length = strlen(word);
char dash[20]; //Will create dashes considering the length of the selected word
int dashCount;
int letterTry;
for(dashCount = 0; dashCount < length; dashCount++)
{
dash[dashCount] = '-';
}
dash[dashCount] = '\0';
chances = 0;
while(chances != 6)
{
drawMan(chances);
printf("\n%s\n", dash);
printf("chances = %d\n", chances);
printf("\n\nPlease enter a letter: ");
fflush(NULL);
//scanf("%c%c", &letterTry, &letterTry);
while(letterTry != '\n')
letterTry = getchar();
letterTry = getchar();
if(!checkWord(letterTry, word, dash, length))
{
chances++;
}
if(strcmp(dash, word) == 0)
{
printf("You Won!");
score++;
break;
}
}
break;
case 2:
printf("The score is: %d", score);
break;
case 3:
printf("Thank you for playing!");
break;
}
}while(menuSelect != 3);
}
char selectWord(char word[])
{
int index;
char list[65][13]={{"sailboat"}, {"school"}, {"rug"}, {"chalk"}, {"tusk"}, {"camera"}, {"pajamas"}, {"cello"}, {"cake"}, {"beehive"}, {"gate"}, {"swing"}, {"lighthouse"}, {"toe"}, {"newspaper"}, {"stingray"}, {"brain"}, {"french"}, {"purse"}, {"soda"}, {"money"}, {"dryer"}, {"scale"}, {"clam"}, {"knee"}, {"artist"}, {"stomach"}, {"ticket"}, {"face"}, {"TV"}, {"can"}, {"blowfish"}, {"popsicle"}, {"beaver"}, {"snowball"}, {"dollar"}, {"sprinkler"}, {"hair"}, {"bathroom"}, {"rain"}, {"skirt"}, {"beach"}, {"suitcase"}, {"garden"}, {"doormat"}, {"pretzel"}, {"watering"}, {"fur"}, {"birthday"}, {"fries"}, {"owl"}, {"chin"}, {"spool"}, {"mushroom"}, {"peach"}, {"pelican"}, {"pineapple"}, {"flagpole"}, {"doghouse"}, {"boot"}, {"flute"}, {"quilt"}, {"hook"}, {"rainbow"}, {"happy"}};
srand(time(NULL));
index = rand()%65;
strcpy(word, list[index]);
return word;
}
int drawMan(int chances)
{
if(chances == 0)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | ");
printf("\n | ");
printf("\n |");
printf("\n |");
printf("\n /|\\\n\n");
}
else if(chances == 1)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | |");
printf("\n |");
printf("\n |");
printf("\n /|\\\n\n");
}
else if(chances == 2)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|");
printf("\n | ");
printf("\n | ");
printf("\n /|\\\n\n");
}
else if(chances == 3)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|\\");
printf("\n | ");
printf("\n | ");
printf("\n /|\\\n\n");
}
else if(chances == 4)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|\\");
printf("\n | ");
printf("\n | ");
printf("\n /|\\\n\n");
}
else if(chances == 5)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|\\");
printf("\n | /");
printf("\n | ");
printf("\n /|\\\n\n");
}
else if(chances == 6)
{
printf("\n\n");
printf("\n +-------+");
printf("\n | |");
printf("\n | O");
printf("\n | /|\\");
printf("\n | / \\");
printf("\n | ");
printf("\n /|\\\n\n");
printf("\n\n\t You have lost!");
}
printf("print complete; exiting successfully");
}
int checkWord(char ltrTry, char word[], char dash[], int length)
{
int count;
int correct = 0; // 0 is incorrect 1 is correct
for(count = 0; count < length; count++)
{
if(ltrTry == word[count])
{
dash[count] = word[count];
correct = 1;
}
}
/* if(correct == 0)
{
trys--;
} */
return correct;
}
关于c - Hangman 程序字符串问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24836666/
如何使用 SPListCollection.Add(String, String, String, String, Int32, String, SPListTemplate.QuickLaunchO
我刚刚开始使用 C++ 并且对 C# 有一些经验,所以我有一些一般的编程经验。然而,似乎我马上就被击落了。我试过在谷歌上寻找,以免浪费任何人的时间,但没有结果。 int main(int argc,
这个问题已经有答案了: In Java 8 how do I transform a Map to another Map using a lambda? (8 个回答) Convert a Map>
我正在使用 node + typescript 和集成的 swagger 进行 API 调用。我 Swagger 提出以下要求 http://localhost:3033/employees/sear
我是 C++ 容器模板的新手。我收集了一些记录。每条记录都有一个唯一的名称,以及一个字段/值对列表。将按名称访问记录。字段/值对的顺序很重要。因此我设计如下: typedef string
我需要这两种方法,但j2me没有,我找到了一个replaceall();但这是 replaceall(string,string,string); 第二个方法是SringBuffer但在j2me中它没
If string is an alias of String in the .net framework为什么会发生这种情况,我应该如何解释它: type JustAString = string
我有两个列表(或字符串):一个大,另一个小。 我想检查较大的(A)是否包含小的(B)。 我的期望如下: 案例 1. B 是 A 的子集 A = [1,2,3] B = [1,2] contains(A
我有一个似乎无法解决的小问题。 这里...我有一个像这样创建的输入... var input = $(''); 如果我这样做......一切都很好 $(this).append(input); 如果我
我有以下代码片段 string[] lines = objects.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.No
这可能真的很简单,但我已经坚持了一段时间了。 我正在尝试输出一个字符串,然后输出一个带有两位小数的 double ,后跟另一个字符串,这是我的代码。 System.out.printf("成本:%.2
以下是 Cloud Firestore 列表查询中的示例之一 citiesRef.where("state", ">=", "CA").where("state", "= 字符串,我们在Stack O
我正在尝试检查一个字符串是否包含在另一个字符串中。后面的代码非常简单。我怎样才能在 jquery 中做到这一点? function deleteRow(locName, locID) { if
这个问题在这里已经有了答案: How to implement big int in C++ (14 个答案) 关闭 9 年前。 我有 2 个字符串,都只包含数字。这些数字大于 uint64_t 的
我有一个带有自定义转换器的 Dozer 映射: com.xyz.Customer com.xyz.CustomerDAO customerName
这个问题在这里已经有了答案: How do I compare strings in Java? (23 个回答) 关闭 6 年前。 我想了解字符串池的工作原理以及一个字符串等于另一个字符串的规则是
我已阅读 this问题和其他一些问题。但它们与我的问题有些无关 对于 UILabel 如果你不指定 ? 或 ! 你会得到这样的错误: @IBOutlet property has non-option
这两种方法中哪一种在理论上更快,为什么? (指向字符串的指针必须是常量。) destination[count] 和 *destination++ 之间的确切区别是什么? destination[co
This question already has answers here: Closed 11 years ago. Possible Duplicates: Is String.Format a
我有一个Stream一个文件的,现在我想将相同的单词组合成 Map这很重要,这个词在 Stream 中出现的频率. 我知道我必须使用 collect(Collectors.groupingBy(..)
我是一名优秀的程序员,十分优秀!