猜数字小游戏[C语言][自制]

大一上的自制,嘛,不保证易读性,能用就用上吧大笑

具体思路:

产生随机数,然后是比较两者的大小,输出答案是大了还是小了,还是正确了。

代码如下:

/*This is a guess number game.Created by Hugh&Zero&Seven.
It is so hard to find a good way to identify 'guessNumber'
between number and character until we found the function
fflush(stdin).*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define ROUND 3       //set the round to make the game easier or more difficult
#define NUMBERS 20    //how many numbers exist
#define MIN_NUMBER 20 //最小数字
#pragma warning(disable:4996)

int main(void)
{
	char name[10];
	int guessTaken = 0, guessNumber;
	srand((unsigned)time(NULL));
	int number = rand() %NUMBERS+MIN_NUMBER;

	printf("Guess Number Game [Version 1.0 ]\n"
		"Copyright <c> 2014  632 Technical Team. All Right Reserved.\n\n");

	printf("====================================\n");
	printf("   What's your name?\n");
	printf("====================================\n");
	scanf("%s", name);
	printf("====================================\n");
	printf("   Hello!%s.I'm thinking about a\ninteger number between %d and %d.You"
		"\nonly have %d times to take a guess!\n", name,MIN_NUMBER,MIN_NUMBER+NUMBERS-1,ROUND);
	printf("====================================\n");
	while (guessTaken < ROUND)
	{
		if ((scanf("%d", &guessNumber)) == 1) //如果输入正确,那么返回值是1
			{
			if (guessNumber > number)
					printf("  Your guess is too high.Try again!\n"
					"====================================\n");
			else if (guessNumber < number)
					printf("  Your guess is too low.Try again!\n"
					"====================================\n");
			else
			{
					printf("====================================\n");
					printf("  You win!The number I am thinking is %d.\n", number);
					printf("You just took %d times to guess it.Congratulations!\n", guessTaken + 1);
					break;
			}
		}
		else {
			printf("O_o!!\nEnter error.Please enter a number.\n"
				"====================================\n");
			fflush(stdin); //清空缓冲输入
			continue;
		}
		guessTaken++;
	}
	if (guessTaken == ROUND)
	{
		printf("====================================\n"
			"  You fail..TAT\n  The number I am thinking about is %d.\n", number);
	}

	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值