永发信息网

c语言编程,吊死鬼程序

答案:1  悬赏:40  手机版
解决时间 2021-02-27 15:41
  • 提问者网友:最爱你的唇
  • 2021-02-26 23:04
一道c语言程序,编一个简单的吊死鬼游戏程序,我编了半天结果总也运行不对,求帮忙修改一下程序,由于长度超过要求,把程序地址贴出来,程序输出结果和我的程序在http://tieba.baidu.com/f?kz=1100262094,这里,谢谢
最佳答案
  • 五星知识达人网友:西岸风
  • 2021-02-27 00:24
你的程序问题在于一些初始化的问题.
我把代码贴上来,你自己比较下哪里不一样,我改一少少.不懂再问吧.

#include
#include
#include
#include

// dictionary of words to play with
char *list[] ={
"banana", "programmer",
"tongue", "favourite",
"serendipity", "sportman",
"harmony", "goalkeeper",
"operation", "hamstring",
"vibration", "wonderful"
};

main()
{
int wordi, lives;
char *word ;

char m;
int i=0;
srand((unsigned int)time(NULL));

while (1){
// this block select a random word to start a game
wordi = rand() % 12;
printf("\n rand index is %d \n ", wordi);
word = list[wordi];
int length=strlen(list[wordi]);
char *str1 = new char [length+1];//开始你那样写是不能通过编译的
char *str = new char[length+1]; // set variables to initial state
memset(str1,0,length+1);
memset(str, 0, length+1);
lives=8;
printf("\n Let's start. You have %d lives \n ", lives);
for (i=0; i {
str1[i]='_';
}
i=0;
strcpy(str, str1);
while(1)
{
printf("\n Next letter: ");
scanf("%s",&m);

// get next letter and work out if the letter is cwin the word
// update stare accordingly and print one of the follwing messages
char *word2 = word;
i = 0;
while(i {

if (m==*word2)
{
str1[i]=m;
i++;
word2++;
}
else
{
i++;
word2++;
}
}
if(strcmp(str, str1) != 0)
{
printf(" Lives: %d, Current word: %s \n", lives, str1);
strcpy(str, str1);
}
else if (strcmp(str, str1)==0)
{
lives--;
printf(" Lives: %d, Current word: %s \n", lives, str1);
}
if (strcmp(str1,list[wordi])==0)
{
printf(" The word is %s, you have won !! \n",str1);
break;
}
else if (lives==0)
{
printf(" Game Over \n");
break;
}
}
char getit[1];
// this loops ask the player if he wants to play again and waits for letter Y/N
// you don't need to change this
while (1)
{
printf("\n Do you want to play again [Y/N] \n");
scanf("%s",&getit[0]);
if ((getit[0] == 'N') || (getit[0] == 'n'))
return 0;
if ((getit[0] == 'Y') || (getit[0] == 'y'))
break;
}

// goes to play again
}

}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯