Thursday 27 November 2014

Application of Random Class: CRAPS

CRAP is a dice game played in casino and back alleys throughout the world.

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, the some of spots n the two upward face is calculated.
if some is 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 continues 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. }// end of while method
  51. // Display won or loss
  52. if (gameStatus == Status.WON)
  53. Console.WriteLine("Palyer Wins");
  54. else
  55. Console.WriteLine("Player Losses");
  56. }//end of method Play
  57. public int RollDice()
  58. {
  59. int die1 = randomNumber.Next(1, 7);
  60. int die2 = randomNumber.Next(1, 7);
  61. int sum = die1 + die2;
  62. //Display result of this roll
  63. Console.WriteLine("Player rolled {0} + {1} = {2}", die1, die2, sum);
  64. return sum;
  65. }// End method roll dice
  66. }
//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. }

Screen Shots of Output: 


Dice game in C-Sharp.Application of Random Class
Fig 1: Snapshot of output



Dice game in C-Sharp.Application of Random Class
Fig 2: Snapshot of output



Dice game in C-Sharp.Application of Random Class
Fig 3: Snapshot of output


Dice game in C-Sharp.Application of Random Class
Fig 4: Snapshot of output