C++ 制作一个“测运”小游戏-rand()函数的应用

游戏说明:

游戏名:Lucky Guy

玩法说明:有2种模式可以选择,一种是一直选择数字,直到抽到炸弹为止。另一种是在0~9个数字中进行选择,有5个炸弹,最高分为5,抽到炸弹即游戏结束。游戏结束后,可以选择继续玩或者直接退出。

主要用到了rand()函数,具体用法可以参考:https://baike.baidu.com/item/rand%E5%87%BD%E6%95%B0/5916603

文件下载:

码云:https://gitee.com/ikaros-521/c_project/tree/master/%E2%80%9C%E6%B5%8B%E8%BF%90%E2%80%9D%E5%B0%8F%E6%B8%B8%E6%88%8F%E2%80%94rand()%E7%9A%84%E5%BA%94%E7%94%A8

程序主界面:

?

源码如下:

#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=‘1‘; //restartstart the game‘s variables 重新开始的变量
    while(restart==‘1‘){
        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==‘2‘)   //Level 2 关卡2
        {

            int map[10]={0,0,0,0,0,0,0,0,0,0};  //10 numbers 10个数字
            srand((unsigned)time(NULL));    //设置随机数获取位置
            int i=0;
            int j;
            int ran[5]={-1,-1,-1,-1,-1};    //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 = 0; i < 5; 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()%10;//Generates a random integer from 0 to 9 of these 10 integers 生成0~9的随机数
                for(j= 0; j < i; ++j)
                {
                    if ( ran[j] == ran[i]){//If you repeat 如果重复了
                        ran[i]=-1;
                        i--;
                    }
                }
            }
            cout<<endl;

            //Output the result in the mine array

            /*for(i=0;i<5;i++){
                cout<<ran[i]<<" ";
            }
            cout<<endl;
            */
            for(i=0;i<5;i++){
                map[ran[i]]=1;
            }

            //Output options
            /*for(i=0;i<10;i++){
                cout<<map[i]<<" ";
            }
            cout<<endl;
        */

            //Statistics section
            int X;
            int flag=0; //End the game‘s game variables 游戏结束的变量
            int luck=0; //Returned lucky index  幸运值
            while(flag==0){
                cout<<"Please enter the number of your choice (don’t choose the one you selected before):";
                cin>>X;
                if(X>9||X<0){   //Exclude numbers that don’t match rules
                    cout<<"Please enter an integer within 0~9."<<endl;
                    continue;
                }
                else if(map[X]==-1)
                {
                    cout<<"This number has already been used"<<endl;
                }
                else{
                    if(map[X]==1){
                        cout<<"Stepping on mines, the current lucky index is:"<<luck<<endl;
                        flag=1;
                    }
                    else{
                        luck++;
                        map[X]=-1;
                        if(luck==5)
                        {
                            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==‘1‘){    //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=0; //Variables to the next level
            int luck=0; //Lucky index 幸运值
            while(flag!=1){
                int map[10]={0,0,0,0,0,0,0,0,0,0};//10 numbers 10个数
                srand((unsigned)time(NULL));   //设置随机数获取位置
                int i=0;
                int j;
                int ran[5]={-1,-1,-1,-1,-1};
                for(i = 0; i < 5; i++) // Subscript increments for later processing
                {
                    ran[i] = rand()%10;
                    for(j= 0; j < i; ++j)
                    {
                        if ( ran[j] == ran[i]){//If you repeat
                            ran[i]=-1;
                            i--;
                        }
                    }
                }
                cout<<endl;
                for(i=0;i<5;i++){
                    map[ran[i]]=1;
                }

                //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>9||X<0){   //Exclude numbers that don’t match rules
                    cout<<"Please enter an integer within 0~9."<<endl;  //输入0~9
                    continue;
                }else{
                    if(map[X]==1){
                    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 0;
}

原文地址:https://www.cnblogs.com/ikaros-521/p/11180073.html

时间: 2024-08-30 13:00:54

C++ 制作一个“测运”小游戏-rand()函数的应用的相关文章

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

? 内容简介 1.课程大纲 2.第一部分第八课:第一个C语言小游戏 3.第一部分第九课预告: 函数 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写三个游戏. C语言编程基础知识 什么是编程? 工欲善其事,必先利其器 你的第一个程序 变量的世界 运算那点事 条件表达式 循环语句 实战:第一个C语言小游戏 函数 练习题 习作:完善第一个C语言小游戏 C语言高级技术 模块化编程 进击的指针,C语言王牌 数组 字符串 预处理 创建你自己的变量类型 文

使用cocos制作一个简易的小闹钟

使用cocos制作一个简易的小闹钟 本文转载至学习使用Cocos制作<闹钟> 使用的引擎版本是cocos2.1 具体开发过程指导 (1)Cocos Studio部分 1.打开Cocos工具,新建一个项目: 2.设置好相关的配置,点击完成,从而发布到Cocos Studio中: 3.Cocos Studio IDE介绍: 左上角的是开发常用的游戏元素.UI控件.容器等,可以像VS2013一样拖拽,并在右边设置对应的属性:左下角是资源导入,可以导入所需的图片背景:下面是时间戳,用于设置基于时间戳的

Cocos2d-X开发一个简单的小游戏

学了这么久Cocos2d-X,今天终于可以做出一个简单的小游戏了,游戏非常简单,通过菜单项控制精灵运动 在做游戏前,先学一个新概念 调度器(scheduler): Cocos2d-x调度器为游戏提供定时事件和定时调用服务.所有Node对象都知道如何调度和取消调度事件,使用调度器有几个好处: 每当Node不再可见或已从场景中移除时,调度器会停止. Cocos2d-x暂停时,调度器也会停止.当Cocos2d-x重新开始时,调度器也会自动继续启动. Cocos2d-x封装了一个供各种不同平台使用的调度

需求:有一个猜数字小游戏,请写一个程序实现在测试类中只能使用5次,超过5次提示:游戏试玩结束,请付费。

package cn.idcast4; import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.Reader;import java.io.Writer;import java.util.Properties; /* * 需求:有一个猜数字小游戏,请写一个程序实现在测试类中只能使用5次, *

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

canvas加面向对象方式的贪吃蛇 2016-08-25 这个小游戏可以增加对面向对象的理解,可以加强js逻辑能力,总之认真自己敲一两遍收获还是不少啊!!适合刚学canvas的同学练习!! 废话不多说,直接来讲思路和代码. ----------------------------------------------------------------------------------------------------------------- 开发思路:首先要有蛇吃的食物,就是一个个canv

我的第一个Apple Watch小游戏——猜数字(Swift)

这是一个在AppleWatch上实现的一个小型App,开发语言为Swift.是一个猜数字的游戏,屏幕上会出现不同数字的滚动,并能控制游戏的开始结束,让别人来猜数字.是不是很有意思.还可以多个人来玩这个游戏,比大家谁最后的数字大. 该应用我已经上传至 https://github.com/chenyufeng1991/GuessNumber   . 由于该应用我主要是在Watch上实现的,所以在手机上不会有任何的效果,只会有一个白色的界面而已.实现步骤如下: (1)新建一个iOS中的Apple W

一个小小的俄罗斯方块小游戏

用了八个小时 也挺不容易的 大神不喜勿喷 #include <iostream> #include <string> #include <ctime> #include <cstdlib> #include <windows.h> #include <conio.h> using namespace std; int block00[4][4] = { { 10,0,0,0 },{ 1,1,1,1 },{ 0,0,0,0 },{ 0,

canvas学习作业,模仿做一个祖玛的小游戏

这个游戏的原理我分为11个步骤,依次如下: 1.布局, 2.画曲线(曲线由两个半径不同的圆构成) 3.画曲线起点起始圆和曲线终点终止圆 4.起始的圆动起来, 5.起始的圆沿曲线走起来 6.起始的圆沿曲线走起来,并在曲线初始位置处产生新圆 7.添加图片,这个图片是为了发射子弹 8.让图片跟随鼠标动起来 9.让动起来的图片跟随鼠标的位置发送子弹,并让子弹的颜色变红 10.图片发射的子弹和轨迹上的小圆碰撞检测 11.碰撞检测后让发射的子弹和轨迹上的小圆消失 这就是该程序步骤的的分解. 第一点:布局 <

Python学习之旅:用Python制作一个打字训练小工具

一.写在前面 说道程序员,你会想到什么呢?有人认为程序员象征着高薪,有人认为程序员都是死肥宅,还有人想到的则是996和 ICU. 别人眼中的程序员:飞快的敲击键盘.酷炫的切换屏幕.各种看不懂的字符代码. 然而现实中的程序员呢?对于很多程序员来说,没有百度和 Google 解决不了的问题,也没有 ctrl + c 和 ctrl + v 实现不了的功能. 那么身为一个程序员,要怎么让自己看起来更加“专业”呢?答案就是加快自己的打字速度了,敲的代码可能是错的,但这个13却是必须装的! 然而还是有不少人