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.注册一个小程序账号,得用一个没注册过公众号的邮箱注册 ...
随机推荐
- 查看.NET应用程序中的异常(上)
内存转储是查明托管.NET应用程序中异常的原因的一种极好的方法,特别是在生产应用程序中发生异常时.当您在无法使用Visual Studio的应用程序中跟踪异常时,cdb和sos.dll的使用技术就变成 ...
- vue-cli3整体迁移至服务端渲染nuxtjs
vue项目与nuxt.js实在有着太多的不同,例如项目结构变化很大,router.js没了,vuex store写法有变化,router钩子没了等等.老项目毕竟也有一些体量,这么折腾我可接受不了,不过 ...
- 【POJ2993】Emag eht htiw Em Pleh
题目传送门 本题知识点:模拟(如果对国际象棋不熟悉的同学可以先百度一下) 本题跟POJ2996是逆过来的操作,如果做过[POJ2996]的同学就不会对题意不理解的了. (以下默认您已AC[POJ299 ...
- 拉格朗日插值法(c++)
已给sin0.32=0.314567,sin0.34=0.333487,sin0.36=0.352274,计算sin0.3367的值 #include <iostream> #includ ...
- 15、基于consul+consul-template+registrator+nginx实现自动服务发现
一.架构图 二.组件介绍 1.Registrator Registrator:一个由Go语言编写的,针对docker使用的,通过检查本机容器进程在线或者停止运行状态,去注册服务的工具.所以我们要做的实 ...
- HBase(一) —— 基本概念及使用
一.安装&启动 下载 https://mirrors.tuna.tsinghua.edu.cn/apache/hbase/2.1.8/ 快速开始文档,HBase2.1.8 http://hba ...
- 中标麒麟(龙芯CPU)--忘记root密码怎么修改?
中标麒麟桌面版和服务器版均采用GRUB2为启动器,无法通过单用户模式重置root密码.下面将介绍如何重置中标麒麟系统的root密码: 桌面版 1.修改grub2引导 在正常系统入口上按下"e ...
- SDM439平台出现部分机型SD卡不能识别mmc1: error -110 whilst initialising SD card【学习笔记】
SDM439平台出现部分机型SD卡不能识别mmc1: error -110 whilst initialising SD card 打印了如下的log: - ::>[ after ms - :: ...
- Error:(23, 0) Could not find method implementation() for arguments [directory 'libs'] on object of t
Error:(28, 0) Could not find method implementation() for arguments [com.android.support:appcompat-v7 ...
- 如何获取select下拉框中option选中的文本值
$(select的id或者class).change(function(){ $(this).find("option:selected").text() }) 源文:https: ...