#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>//用strlen计算字符块长度的头文件
#include<time.h>//时间戳头文件
#include<stdlib.h>//srand函数头文件
#include<windows.h>//用system("cls")清空屏幕的头文件
void game()//定义游戏
{
//srand((unsigned int)time(NULL));
int ret = rand() % 100 + 1;//%100+1是为了取1到100的随机数
//printf("%d\n", ret);
int guess = 0;
while (1)
{
scanf("%d", &guess);
if (guess > ret)
{
printf("猜大了\n");
}
else if (guess < ret)
{
printf("猜小了\n");
}
else if (guess == ret)
{
printf("恭喜你!猜对了");
break;
}
}
}
void menu()//在外面定义一个菜单
{
char arr1[] = "***************************\n";
char arr2[] = "***************************\n";
char arr3[] = "***************************\n";
char arr4[] = "***************************\n";
char arr5[] = "********** 1.开始游戏 ***\n";
char arr6[] = "********** 0.退出 *******\n";
int left = 0;
int right = strlen(arr3) - 1;
while (left <= right)
{
arr2[left] = arr5[left];
arr2[right] = arr5[right];
arr3[left] = arr6[left];
arr3[right] = arr6[right];
Sleep(150);//延迟150毫秒
system("cls");//清空屏幕
printf("%s\n", arr1);
printf("%s\n", arr2);
printf("%s\n", arr3);
printf("%s\n", arr4);
left++;
right--;
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));//在main函数中引用时间戳随机函数//time(NULL)时间戳函数
do //并且用unsigned int进行强制类型转换
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("请猜一个1-100的数");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择\n");
}
} while (input);
return 0;
}