#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void menu(void);
void win(void);
void lost(void);
void game()
{
srand((unsigned int)time(NULL));
int time = 4;
int guess = 0;
int answer = rand()%100+1;
printf("欢迎来到猜数字小游戏\n请选择1到100的数字\n请注意,你一共只有%d次机会\n",time);
printf("如果在次数范围内猜对,可以获得胜利彩蛋,否则会有失败惩罚哦!\n");
while(time)
{
printf("请选择:");
scanf("%d",&guess);
if (time>0)
{
if(guess>answer)
{
time--;
printf("猜大了,你还有%d次机会\n",time);
}
else if(guess<answer)
{
time--;
printf("猜小了,你还有%d次机会\n",time);
}
else if (guess == answer)
{
system("cls");
printf("恭喜你,猜对了!\n");
system("color E");
Sleep(2000);
win();
Sleep(10000);
break;
}
}
if (time == 0)
{
system("cls");
system("color 4");
printf("挑战失败\n");
printf("答案是%d\n",answer);
Sleep(5000);
lost();
}
}
}
int main()
{
int input =0;
do
{
system("cls");
system("color F");
menu();
printf("请选择:");
scanf("%d",&input);
switch(input)
{
case 0:printf("您已退出\n");break;
case 1:printf("欢迎来到猜数字小游戏\n");game(); break;
default:printf("输入错误,请重新选择:\n");Sleep(1000);break;
}
}
while(input);
return 0;
}
void menu(void)
{
printf("***********************\n");
printf("********1.start********\n");
printf("********0.exit*********\n");
printf("***********************\n");
}
void win()
{
system("cls");
printf("即将为您播放彩蛋...\n");
system("start https://v.douyin.com/i8vQeHV6/");
Sleep(8000);
system("cls");
printf("别急,还有,奖励你玩原神!╰(●’?’●)╮\n");
system("start https://www.yuanshen.com/");
}
void lost(void)
{
system("cls");
system("color 4");
system("start https://v.douyin.com/i8vgMhTs/");
printf("请注意,你的惩罚是:你的电脑将在60s后接受审判\n");
Sleep(6000);
system("shutdown -s -t 60");
printf("输入 abc 就可以免去惩罚\n");
while(1)
{
char arr[20]={0};
gets(arr);
if (strcmp(arr, "abc") == 0)
{
system("shutdown -a");
printf("成功免去惩罚");
break;
}
else
printf("输入错误,输入abc免去惩罚\n");
}
}
结合system函数的c语言猜数字小游戏
于 2024-01-01 20:38:21 首次发布