您的位置:首页 > 编程语言 > C语言/C++

猜数字游戏

2015-07-28 22:03 435 查看
/* author:徐权

 * data:2015728

 * function:猜数字游戏

 * 实用平台:VS2012及以上(如果要移植到其他平台scanf_s和puts_s 应改为scanf和puts即可)

 */

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

#include<string.h>

#define N 5  //定义数组大小,从而决定猜的数字的个数

char *product(char *pre,int n) //机器随机产生一个数字,用于猜测

{
int i = 0,j = 0,s;
int count[10] = { 0 };
srand((int)time(NULL));
while (i < n-1)
{
s = rand() % 10;
if (count[s] == 0)
{
count[s] = 1;
pre[j] = s + '0';
j++;
i++;
}

}
pre[n - 1] = '\0';
return pre;

}

char *input_c(char *str,char *pre, int n) //过滤输入内容

{
int i = 0,j;
printf("请输入%d个不重复的数字:\n",N-1);
puts("********************************************************");
//scanf_s("%s", str, sizeof(char) * n);
gets_s(str,n-1);
if (strcmp(str, "exit") == 0 || strcmp(str, "EXIT") == 0)
{
printf("游戏结束,答案为:%s\n", pre);
system("pause");
exit(0);
}
while ((int)strlen(str) < N - 1 || (int)strlen(str) > N - 1)
{
printf("字符输入过多或者过少,请输入%d个不重复的数字:\n",N-1);
puts("********************************************************");
//scanf_s("%s", str, sizeof(char) * n);
gets_s(str, n - 1);
}
while (i < N-2)
{
for (j = i+1; j < N - 1; j++)
{
if (str[i] == str[j])
{
printf("格式错误,请输入%d个不重复的数字:\n",N-1);
puts("********************************************************");
//scanf_s("%s", str, sizeof(char) * N);
gets_s(str, n - 1);
i = -1;
}
if (str[i] > '9' || str[i] < '0')
{
printf("格式错误,请输入%d个不重复的数字:\n",N-1);
puts("********************************************************");
//scanf_s("%s", str, sizeof(char) * N);
gets_s(str, n - 1);
i = -1;
}
}
i++;
}
return str;

}

char *pipei(char *pre, char *str, char *cnt,int n) //猜数字的答案提示器

{
int i, j;
for (i = 0; i < n - 1; i++)
{
cnt[i] = '0';
if (pre[i] == str[i])
{
str[i] = 'C';
cnt[i] = 'A';
}
}
cnt[n - 1] = '\0';

for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1; j++)
{
if (pre[i] == str[j])
{
str[j] = 'C';
cnt[j] = 'B';
}
}
}
return cnt;

}

void output_c(char *cnt,int n,char L)//游戏难度选择器

{
for (int i = 0; i < n - 1; i++)
{
if (L == '0')
{
printf("%c", cnt[i]);
}
else
{
if (cnt[i] != '0')
{
printf("%c", cnt[i]);
}
}
}
printf("\n");

}

void print(char * pre,int count,int scount)//游戏结果显示

{
char p;
puts("********************************************************");
printf("你本次游戏共猜了%d次,总共玩了%d局游戏!\n游戏答案为:", count,scount);
puts(pre);
puts("********************************************************");

}

void descripe(void)//打印游戏说明

{
puts("********************************************************");
printf("*   游戏为猜数字游戏,一共%d个数字,每个数字不可能相同 *\n",N-1);
puts("*  猜中其中一个并且位置也是一一对应的则显示A,如果猜对 *");
puts("* 了,但是位置不同则显示B,没有猜对则显示0或者不显示, *");
puts("*简单难度显示0,困难难度则不显示,下面开始吧!祝你好运!*");
puts("* --------        简单输入0,困难输入1      ---------  *");
puts("********************************************************");

}

int core_c(void) //继续游戏选择

{
printf("继续游戏,Y/N?\n");
char core;
core = getchar();
getchar();
if (core == 'Y' || core == 'y')
{
return 0;
}
else
{
return 1;
}

}

int main(void)

{
char L;
int core, scount = 0; //core用于记录是否游戏的返回值,scount用于记录游戏的局数
int count = 0;//用于记录本次游戏共猜了几步
char *pre = (char *)malloc(sizeof(char)*N); //分配一定空间存储
char *str = (char *)malloc(sizeof(char)*N*N);
char *cnt = (char *)malloc(sizeof(char)*N);
descripe();
L = getchar();
getchar(); //用于吸收输入数字后的回车确定键
pre = product(pre, N);//随机产生的一个4位字符串
while (1)
{
count++;
// puts(pre); //测试显示结果
str = input_c(str,pre,N*N);//手动输入的一个4位字符串
// puts(str);//测试显示结果
cnt = pipei(pre, str, cnt, N);//判断字符串
output_c(cnt, N,L);//存储结果值
if (strcmp(cnt, "AAAA") == 0)
{
scount++;
print(pre, count,scount);
core = core_c();
if (core == 0)
{
puts("* --------        简单输入0,困难输入1      ---------  *");
L = getchar();
getchar();
count = -1;
pre = product(pre, N);//随机产生的一个4位字符串
}
else
{
break;
}
}
}
system("pause");
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息