实现起来还是比较简单的,就是里面有一个小问题还没弄懂....定义在main函数之外的全局变量为什么初始化不成功呢?
还有就是个人一个比较大的问题...读程序的能力太差了(可能是跟平时看程序看少了有关吧...
#include<iostream>
#include<cstring>
using namespace std;
int time = 0; int Find = 0;//time 是猜错的次数
const int MAXtime = 7,MAXN = 30;
char com[MAXN], you[MAXN];//com是计算机的单词 you是用户输入的单词
void deletecom(int j, int lencom);//把猜对的词置为空格
void guess(int i);//判断用户输入的第i个字符
int main()
{
int num;
while (cin >> num&&num != -1)
{
time = 0;
Find = 0;
for (int i = 0; i < MAXN; i++)
{
com[i] = 0;
you[i] = 0;
}
//初始化
//DeleteAllData; 为什么不行呢
cin >> com;
cin >> you;
int lenyou = strlen(you);
for (int i = 0; i < lenyou; i++)
{
guess(i);//判断第i个字符
if (time >= MAXtime)break;
}
cout << "(test) time is " << time <<"the len of you input is "<<strlen(you)<< endl;//测试输出中间数字
cout << "After test the computer word is " << com << endl;
cout << "Round " << num << endl;
cout << "You ";
if (time >= MAXtime)//lose
{
cout << "lose." << endl;
}
else if (Find==strlen(com))//所有都猜对了
{
cout << "win." << endl;
}
else
{
cout << "chickened out." << endl;
}
}
return 0;
}
void deletecom(int j, int lencom)//把猜对的词置为空格
{
for (int i = 0; i < lencom; i++)
{
if (i == j)continue;
else if (com[i] == com[j]) com[i] = ' ';
}
com[j] = ' ';
}
void guess(int i)//判断用户输入的第i个字符
{
int lencom = strlen(com);
for (int j = 0; j < lencom; j++)
{
if (you[i] == com[j])//找到了...要把所有相同的都置为空格
{
deletecom(j, lencom);
Find++;
return;
}
}
time++;//遍历了之后还没找到 说明...猜错了
Find++;
}
/*bool win(void)//bugbugbug
{
int lencom = strlen(com);
for (int i = 0; i < lencom; i++)
{
if (com[i] != ' ' || com[i] != 0)
return 0;//说明还有没猜到的
}
return 1;
}*/
/*void DeleteAllData(void)//为什么这样初始化 time失败了
{
time = 0;
for (int i = 0; i < MAXN; i++)
{
com[i] = 0;
you[i] = 0;
}
}*/
问题part https://paste.ubuntu.com/26283985/
解决了: 函数即使没有参数 调用的时候也要在后面加上括号(只有函数名 代表的是一个函数指针 没有调用函数 )