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

2020-12-13 06:27

阅读:547

标签:ESS   aaaaaa   alt   ||   exit   getch   数字   sel   存储   

 游戏说明:

    游戏名: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 
#include
#include 
# include"time.h"
using namespace std;

int main()
{

    cout"Game:Lucky Guy"//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;
    //system("bash ~/Desktop/lucky/checkpoint.sh");
        cout"Select the level: 1. Endless mode 2. After one stop"//选择模式

        scanf("%c",&checkpoint);
        coutendl;

        //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"//0~9中有5个炸弹
            coutendl;
            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"//最高幸运值为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 j)
                {
                    if ( ran[j] == ran[i]){//If you repeat 如果重复了
                        ran[i]=-1;
                        i--;
                    }
                }
            }
            coutendl;

            //Output the result in the mine array

            /*for(i=0;i*/
            for(i=0;i5;i++){
                map[ran[i]]=1;
            }

            //Output options
            /*for(i=0;i*/


            //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||X0){   //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:"endl;
                        flag=1;
                    }
                    else{
                        luck++;
                        map[X]=-1;
                        if(luck==5)
                        {
                            coutendl;
                            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:"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"//0~9随机设置5个炸弹
            coutendl;
            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 j)
                    {
                        if ( ran[j] == ran[i]){//If you repeat
                            ran[i]=-1;
                            i--;
                        }
                    }
                }
                coutendl;
                for(i=0;i5;i++){
                    map[ran[i]]=1;
                }

                //Output array results
                /*for(i=0;i*/


                //Output options
                /*for(i=0;i*/

                int X;  //存储输入的数字
                cout"Please enter the number you choose:";    //请输入你选择的数字
                cin>>X;
                if(X>9||X0){   //Exclude numbers that don’t match rules
                    cout"Please enter an integer within 0~9."//输入0~9
                    continue;
                }else{
                    if(map[X]==1){
                    cout"Stepping on mines, the current lucky index is:"//选中,显示当前幸运值
                        flag++;
                    }else{
                        luck++;
                        cout"Good luck, now the lucky index is:"//结束,返回幸运值
                    }
                }
            }
        }else{  //When entering the wrong number of levels, exclude non-conforming inputs
            cout"Please enter the correct number of levels."//请输入正确的数字
            continue;
        }

        //游戏结束模块
        coutendl;
        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"//输入1继续游戏
        cout"Enter any character other than 1 to exit the game"//输入其他任意字符结束游戏
        cin>>restart;
        getchar();

    }

    return 0;
}

 

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

标签:ESS   aaaaaa   alt   ||   exit   getch   数字   sel   存储   

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


评论


亲,登录后才可以留言!