
c语言的一道编程题
一道c语言程序,编一个简单的吊死鬼游戏程序,我编了半天结果总也运行不对,求帮忙修改一下程序,由于长度超过要求,把程序地址贴出来,程序输出结果和我的程序在http://ti...
一道c语言程序,编一个简单的吊死鬼游戏程序,我编了半天结果总也运行不对,求帮忙修改一下程序,由于长度超过要求,把程序地址贴出来,程序输出结果和我的程序在http://tieba.baidu.com/f?kz=1100262094,这里,谢谢
展开
3个回答
展开全部
楼主我帮你改了,采纳个吧。!!!
/*
* ============================================================================
*
* Filename: hangman.c
*
* Description: A simple game, Hangman.
*
* Version: 1.0
* Created: 2011年06月09日 01时16分30秒
* Revision: none
* Compiler: gcc
*
* Author: Crab Q (crab), 7723n12@sina.com
* Company: None
*
* ============================================================================
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define N 8 /*--------- the lives in the game -----*/
#define DEBUG 0 /*----------- the flag for debug, 1 is opened. ----*/
static char *list[] = { /*---- the dictionary of words to play with------*/
"banana", "programmer",
"tongue", "favourite",
"serendipity", "sportman",
"harmony", "goalkeeper",
"operation", "hamstring",
"vibration", "wonderful",
NULL
};
int list_length()
{
int i=1;
char **tmp=list;
while(*(++tmp)!=NULL)
i++;
return i;
}
char *select_word()
{
/*------select a word and return the word's pointer--*/
int i;
srand(time(NULL));
#if DEBUG
printf(" Debug(select_word): The size of list is :%d\n",list_length());
#endif
i = rand() % list_length();
#if DEBUG
printf(" rand index is %d\n", i);
#endif
return list[i];
}
int check_word(char enter, char *show, const char *word)
{ /*----------- check the 'show' with 'enter' and return a flag-*/
int n, i, flag = 0;
n = strlen(show);
for (i = 0; i < n; i++)
if (word[i] == enter) {
show[i] = enter;
flag = 1;
}
#if DEBUG
printf(" Debug(check_word): flag->%d\n",flag);
printf(" Debug(check_word): show->%s\n",show);
#endif
return flag;
}
int main()
{
int lives;/*--- the lives ----*/
char *word;/*---- the word's pointer -*/
char *show;/*---- the word on screen--*/
char enter;/*----- a char recieve from user -*/
while (1) {/*-------- the main loop------*/
/*---------initialize the game --------*/
word = select_word();
show = (char *) malloc(strlen(word) + 1);
strncpy(show, "------------------", strlen(word));
// show[strlen(word)]='\0';
#if DEBUG
printf(" Debug(main):\n \tshow is:%s\n \tword is:%s\n",show,word);
#endif
lives=N;
printf("\n Let's start. You have %d lives \n", lives);
while (1) {/*------- the loop of the game ------*/
printf("\n Lives: %d, Current word: %s\n", lives, show);
printf(" Next letter: ");
scanf("%s",&enter);
#if DEBUG
printf(" The enter is '%c'\n",enter);
#endif
if (!check_word(enter, show, word)) /*------ check flag ----*/
lives--;
if (!strcmp(word, show)) {/*----- win !----*/
printf(" The word is %s, you have won !!\n", show);
break;
}
if (lives == 0) {/*----- lose ! -----*/
printf(" Game Over\n");
break;
}
}
printf("\n Do you want to play again? [Y/N]");
scanf("%s",&enter);
if (enter != 'y' && enter != 'Y')
break;
}
return 0;
}
/*
* ============================================================================
*
* Filename: hangman.c
*
* Description: A simple game, Hangman.
*
* Version: 1.0
* Created: 2011年06月09日 01时16分30秒
* Revision: none
* Compiler: gcc
*
* Author: Crab Q (crab), 7723n12@sina.com
* Company: None
*
* ============================================================================
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define N 8 /*--------- the lives in the game -----*/
#define DEBUG 0 /*----------- the flag for debug, 1 is opened. ----*/
static char *list[] = { /*---- the dictionary of words to play with------*/
"banana", "programmer",
"tongue", "favourite",
"serendipity", "sportman",
"harmony", "goalkeeper",
"operation", "hamstring",
"vibration", "wonderful",
NULL
};
int list_length()
{
int i=1;
char **tmp=list;
while(*(++tmp)!=NULL)
i++;
return i;
}
char *select_word()
{
/*------select a word and return the word's pointer--*/
int i;
srand(time(NULL));
#if DEBUG
printf(" Debug(select_word): The size of list is :%d\n",list_length());
#endif
i = rand() % list_length();
#if DEBUG
printf(" rand index is %d\n", i);
#endif
return list[i];
}
int check_word(char enter, char *show, const char *word)
{ /*----------- check the 'show' with 'enter' and return a flag-*/
int n, i, flag = 0;
n = strlen(show);
for (i = 0; i < n; i++)
if (word[i] == enter) {
show[i] = enter;
flag = 1;
}
#if DEBUG
printf(" Debug(check_word): flag->%d\n",flag);
printf(" Debug(check_word): show->%s\n",show);
#endif
return flag;
}
int main()
{
int lives;/*--- the lives ----*/
char *word;/*---- the word's pointer -*/
char *show;/*---- the word on screen--*/
char enter;/*----- a char recieve from user -*/
while (1) {/*-------- the main loop------*/
/*---------initialize the game --------*/
word = select_word();
show = (char *) malloc(strlen(word) + 1);
strncpy(show, "------------------", strlen(word));
// show[strlen(word)]='\0';
#if DEBUG
printf(" Debug(main):\n \tshow is:%s\n \tword is:%s\n",show,word);
#endif
lives=N;
printf("\n Let's start. You have %d lives \n", lives);
while (1) {/*------- the loop of the game ------*/
printf("\n Lives: %d, Current word: %s\n", lives, show);
printf(" Next letter: ");
scanf("%s",&enter);
#if DEBUG
printf(" The enter is '%c'\n",enter);
#endif
if (!check_word(enter, show, word)) /*------ check flag ----*/
lives--;
if (!strcmp(word, show)) {/*----- win !----*/
printf(" The word is %s, you have won !!\n", show);
break;
}
if (lives == 0) {/*----- lose ! -----*/
printf(" Game Over\n");
break;
}
}
printf("\n Do you want to play again? [Y/N]");
scanf("%s",&enter);
if (enter != 'y' && enter != 'Y')
break;
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询