Advertisement
Farjana_akter

Untitled

Jan 24th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))
  5.  
  6.  
  7. string ques, demo, s, ms;
  8. int arr[50];
  9.  
  10. int main(){
  11. FILE *fp;
  12. fp = fopen("game2.txt", "r");
  13.  
  14. int bestscore;
  15. fscanf(fp, "%d", &bestscore);
  16. fclose(fp);
  17. bool flag=1;
  18. while(1){
  19. cout<<"There are 20 boxes Numbered from 1 to 20"<<endl<<endl;
  20. cout<<"|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|\n'-'-'-'-'-'-'-'-'-'--'--'--'--'--'--'--'--'--'--'--'"<<endl<<endl;
  21. cout<<"Some has Chocolates some don't.\nThe game is to tell how many boxes has chocolates."<<endl<<endl;
  22. 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;
  23. cout<<"For Example if you input 1 6 I will output \"YES\" or \"NO\""<<endl<<endl;
  24. cout<<"YES means there is one or more chocolates in the boxes numbered from 1 to 6"<<endl<<endl;
  25. cout<<"NO means there is no chocolate in the the boxes numbered from 1 to 6"<<endl<<endl;
  26. cout<<"Lets get Started?"<<endl<<endl;
  27. cout<<"PRESS ENTER TO START"<<endl;
  28. while(1){
  29. getline(cin,s);
  30. if(s=="")break;
  31. }
  32. srand(rand()-time(0));
  33. memset(arr,0,sizeof arr);
  34. int lim=ran(2,15);
  35. for(int i=0;i<lim;i++){
  36. int ind=ran(0,19);
  37. while(arr[ind]){
  38. ind=ran(0,19);
  39. }
  40. arr[ind]=1;
  41. }
  42. int moves=0;
  43. while(1){
  44. cout<<"1. I want to know if the range has chocolate"<<endl<<"2. I want to answer"<<endl<<endl;
  45. int x,y;
  46. cin>>x;
  47. if(x==1){
  48. moves++;
  49. cout<<"Enter a valid range: ";
  50. cin>>x>>y;
  51. while(x>y||x<1||y>20){
  52. cout<<"Enter a valid range: ";
  53. }
  54. bool flag=false;
  55. for(int i=x;i<=y;i++){
  56. if(arr[i]){
  57. flag=true;
  58. }
  59. }
  60. if(flag) cout<<"YES"<<endl;
  61. else cout<<"NO"<<endl;
  62. }
  63. else if(x==2){
  64. moves++;
  65. cout<<"Enter Your Guess: ";
  66. cin>>x;
  67. if(x==lim){
  68. cout<<"You are Absolutely Correct!!! And you finished it in "<<moves<<"move(s)"<<endl<<endl;
  69. if(bestscore<=moves){
  70. cout<<"But couldn't beat the Best Score :("<<endl<<endl;
  71. }
  72. else{
  73. cout<<"Congrats you won!!!"<<endl<<endl;
  74. bestscore=moves;
  75. }
  76. cout<<"Bestscore: "<<bestscore<<endl<<endl;
  77. fp = fopen("game2.txt", "w");
  78. fprintf(fp, "%d", bestscore);
  79. fclose(fp);
  80. break;
  81. }
  82. else{
  83. cout<<"Sorry :( your answer didn't match"<<endl<<endl;
  84. }
  85. }
  86. }
  87. cout<<"1. Continue to Play"<<endl<<"2. Exit"<<endl;
  88. int x;
  89. cin>>x;
  90. system("CLS");
  91. if(x==1){
  92. continue;
  93. }
  94. else{
  95. break;
  96. }
  97. }
  98. fp = fopen("game2.txt", "w");
  99. fprintf(fp, "%d", bestscore);
  100. fclose(fp);
  101. return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement