Application of Random Class: CRAPS

A Game of Chance CRAP

Rules of the game:

You roll two dice. Each die has a six faces, which contain one, two, three, four, five, six spots, respectively. After the dice have come to rest, then some spots on the two upward faces is calculated.

  • If some are 7 or 11 on the first throw, you win.
  • If some is 2,3 or 12 on the first throw (called CRAPS) you lose (i.e. The house wins).
  • If some is 4,5,6,8,9 or 10 on the first throw, that sum become "your point".

To win you must continue rolling your dice until you "make your point " ( i.e. roll that same value).

You lose by rolling a 7 before making your point.

Source code:

// class crap.cs

  1. using System;  
  2. public class CRAP  
  3. {  
  4.    //Creating a random number generator for use in methd RollDice  
  5.    private Random randomNumber = new Random();  
  6.    private enum Status //ENUM WITH CONSTATNT THAT REPRESENT GAME STATUS  
  7.    {  
  8.       CONTINUE,  
  9.       WON,  
  10.       LOST  
  11.    }  
  12.    private enum DiceNames  
  13.    {  
  14.       SNAKE_EYES = 2,  
  15.       TREY = 3,  
  16.       SEVEN = 7,  
  17.       YO_LEVEN = 11,  
  18.       BOX_CARS = 12  
  19.    }  
  20.    // PLAY ONE GAME OF CRAP  
  21.    public void Play()  
  22.    {  
  23.       Status gameStatus = Status.CONTINUE;  
  24.       int myPoint = 0;  
  25.       int sumOfDice = RollDice();  
  26.       switch ((DiceNames)sumOfDice)  
  27.       {  
  28.          case DiceNames.SEVEN:  
  29.          case DiceNames.YO_LEVEN:  
  30.          gameStatus = Status.WON;  
  31.          break;  
  32.          case DiceNames.BOX_CARS:  
  33.          case DiceNames.SNAKE_EYES:  
  34.          case DiceNames.TREY:  
  35.          gameStatus = Status.LOST;  
  36.          break;  
  37.          default:  
  38.          gameStatus= Status.CONTINUE;  
  39.          myPoint = sumOfDice;  
  40.          Console.WriteLine("Point is {0}", myPoint);  
  41.          break;  
  42.       }  
  43.       while (gameStatus == Status.CONTINUE)  
  44.       {  
  45.          sumOfDice = RollDice(); // roll dice again  
  46.          if (sumOfDice == myPoint)  
  47.          gameStatus = Status.WON;  
  48.          if (sumOfDice == (int)DiceNames.SEVEN)  
  49.          gameStatus = Status.LOST;  
  50.       }
  51.       // end of while method  
  52.       // Display won or loss  
  53.       if (gameStatus == Status.WON)  
  54.       Console.WriteLine("Palyer Wins");  
  55.       else  
  56.       Console.WriteLine("Player Losses");  
  57.    }//end of method Play  
  58.    public int RollDice()  
  59.    {  
  60.       int die1 = randomNumber.Next(1, 7);  
  61.       int die2 = randomNumber.Next(1, 7);  
  62.       int sum = die1 + die2;  
  63.       //Display result of this roll  
  64.       Console.WriteLine("Player rolled {0} + {1} = {2}", die1, die2, sum);  
  65.       return sum;  
  66.    }// End method roll dice  
  67. }  
//class CrapTest.cs
  1. using System;  
  2. class CrapTets  
  3. {  
  4.    public static void Main()  
  5.    {  
  6.       CRAP game = new CRAP();  
  7.       game.Play();  
  8.       Console.ReadLine();  
  9.    }  
  10. }  
Screenshots of output

snapshot of output
                                                                  Fig 1: Snapshot of output

Screen Shots of Output
                                                             Fig 2: Snapshot of output


                                                              Fig 3: Snapshot of output


                                                               Fig 2: Snapshot of output

Ebook Download
View all
Learn
View all