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.注册一个小程序账号,得用一个没注册过公众号的邮箱注册 ...
随机推荐
- 关于System.ExecutionEngineException异常
什么是System.ExecutionEngineException 公共语言运行库的执行引擎中出现内部错误时引发的异常.这个类不能继承. 继承 Object Exception SystemExce ...
- WinDbg常用命令系列---显示段选择器dg、链接列表dl和字符串ds/dS
dg (Display Selector) dg命令显示指定选择器的段描述符. dg FirstSelector [LastSelector] 参数: FirstSelector指定要显示的第一个选择 ...
- DOM内容梳理2
JavaScript-DOM2(内容整理) 这两天新的知识有点多有点杂一时半会没有整理过来,以后不出意外会一直更行. js节点类型(NODETYPE) 查看节点类型 nodetype属性,返回的结果会 ...
- JS变量和变量交换的三种方法
一.what 变量就是用来存储数据的容器 二.how 通过var 关键字定义一个变量 var n1; //定义变量 变量的赋值:通过赋值运算符“=” 给变量赋值. var n2=123; //定义变量 ...
- nginx 添加用户认证
nginx 添加用户认证 nginx 配置文件添加: 配置代理添加用户认证:server { listen ; server_name localhost; location ...
- Configure JSON.NET to ignore DataContract/DataMember attributes
https://stackoverflow.com/questions/11055225/configure-json-net-to-ignore-datacontract-datamember-at ...
- ES6 数组方法 forEach map filter find every some reduce
1. forEach const colors = ['red', 'blue', 'green'] colors.forEach(function (params) { console.log(pa ...
- Detection of Glacier Calving Margins with Convolutional Neural Networks: A Case Study
利用Unet结构对landsat数据进行冰川裂缝提取,结构如下:训练集很小只有123张152*240图片
- 【POJ2488】A Knight's Journey
题目传送门 本题知识点:深度优先搜索 + 回溯 + 剪枝 + 字典序 题意是给你一个由 p,q 组成一个矩形的棋盘,让你用马棋在这上面走,是否能一线走完这 p * q 个格子. 关于这条路线是怎么走的 ...
- [Beta]第四次 Scrum Meeting
[Beta]第四次 Scrum Meeting 写在前面 会议时间 会议时长 会议地点 2019/5/12 22:00 30min 大运村公寓6F楼道 附Github仓库:WEDO 例会照片 工作情况 ...