Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Code: Problem B: Quasar game ( This code must be written in C++, Thanks) Sup

ID: 3802749 • Letter: C

Question


C++ Code:

Problem B: Quasar game (This code must be written in C++, Thanks)

Suppose there is a game with two options: A and B. Each of these options adds a different amount of points from a range of numbers. You start with 0 points and your goal is to reach 19 or 20 points.

For example, suppose you had the following options:

A = number between 4 through 7.

B = number between 1 through 8.

A sample game might go:

Points = 0, choose option A(add 4-7).

Points = 4, choose option B(add 1-8).

Points = 12, choose option B(add 1-8).

Points = 16, choose option B(add 1-8).

Points = 21, you lose.

Make a program that asks the user to give the ranges for both options A and B. You may assume that both options A and B have a minimum of adding at least one point.

To figure out which moves are the best, simulate 10,000,000 games (this should take about 5 seconds to run) with random choices at each turn. Record how often you won/lost the game for each point value and each option picked. If you won the game, each point/option along the whole game should get a +1 and each loss a -1.

Thus if the sample game above was the first game played, you should keep the tally:

0 Points: A=-1, B=0

1Points: A=0, B=0

2 Points: A=0, B=0

3 Points: A=0, B=0

4 Points: A=0, B=-1

5 Points: A=0, B=0

6 Points: A=0, B=0

7 Points: A=0, B=0

8 Points: A=0, B=0

9 Points: A=0, B=0

10 Points: A=0, B=0

11 Points: A=0, B=0

12 Points: A=0, B=-1

13 Points: A=0, B=0

14 Points: A=0, B=0

15 Points: A=0, B=0

16 Points: A=0, B=-1

17 Points: A=0, B=0

18 Points: A=0, B=0

At the end, show which option is best to play for each point value (i.e. whichever tally is higher). (Note: there is also a mathematical way to figure out the best moves in games like this involving expected values of random variables.)

------------------------------------------------------------------------------------------

Example 1:

Enter Option A range:

4 - 7

Enter Option B range:

1 - 8

Points=0, should play B

Points=1, should play A

Points=2, should play A

Points=3, should play A

Points=4, should play B

Points=5, should play B

Points=6, should play B

Points=7, should play A

Points=8, should play A

Points=9, should play A

Points=10,should play B

Points=11, should play B

Points=12, should play B

Points=13, should play A

Points=14, should play A

Points=15, should play A

Points=16, should play B

Points=17, should play B

Points=18, should play B

--------------------------------------

Example 2:

Enter Option A range:

1 - 10

Enter Option B range:

5 - 5

Points=0, should play B

Points=1, should play A

Points=2, should play A

Points=3, should play A

Points=4, should play B

Points=5, should play B

Points=6, should play A

Points=7, should play A

Points=8, should play A

Points=9, should play B

Points=10, should play B

Points=11, should play A

Points=12, should play A

Points=13, should play A

Points=14, should play B

Points=15, should play B

Points=16, should play A

Points=17, should play A

Points=18, should play A

--------------------------------------------------------------------------------------------------------------------------------------------------

Example 3: Note: (in this example the choices points below 10 are random and suggested move for points=16 will not be optimal)

Enter Option A range:

1 - 1

Enter Option B range:

1 - 5

Points=0, should play A

Points=1, should play B

Points=2, should play B

Points=3, should play B

Points=4, should play A

Points=5, should play B

Points=6, should play B

Points=7, should play A

Points=8, should play A

Points=9, should play B

Points=10, should play B

Points=11, should play B

Points=12, should play A

Points=13, should play A

Points=14, should play B

Points=15, should play B

Points=16, should play B

Points=17, should play A

Points=18, should play A

------------------------------------------------------------------

Again code should be in C++

Thanks

Explanation / Answer


// Quasar game C++ code
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <vector>
#include <limits.h>
#include <time.h>
using namespace std;

int main ()
{
   srand(time(NULL));

   int minimumA, maximumA, minimumB, maximumB;
   cout << "Enter range for A: ";
   cin >> minimumA; cin >> maximumA;
cout << "Enter range for B: ";
   cin >> minimumB; cin >> maximumB;

   int pointToACount[19];
   int pointToBCount[19];

   int Arange = maximumA-minimumA+1;
int Brange = maximumB-minimumB+1;

int point = 0;
   
   // Change the number of games during simulation
    // simulate 10,000,000 games

for (int i=0; i<10000; i++)
{
int pointsList[100000] = {-1};
   int optionsList[100000] = {-1};
int idx1 = 0, idx2= 0;

point = 0;
while (point <= 18)
{
int option = rand()%(2);
  
pointsList[idx1] = point;
optionsList[idx2] = option;
idx1++;
idx2++;
  
if (option == 0)
{
point += (rand()%(Arange) + minimumA);
}
else
{
point += (rand()%(Brange) + minimumB);
}
}
  
int sumOffset = -1;
if (point == 19 || point == 20)
{
sumOffset = 1;
}
  
   //cout << idx1 << endl;
for (int k=0; k < idx1; k++)
{
        if( optionsList[k] != -1)
   {
       if (optionsList[k] == 0)
   {
   pointToACount[pointsList[k]] += sumOffset;
   }
   else
   {
   pointToBCount[pointsList[k]] += sumOffset;
   }
   }
}
}

for (int i=0; i<=18; i++)
{
cout << "Point=" << i << ", Should play " << ((pointToACount[i] >= pointToBCount[i] ? "A" : "B")) << endl;
}

   return 0;
}

/*
output:

Enter range for A: 1 7
Enter range for B: 2 8

Point=0, Should play B
Point=1, Should play A
Point=2, Should play A
Point=3, Should play A
Point=4, Should play B
Point=5, Should play B
Point=6, Should play A
Point=7, Should play A
Point=8, Should play A
Point=9, Should play A
Point=10, Should play A
Point=11, Should play B
Point=12, Should play B
Point=13, Should play A
Point=14, Should play A
Point=15, Should play A
Point=16, Should play A
Point=17, Should play A
Point=18, Should play A


*/