Wednesday, May 13, 2009

lab 10 Study Gamble with Crap

Lab 10 Be sure to put each class in its own file. Put the craps game in a loop that continues when you press enter and quits only if you enter ‘q’. After each game display the (1) number of games, (2) number of wins, (3) number of loses, (4) percent wins and (5) percent loses.

// Fig. 7.9: Craps.cs
// Craps class simulates the dice game craps.
using System;

public class Craps
{
// create random number generator for use in method RollDice
private 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
{
SNAKE_EYES = 2,
TREY = 3,
SEVEN = 7,
YO_LEVEN = 11,
BOX_CARS = 12
}

// plays one game of craps
public int Play()
{
// gameStatus can contain CONTINUE, WON or LOST
Status gameStatus = Status.CONTINUE;
int playgame=0;
int myPoint = 0; // point if no win or loss on first roll

int sumOfDice = RollDice(); // first roll of the dice

// determine game status and point based on first roll
switch ( ( DiceNames ) sumOfDice )
{
case DiceNames.SEVEN: // win with 7 on first roll
case DiceNames.YO_LEVEN: // win with 11 on first roll
gameStatus = Status.WON;
break;
case DiceNames.SNAKE_EYES: // lose with 2 on first roll
case DiceNames.TREY: // lose with 3 on first roll
case DiceNames.BOX_CARS: // 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
Console.WriteLine( "Point is {0}", myPoint );
break;
} // end switch

// while game is not complete
while ( gameStatus == Status.CONTINUE ) // game not WON or LOST
{
sumOfDice = RollDice(); // roll dice again

// 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;
} // end while
// display won or lost message
if (gameStatus == Status.WON)
{
Console.WriteLine("Player wins");
playgame = playgame + 1;
}
else
Console.WriteLine("Player loses");
return playgame;
} // end method Play

// 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

// display results of this roll
Console.WriteLine( "Player rolled {0} + {1} = {2}",
die1, die2, sum );
return sum; // return sum of dice
} // end method RollDice

} // end class Craps

// Fig. 7.10: CrapsTest.cs
// Application to test class Craps.
using System;
public class CrapsTest
{
public static void Main( string[] args )
{
Craps game = new Craps();
String inputkey="n";
int totalgame = 0;
int won = 0;
int lost = 0;
decimal percentwin, percentlost;
while (inputkey != "q" && inputkey != "Q")
{
int win=game.Play(); // play one game of craps
totalgame = totalgame + 1;
won = won + win;
Console.WriteLine("\nTotal Game play {0}", totalgame);
Console.WriteLine("Total Winning Game {0}", won);
lost = totalgame - won;
percentwin = (decimal)won / totalgame * 100;
percentlost = (decimal)lost / totalgame * 100;
Console.WriteLine("Total losing game {0}", lost);
Console.WriteLine("Winning Percentage {0:N} %", percentwin);
Console.WriteLine("Losing Percentage {0:N} % ", percentlost);
Console.WriteLine("Press Enter to continue, Press q to quit!");
inputkey = Convert.ToString(Console.ReadLine());

Console.WriteLine("");

} //while loop

} // end Main
} // end class CrapsTest

No comments:

Post a Comment