游戏说明:

游戏名: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. learning java 处理流的用法

    有点重定向使用的味道 import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.Pri ...

  2. /etc/rc.local

    /etc/rc.local是/etc/rc.d/rc.local的软连接 应用于指定开机启动的进程 开机启动不生效,则首先需要检查下/etc/rc.d/rc.local是否具有可执行权限 在配置文件中 ...

  3. [总结] MSF攻击数据库服务

    0x01 攻击Mysql服务 1.1 目标探测 auxiliary/scanner/mysql/mysql_version 常用于内网中的批量mysql主机发现: 1.2 爆破登录 auxiliary ...

  4. Server concepts 详解

    server status 是由 vm_state和task_state 计算出来的,vm_state是虚机当前的稳定状态(例如Active, Error),task_state是虚机当前的瞬间状态( ...

  5. Nacos学习

    Nacos是阿里开源的一个新框架,在分布式的架构中,Nacos同时扮演着服务注册中心和配置中心的角色.今天主要讲的是Nacos作为服务注册中心. 分布式中著名的CAP理论,任何一种服务注册中心都只能实 ...

  6. 使用 Spring Cloud Sleuth、Elastic Stack 和 Zipkin 做微服务监控

    关于迁移微服务架构,最常被提及的挑战莫过于监控.每个微服务应独立于其他服务的运行环境,所以他们之间不会共享如数据源.日志文件等资源. 然而,较容易的查看服务的调用历史,并且能够查看多个微服务的请求传播 ...

  7. nmon报告分析

    nmon结果说明及分析 2018年09月29日 16:12:10 Jio_2018 阅读数 2334   使用nmon analyser生成的结果文件包含了N多个sheet页,下面只是结合个人经验对几 ...

  8. Mac下 python2和python3共存

    一般是python2默认安装了,python3没有安装,这时候一般使用命令:brew install python3 进行安装 不同方法安装python的路径是不一样的,如下所示: 接下来就要看具体步 ...

  9. server2008r2 安装CentOS

    一:安装CentOS 二:配置虚拟网络: 三:设置创建的虚拟机使用刚才创建的网卡: 四:运行CentOs,输入用户:root  密码:root,登录后输入: dhclient   自动获取IP ip ...

  10. .Net Core个人笔记

    目录 前言 IOC注册 三种生命周期 如何注册一个IOC服务 .Net Core部署IIS之后500错误 管道和中间件 示意图 管道方法 中间件 加日志观看 使用MVC MVC服务注入 MVC管道调用 ...