0.0

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

 游戏说明:

    游戏名:Lucky Guy

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

    主要用到了rand()函数,具体用法可以参考:百度百科

文件下载:

码云:传送门

程序主界面:

源码如下:

  1 #include <iostream>
  2 #include<stdlib.h>
  3 #include <stdio.h>
  4 # include"time.h"
  5 using namespace std;
  6 
  7 int main()
  8 {
  9 
 10     cout<<"Game:Lucky Guy"<<endl; //Game name游戏名
 11     //system("bash ~/Desktop/lucky/gameName.sh");
 12     cout<<"_(:з」∠)_"<<endl;
 13 
 14     char restart='1'; //restartstart the game's variables 重新开始的变量
 15     while(restart=='1'){
 16         char checkpoint; //Level selection variables 选择模式的变量
 17 
 18         cout<<"To measurestart today's lucky index!"<<endl<<endl;
 19     //system("bash ~/Desktop/lucky/checkpoint.sh");
 20         cout<<"Select the level: 1. Endless mode 2. After one stop"<<endl;  //选择模式
 21 
 22         scanf("%c",&checkpoint);
 23         cout<<endl;
 24 
 25         //Level selection module
 26         if(checkpoint=='2')   //Level 2 关卡2
 27         {
 28 
 29             int map[10]={0,0,0,0,0,0,0,0,0,0};  //10 numbers 10个数字
 30             srand((unsigned)time(NULL));    //设置随机数获取位置
 31             int i=0;
 32             int j;
 33             int ran[5]={-1,-1,-1,-1,-1};    //5 mines 存储5个炸弹
 34             cout<<"Level 2"<<endl;
 35             cout<<"We randomly generated 5 mines and generated mine numbers between 0 and 9"<<endl; //0~9中有5个炸弹
 36             cout<<endl;
 37             cout<<"---------------------"<<endl;
 38             cout<<"|0|1|2|3|4|5|6|7|8|9|"<<endl;
 39             cout<<"---------------------"<<endl;
 40             cout<<"The highest lucky index in the end-stop mode is: 5"<<endl;   //最高幸运值为5
 41 
 42             //To determine if mines are duplicates
 43             for(i = 0; i < 5; i++) // Subscript increments for later processing
 44             {
 45                 //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.
 46                 ran[i] = rand()%10;//Generates a random integer from 0 to 9 of these 10 integers 生成0~9的随机数
 47                 for(j= 0; j < i; ++j)
 48                 {
 49                     if ( ran[j] == ran[i]){//If you repeat 如果重复了
 50                         ran[i]=-1;
 51                         i--;
 52                     }
 53                 }
 54             }
 55             cout<<endl;
 56 
 57             //Output the result in the mine array
 58 
 59             /*for(i=0;i<5;i++){
 60                 cout<<ran[i]<<" ";
 61             }
 62             cout<<endl;
 63             */
 64             for(i=0;i<5;i++){
 65                 map[ran[i]]=1;
 66             }
 67 
 68             //Output options
 69             /*for(i=0;i<10;i++){
 70                 cout<<map[i]<<" ";
 71             }
 72             cout<<endl;
 73         */
 74 
 75 
 76             //Statistics section
 77             int X;
 78             int flag=0; //End the game's game variables 游戏结束的变量
 79             int luck=0; //Returned lucky index  幸运值
 80             while(flag==0){
 81                 cout<<"Please enter the number of your choice (don’t choose the one you selected before):";
 82                 cin>>X;
 83                 if(X>9||X<0){   //Exclude numbers that don’t match rules
 84                     cout<<"Please enter an integer within 0~9."<<endl;
 85                     continue;
 86                 }
 87                 else if(map[X]==-1)
 88                 {
 89                     cout<<"This number has already been used"<<endl;
 90                 }
 91                 else{
 92                     if(map[X]==1){
 93                         cout<<"Stepping on mines, the current lucky index is:"<<luck<<endl;
 94                         flag=1;
 95                     }
 96                     else{
 97                         luck++;
 98                         map[X]=-1;
 99                         if(luck==5)
100                         {
101                             cout<<endl;
102                             cout<<"Wow, the lucky index is: 5, clearance! You are today's lucky!"<<endl;
103                             flag++;
104                         }else{
105                 //system("luckyindex.sh");
106                             cout<<"Good luck, now the lucky index is:"<<luck<<endl;
107                         }
108                     }
109                 }
110             }
111         }else if(checkpoint=='1'){    //first round
112             cout<<"Level 1"<<endl;
113             cout<<"We randomly generated 5 mines and generated mine numbers between 0 and 9"<<endl; //0~9随机设置5个炸弹
114             cout<<endl;
115             cout<<"---------------------"<<endl;
116             cout<<"|0|1|2|3|4|5|6|7|8|9|"<<endl;
117             cout<<"---------------------"<<endl;
118 
119             int flag=0; //Variables to the next level
120             int luck=0; //Lucky index 幸运值
121             while(flag!=1){
122                 int map[10]={0,0,0,0,0,0,0,0,0,0};//10 numbers 10个数
123                 srand((unsigned)time(NULL));   //设置随机数获取位置
124                 int i=0;
125                 int j;
126                 int ran[5]={-1,-1,-1,-1,-1};
127                 for(i = 0; i < 5; i++) // Subscript increments for later processing
128                 {
129                     ran[i] = rand()%10;
130                     for(j= 0; j < i; ++j)
131                     {
132                         if ( ran[j] == ran[i]){//If you repeat
133                             ran[i]=-1;
134                             i--;
135                         }
136                     }
137                 }
138                 cout<<endl;
139                 for(i=0;i<5;i++){
140                     map[ran[i]]=1;
141                 }
142 
143                 //Output array results
144                 /*for(i=0;i<5;i++){
145                     cout<<ran[i]<<" ";
146                 }
147                 cout<<endl;
148         */
149 
150 
151                 //Output options
152                 /*for(i=0;i<10;i++){
153                     cout<<map[i]<<" ";
154                 }
155                 cout<<endl;
156                 */
157 
158                 int X;  //存储输入的数字
159                 cout<<"Please enter the number you choose:";    //请输入你选择的数字
160                 cin>>X;
161                 if(X>9||X<0){   //Exclude numbers that don’t match rules
162                     cout<<"Please enter an integer within 0~9."<<endl;  //输入0~9
163                     continue;
164                 }else{
165                     if(map[X]==1){
166                     cout<<"Stepping on mines, the current lucky index is:"<<luck<<endl; //选中,显示当前幸运值
167                         flag++;
168                     }else{
169                         luck++;
170                         cout<<"Good luck, now the lucky index is:"<<luck<<endl; //结束,返回幸运值
171                     }
172                 }
173             }
174         }else{  //When entering the wrong number of levels, exclude non-conforming inputs
175             cout<<"Please enter the correct number of levels."<<endl;//请输入正确的数字
176             continue;
177         }
178 
179         //游戏结束模块
180         cout<<endl<<endl;
181         cout<<"********************************************************"<<endl;
182         cout<<"* Think you are European Emperor? Then fight it again! *"<<endl;
183         cout<<"********************************************************"<<endl;
184     //system("bash ~/Desktop/lucky/restart.sh");
185         cout<<"Enter 1 to continue the game"<<endl; //输入1继续游戏
186         cout<<"Enter any character other than 1 to exit the game"<<endl;    //输入其他任意字符结束游戏
187         cin>>restart;
188         getchar();
189 
190     }
191 
192     return 0;
193 }

 

posted @ 2019-07-13 12:02  Love丶伊卡洛斯  阅读(446)  评论(0编辑  收藏  举报