All in English, the likelihood of winning using the binary algorithm may be higher.
#include<iostream> #include<time.h> //Random Number Seed #include<conio.h> //"Getch" Function #include<windows.h> //"System" Function using namespace std; int random(int x,int y){ return rand()%(y-x+1)+x; } //Generate Random Number Function void InitialScreen(){ //Generate Initial Screen Function cout<<" *GUESS THE NUMBER* "<<endl; cout<<" =============================================="<<endl; cout<<" === 1.Inaugurate ==="<<endl; cout<<" === 2.Illustrate ==="<<endl; cout<<" === 3.Alteration ==="<<endl; cout<<" === 4.Disaffiliate ==="<<endl; cout<<" =============================================="<<endl; } void AlterColor(){ //Change Color Function system("cls"); cout<<" *Alteration* "<<endl; cout<<" =============================================="<<endl; cout<<" === 1.Primary ==="<<endl; cout<<" === 2.LakeBlue ==="<<endl; cout<<" === 3.Lavender ==="<<endl; cout<<" === 4.LightRed ==="<<endl; cout<<" === 5.InitialScreen ==="<<endl; cout<<" =============================================="<<endl; char x=getch(); if(x=='1')system("color 07"); if(x=='2')system("color 3A"); if(x=='3')system("color D4"); if(x=='4')system("color C5"); if(x=='5'){ system("cls"); //Clear the screen InitialScreen();} } void LoadingScreen(){ //Load Effect Function Sleep(500); cout<<" ______ ______ _____ _____"<<endl; cout<<" | | | | | | | "<<endl; cout<<" | __ | | |____ |____ |____ "<<endl; cout<<" | | | | | | | |"<<endl; cout<<" |_____| |_____| |_____ _____| _____|"<<endl; Sleep(1000); cout<<endl<<endl<<" LOADING"; for(int i=1;i<=10;i++){ cout<<"----"; Sleep(250);} cout<<"Ready"; Sleep(500); } int main(){ InitialScreen(); //Reference generated random screen function int step,lim,n,guess; char a=getch(); if(a=='4')return 0; if(a=='3')AlterColor(); a=getch(); //Re-read, otherwise the program will end if(a=='2'){ system("cls"); cout<<"The computer will randomly generate a positive integer for you to guess."<<endl; cout<<"Every time you input a number you guessed, it will tell you whether you guessed it big or small."<<endl; cout<<"You need to guess this number within the specified number of steps, otherwise you will lose."<<endl; cout<<"Press 1 to inaugurate the game, and press 3 to exit."<<endl; a=getch(); } if(a=='3')return 0; if(a=='1'){ system("cls"); cout<<" *DIFFICULTY* "<<endl; cout<<" ===================================================="<<endl; cout<<" === 1.EASY(1~100,10steps) ==="<<endl; cout<<" === 2.MID(1~1000,15steps) ==="<<endl; cout<<" === 3.HARD(1~10000,20steps) ==="<<endl; cout<<" === 4.HELL(1~100000,30steps) ==="<<endl; cout<<" ===================================================="<<endl; a=getch(); } srand((unsigned int)time(0)); //Generate different random numbers each time if(a=='1')n=random(1,100),step=0,lim=10; //Generate a random number if(a=='2')n=random(1,1000),step=0,lim=15; if(a=='3')n=random(1,10000),step=0,lim=20; if(a=='4')n=random(1,100000),step=0,lim=30; system("cls"); LoadingScreen(); system("cls"); while(1){ //Determine cout<<"Please guess:"<<endl; cin>>guess; step+=1; //Reduce steps if(guess==n){cout<<"Guess right,it only took "<<step<<" steps!"<<endl;return 0;} if(guess>n)cout<<"Bigger!Only "<<lim-step<<" steps!"<<endl<<endl; if(guess<n)cout<<"Smaller!Only "<<lim-step<<" steps!"<<endl<<endl; if(step>lim){ cout<<"Unfortunately, you didn't guess the correct number, you've failed"<<endl; return 0;} } return 0;}
The difficulty of this small game lies in changing colors, generating different random numbers each time, and making judgments when reading.
猜数小游戏1.0(纯原创)
于 2023-07-13 13:57:26 首次发布