Java Dice Game Code (FREE)


SUBMITTED BY: yppah

DATE: Jan. 24, 2016, 5:21 a.m.

FORMAT: Text only

SIZE: 3.8 kB

HITS: 5078

  1. SAMPLE DICE GAME
  2. import java.util.Random;
  3. /**
  4. * Models a playing die with sides numbered 1 to N.
  5. * All sides have uniform probablity of being rolled.
  6. *
  7. * @author Summer CS 307 class
  8. */
  9. public class Die
  10. { public static final int DEFAULT_SIDES = 6;
  11. private static Random ourRandNumGen = new Random();
  12. private final int iMyNumSides;
  13. private int iMyResult;
  14. /**
  15. * Default constructor.<p>
  16. * pre: none<br>
  17. * post: getNumSides() = DEFAULT_SIDES, getResult() = 1
  18. */
  19. public Die()
  20. { this(DEFAULT_SIDES);
  21. }
  22. /**
  23. * Create a Die with numSides sides<p>
  24. * pre: numSides > 1<br>
  25. * post: getNumSides() = numSides, getResult() = 1<br>
  26. * An exception will be generated if the preconditions are not met
  27. */
  28. public Die(int numSides)
  29. { assert numSides > 1 : "Violation of precondition: numSides = " + numSides + "numSides must be greater than 1";
  30. iMyNumSides = numSides;
  31. iMyResult = 1;
  32. assert getResult() == 1 && getNumSides() == numSides;
  33. }
  34. /**
  35. * Create a Die with numSides and top side and result set to result<p>
  36. * pre: numSides > 1, 1 <= result <= numSides<br>
  37. * post: getNumSides() = numSides, getResult() = 1<br>
  38. * An exception will be generated if the preconditions are not met
  39. */
  40. public Die(int numSides, int result)
  41. { assert numSides > 1 && 1 <= result && result <= numSides : "Violation of precondition";
  42. iMyNumSides = numSides;
  43. iMyResult = result;
  44. }
  45. /**
  46. * roll this Die. Every side has an equal chance of being the new result<p>
  47. * pre: none<br>
  48. * post: 1 <= getResult() <= getNumSides()
  49. * @return the result of the Die after the roll
  50. */
  51. public int roll()
  52. { iMyResult = ourRandNumGen.nextInt(iMyNumSides) + 1;
  53. assert ( 1 <= getResult() ) && ( getResult() <= getNumSides() );
  54. return iMyResult;
  55. }
  56. /**
  57. * return how many sides this Die has<p>
  58. * pre: none<br>
  59. * post: return how many sides this Die has
  60. * @return the number of sides on this Die
  61. */
  62. public int getNumSides()
  63. { return iMyNumSides; }
  64. /**
  65. * get the current result or top number of this Die<p>
  66. * pre: none<br>
  67. * post: return the number on top of this Die
  68. * @return the current result of this Die
  69. */
  70. public int getResult()
  71. { return iMyResult; }
  72. /**
  73. * returns true if this Die and the parameter otherObj are equal<p>
  74. * pre: none<br>
  75. * post: return true if the parameter is a Die object with the same number of sides as this Die and currently has the same result.
  76. * @return true if the the two Dice are equal, false otherwise
  77. */
  78. public boolean equals(Object otherObj)
  79. { boolean result = true;
  80. if(otherObj == null)
  81. result = false;
  82. else if(this == otherObj)
  83. result = true;
  84. else if(this.getClass() != otherObj.getClass())
  85. result = false;
  86. else
  87. { Die otherDie = (Die)otherObj;
  88. result = this.iMyResult == otherDie.iMyResult
  89. && this.iMyNumSides == otherDie.iMyNumSides;
  90. }
  91. return result;
  92. }
  93. /**
  94. * returns a String containing information about this Die<p>
  95. * pre: none<br>
  96. * post: return a String with information about the current state of this Die
  97. * @return: A String with the number of sides and current result of this Die
  98. */
  99. public String toString()
  100. { return "Num sides " + getNumSides() + " result " + getResult();
  101. }
  102. }// end of Die class

comments powered by Disqus