You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <bits/stdc++.h>
# define maxn 100
int left , chance ; //还需要猜left个位置,错chance次之后就会输
char s [ maxn ] , s2 [ maxn ] ; //答案是字符串s,玩家猜的字母序列是s2
bool win , lose ; //win=1表示已经赢了;lose=1表示已经输了
/**
* 功能:猜字符函数
* @param ch
*/
void guess ( char ch ) {
bool bad = true ;
for ( int i = 0 ; i < strlen ( s ) ; i + + )
if ( s [ i ] = = ch ) {
left - - ;
s [ i ] = ' ' ; //阅后即焚~,标识为空格
bad = false ;
}
if ( bad ) - - chance ; //错一次,机会减少一次
if ( ! chance ) lose = true ; //没有机会了,就是输了
if ( ! left ) win = true ;
}
int main ( ) {
int rnd ; //轮次 一级公民, 在scanf中使用, 需要使用&符,表示按地址引用。
// s和s2, 为字符串数组, 二级公民, 不需要使用&符!
/*
例如scanf("%d %d",&a,&b) 若a, b均正确得到值, 则返回2
若a得到值, b不得到值, 则返回1( 返回成功接收到赋值的个数)
如果遇到错误或遇到end of file, 返回值为EOF
*/
while ( scanf ( " %d%s%s " , & rnd , s , s2 ) = = 3 & & rnd ! = - 1 ) //rnd=-1表示输入-1就停止
{
printf ( " Round %d \n " , rnd ) ;
win = lose = false ; //求解一组新数据之前要初始化
//测长
left = strlen ( s ) ;
//机会有7次
chance = 7 ;
//遍历每一个字符
for ( int i = 0 ; i < strlen ( s2 ) ; i + + ) {
guess ( s2 [ i ] ) ; //猜一个字母
if ( win | | lose ) break ; //输了,赢了都完事,当然,也可能没猜全,就是没输也没赢
}
//根据结果进行输出
if ( win ) printf ( " You win. \n " ) ;
else if ( lose ) printf ( " You lose. \n " ) ;
else printf ( " You chickened out. \n " ) ;
}
return 0 ;
}