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

!!!!!!! IN C++ ONLY !!!!!!!! Functions We list two functions that are required,

ID: 643594 • Letter: #

Question

!!!!!!! IN C++ ONLY !!!!!!!!

Functions

We list two functions that are required, we provide an idea for third optional function and you can

implement other additional functions that you find useful. You should write function comments for

each of the functions you define, containing an @brief, @param and @return similar to those we

have previously provided for you. R'Sub will test each function individually.

The first function will simulate the dice roll. Once again we want the function to be as general as

possible, so we will parameterize both the number of dice and the number of sides on a dice.

The second function returns the cell with most landings within a closed interval of board spots.

(The closed interval [5,10] means any of the values 5, 6, 7, 8, 9, or 10.)

rollNDice

? simulates rolling N dice, each of which has the specified number of sides

? parameters: two integers by value:

? first integer: the number of dice to roll, by value

? second integer - the number of sides on the dice, by value

? return type: integer, the summation of all N dice rolls

mostLandings

? returns the cell on the board with the most "landings", within an inclusive interval

? parameters: a vector<int>; two integers

? first parameter: the game board vector, by const reference

? second parameter: the start of the interval to be tested, by value

? third parameter: the end of the interval to be tested, by value

? return type: integer, the index of the first cell in the interval with the most landings

Optionally, you may want (in addition to other functions you think of):

printBoard

? procedure, prints out game board, showing number of landings on each cell

? parameters: a vector<int>

? board - the game board vector, by const reference

? return type: void

Please Help

Explanation / Answer

// C++ Monte carlo method

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

const int SCORE_LIMIT = 100;
int diceRoll();
int humanTurn(int& humanTotalScore);
int computerTurn(int& computerTotalScore);

int main()
{
bool win = 1;
int humanTotalScore = 0, computerTotalScore = 0;

cout << "It is your turn. Press r to roll. ";
if ((humanTotalScore < SCORE_LIMIT) && (computerTotalScore < SCORE_LIMIT))
{
win = 1;
}
else
{
win = 0;
}
do
{
cout << "Computers Score: " << computerTotalScore << endl;

humanTurn(humanTotalScore);
computerTurn(computerTotalScore);
}
while(win = 1);

if ((win = 0) && (humanTotalScore > computerTotalScore))
{
cout << "You win!";
}
else
{
cout << "You lost!";
}


return 0;
}


int humanTurn(int& humanTotalScore)
{
int currentScore = 0;
int lastRoll;
char rollOrHold;

cout << "Your total score is: " << humanTotalScore << "." << endl;
cout << "Press r to roll again, or h to hold." << endl;
cin >> rollOrHold;

while (rollOrHold == 'r')
{
srand (time(NULL));
lastRoll = diceRoll();
if (lastRoll == 1)
{
cout << "You rolled a 1, ending your turn." << endl;
break;
}
else
{
currentScore += lastRoll;
cout << "You roll a " << lastRoll << ". Your score this turn is: " << currentScore << endl;
cout << "Press r to roll again, or h to hold." << endl;
cin >> rollOrHold;
}
}
while (rollOrHold == 'h')
{
humanTotalScore += currentScore;
break;
}

return humanTotalScore;
}

int computerTurn(int& computerTotalScore)
{
int currentScore = 0;
int lastRoll;

cout << "Computer's total score is: " << computerTotalScore << "." << endl;
while ((currentScore <= 20) && (currentScore != 1))
{
lastRoll = diceRoll();
if (lastRoll == 1)
{
cout << "The computer rolled a 1, ending their turn." << endl;
break;
}
else
{
currentScore += lastRoll;
cout << "The computer rolls a " << lastRoll << ". Computer's score this turn is: " << currentScore << endl;
}
}
if(currentScore >= 20)
{
computerTotalScore += currentScore;
cout << "After the computer's turn, they have gained an additional " << lastRoll << " points." << endl;
}

return computerTotalScore;
}

int diceRoll()
{
int roll;

roll = (int)(rand()%6)+1;

return roll;
}


//Java Code for dice

import java.io.*;
public class Dice{

static java.text.DecimalFormat sixDigits =
new java.text.DecimalFormat("0.000000");
  

static int numRolls;

public static void main(String[] args)
throws IOException {
int[] d = new int[13];

long startTime = System.currentTimeMillis();
  
System.out.print("Enter # of rolls ---> ");
int ch;
numRolls = 0;
for (;;) {
ch = System.in.read();
if (!Character.isDigit((char)ch)) break;
numRolls = numRolls*10 + (ch - '0');
}
initArray(d);
System.out.println("Total rolls: " +
numRolls + " ");
for (int i = 0; i < numRolls; i++)
d[roll() + roll()]++;
printArray(d);
fancyPrint(d);
System.out.println("Elapsed time (millis) " +
(System.currentTimeMillis() - startTime));

}

/* initArray: initialize statistic array */
static void initArray(int[] d) {
  
for (int i = 2; i < 13; i++)
d[i] = 0;
}

/* printArray: print each num of rolls */
static void printArray(int[] d) {
  
double[] e = {0, 0,
1.0/36.0, 2.0/36.0, 3.0/36.0, 4.0/36.0,
5.0/36.0, 6.0/36.0, 5.0/36.0, 4.0/36.0,
3.0/36.0, 2.0/36.0, 1.0/36.0};
System.out.println("Sum Times Freq (%)" +
" Exact (%) Diff ");
for (int i = 2; i < 13; i++)
System.out.println(
i + " " +
d[i] + " " +
sixDigits.format((double)d[i] /
numRolls*100.0) + " " +
sixDigits.format(e[i]*100.0) + " " +
sixDigits.format(((double)d[i] -
e[i]*numRolls) / numRolls*100.0));
}

/* printStar: print n stars */
static void printStar(int n) {
while (n > 0) {
System.out.print("*");
n--;
}
}

/* fancyPrint: print bar graph */
static void fancyPrint(int[] d) {

System.out.print(" ");
for (int i = 2; i < 13; i++) {
System.out.print("Sum: ");
if (i < 10) System.out.print(" ");
System.out.print(i + " |");
printStar(300*d[i]/numRolls);
System.out.print(" ");
}
System.out.print(" ");
}

/* roll: simulate rolling a die */
static int roll() {
return (int) (6.0*Math.random()
+ 1.0);
}
}

C program

C Program in monte carlo program

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

void initarray(int d[]);
void printarray(int d[]);
void printstar(int n);
void fancyprint(int d[]);
int roll(void);
int numrolls;

int main(void) {

int d[13]; /* 2-12 hold num of rolls */
int i; /* loop variable */
int starttime = time(NULL); /* clock time */
srand((long)starttime);
printf("Enter # of rolls ---> ");
scanf("%i", &numrolls); /* need & on var */


initarray(d);
printf("Total rolls: %1d ", numrolls);

for (i = 0; i < numrolls; i++)
d[roll() + roll()]++;
printarray(d);
fancyprint(d);
printf("Elapsed time: %ld seconds ",
time(NULL) - starttime);
return 0;
}

/* initarray: initialize statistic array */
void initarray(int d[]) {
int i;
for (i = 2; i < 13; i++)
d[i] = 0;
}

/* printarray: print each num of rolls */
void printarray(int d[]) {
int i;
double e[] = {0, 0,
1.0/36.0, 2.0/36.0, 3.0/36.0, 4.0/36.0,
5.0/36.0, 6.0/36.0, 5.0/36.0, 4.0/36.0,
3.0/36.0, 2.0/36.0, 1.0/36.0};
printf("Sum Times Frequency");
printf(" Exact Diff ");
for (i = 2; i < 13; i++)
printf("%2d %7d %11.7f %10.7f %8.4f ",
i,
d[i],
(double)d[i]/numrolls*100.0,

e[i]*100.0,
((double)(d[i]) -
e[i]*numrolls)/numrolls*100.0);
}

/* printstar: print n stars */
void printstar(int n) {
while (n > 0) {
printf("*");
n--;
}
}

/* fancyprint: print bar graph */
void fancyprint(int d[]) {
int i;
printf(" ");
for (i = 2; i < 13; i++) {
printf("Sum:%3d |", i);

printstar(300*d[i]/numrolls);
  
printf(" ");
}
printf(" ");
}

/* roll: simulate rolling a die */
int roll(void) {
return (int) (6.0*(rand()/(double)RAND_MAX)
+ 1.0);
}