Visual C# 8.18 (Game of Craps )

This Solution is for the “Game of Craps” of chapter 8 in the textbook; Visual c# How to program 6th edition by Deitel.

The first portion is the Program, the second portion is the needed Class;

//Date: 6/1/2017

/*Description:
program runs 1000000 games of craps and answers; how many games are won and lost on the: first roll thru the twentieth roll
and how many after the twentieth roll, what the chances of winning at craps are,
and what the average length of a game of craps is.
*/
using System;
class Program
{
static void Main(string[] args)
{
Craps run = new Craps();
run.amillioncraps();//calls method amillioncraps
run.answers_abcd();//calls method answers_abcd
}
}

The following is the Class “Craps”;

using System;

//this first portion is a modified version fig 7.8 af chapter 7 in the textbook; Visual c# How to program 6th edition by Deitel.

public class Craps
{
// create random-number generator for use in method RollDice
private static Random randomNumbers = new Random();

// enumeration with constants that represent the game status
private enum Status { Continue, Won, Lost }

// enumeration with constants that represent common rolls of the dice
private enum DiceNames
{
SnakeEyes = 2,
Trey = 3,
Seven = 7,
YoLeven = 11,
BoxCars = 12
}

int[] winarray;
int[] lossarray;
const int arraylength = 22;

int totalwins;
int totalloss;
int gamecounter;

 

// plays one million games of craps
public void amillioncraps()
{
winarray = new int[arraylength];
lossarray = new int[arraylength];

// gameStatus can contain Continue, Won or Lost
Status gameStatus = Status.Continue;
int myPoint = 0; // point if no win or loss on first roll

int rollcounter;//initialises variable that determines the index of its respective array

for (int index = 1; index <= 1000000; ++index)
{
int sumOfDice = RollDice(); // first roll of the dice
rollcounter = 1;

// determine game status and point based on first roll
switch ((DiceNames)sumOfDice)
{
case DiceNames.Seven: // win with 7 on first roll
case DiceNames.YoLeven: // win with 11 on first roll
gameStatus = Status.Won;
break;
case DiceNames.SnakeEyes: // lose with 2 on first roll
case DiceNames.Trey: // lose with 3 on first roll
case DiceNames.BoxCars: // lose with 12 on first roll
gameStatus = Status.Lost;
break;
default: // did not win or lose, so remember point
gameStatus = Status.Continue; // game is not over
myPoint = sumOfDice; // remember the point
break;
}

// while game is not complete
while (gameStatus == Status.Continue) // game not Won or Lost
{
sumOfDice = RollDice(); // roll dice again

++rollcounter;//increments for index

// determine game status
if (sumOfDice == myPoint) // win by making point
{
gameStatus = Status.Won;
}
else
{
// lose by rolling 7 before point
if (sumOfDice == (int)DiceNames.Seven)
{
gameStatus = Status.Lost;
}
}
}

//all rolls over 20 are stored as index 21 in its respective array (winarry or lossarray)
if (rollcounter > 21)
{
rollcounter = 21;
}

// if game is won, increments element in respective index
if (gameStatus == Status.Won)
{
++winarray[rollcounter];
++totalwins;//tracks totalwins
++gamecounter;//tracks total number of games played
}

//if game is lost, increments element in respective index
else
{
++lossarray[rollcounter];
++totalloss;//tracks total losses
++gamecounter;//tracks total number of games played
}

 

}
}

 

 

// roll dice, calculate sum and display results
public int RollDice()
{
// pick random die values
int die1 = randomNumbers.Next(1, 7); // first die roll
int die2 = randomNumbers.Next(1, 7); // second die roll

int sum = die1 + die2; // sum of die values

return sum; // return sum of dice
}

 

 

//method that calculates and prints the answers to questions a) thru d)
public void answers_abcd()
{
int lengthOgame = 0;//initialises variable for length of game

//prints how many rolls are won/lost on the first, second, … twentieth roll and rolls after 20
for (int index = 1; index <= 21; ++index)
{
//calculates the length of each game
lengthOgame += ( index * winarray[index] + index * lossarray[index]);

if (index == 21)
{
Console.WriteLine($” {winarray[index]} games resulted in a WIN! after the 20th roll.”);
}
else
{
Console.WriteLine($” {winarray[index]} games resulted in a WIN! after {index} roll(s).”);
}

if (index == 21)
{
Console.WriteLine($” While {lossarray[index]} games resulted in a LOSS after the 20th roll.\n”);
}
else
{
Console.WriteLine($” While {lossarray[index]} games resulted in a LOSS after {index} roll(s).\n”);
}
}

Console.WriteLine($”\n The chances of winning at craps is {totalwins} out of a million.\n”);//prints chances of winning at craps out of a million

Console.WriteLine($” The average Length of a game of craps lasts { (lengthOgame / gamecounter) } rolls.\n”);//calculates the average Length of a game of craps
}
}

Program Output:

222106 games resulted in a WIN! after 1 roll(s).
While 111265 games resulted in a LOSS after 1 roll(s).

76761 games resulted in a WIN! after 2 roll(s).
While 111369 games resulted in a LOSS after 2 roll(s).

54950 games resulted in a WIN! after 3 roll(s).
While 79962 games resulted in a LOSS after 3 roll(s).

39277 games resulted in a WIN! after 4 roll(s).
While 57720 games resulted in a LOSS after 4 roll(s).

28429 games resulted in a WIN! after 5 roll(s).
While 40922 games resulted in a LOSS after 5 roll(s).

19847 games resulted in a WIN! after 6 roll(s).
While 29532 games resulted in a LOSS after 6 roll(s).

14387 games resulted in a WIN! after 7 roll(s).
While 21315 games resulted in a LOSS after 7 roll(s).

10267 games resulted in a WIN! after 8 roll(s).
While 15287 games resulted in a LOSS after 8 roll(s).

7383 games resulted in a WIN! after 9 roll(s).
While 11145 games resulted in a LOSS after 9 roll(s).

5282 games resulted in a WIN! after 10 roll(s).
While 7885 games resulted in a LOSS after 10 roll(s).

3764 games resulted in a WIN! after 11 roll(s).
While 5986 games resulted in a LOSS after 11 roll(s).

2703 games resulted in a WIN! after 12 roll(s).
While 4240 games resulted in a LOSS after 12 roll(s).

1890 games resulted in a WIN! after 13 roll(s).
While 3015 games resulted in a LOSS after 13 roll(s).

1469 games resulted in a WIN! after 14 roll(s).
While 2174 games resulted in a LOSS after 14 roll(s).

1101 games resulted in a WIN! after 15 roll(s).
While 1597 games resulted in a LOSS after 15 roll(s).

774 games resulted in a WIN! after 16 roll(s).
While 1191 games resulted in a LOSS after 16 roll(s).

522 games resulted in a WIN! after 17 roll(s).
While 818 games resulted in a LOSS after 17 roll(s).

384 games resulted in a WIN! after 18 roll(s).
While 617 games resulted in a LOSS after 18 roll(s).

280 games resulted in a WIN! after 19 roll(s).
While 416 games resulted in a LOSS after 19 roll(s).

199 games resulted in a WIN! after 20 roll(s).
While 311 games resulted in a LOSS after 20 roll(s).

557 games resulted in a WIN! after the 20th roll.
While 901 games resulted in a LOSS after the 20th roll.

 

The chances of winning at craps is 492332 out of a million.

The average Length of a game of craps lasts 3 rolls.

Press any key to continue . . .

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s