UVA 489 Hangman Judge

 1 #include "stdio.h"
 2 #include "string.h"
 3 int left,chance;//还猜left个字母,错chance次后就会输
 4 char s[100],s2[100];//s是答案 ,s2是玩家猜的序列
 5 int win,lose;//win=1表示赢了 ,lose=1表示输了
 6 
 7 void guess(char ch)
 8 {
 9     int bad=1;
10     for(int i=0;i<strlen(s);i++)
11     {
12         if(ch==s[i])
13         {
14             s[i]=' ';//猜过的字母再猜算错,将猜对的置为空格
15             --left;
16             bad=0;
17         }
18     }
19     if(bad)
20         --chance;
21     if(chance==0)
22         lose=1;
23     if(left==0)
24         win=1;
25 }
26 
27 int main()
28 {
29     int rnd;
30     while(scanf("%d%s%s",&rnd,s,s2)==3&&rnd!=-1)
31     {
32         printf("Round %d\n",rnd);
33         left=strlen(s);
34         win=lose=0;//初始化
35         chance=7;//初始化
36         for(int i=0;i<strlen(s2);i++)
37         {
38             //guess
39             guess(s2[i]);//猜一个字母
40             if(win||lose)//检查状态
41                 break;
42         }
43         if(win)//输出结果
44             printf("You win.\n");
45         else if(lose)
46             printf("You lose.\n");
47         else
48             printf("You chickened out.\n");
49     }
50     return 0;
51 }

 

posted @ 2019-01-22 10:50  付玬熙  阅读(88)  评论(0编辑  收藏  举报