一个猜单词的游戏~每次可以猜一个字母~总共可以猜8次,单词先显示“----”,猜到一个就用那个字母代替‘-’~直到猜中~
现在只存了2个单词~计算机会从2个中任选一个让玩家猜
大家帮我看看吧~~~为什么无法正常运行?
#include "stdio.h"
#include "Malloc.h"
#include "string.h"
char gamestart();
void gamejudge(char str[]);
struct w /*先用结构题存单词(那时没想到用*P[n]的字符串数组)*/
{ char *name;
int number;
};
char answer;
int times=8;
int i;
struct w words[2]={{"book",0},{"car",1}};存了2个单词
main()
{ char *wd;
wd=gamestart();
while(times!=0)
{gamejudge(wd);}
getch();
}
char gamestart()
{
int n,j;
char *word;
i=rand()%2;
n=strlen(words[i].name); 测量单词的长度用来显示多少个‘-’
word=malloc(n); 定义一个动态CHAR数组
for(j=0;j<n;j++) 让数组中个每个元素都为‘-’
word[j]='_';
return(word);
}
void gamejudge(char *str)
{
int j;
printf("The word now looks like %s\n",str);
printf("You have %d guesses left\n",times);
printf("You guess: "); 接收输入的一个字母
scanf("%c",&answer);
times=times-1; 猜的次数减1
for(j=0;j<strlen(str);j++)
if(str[j]==answer) 判断有没有猜中,猜中就替换
{ str[j]=answer;
printf("\nThat is correct\n");
break;
}
else if(j==strlen(str)) 判断没猜中
printf("There is no %c in the word\n",answer);
if(strcmp(words[i].name,str)==0)
{printf("You guess the word %s",str);
printf("You win ");
}
}
[此贴子已经被作者于2006-5-17 18:16:16编辑过]