游戏说明:

游戏名: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()函数的应用的更多相关文章

  1. 【C语言探索之旅】 第一部分第八课:第一个C语言小游戏

    ​ 内容简介 1.课程大纲 2.第一部分第八课:第一个C语言小游戏 3.第一部分第九课预告: 函数 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写 ...

  2. python小练习:使用循环和函数实现一个摇骰子小游戏。游戏规则如下:游戏开始,首先玩家选择Big or Small(押大小),选择完成后开始摇三个骰子,计算总值,11<=总值<=18为“大”,3<=总值<=10为“小”。然后告诉玩家猜对或者是猜错的结果。

    python小练习:使用循环和函数实现一个摇骰子小游戏.游戏规则如下:游戏开始,首先玩家选择Big or Small(押大小),选择完成后开始摇三个骰子,计算总值,11<=总值<=18为“ ...

  3. 用原生javascript做的一个打地鼠的小游戏

    学习javascript也有一段时间了,一直以来分享的都是一些概念型的知识,今天有空做了一个打地鼠的小游戏,来跟大家分享一下,大家也可以下载来增加一些生活的乐趣,下面P出代码:首先是HTML部分代码: ...

  4. 用 Python 制作一个艺术签名小工具,给自己设计一个优雅的签名

    生活中有很多场景都需要我们签字(签名),如果是一些不重要的场景,我们的签名好坏基本无所谓了,但如果是一些比较重要的场景,如果我们的签名比较差的话,就有可能给别人留下不太好的印象了,俗话说字如其人嘛,本 ...

  5. 小游戏——js+h5[canvas]+cs3制作【五子棋】小游戏

    五子棋小游戏学习—— 有一个问题是,棋盘线的颜色,在canvas中,明明设置了灰色,但在我的预览中还是黑色的,很重的颜色. 以下是复刻的源码: <!DOCTYPE html> <ht ...

  6. html5面向对象做一个贪吃蛇小游戏

    canvas加面向对象方式的贪吃蛇 2016-08-25 这个小游戏可以增加对面向对象的理解,可以加强js逻辑能力,总之认真自己敲一两遍收获还是不少啊!!适合刚学canvas的同学练习!! 废话不多说 ...

  7. 第一个leapmotion的小游戏

    自从看过leapmotion的宣传视频,就被吸引住了.觉得这东西迟早要替代鼠标,然后关注了一年多leapmotion的动态,终于在今年8月份入手了一只.//675大洋啊,心疼~ 一直想写份评测,一直想 ...

  8. python新手如何编写一个猜数字小游戏

    此文章只针对新手,希望大家勿喷,感谢!话不多说先上代码: import random if __name__ == '__main__': yourname = input("你好! 你的名 ...

  9. 微信小程序-从零开始制作一个跑步微信小程序

    来源:伯乐在线 - 王小树 链接:http://ios.jobbole.com/90603/ 点击 → 申请加入伯乐在线专栏作者 一.准备工作 1.注册一个小程序账号,得用一个没注册过公众号的邮箱注册 ...

随机推荐

  1. If...else 条件判断和If else嵌套

    If(条件表达式){ 如果条件表达式结果为true,执行该处代码. 如果条件表达式结果为false,执行下边代码. }else{ 如果条件表达式结果为false,执行该处代码. } If(条件表达式) ...

  2. 捷配制作PCB流程

    https://www.jiepei.com/orderprocess.html 以我的板子为例 查看下自己板子的信息 切换到mm 键盘 Q 压缩PCB文件 付款什么的自己哈 改天我有贴片的订单的时候 ...

  3. CF468C 【Hack it!】

    构造题果然都非常神仙啊 首先翻译有点问题,\(L, R\)的范围应该为\([1, 10^{200}]\) 由于模数a达到了\(10^{18}\),所以我们可以发现,当\(i<10^{18}\)时 ...

  4. shell脚本编程基础知识点

    整数比较: -eq:测试两个整数是否相等:相等为真,不等为假 -ne:测试两个整数是否不等:不等为真,相等为假 -gt:测试一个数是否大于另一个数:大于为真,否则为假 -lt:测试一个数是否小于另一个 ...

  5. Python各种扩展名(py, pyc, pyw, pyo, pyd)区别

    扩展名 在写Python程序时我们常见的扩展名是py, pyc,其实还有其他几种扩展名.下面是几种扩展名的用法. py py就是最基本的源码扩展名 pyw pyw是另一种源码扩展名,跟py唯一的区别是 ...

  6. URL的作用是什么?它由几部分组成?

    URL是统一资源定位符,对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址.互联网上的每个文件都有一个唯一的URL,它包含的信息指出文件的位置以及浏览器应该怎么处理它 ...

  7. eclipse为项目设置jdk

    1)在项目上右键选中properties,会进入如下界面 (2)然后点击Add Library,进入设置Library的界面 (3)选中JRE System Library进入下一界面就可以设置jdk ...

  8. Java串口通信--------基于RXTX (附带资源地址)

    最近帮老师做了一个小项目,一个牧场公司想用传感器收集一些环境信息,记录到数据库里去,然后加以分析查看.这里面和传感器通信用到了串口通信,我也是接触了一下,把用到的东西分享出来. 准备工作: RXTX: ...

  9. $(window).load()方法的使用场景

    一.$(window).load().window.onload=function(){}和$(document).ready()方法的区别 1.$(window).load() 和window.on ...

  10. css3学习之--伪类与圆角

    随着css3.0的发布到逐渐完善,目前已经大部分浏览器已经能较好地适配,所以写一些css3的学习经历,分享心得,主要以案例讲解为主,话不多说,今天以css3的新增的“圆角”属性来讲解,基于web画一个 ...