C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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

程序主界面:

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

C++ 制作一个“测运”小游戏-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=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};  //10 numbers 10个数字
            srand((unsigned)time(NULL));    //设置随机获取位置
            int i=0;
            int j;
            int ran[5]={-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};//10 numbers 10个数
                srand((unsigned)time(NULL));   //设置随机获取位置
                int i=0;
                int j;
                int ran[5]={-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;
}

大佬总结

以上是大佬教程为你收集整理的C++ 制作一个“测运”小游戏-rand()函数的应用全部内容,希望文章能够帮你解决C++ 制作一个“测运”小游戏-rand()函数的应用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。