C编程问题中的刽子手
我的代码正在运行,但它不会将错误与正确答案区分开来。在游戏重新开始之前,你必须犯六个错误,但是代码允许你输入六个字符,并且不管字符是对还是错 此外,该计划的高分部分也不起作用。我在网上搜索过,但只能找到数组和结构。我需要制作和编辑文件。非常感谢!!:)C编程问题中的刽子手,c,arrays,file,iteration,structure,C,Arrays,File,Iteration,Structure,我的代码正在运行,但它不会将错误与正确答案区分开来。在游戏重新开始之前,你必须犯六个错误,但是代码允许你输入六个字符,并且不管字符是对还是错 此外,该计划的高分部分也不起作用。我在网上搜索过,但只能找到数组和结构。我需要制作和编辑文件。非常感谢!!:) #包括 #包括 类型定义结构 { 字符名[10]; 智力得分; }玩家; 无效错误μm(整数x); 无效菜单(); 字符选择(); 无效游戏(玩家*p); 虚空高分(); void getusername(玩家*p); int main(){ 智
#包括
#包括
类型定义结构
{
字符名[10];
智力得分;
}玩家;
无效错误μm(整数x);
无效菜单();
字符选择();
无效游戏(玩家*p);
虚空高分();
void getusername(玩家*p);
int main(){
智力选择;
玩家游戏;
while(选项!=3){
菜单();
scanf(“%d”,选择(&C);
开关(选择){
案例1:getusername(和播放);
游戏;
打破
案例2:最高分();
打破
案例3:printf(“再见”);
}
}
}
无效菜单(){
printf(“\n欢迎来到刽子手!!!\n”);
printf(“请选择一个数字”);
printf(“1.玩一个新游戏\n”);
printf(“2.见最高分数\n”);
printf(“3.退出\n”);
}
无效getusername(玩家*游戏){
printf(“请输入您的姓名:”);
scanf(“%s”,&play->name);
}
无效游戏(玩家*游戏){
炭类[20];
字符字[20];
文件*hmPtr;
如果((hmPtr=fopen(“hangman.dat”,“r”))==NULL){
printf(“无法打开文件\n”);
}
否则{
int i=0;
srand(时间(空));
int j=rand()%4;
而(!feof(hmPtr)){
fscanf(hmPtr,“%s%s”,类别,单词);
如果(i==j){
整数匝数=0;
int x;
字符输入[1];
char-get[20];
字符打印[20];
strcpy(get,word);
printf(“类别:%s\n\n”,类别);
单词[strlen(单词)];
对于(x=0;x到底是什么问题?代码有两个问题。1.代码不会计算未命中的次数。它会计算您输入的每个字符。因此,您不是犯六个错误来结束游戏,而是输入六个字符来结束游戏。2.程序中的高分列表必须位于代码之外的文件中。此外,如果有新的高分球员上场,它应该被编辑。最大的错误在第98行。我编辑了代码。我删除了第98行。它现在可以工作了。@royzxczxczxc我可以知道Hangman.dat文件的格式吗?或者更好,我可以上传并提供链接到它。
#include <stdio.h>
#include <string.h>
typedef struct
{
char name[10];
int score;
} player;
void error_m(int x);
void menu();
char choice();
void game(player *p);
void highestscores();
void getusername(player *p);
int main() {
int choice;
player play;
while(choice!=3) {
menu();
scanf("%d",&choice);
switch(choice){
case 1: getusername(&play);
game(&play);
break;
case 2: highestscores();
break;
case 3: printf("Good Bye!");
}
}
}
void menu() {
printf("\nWELCOME TO HANGMAN!!!\n");
printf("Please choose a number\n");
printf("1. Play a new game\n");
printf("2. See highest score\n");
printf("3. Exit\n");
}
void getusername(player *play){
printf("Please enter your name: ");
scanf("%s", &play->name);
}
void game(player *play) {
char category[20];
char word[20];
FILE *hmPtr;
if ( (hmPtr = fopen( "hangman.dat", "r" )) == NULL ) {
printf( "File could not be opened\n" );
}
else {
int i = 0;
srand ( time(NULL) );
int j = rand() % 4;
while(!feof(hmPtr)) {
fscanf(hmPtr,"%s %s",category, word);
if (i == j) {
int turns=0;
int x;
char input[1];
char get[20];
char print[20];
strcpy(get,word);
printf("Category: %s\n\n", category);
word[strlen(word)];
for(x=0; x<strlen(word); x++) {if
(word[x]=='_'){printf(" ");}else{
printf("-");}
}
while(turns < 6) {
printf("\nEnter a letter: ");
getchar();
input[0] = getchar();
printf("\n");
for(x=0; x<strlen(word); x++) {
if(word[x]=='_'){printf(" ");}
else if(get[x]==input[0]) {
printf("%c",input[0]);
print[x]=input[0];
}
else if(get[x]==print[x]) {
printf("%c",print[x]);
}
else {
printf("-");
}
}
if(strstr(print,word)) {
printf("\n\nYou got the correct answer!\n");
break;
}
else if(!strstr(get,&input[0])) {
turns++;
}
}
printf("\n");
}
}
fclose( hmPtr );
}
}
void highestscores(){
printf("Highest Scores:\n");
}
void error_m(int x) {
switch (x) {
case 0 :
printf("Amount of wrong letters: %d\n\n", x);
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("____________\n\n");
break;
case 1 :
printf("Amount of wrong letters: %d\n\n", x);
printf("\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("__|_________\n\n");
break;
case 2 :
printf("Amount of wrong letters: %d\n\n", x);
printf(" _______\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("__|_________\n\n");
break;
case 3 :
printf("Amount of wrong letters: %d\n\n", x);
printf(" _______\n");
printf(" |/\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("__|_________\n\n");
break;
case 4 :
printf("Amount of wrong letters: %d\n\n", x);
printf(" _______\n");
printf(" |/ | \n");
printf(" | O \n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("__|_________\n\n");
break;
case 5 :
printf("Amount of wrong letters: %d\n\n", x);
printf(" _______\n");
printf(" |/ | \n");
printf(" | O \n");
printf(" | |\n");
printf(" | |\n");
printf(" |\n");
printf("__|_________\n\n");
break;
case 6 :
printf("Amount of wrong letters: %d\n\n", x);
printf(" _______\n");
printf(" |/ | \n");
printf(" | O \n");
printf(" | \\|\n");
printf(" | | \n");
printf(" |\n");
printf("__|_________\n\n");
break;
case 7 :
printf("Amount of wrong letters: %d\n\n", x);
printf(" _______\n");
printf(" |/ | \n");
printf(" | O \n");
printf(" | \\|/\n");
printf(" | | \n");
printf(" |\n");
printf("__|_________\n\n");
break;
case 8 :
printf("Amount of wrong letters: %d\n\n", x);
printf(" _______\n");
printf(" |/ | \n");
printf(" | O \n");
printf(" | \\|/\n");
printf(" | | \n");
printf(" | /\n");
printf("__|_________\n\n");
break;
case 9 :
printf("Amount of wrong letters: %d\n\n", x);
printf(" _______\n");
printf(" |/ | \n");
printf(" | O \n");
printf(" | \\|/\n");
printf(" | | \n");
printf(" | / \\\n");
printf("__|_________\n\n");
break;
case 10 :
printf("Amount of wrong letters: %d\n\n", x);
printf(" _______\n");
printf(" |/ | \n");
printf(" | X \n");
printf(" | \\|/\n");
printf(" | | \n");
printf(" | / \\\n");
printf("__|_________\n\n");
break;
}
}