CSCI 102 - Fall 2023 Fundamentals of Computation

Week 14 Lab

Q&A on Material

Sign in to lab!

HW Check in

Check in on how the homework is going and if anyone has general questions on approaches. Take care to do your own work, but the TA/CPs can help you individually.

Exercises

Walk through this exercises and complete them as a group (if time allows):

cpp/cs102/practice13/stringDebug

Dice Game of ‘Craps’ Debugging

Below is a program to simulate how many rolls an average player’s turn will last in the casino game of craps. It will also determine the expected monetary return on betting 1 dollar on the “pass line” (which is the only part of the game we simulate).

Craps rules

  1. The player rolls 2 dice.
  2. If the sum of the dice is 7 or 11 the player wins their bet and the turn continues (go back to step 1).
  3. If the sum of the dice is 2, 3, or 12 the player loses their bet, but the turn continues (go back to step 1).
  4. If the sum is any other number (besides 2, 3, 7, 11, or 12) then that value is known as the point number and play continues.
  5. The player rolls the dice until…
    1. The sum of the dice is 7 in which case the player loses their bet and the turn ENDS
    2. The sum of the dice is the same as the point value in which case the player wins their bet and the turn continues, starting over at step 1.

Debug the program by copy/pasting the code into (onlineGDB.com)[www.onlinegdb.com], setting breakpoints and examining the operation. When you run it for debugging use a small number of simulations (1-3) so you can step through the code and see if it behaves as expected for the variable values. Only 2 lines are in error.

When you think it works, you can use 100,000-500,000 simulations to see if you get the desired averages (or close to it). The seed number can be any positive integer. If you enter the same seed on multiple program executions, it will cause the same random numbers to be generated. Different seeds should lead to different random numbers on each program run.

// Program to simulate how many rolls an average player's turn 
// will last in the casino game of craps.
// Also determines the expected monetary return on betting 1 dollar
// on the pass line.
//
// Craps rules:
//
//  1. The player rolls 2 dice.
//  2. If the sum of the dice is 7 or 11 the player wins their bet
//     and the turn continues (go back to step 1).
//  3. If the sum of the dice is 2, 3, or 12 the player loses their bet,
//     but the turn continues (go back to step 1).
//  4. If the sum is any other number (besides 2, 3, 7, 11, or 12)
//     then that value is known as the point number and play continues.
//  5. The player rolls the dice until...
//     a. The sum of the dice is 7 in which case the player loses their
//        bet and the turn ENDS
//     b. The sum of the dice is the same as the point value in which case
//        the player wins their bet and the turn continues, starting
//        over at step 1.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
  int numsims, seed;
  int totalrolls = 0, numrolls = 0, numgames=0;
  int dollars = 0;
  

  cout << "Enter the number of simulations and a seed:" << endl;
  cin >> numsims >> seed;
  srand(time(0));

  dollars = 0;
  for(int i = 0; i < numsims; i++){
    // start a player's turn
    bool done = false;
    numrolls = 0;
    while( !done ){
      // First roll
      int d1 = 1+rand()%6;
      int d2 = 1+rand()%6;
      int r1 = d1+d2;
      numrolls++;
      // 7 or 11 wins but play continues
      if(r1 == 7 || r1 == 11){
        dollars++;
      }
      // 2, 3, or 12 loses but play continues
      else if(r1 == 2 || r1 == 3 || r1 == 12){
        dollars--;
      }
      else {
        // a "point" number has been set
        do {
          d1 = 1+rand()%6;
          d2 = 1+rand()%6;
          numrolls++;
        } while( r1 != d1+d2  && r1 != 7);
        // Determine why the game ended
        if(r1 == 7){
          dollars--;
          // if a loss, the turn is over
          done = true;
        }
        else {
          // If a win, play continues
          dollars++;
        }
      } // else
      numgames++;
    } // while
    totalrolls += numrolls;
  } // for

  cout << "Average rolls: " << totalrolls / (double) numsims << endl;
  cout << "\tExpected: approx 8.525" << endl;
  cout << "Average earnings: " << dollars / (double) numgames << endl;
  cout << "\tExpected: approx -.0141" << endl;
  return 0;
}

More Debugging

Complete any exercises in the Vocareum In-Class - Debugging-2 Exercises not performed in class (i.e. primes and sumpairs)

Open-ended Help

Any students with questions may stay after to get help. Anyone else may leave at this stage.