猜数字游戏详解(附代码)(保姆级教学)

本文介绍了如何使用C语言编写猜数字游戏。首先通过创建源文件并在VS2019中设置,接着设计游戏流程,包括生成菜单、使用do-while和switch-case控制游戏循环。游戏核心是生成0到100的随机数,玩家进行猜测,程序根据猜测给出反馈。文章提供了详细的代码实现和游戏结束后的处理。
摘要由CSDN通过智能技术生成

前言

我们都知道猜数字的玩法,那怎么通过C语言来实现呢?希望通过阅读这篇文章可以给你带来帮助,我们从创建源文件开始,到最后实现整个游戏,全程一步一步手拿把掐的分享博主的经验,以下内容是详细过程及代码。话不多说,直接上货!

一·创建源文件

我们在vs2019创建一个源文件,如下图所示:

二.游戏的设计 

首先在写代码前,我们要想通整个游戏过程。首先我们要给游戏写一个菜单代码

代码如下展示:

void menu()
{
	printf("******************************\n");
	printf("******* 1.play 0.exit ********\n");
	printf("******************************\n");
}

然后我们要想到这是一个猜数字的游戏,我们要让电脑生成一个随机数,我们来猜,所以这时候引入一个产生随机数的函数,此后玩家不会只玩一次游戏,我们等到游戏结束以后,再给玩家选择是否需要再进行一次游戏,所以我们使用do-while语句,在每次进入游戏时,打印一个菜单,然后进行选择时,我们使用到了switch-case语句,当我们选择1的时候,进入游戏函数,当我们选择0的时候,退出游戏,当我们选择非0非1的时候,提醒玩家输入错误,请重新输入。

代码展示如下:

int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请输入:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			{
				game();
				break;
			}
		case 0:
		{
			printf("退出游戏\n");
			break;
		}
		default:
		{
			printf("输入错误\n");
		}
		}
	} while (input);
	return 0;
}

三.游戏的实现

我们刚才提到了一个游戏函数,现在我们来把它实现一下。首先我们要确定随机数产生的范围,0~100,否则太大了玩家也不好猜,所以我们控制在这个范围。我们在这里设置一个变量max来存放刚才产生的随机数。

下一步我们要输入我们猜的数字,我们设置一个变量guess来存放输入的数字,这时我们if语句判断两个数字的大小,一共有三种情况,猜大了,猜小了,猜对了。

代码如下展示:

void game()
{
	RAND_MAX;
	int max = rand() % 100 + 1;
	int guess = 0;
	while (1)
	{
		printf("请猜数字:>");
		scanf("%d", &guess);
		if (guess > max)
		{
			printf("猜大了\n");
		}
		else if (guess < max)
		{
			printf("猜小了\n");
		}
		else
		{
			printf("猜对了\n");
			break;
		}
	}

}

四.游戏结果展示

 五.结束语

这就是猜数字游戏有关的全部内容啦~希望这篇文章可以给你带来实质性的帮助,最后也祝你早日写出自己的猜数字代码哦~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
guessing and betting number game The user starts with 100 dollars, and is able to bet and guess until they quit or have no more money. In each turn, they are asked for a bet. If they enter 0, then the program should give a farewell message and quit. The bet must be positive and within their available balance, If they enter incorrectly, the program should set the bet to their balance and tell them this. Once a bet is entered, they must pick a number between 1 and 10 (you do not need to errorcheck or correct this). This guess is then compared to a number the computer randomly generates each time (also between 1 and 10). If the guess is correct, the user wins the amount of their bet. If the guess is incorrect, the user will lose their bet divided by 5 and multiplied by the distance from the correct number - e.g. if the bet is 50, the computer’s number is 5 and the user guesses 7, then the user will lose 20 dollars( 50/5 * (7-5) = 20 ). However, they should not lose more than their balance, so if their balance is 50, the bet is 40, the computer’s number is 1 and the user guesses 10, (40/5 * (10-1) = 72 > 50 )then they should lose 50. After each turn, the program should display that turn's winnings (or loss amount) and the new balance,and then repeat the game until the user either quits or has no money left. Dollar values should be formatted in the standard way with two decimal places and a $ in front. Sample output from the program is below. You should make your program match this exactly (except for your name). An appropriate welcome message with your name in it should be shown at the start of the program. Assignment 1 : Guessing Game Written by yourname Please enter your bet (up to $100.00): $50 Guess a number between 1 and 10: 5 Correct! You win $50.00 Your new balance is $150.00 Please enter your bet (up to $150.00): $200 Your bet is $150.00 Guess a number between 1 and 10: 6 Wrong! The computer chose: 4 You lose $60.00 Your new balance is $90.00 Please enter your bet (up to $90.00): $80 Guess a number between 1 and 10: 1 Wrong! The computer chose: 7 You lose $90.00 Thank you for playing! Press any key to continue . . . Another Sample: CP1200 Guessing Game Written by Lindsay Ward Please enter your bet (up to $100.00): $-20 Your bet is $100.00 Guess a number between 1 and 10: 5 Wrong! The computer chose: 2 You lose $60.00 Your new balance is $40.00 Please enter your bet (up to $40.00): $10.50 Guess a number between 1 and 10: 12 Wrong! The computer chose: 2 You lose $21.00 Your new balance is $19.00 Please enter your bet (up to $19.00): $0 Thank you for playing! Press any key to continue . . . srand( static_cast<int>(time(0))) ; 初始化 computerNumber = 1 + rand( ) % 10 ; 产生随机数 cout << fixed << setprecision(2) ; 控制输出显示2位小数
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值