C 刽子手程序字符串问题
我在一门C编程入门课上,我们被指派编写一个程序 在游戏中,电脑随机选择一个单词并显示它有多少个字母。用户必须通过输入他们认为可能在单词中的字母来猜测单词。此外,用户只有六次机会获得正确的单词。随着每一次错误的猜测,被绞死的照片都将完成。该计划需要有一个主菜单与三个选项(新游戏,得分和退出)。该程序还需要具备以下三个功能:C 刽子手程序字符串问题,c,arrays,string,C,Arrays,String,我在一门C编程入门课上,我们被指派编写一个程序 在游戏中,电脑随机选择一个单词并显示它有多少个字母。用户必须通过输入他们认为可能在单词中的字母来猜测单词。此外,用户只有六次机会获得正确的单词。随着每一次错误的猜测,被绞死的照片都将完成。该计划需要有一个主菜单与三个选项(新游戏,得分和退出)。该程序还需要具备以下三个功能: 选择Word以随机选择单词(我在此函数中创建了一个单词字符串) 拉手画刽子手 checkWord检查输入是否正确,并用正确的字母替换破折号 我的问题发生在我运行游戏时,并没有显
#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;
}
}
}
#包括
#包括
#包括
字符选择字(字符[]);
国际绘图员(国际);
无效校验字(char,char[],char[],int,int);
int main()
{
int菜单选择;
int=0;
字符字[13];
整数长度;
智力得分;
做
{
printf(“\n\t\t\t刽子手游戏v1.0已启动\n\n”);
printf(“\t\t[1]\t创建新游戏\n”);
printf(“\t\t[2]\t查看分数\n”);
printf(“\t\t[3]\t退出游戏\n”);
printf(“请从菜单中输入一个数字:”);
scanf(“%d”,菜单选择(&menuSelect);
开关(菜单选择)
{
案例1:
选择word(word);
长度=strlen(字);
字符破折号[20];//将根据所选单词的长度创建破折号
int dashCount;
内特莱特里;
int-wordMatch=0;
对于(dashCount=0;dashCountfor(dashCount = 0; dashCount < length; dashCount++)
{
dash[dashCount] = '-';
}
dash[dashCount] = '\0';
printf("\n%s", dash);
printf("\n%s", dash[dashCount]);
int checkWord(char ltrTry, char word[], char dash[], int length, int trys)
{
...
return correct;
}
if(!checkWord(letterTry, word, dash, length, chances))
{
chances++;
}
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;
}