Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))
- string ques, demo, s, ms;
- int arr[50];
- int main(){
- FILE *fp;
- fp = fopen("game2.txt", "r");
- int bestscore;
- fscanf(fp, "%d", &bestscore);
- fclose(fp);
- bool flag=1;
- while(1){
- cout<<"There are 20 boxes Numbered from 1 to 20"<<endl<<endl;
- cout<<"|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|\n'-'-'-'-'-'-'-'-'-'--'--'--'--'--'--'--'--'--'--'--'"<<endl<<endl;
- cout<<"Some has Chocolates some don't.\nThe game is to tell how many boxes has chocolates."<<endl<<endl;
- cout<<"In each move you can either ans how many boxes you think have chocolates or you can ask me is if a range has chocolate"<<endl<<endl;
- cout<<"For Example if you input 1 6 I will output \"YES\" or \"NO\""<<endl<<endl;
- cout<<"YES means there is one or more chocolates in the boxes numbered from 1 to 6"<<endl<<endl;
- cout<<"NO means there is no chocolate in the the boxes numbered from 1 to 6"<<endl<<endl;
- cout<<"Lets get Started?"<<endl<<endl;
- cout<<"PRESS ENTER TO START"<<endl;
- while(1){
- getline(cin,s);
- if(s=="")break;
- }
- srand(rand()-time(0));
- memset(arr,0,sizeof arr);
- int lim=ran(2,15);
- for(int i=0;i<lim;i++){
- int ind=ran(0,19);
- while(arr[ind]){
- ind=ran(0,19);
- }
- arr[ind]=1;
- }
- int moves=0;
- while(1){
- cout<<"1. I want to know if the range has chocolate"<<endl<<"2. I want to answer"<<endl<<endl;
- int x,y;
- cin>>x;
- if(x==1){
- moves++;
- cout<<"Enter a valid range: ";
- cin>>x>>y;
- while(x>y||x<1||y>20){
- cout<<"Enter a valid range: ";
- }
- bool flag=false;
- for(int i=x;i<=y;i++){
- if(arr[i]){
- flag=true;
- }
- }
- if(flag) cout<<"YES"<<endl;
- else cout<<"NO"<<endl;
- }
- else if(x==2){
- moves++;
- cout<<"Enter Your Guess: ";
- cin>>x;
- if(x==lim){
- cout<<"You are Absolutely Correct!!! And you finished it in "<<moves<<"move(s)"<<endl<<endl;
- if(bestscore<=moves){
- cout<<"But couldn't beat the Best Score :("<<endl<<endl;
- }
- else{
- cout<<"Congrats you won!!!"<<endl<<endl;
- bestscore=moves;
- }
- cout<<"Bestscore: "<<bestscore<<endl<<endl;
- fp = fopen("game2.txt", "w");
- fprintf(fp, "%d", bestscore);
- fclose(fp);
- break;
- }
- else{
- cout<<"Sorry :( your answer didn't match"<<endl<<endl;
- }
- }
- }
- cout<<"1. Continue to Play"<<endl<<"2. Exit"<<endl;
- int x;
- cin>>x;
- system("CLS");
- if(x==1){
- continue;
- }
- else{
- break;
- }
- }
- fp = fopen("game2.txt", "w");
- fprintf(fp, "%d", bestscore);
- fclose(fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement