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

Problem B: Simulating a simple coin-flip game (20 Points) In this problem, you w

ID: 3872490 • Letter: P

Question

Problem B: Simulating a simple coin-flip game (20 Points) In this problem, you will write a program that simulates a simple 2-person coin-flip game, defined here: each player (that is, the user and computer) has after the n rounds of coin-flip. 5. Steps 2 through 5 above (after setting the random seed and allocating 100 dollars apiece to the user and the computer) should be in a do-while comtinuation loop. In other words, the first allocation of 100 dollars happened once when you start the program and should not be reset after each game. L. As with problem A above, in order to control the execution of this "random" program, you need to set up the random number generator so that it can repeat the sequence of numbers it generates-this will enable us you (and us) to test your program. To accomplish this, your program will ask the user to input a number used as a seed for the (of type int) for the predefined random number generator rand, and call the random number seed function srand with that input number as its argument. Specifically, right after the variable declarations in your program you should have the following sequence of statements: Below is a sample run illustrating what the input to and output from your program should be similar to (user input is in bold font) after your program compiles without syntax, semantic, or run-time crrors Enter a seed value for the random number generator: 246 To begin, you and your adversary, the conputer, each have 100 dollars How many rounds would you like to play1000): 50 cout seed; srand (seed) Your wins: 24 Computer Wins: 26 Your total dollars after qames: 102 Computer's total dollars after ganes: 98 Your program should then behave as follows: Enter "y' or ·Y' to repeat this process: y Allocate the user and the computer (the user's adversary in this game) 100 dollars each for the entire match. 1. How many rounds would you like to play {1-1000): 100 Your wins: 48 Computer Wins: 52 Your total dollars after games: 84 Computer's total dollars after ganes: 116 2. Ask the user how many rounds (n) of coin-flip that they would like to play Enter 'y' or 'Y" to repeat this process: y Check to make sure that the user has entered a number n that adheres to the mathematical constraint: 1

Explanation / Answer

#include<iostream>
#include<time.h>
#include<stdlib.h>

using namespace std;

bool FlipCoin(){

    if (rand() % 2 == 0) //head
       return true;
    else //tail
       return false;
}

int main(){

   string inp;
   int usr_amt = 100;
   int cmp_amt = 100;
   int usr_wins = 0;
   int cmp_wins = 0;
   bool flip1;
   bool flip2;
   int n;
   cout << "Enter a seed value of the number of generator :";
   cin >> n;
   if (n < 0){
      cout << " Incorrect value , setting seed as 246 ";
   }
   cout << " To begin, you and your adversary. the computer, each have 100 dollars ";
   srand(n);
   do {
      cout << " How many rounds you would like to play(1-1000) :";
      cin>>n;
      if (n <1 || n >1000){
         cout << " Incorrect value , setting number of rounda as 1 ";
      }
      int count = 0;
      while (count < n && usr_amt > 0 && cmp_amt > 0){
         if (count % 2 != 0){ //Computer's turn
             flip1 = FlipCoin();
             flip2 = FlipCoin();
             if (flip1 && flip2){
                usr_amt = usr_amt + 3;
                cmp_amt = cmp_amt -3;
                usr_wins++;
             }
             else if (!flip1 && !flip2){
                usr_amt = usr_amt + 1;
                cmp_amt = cmp_amt -1;
                usr_wins++;
             }
             else {
                usr_amt = usr_amt - 2;
                cmp_amt = cmp_amt + 2;
                cmp_wins++;
             }

         }
         else { // user's turn
             flip1 = FlipCoin();   //1 is true 0 is false;
             flip2 = FlipCoin();
             if (flip1 && flip2){
                usr_amt = usr_amt + 3;
                cmp_amt = cmp_amt -3;
                usr_wins++;
             }
             else if (!flip1 && !flip2){
                usr_amt = usr_amt + 1;
                cmp_amt = cmp_amt -1;
                usr_wins++;
             }
             else {
                usr_amt = usr_amt - 2;
                cmp_amt = cmp_amt + 2;
                cmp_wins++;
             }
         }
         count++;
      }
      cout << "Your wins : " << usr_wins << "           Your total dollar after the games: " << usr_amt << endl;
      cout << "Computer wins : " << cmp_wins << "       Computer's total dollar after the games: " << cmp_amt << endl;
  
      cout << " Enter 'y' or 'n' to repeat this process :";
      cin >> inp;
      if ( inp[0] == 'n')
         break;  
   } while(1);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote