MIPS

profileVinne
NewMicrosoftWord3.docx

Create a MIPs program to simulate playing multiple games of "Craps" (a popular casino game) to determine an approximate winning percentage.: To simulate throwing two independent dice, you need to get two random numbers in the range of 1 to 6 and add them together. Do not get a single random number in the range of 2 to 12, as each value will not reflect the odds of rolling a particular number

Algorithm to help you with a coding.

ASSUMPTIONS: (a) Two dice that can be randomly “thrown”, each producing a value from 1 to 6. (b) Rules for playing one game of craps:                 First roll (“Come Out”): If combined value of dice is 7 or 11, the player wins and game is over                 If combined value of dice is 2, 3 or 12 the player loses and the game is over               If not a win or loss, the value of the dice becomes the “point” value. Keep rolling the pair of                 dice until their combined value matches the “point” (win; game over) or a 7 (loss, game over)     

INPUT: The number of games to be played to be entered (must be >= 1)

OUTPUT: Average percentage (whole number) that the player wins an individual games of craps

TEST PLAN (Inputs, expected results)

NOTE: Consider trying these values for a single game and assign come-out value to defined values without random numbers to test your program.

1. Test 1                 7 or 11 (player wins)

2. Test 1.2                 2, 3, or 12 (player loses)

3. Test 1.3                4, 5, 6, 8, 9, or 10 (player neither wins or loses – continue rolling)                 Test 1.3a              rolls a 7 (player loses)                 Test 1.3b              roll matches come-out roll (“point” value)

ALGORITHM (Pseudo-code)

1. Prompt and get the number of games to simulate (error if <=0)

2. Set total number of wins to 0.

3. Repeat the following for the number of games to simulate

a. Calculate roll of two dice (store as point-value)

b. IF roll is 7 or 11 THEN                 Increment number of wins; end this game ELSE       IF 4, 5, 6, 8, 9, or 10 THEN                                 Repeat until player wins or loses this game                                                 Calculate new roll of two dice                                                 IF roll = point-value THEN                                                                 increment number of wins; end this game                                                 ELSE IF roll=7 THEN player loses; end this game

4. Calculate and output the percentage of wins (wins / games played)

import java.util.Scanner;

import java.text.DecimalFormat;

import java.util.Random;

public class Craps

{

public static void main(String []args)

{

Scanner keyboard = new Scanner(System.in);

DecimalFormat percent = new DecimalFormat("0.0%");

Random randomGenerator = new Random();

double probWin;

int numberGames, numberWins, dice1, dice2, comeOutRoll, totalRoll;

boolean contRoll;

System.out.println("PROBABIITY OF WINNING AT CRAPS \n");

do

{

System.out.println("Enter number of games to play:");

numberGames = keyboard.nextInt();

if(numberGames < 1)

System.out.println("Must enter a positive integer number \n");

}

while(numberGames < 1);

numberWins = 0;

for (int games = 1; games <= numberGames; games ++)

{

dice1 = randomGenerator.nextInt(6) + 1;

dice2 = randomGenerator.nextInt(6) + 1;

comeOutRoll = dice1 + dice2;

if (comeOutRoll == 7 || comeOutRoll == 11)

numberWins ++;

else if (comeOutRoll > 3 && comeOutRoll < 11)

{

contRoll = true;

do

{

dice1 = randomGenerator.nextInt(6) + 1;

dice2 = randomGenerator.nextInt(6) + 1;

totalRoll = dice1 + dice2;

if (totalRoll == comeOutRoll)

{

numberWins ++;

contRoll = false;

}

else if (totalRoll == 7)

contRoll = false;

}

while ( contRoll );

}

}

probWin = ((double)numberWins) / numberGames;

System.out.println("\nProbability of winning is " + percent.format(probWin));

}

}