手把手带你学会猜数字游戏代码编写

本文通过手把手的方式教你如何编写猜数字游戏的代码,包括1-100的随机数生成、游戏模式设计(简单和困难)以及游戏循环结构。文章详细分析了代码编写思路,包括整体框架、重难点和相关细节,并提供了源代码供读者参考。
摘要由CSDN通过智能技术生成

声明:因本人为非科班转码,个人水平有限,博客中难免有不准确之处,如有读者发现,本人恳请大家积极指出,本人也想知道自己的错误在哪里。欢迎大家一起指正,共同进步。

  • 联系方式:3146367553@qq.com

想直接看源代码的小可爱可以直接跳转至3.源代码。


目录

1.程序功能大致描述

2.代码编写思路分析

2.1 整体框架分析

2.2 重难点分析(1-100的随机数如何生成)

2.3 相关细节与两种模式

3. 源代码与截图


1.程序功能大致描述

由系统生成1-100的随机数(整型),由玩家在键盘上进行猜测,玩家无论猜大猜小电脑都会告知你,如果猜对了会恭喜你。同时,玩家可以在玩游戏的中途退出游戏。个人设置了简单和困难两种模式可以选择。


2.代码编写思路分析

2.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位小数
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值