猜数字游戏

/* 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;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值