#include<stdlib.h>
#include<stdio.h>
#include<time.h>
void menu()
{
printf("###########################\n");
printf("###### 1. game ######\n");
printf("###### 0. exit ######\n");
printf("###########################\n");
}
void game()
{
//取模100是不可能超过100滴,加一就是1 - 100的数字
int ret = rand() % 100 + 1;
int guess = 0;
while (1)
{
printf("请输入您猜的数字>");
scanf("%d", &guess);
if (guess > ret)
{
printf("猜大了\n");
}
else if (guess < ret)
{
printf("猜小了\n");
}
else
{
printf("才对了\n");
break;
}
}
}
int main()
{
int intput = 0;
int i = 3;
//假设密码是:abcdef
int password[20] = { 0 };
//设置随机数的生成起点只需要生成一次就好了
srand((unsigned int)time(NULL));
printf("####欢迎来到猜数字游戏!####\n");
printf("####请输入登录游戏密码!####\n");
while (i > 0)
{
scanf("%s", &password);
if (strcmp(password, "abcdef")== 0)
{
printf("登录成功,欢迎进入游戏界面\n");
do
{
menu();
scanf("%d", &intput);
switch (intput) {
case 1:
game();
break;
case 0:
printf("退出游戏.\n");
break;
default:
printf("选择错误,请重新选择!\n");
break;
}
} while (intput);
break;//结束程序
}
else
{
if((i-1) != 0)
printf("密码错误,您还有%d次机会,请重新输入!\n", i-1);
}
i--;
}
if (i == 0)
{
printf("登录失败,程序退出\n");
}
return 0;
}