📣 独立日限时特惠: VIP通行证立减$68
查看: 2760| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

问个编程的问题

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
最近在做final project for intro to C,让做一个hangman游戏,就是猜一个单词中的字母,猜错了画个吊死的小人那个游戏(link to wiki),要求可以让用户猜,也可以让电脑猜,单词表从一个外部文件读进来。我已经写好了这个程序,但是电脑猜这一部分我觉得有点问题,就是用的策略太笨了,我的办法是先让计算机算出所有字母的频率,然后从高到低猜,我觉得这样不太smart,但是一时想不出更好的办法。
代码我贴在下面了,运行时需要的另外两个文件我也附在附件里,小弟在这里请大家帮忙想想办法,看看有没有更好的策略,第二也是想让各位对我的代码提点意见,风格上是不是不太好。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>

  5. #define  MAXLEN                50
  6. #define  SCREEN                "hangman.txt"
  7. #define  DICT                        "dict.txt"
  8. #define  TRI                        12

  9. typedef struct screen{
  10.         char        pixel[65][15];
  11. }screen;

  12. typedef struct dicent{
  13. long int index;
  14. char*         entry;
  15. struct         dicent* next;
  16. }dicent;

  17. typedef struct sentence{
  18.         char words[MAXLEN];
  19.         int  x;
  20.         int  y;
  21. }sentence;

  22. screen loadscreen(FILE* filptr);
  23. //It loads a screen from a file.

  24. void printscreen(screen current);
  25. //It output a screen.

  26. int askcommand(int state);
  27. //It asks users to input a command.

  28. int userguess(screen current,int state);
  29. //It is the user guess routine.

  30. int computerguess(screen current,int state);
  31. //It is the computer guess routine.

  32. int readict(FILE *filptr,dicent* head);
  33. //It reads the dictionary.

  34. int        randword(dicent* head,char* wordsel,int number);
  35. //It gives a random word.

  36. screen setscreen(screen current,int ulx,int uly,int drx,int dry);
  37. //It sets certain area to blank.

  38. screen sentosrc(screen current,sentence temp);
  39. //It sets certain words to screen.

  40. screen chartosrc(screen current,char letter,int locx,int locy);
  41. //It sets certain letter to certain location.

  42. int checkletter(char letter);
  43. //It checks whether the letter is illegal or not.

  44. void setchecklist(void);
  45. //It set the check list to all 0s.

  46. void setchance(int* chance,dicent* head);
  47. //It caculates out every letter's chances.

  48. int checklist[26]={0};

  49. int main(void){
  50.         int state=0;
  51.         FILE* toscr;
  52.         screen current;
  53.         toscr=fopen(SCREEN,"r");
  54.         if(toscr==NULL){state=-1;}
  55.         while(state!=3)
  56.         {       
  57.                 if(state==-1){
  58.                         system("cls");
  59.                         printf("Program need hangman.txt\n");
  60.                         exit(1);
  61.                 }
  62.                 if(state==-2){
  63.                         printf("There is something wrong with your input,\nSorry but I have to force you to quit\n");
  64.                         exit(1);
  65.                 }
  66.                 if(state==-3){
  67.                         printf("Program need dict.txt\n");
  68.                         exit(1);
  69.                 }
  70.                 if(state==0){current=loadscreen(toscr);}
  71.                 printscreen(current);
  72.                 state=askcommand(state);
  73.                 if(state==1)
  74.                 {state=userguess(current,state);setchecklist();}
  75.                 if(state==2)
  76.                 {state=computerguess(current,state);}
  77.         }
  78.         fcloseall();
  79.         if(state==3)
  80.         {return 0;}
  81.         else
  82.         {return 1;}
  83. }
  84. screen loadscreen(FILE* filptr){
  85.         int i,j;
  86.         screen target;
  87.         for(j=0;j<15;j++){
  88.                 for(i=0;i<65;i++)
  89.                 {target.pixel[i][j]=fgetc(filptr);}
  90.         }
  91.         rewind(filptr);
  92.         return target;
  93. }
  94. void printscreen(screen cureent){
  95.         int i,j;
  96.         system("cls");
  97.         for(j=0;j<15;j++){
  98.                 for(i=0;i<65;i++){
  99.                         putchar(cureent.pixel[i][j]);
  100.                 }
  101.         }
  102. }
  103. int askcommand(int state){
  104.         char command;
  105.         if(state==0){
  106.                 printf("Input command here:");
  107.                 fflush(stdin);
  108.                 command=getchar();
  109.                 state=command-'0';
  110.         }
  111.         if(state==4){
  112.                 printf("Try your letter(lower case please):");
  113.                 fflush(stdin);
  114.                 command=getchar();
  115.                 if(command>='a'&&command<='z')return command;
  116.                 else return -2;
  117.         }
  118.         if(state==7){
  119.                 printf("Please input the number of letters in your word:");
  120.                 fflush(stdin);
  121.                 command=getchar();
  122.                 if(command>'0'&&command<=('0'+MAXLEN))return (command-'0');
  123.                 else return -2;
  124.         }
  125.         if(state==9){
  126.                 fflush(stdin);
  127.                 command=getchar();
  128.                 if(command=='y'||command=='n')return command;
  129.                 else return -2;
  130.         }
  131.         if(state==10){
  132.                 printf("Now I need the position of the letter:\n");
  133.                 fflush(stdin);
  134.                 command=getchar();
  135.                 if(command>='0')return (command-'0'-1);
  136.                 else return -2;
  137.         }
  138.         if(state==11){
  139.                 printf("Please input the position,if you have done with the input plesase input 0\n");
  140.                 fflush(stdin);
  141.                 command=getchar();
  142.                 if(command>='0')return (command-'0'-1);
  143.                 else return -2;
  144.         }
  145.         if(state!=3&&state!=2&&state!=1) state=-2;
  146.         return state;
  147. }
  148. int userguess(screen current,int state){
  149.         int i,j=0,flag=0,success=0,trial=0;
  150.         FILE *todict;
  151.         sentence temp;
  152.         long int number,wordlen=0;
  153.         char wordsel[MAXLEN],letter;
  154.         dicent* head,* ptr,*te;

  155.         todict=fopen(DICT,"r");
  156.         head=(dicent *)malloc(sizeof(dicent));
  157.         if(todict==NULL){state=-3;}
  158.         else{
  159.                 state=4;
  160.                 number=readict(todict,head);
  161.                 wordlen=randword(head,wordsel,number);
  162.                 current=setscreen(current,0,3,27,11);
  163.                 strcpy(temp.words,"The word for you:");
  164.                 temp.x=0;
  165.                 temp.y=3;
  166.                 current=sentosrc(current,temp);
  167.                 strcpy(temp.words,"                 ");
  168.                 for(i=0;i<strlen(wordsel);i++)
  169.                 {
  170.                         temp.words[i]='_';
  171.                 }
  172.                 temp.x=0;
  173.                 temp.y=4;
  174.                 current=sentosrc(current,temp);
  175.                 strcpy(temp.words,"Worng letters:");
  176.                 temp.x=0;
  177.                 temp.y=5;
  178.                 current=sentosrc(current,temp);
  179.                 printscreen(current);
  180.                 while(success!=strlen(wordsel)&&state!=-2&&trial<TRI){
  181.                         printscreen(current);
  182.                         printf("You have used %d/%d trials\n",trial,TRI);
  183.                         printf("The word contains %d letters.\n",strlen(wordsel));
  184.                         letter=askcommand(state);
  185.                         if(letter==-2)state=-2;
  186.                         else{
  187.                                 if(checkletter(letter)){
  188.                                 flag=0;
  189.                                 for(i=0;i<strlen(wordsel);i++){
  190.                                         if(letter==wordsel[i]){
  191.                                                 current=chartosrc(current,letter,i,4);
  192.                                                 printscreen(current);
  193.                                                 success++;
  194.                                                 flag=1;
  195.                                         }}
  196.                                 if(flag==0){current=chartosrc(current,letter,j,6);j++;}}
  197.                                 else{
  198.                                 printf("You have input the letter once.\nPlease press any key to continue.");
  199.                                 fflush(stdin);
  200.                                 getchar();}
  201.                         }
  202.                         trial++;
  203.                 }
  204.         }
  205.         if(trial==TRI)state=5;
  206.         if(success==strlen(wordsel))state=6;
  207.         //notice!!
  208.         if(state==5){
  209.                 strcpy(temp.words,wordsel);
  210.                 temp.x=0;
  211.                 temp.y=4;
  212.                 current=sentosrc(current,temp);
  213.                 printscreen(current);
  214.                 printf("Sorry you have used up your chance.\n");
  215.                 printf("Please press any key to get back to main menu.\n");
  216.                 fflush(stdin);
  217.                 getchar();
  218.                 state=0;
  219.         }
  220.         if(state==6){
  221.                 printf("Bravo!You are good at this!\n");
  222.                 printf("Please press any key to get back to main menu.\n");
  223.                 fflush(stdin);
  224.                 getchar();
  225.                 state=0;
  226.         }
  227.         ptr=head;
  228.         while(ptr->next!=NULL)
  229.         {
  230.                 te=ptr->next;
  231.                 free(ptr);
  232.                 ptr=te;
  233.         }
  234.         free(te);
  235.         fclose(todict);
  236.         return state;
  237. }
  238. int readict(FILE* filptr,dicent* head){
  239.         long int number=0;
  240.         char temp[MAXLEN];
  241.         dicent* ptr;
  242.         ptr=head;
  243.         while(fscanf(filptr,"%s",temp)!=EOF){
  244.                 ptr->index=number;
  245.                 ptr->entry=(char *)malloc(MAXLEN*sizeof(char));
  246.                 strcpy(ptr->entry,temp);
  247.                 ptr->next=(dicent *)malloc(sizeof(dicent));
  248.                 ptr=ptr->next;
  249.                 number++;
  250.         }
  251.         ptr->next=NULL;
  252.         rewind(filptr);
  253.         return number;
  254. }
  255. int        randword(dicent* head,char* wordsel,int number){
  256.         dicent* ptr;
  257.         int len,tag;
  258.         ptr=head;
  259.         srand((unsigned)time(NULL));
  260.         tag=rand()%number;
  261.         while(ptr->index!=tag){
  262.                 ptr=ptr->next;
  263.         }
  264.         strcpy(wordsel,ptr->entry);
  265.         len=strlen(wordsel);
  266.         return len;
  267. }
  268. screen setscreen(screen current,int ulx,int uly,int drx,int dry){
  269.         int i,j;
  270.         for(j=uly;j<=dry;j++){
  271.                 for(i=ulx;i<=drx;i++){
  272.                         current.pixel[i][j]=' ';
  273.                 }
  274.         }
  275.         return current;
  276. }
  277. screen sentosrc(screen current,sentence temp){
  278.         int i;
  279.         for(i=temp.x;i<(temp.x+strlen(temp.words));i++)
  280.         {
  281.                 current.pixel[i][temp.y]=temp.words[i-temp.x];
  282.         }
  283.         return current;
  284. }
  285. screen chartosrc(screen current,char letter,int locx,int locy){
  286.         current.pixel[locx][locy]=letter;
  287.         return current;
  288. }
  289. int checkletter(char letter){
  290.         if(checklist[letter-'a']==0){checklist[letter-'a']=1;return 1;}
  291.         else return 0;
  292. }
  293. void setchecklist(void){
  294.         int i;
  295.         for(i=0;i<26;i++)
  296.         {checklist[i]=0;}
  297. }
  298. int computerguess(screen current,int state){
  299.         int i,number,success=0,flag=0,len,chance[26]={0},tem,pos;
  300.         FILE *todict;
  301.         sentence temp;
  302.         dicent* head,* ptr,*te;
  303.         char letter,command;
  304.         todict=fopen(DICT,"r");
  305.         head=(dicent *)malloc(sizeof(dicent));
  306.         if(todict==NULL){state=-3;}
  307.         else{
  308.                 state=7;
  309.                 number=readict(todict,head);
  310.                 setchance(chance,head);
  311.                 current=setscreen(current,0,3,27,11);
  312.                 printscreen(current);
  313.                 strcpy(temp.words,"Hi!I am to guess your word!");
  314.                 temp.x=0;
  315.                 temp.y=3;
  316.                 current=sentosrc(current,temp);
  317.                 strcpy(temp.words,"Your Word:");
  318.                 temp.x=0;
  319.                 temp.y=4;
  320.                 current=sentosrc(current,temp);
  321.                 strcpy(temp.words,"          ");
  322.                 len=askcommand(state);
  323.                 if(len==-2){state=-2;}
  324.                 for(i=0;i<len;i++)
  325.                 {
  326.                         temp.words[i]='_';
  327.                 }
  328.                 temp.x=0;
  329.                 temp.y=5;
  330.                 current=sentosrc(current,temp);
  331.                 printscreen(current);
  332.                 flag=0;
  333.                 while(success<len&&state!=-2&&flag<TRI){
  334.                         state=9;
  335.                         printscreen(current);
  336.                         printf("This is the computer's %d/%d trial\n",flag+1,TRI);
  337.                         flag++;
  338.                         tem=0;
  339.                         for(i=0;i<26;i++){
  340.                                 if(tem<chance[i]){tem=chance[i];letter='a'+i;}
  341.                         }
  342.                         chance[letter-'a']=0;
  343.                         printf("Humm...My guess is %c,is it right(y/n)?\n",letter);//notice!
  344.                         if((command=askcommand(state))=='y'){
  345.                                 printscreen(current);
  346.                                 state=10;
  347.                                 pos=askcommand(state);//tested
  348.                                 if(pos!=-2&&pos<len){
  349.                                         success++;
  350.                                         current=chartosrc(current,letter,pos,5);
  351.                                         printscreen(current);
  352.                                 }
  353.                                 else {state=-2;break;}
  354.                                 printf("Is there any more this letter(y/n)?\n");
  355.                                 command=askcommand(9);//tested
  356.                                 if(command==-2){state=-2;break;}
  357.                                 else{
  358.                                         if(command=='y'){
  359.                                         state=11;
  360.                                         while(state==11){
  361.                                                 pos=askcommand(state);
  362.                                                 if(pos==-1)state=0;
  363.                                                 else{
  364.                                                         if(pos==-2||pos>=len){state=-2;break;}
  365.                                                 else{
  366.                                                         success++;
  367.                                                         current=chartosrc(current,letter,pos,5);
  368.                                                         printscreen(current);
  369.                                                 }}}}                                       
  370.                                         if(command=='n')state=0;
  371.                                 }
  372.                         }
  373.                         else{
  374.                                  if(command=='n')state=0;
  375.                                  if(command==-2)state=-2;
  376.                         }  
  377.                 }
  378.         }
  379.         if(success==len){state=8;}
  380.         else if(success>=0&&state!=-2)state=12;
  381.         if(state==8){
  382.                 printf("Computer is so smart!!\n");
  383.                 printf("Please press any key to get back to main menu.\n");
  384.                 fflush(stdin);
  385.                 getchar();
  386.                 state=0;
  387.         }
  388.         if(state==12){
  389.                 printf("I have lost the game :-(\n");
  390.                 printf("Please press any key to get back to main menu.\n");
  391.                 fflush(stdin);
  392.                 getchar();
  393.                 state=0;
  394.         }
  395.         ptr=head;
  396.         while(ptr->next!=NULL)
  397.         {
  398.                 te=ptr->next;
  399.                 free(ptr);
  400.                 ptr=te;
  401.         }
  402.         free(te);
  403.         fclose(todict);
  404.         return state;
  405. }
  406. void setchance(int* chance,dicent* head){
  407.         int i=0,j=0;
  408.         dicent *ptr;
  409.         char temp[MAXLEN];
  410.        
  411.         ptr=head;
  412.         while(ptr->next!=NULL){
  413.                 strcpy(temp,ptr->entry);
  414.                 i=0;
  415.                 while(*(temp+i)!='\0'){
  416.                           for(j=0;j<26;j++){
  417.                                 if(*(temp+i)=='a'+j||*(temp+i)=='A'+j)
  418.                                 {(*(chance+j))++;break;}
  419.                           }
  420.                           i++;
  421.                 }
  422.                 ptr=ptr->next;
  423.         }
  424. }
复制代码

dict.txt

100.37 KB, 下载次数: 4, 下载积分: 大米 -1 颗

hangman.txt

990 Bytes, 下载次数: 3, 下载积分: 大米 -1 颗


上一篇:关于程序抄袭雷同的问题
下一篇:拿到AD, 惶恐!! , 毕竟离毕业多年了---有70后的介绍下经验
🔗
newlxnewlx 2011-12-20 20:43:41 | 只看该作者
全局:
intro to C, 莫非lz是大一的海本?  
可惜没玩过这个游戏。。。
回复

使用道具 举报

🔗
chong2006tian 2012-1-19 01:09:21 | 只看该作者
全局:
楼上很厉害,路过学习。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表