C++ 制作一个“测运”小游戏-rand()函数的应用
游戏说明:
游戏名:Lucky Guy
玩法说明:有2种模式可以选择,一种是一直选择数字,直到抽到炸弹为止。另一种是在0~9个数字中进行选择,有5个炸弹,最高分为5,抽到炸弹即游戏结束。游戏结束后,可以选择继续玩或者直接退出。
主要用到了rand()函数,具体用法可以参考:百度百科
文件下载:
码云:传送门
程序主界面:

源码如下:
#include <iostream>
#include<stdlib.h>
#include <stdio.h>
# include"time.h"
using namespace std; int main()
{ cout<<"Game:Lucky Guy"<<endl; //Game name游戏名
//system("bash ~/Desktop/lucky/gameName.sh");
cout<<"_(:з」∠)_"<<endl; char restart=''; //restartstart the game's variables 重新开始的变量
while(restart==''){
char checkpoint; //Level selection variables 选择模式的变量 cout<<"To measurestart today's lucky index!"<<endl<<endl;
//system("bash ~/Desktop/lucky/checkpoint.sh");
cout<<"Select the level: 1. Endless mode 2. After one stop"<<endl; //选择模式 scanf("%c",&checkpoint);
cout<<endl; //Level selection module
if(checkpoint=='') //Level 2 关卡2
{ int map[]={,,,,,,,,,}; //10 numbers 10个数字
srand((unsigned)time(NULL)); //设置随机数获取位置
int i=;
int j;
int ran[]={-,-,-,-,-}; //5 mines 存储5个炸弹
cout<<"Level 2"<<endl;
cout<<"We randomly generated 5 mines and generated mine numbers between 0 and 9"<<endl; //0~9中有5个炸弹
cout<<endl;
cout<<"---------------------"<<endl;
cout<<"|0|1|2|3|4|5|6|7|8|9|"<<endl;
cout<<"---------------------"<<endl;
cout<<"The highest lucky index in the end-stop mode is: 5"<<endl; //最高幸运值为5 //To determine if mines are duplicates
for(i = ; i < ; i++) // Subscript increments for later processing
{
//rand()Without arguments, it returns an integer from 0 to the maximum random number. The size of the largest random number is usually a fixed large integer.
ran[i] = rand()%;//Generates a random integer from 0 to 9 of these 10 integers 生成0~9的随机数
for(j= ; j < i; ++j)
{
if ( ran[j] == ran[i]){//If you repeat 如果重复了
ran[i]=-;
i--;
}
}
}
cout<<endl; //Output the result in the mine array /*for(i=0;i<5;i++){
cout<<ran[i]<<" ";
}
cout<<endl;
*/
for(i=;i<;i++){
map[ran[i]]=;
} //Output options
/*for(i=0;i<10;i++){
cout<<map[i]<<" ";
}
cout<<endl;
*/ //Statistics section
int X;
int flag=; //End the game's game variables 游戏结束的变量
int luck=; //Returned lucky index 幸运值
while(flag==){
cout<<"Please enter the number of your choice (don’t choose the one you selected before):";
cin>>X;
if(X>||X<){ //Exclude numbers that don’t match rules
cout<<"Please enter an integer within 0~9."<<endl;
continue;
}
else if(map[X]==-)
{
cout<<"This number has already been used"<<endl;
}
else{
if(map[X]==){
cout<<"Stepping on mines, the current lucky index is:"<<luck<<endl;
flag=;
}
else{
luck++;
map[X]=-;
if(luck==)
{
cout<<endl;
cout<<"Wow, the lucky index is: 5, clearance! You are today's lucky!"<<endl;
flag++;
}else{
//system("luckyindex.sh");
cout<<"Good luck, now the lucky index is:"<<luck<<endl;
}
}
}
}
}else if(checkpoint==''){ //first round
cout<<"Level 1"<<endl;
cout<<"We randomly generated 5 mines and generated mine numbers between 0 and 9"<<endl; //0~9随机设置5个炸弹
cout<<endl;
cout<<"---------------------"<<endl;
cout<<"|0|1|2|3|4|5|6|7|8|9|"<<endl;
cout<<"---------------------"<<endl; int flag=; //Variables to the next level
int luck=; //Lucky index 幸运值
while(flag!=){
int map[]={,,,,,,,,,};//10 numbers 10个数
srand((unsigned)time(NULL)); //设置随机数获取位置
int i=;
int j;
int ran[]={-,-,-,-,-};
for(i = ; i < ; i++) // Subscript increments for later processing
{
ran[i] = rand()%;
for(j= ; j < i; ++j)
{
if ( ran[j] == ran[i]){//If you repeat
ran[i]=-;
i--;
}
}
}
cout<<endl;
for(i=;i<;i++){
map[ran[i]]=;
} //Output array results
/*for(i=0;i<5;i++){
cout<<ran[i]<<" ";
}
cout<<endl;
*/ //Output options
/*for(i=0;i<10;i++){
cout<<map[i]<<" ";
}
cout<<endl;
*/ int X; //存储输入的数字
cout<<"Please enter the number you choose:"; //请输入你选择的数字
cin>>X;
if(X>||X<){ //Exclude numbers that don’t match rules
cout<<"Please enter an integer within 0~9."<<endl; //输入0~9
continue;
}else{
if(map[X]==){
cout<<"Stepping on mines, the current lucky index is:"<<luck<<endl; //选中,显示当前幸运值
flag++;
}else{
luck++;
cout<<"Good luck, now the lucky index is:"<<luck<<endl; //结束,返回幸运值
}
}
}
}else{ //When entering the wrong number of levels, exclude non-conforming inputs
cout<<"Please enter the correct number of levels."<<endl;//请输入正确的数字
continue;
} //游戏结束模块
cout<<endl<<endl;
cout<<"********************************************************"<<endl;
cout<<"* Think you are European Emperor? Then fight it again! *"<<endl;
cout<<"********************************************************"<<endl;
//system("bash ~/Desktop/lucky/restart.sh");
cout<<"Enter 1 to continue the game"<<endl; //输入1继续游戏
cout<<"Enter any character other than 1 to exit the game"<<endl; //输入其他任意字符结束游戏
cin>>restart;
getchar(); } return ;
}
C++ 制作一个“测运”小游戏-rand()函数的应用的更多相关文章
- 【C语言探索之旅】 第一部分第八课:第一个C语言小游戏
内容简介 1.课程大纲 2.第一部分第八课:第一个C语言小游戏 3.第一部分第九课预告: 函数 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写 ...
- python小练习:使用循环和函数实现一个摇骰子小游戏。游戏规则如下:游戏开始,首先玩家选择Big or Small(押大小),选择完成后开始摇三个骰子,计算总值,11<=总值<=18为“大”,3<=总值<=10为“小”。然后告诉玩家猜对或者是猜错的结果。
python小练习:使用循环和函数实现一个摇骰子小游戏.游戏规则如下:游戏开始,首先玩家选择Big or Small(押大小),选择完成后开始摇三个骰子,计算总值,11<=总值<=18为“ ...
- 用原生javascript做的一个打地鼠的小游戏
学习javascript也有一段时间了,一直以来分享的都是一些概念型的知识,今天有空做了一个打地鼠的小游戏,来跟大家分享一下,大家也可以下载来增加一些生活的乐趣,下面P出代码:首先是HTML部分代码: ...
- 用 Python 制作一个艺术签名小工具,给自己设计一个优雅的签名
生活中有很多场景都需要我们签字(签名),如果是一些不重要的场景,我们的签名好坏基本无所谓了,但如果是一些比较重要的场景,如果我们的签名比较差的话,就有可能给别人留下不太好的印象了,俗话说字如其人嘛,本 ...
- 小游戏——js+h5[canvas]+cs3制作【五子棋】小游戏
五子棋小游戏学习—— 有一个问题是,棋盘线的颜色,在canvas中,明明设置了灰色,但在我的预览中还是黑色的,很重的颜色. 以下是复刻的源码: <!DOCTYPE html> <ht ...
- html5面向对象做一个贪吃蛇小游戏
canvas加面向对象方式的贪吃蛇 2016-08-25 这个小游戏可以增加对面向对象的理解,可以加强js逻辑能力,总之认真自己敲一两遍收获还是不少啊!!适合刚学canvas的同学练习!! 废话不多说 ...
- 第一个leapmotion的小游戏
自从看过leapmotion的宣传视频,就被吸引住了.觉得这东西迟早要替代鼠标,然后关注了一年多leapmotion的动态,终于在今年8月份入手了一只.//675大洋啊,心疼~ 一直想写份评测,一直想 ...
- python新手如何编写一个猜数字小游戏
此文章只针对新手,希望大家勿喷,感谢!话不多说先上代码: import random if __name__ == '__main__': yourname = input("你好! 你的名 ...
- 微信小程序-从零开始制作一个跑步微信小程序
来源:伯乐在线 - 王小树 链接:http://ios.jobbole.com/90603/ 点击 → 申请加入伯乐在线专栏作者 一.准备工作 1.注册一个小程序账号,得用一个没注册过公众号的邮箱注册 ...
随机推荐
- windbg在加载模块时下断点
假设我们希望在加载特定的dll时中断调试器,例如,我想启用一些SOS命令,而clr还没有加载,当您遇到程序中过早发生的异常,并且您不能依赖手动尝试在正确的时间中断时,这尤其有用.例如,在将调试器附加到 ...
- 70: libreoj #2424 区间dp
$des$ $sol$ $f_{i, j, k} => a => [1, i], b => [1, j], a_i = b_j | a_i != b_j , a_i => 0 ...
- linux命令之------Find命令
Find命令 1)作用:用来在指定目录下查找文件. 2)案例:将当前目录及其子目录下所有延申档名是c的文件列出来. Find -name “*.txt” 其中:*表示通配任意个字符:?表示通配单个字符 ...
- HDU-5215 Cycle(边双/判奇偶环)
题目 HDU-5215 Cycle 网上那个啥dfs的垃圾做法随便弄组数据已经hack掉了 做法 纯奇环偶环通过dfs树上,染色判断(由于偶环可能有两个奇环,通过一点相交,dfs树上并不能判完) 两环 ...
- hbase 监控指标项
名词解释 JMX:Java Management Extensions,用于用于Java程序扩展监控和管理项 GC:Garbage Collection,垃圾收集,垃圾回收机制 指标项来源 主机名 u ...
- element ui里面table分页,页数从0开始的怎么做?
需求: 后台请求的接口是从0页开始的,但是pagination是从1开始的,就是在点击pagination的第1页是后台转0 1首先在data里面定义为1,其他地方也是定义1 return { for ...
- 牛顿插值法(c++)
X Y 0.40 0.41075 0.55 0.57815 0.65 0.69675 0.80 0.88811 0.90 1.02652 1.05 1.25382 #include using nam ...
- 美团-2019Q2述职总结
述职要求: 产品对平台化的规划并不清晰:内部因素:对SaaS平台的理解不够深刻: 对公司相关脚手架,服务搭建相关需要注意的点,有更深入的认识.对做系统服务的关注点有了更深入的理解. 功能权限的话: Q ...
- 忘记zip密码咋办?python在手密码我有
整理资料时发现几个 zip 文件的密码忘记了,于是尝试用python暴力破解 首先是读取和解压zip文件,使用 zipfile 库 import zipfile z = zipfile.ZipFile ...
- java中JSON转含泛型对象
public static void main(String[] args) { UserDto userDto=new UserDto("test","14" ...