Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 21.07 KB | None | 0 0
  1. import static org.junit.Assert.*;
  2. import org.junit.*;
  3. import java.util.Random;
  4. /*******************************************
  5.  * The test class for PokerDice - phases 1 - 3
  6.  *
  7.  * @author - Ana Posada
  8.  * @version - February 2020
  9.  ******************************************/
  10. public class MyPokerDiceJUnit{
  11.     /** object of the PokerDice class */
  12.     private PokerDice game;
  13.  
  14.     /** Constants  */
  15.     private static final int NUM_ROLLS = 3;
  16.     private static final int NUM_ROUNDS = 7;
  17.     private static final int NUM_DICE = 5;
  18.  
  19.     private static final int FIVE_OF_A_KIND = 50;
  20.     private static final int FOUR_OF_A_KIND = 40;  
  21.     private static final int THREE_OF_A_KIND = 25;      
  22.     private static final int FULL_HOUSE = 35;
  23.     private static final int SMALL_STRAIGHT = 30;      
  24.     private static final int LARGE_STRAIGHT = 45;      
  25.  
  26.     /** the game dice */
  27.     private GVdie [] dice;
  28.  
  29.     /******************************************************
  30.      * Instantiate - PokerGame
  31.      * Called before every test case method.
  32.      *****************************************************/
  33.     @Before
  34.     public void setUp()
  35.     {
  36.         game = new PokerDice();  
  37.         dice = game.getDice();
  38.     }
  39.  
  40.     /******************************************************
  41.      * Test initial values of the constructor
  42.      *****************************************************/
  43.     @Test
  44.     public void testConstructor()
  45.     {
  46.         assertEquals("Constructor: rounds should be zero",
  47.             0, game.getNumRounds());  
  48.  
  49.         assertEquals("Constructor: rolls should be zero",
  50.             0, game.getNumRolls());
  51.  
  52.         assertEquals("Constructor: score should be zero",
  53.             0, game.getScore());
  54.  
  55.         assertEquals("Constructor: dice array should have 5 GVdie",
  56.             5, game.getDice().length);
  57.  
  58.         for (GVdie d : dice) {
  59.             assertFalse("resetGame(): the dice should not be held", d.isHeld());
  60.         }
  61.     }
  62.  
  63.     /******************************************************
  64.      * Test resetGame
  65.      *****************************************************/
  66.     @Test
  67.     public void testResetGame()
  68.     {
  69.         game.resetGame();
  70.         assertEquals("Instance variables should be reset to zero in resetGame()",
  71.             0, game.getScore());
  72.         assertTrue ("resetGame(): did you reset number of rolls?", game.okToRoll());
  73.         assertFalse ("resetGame(): did you reset number of rounds?", game.gameOver());
  74.  
  75.         for (GVdie d : dice) {
  76.             assertFalse("resetGame(): the dice should not be held", d.isHeld());
  77.         }
  78.     }
  79.  
  80.     /******************************************************
  81.      * Test OkToRoll or not
  82.      *****************************************************/
  83.     @Test
  84.     public void testOKToRoll()
  85.     {
  86.         game.resetGame();
  87.         for (int i = 0; i < NUM_ROLLS; i++)  {
  88.             assertTrue("okToRoll(): should allow " + NUM_ROLLS +
  89.                 " rolls per round",  game.okToRoll());
  90.             game.rollDice();
  91.         }
  92.         assertFalse("okToRoll(): should not allow more than " + NUM_ROLLS +
  93.             " rolls per round",  game.okToRoll());
  94.     }
  95.  
  96.     /******************************************************
  97.      * Test game over or not over
  98.      *****************************************************/
  99.     @Test
  100.     public void testGameOver()
  101.     {
  102.         game.resetGame();
  103.         for (int i = 0; i < NUM_ROUNDS; i++) {
  104.             assertFalse("gameOver(): should allow " + NUM_ROUNDS +
  105.                 " rounds per game",  game.gameOver());
  106.             game.checkChance();
  107.         }
  108.         assertTrue("gameOver(): the game should be over after " + NUM_ROUNDS +
  109.             " rounds",  game.gameOver());
  110.     }
  111.  
  112.     /******************************************************
  113.      * generate an array of NUM-DICE random numbers
  114.      * between 1 - 6 (inclusive)
  115.      * @return int [] - array of NUM_DICE random numbers
  116.      *****************************************************/
  117.     private int[] generateRandomVals() {
  118.         Random rand = new Random ();
  119.         int [] values = new int [NUM_DICE];
  120.         for (int i = 0; i < values.length; i++)
  121.             values[i] = rand.nextInt(6) + 1;
  122.         return values;
  123.     }
  124.  
  125.     /******************************************************
  126.      * generate an array of NUM-DICE random numbers
  127.      * between 1 - 6 (inclusive)
  128.      * @param int - number of equal values to generate
  129.      * @return int [] - array contains ofAKind values
  130.      *****************************************************/
  131.     private int []  genValuesOfAKind(int ofAKind) {
  132.         int [] values = new int [NUM_DICE];
  133.         Random rand = new Random ();
  134.         int count = 0;
  135.         int num = 0;
  136.         int index = 0;
  137.         for (int i = 0 ; i < NUM_DICE; i++)
  138.             values [i] = 0;
  139.  
  140.         if (ofAKind >= 3 && ofAKind <= 5){              
  141.             // populating array of equal numbers ofAKind at random indexes    
  142.             num = rand.nextInt(6) + 1;
  143.             while (count < ofAKind) {
  144.                 index = rand.nextInt (NUM_DICE);
  145.                 if (values [index] == 0) {    
  146.                     values [index] = num;
  147.                     count++;
  148.                 }
  149.             }
  150.  
  151.             // populating the rest values at random
  152.             for (int i = 0 ; i < NUM_DICE; i++)
  153.                 if (values [i] == 0)
  154.                     values [i] = rand.nextInt(6) + 1;
  155.  
  156.         }
  157.         return values;
  158.  
  159.     }
  160.  
  161.     /******************************************************
  162.      * generate an array of NUM-DICE random numbers
  163.      * between 1 - 6 (inclusive)
  164.      * @return int[] -generates 3 of a kind and a exact pair
  165.      *****************************************************/
  166.     private int []  genValuesFullHouse() {
  167.         int [] values = new int [NUM_DICE];
  168.         Random rand = new Random ();
  169.         int count = 0;
  170.         int num = 0;
  171.         int numPair = 0;
  172.         int index = 0;
  173.         for (int i = 0 ; i < NUM_DICE; i++)
  174.             values [i] = 0;
  175.  
  176.         // populating 3 of a kind
  177.         num = rand.nextInt(6) + 1;
  178.         while (count < 3) {
  179.             index = rand.nextInt (NUM_DICE);
  180.             if (values [index] == 0) {    
  181.                 values [index] = num;
  182.                 count++;
  183.             }
  184.         }
  185.  
  186.         // populating the rest values to an exact number
  187.         numPair = rand.nextInt(6) + 1;
  188.         while (numPair == num)
  189.             numPair = rand.nextInt(6) + 1;
  190.         for (int i = 0 ; i < NUM_DICE; i++)
  191.             if (values [i] == 0)
  192.                 values [i] = numPair;
  193.  
  194.         return values;
  195.     }
  196.  
  197.     /******************************************************
  198.      * generate an array of NUM-DICE random numbers
  199.      * between 1 - 6 (inclusive)
  200.      * @param int - sequence to generate
  201.      * small - 4
  202.      * large - 5
  203.      * @return int [] - small or large straight generated at random
  204.      *****************************************************/
  205.     private int []  genValuesStraight(int sequence) {
  206.         int [] values = new int [NUM_DICE];
  207.         Random rand = new Random ();
  208.         int count = 0;
  209.         int num = 0;
  210.         int index = 0;
  211.         for (int i = 0 ; i < NUM_DICE; i++)
  212.             values [i] = 0;
  213.  
  214.         if (sequence == 4)                  
  215.             num = rand.nextInt(3) + 1;
  216.         else if (sequence == 5)
  217.             num = rand.nextInt(2) + 1;
  218.  
  219.         // populating the sequence at random indexes
  220.         while (count < sequence) {
  221.             index = rand.nextInt (NUM_DICE);
  222.             if (values [index] == 0) {    
  223.                 values [index] = num;
  224.                 num++;
  225.                 count++;
  226.             }
  227.         }
  228.  
  229.         // populating the rest values at random
  230.         for (int i = 0 ; i < NUM_DICE; i++)
  231.             if (values [i] == 0)
  232.                 values [i] = rand.nextInt(6) + 1;
  233.  
  234.         return values;
  235.     }
  236.  
  237.     /******************************************************
  238.      * tests THREE_OF_A_KIND
  239.      *****************************************************/
  240.     @Test
  241.     public void testOkThreeOfAKind()
  242.     {
  243.         int [] values = genValuesOfAKind (3);
  244.         int scoreBefore = game.getScore();
  245.         game.setDice (values);
  246.         game.checkThreeOfAKind ();
  247.         assertEquals (valuesToString (values) + " Three of a kind should increment score by " +
  248.             THREE_OF_A_KIND ,  scoreBefore + THREE_OF_A_KIND , game.getScore());
  249.  
  250.         // four of a kind has a three of a kind
  251.         values = genValuesOfAKind (4);
  252.         scoreBefore = game.getScore();
  253.         game.setDice (values);
  254.         game.checkThreeOfAKind ();
  255.         assertEquals (valuesToString (values) + " Four of a kind has a Three of a kind -  should increment score by " +
  256.             THREE_OF_A_KIND ,  scoreBefore + THREE_OF_A_KIND , game.getScore());
  257.  
  258.         // five of a kind has a three of a kind
  259.         values = genValuesOfAKind (5);
  260.         scoreBefore = game.getScore();
  261.         game.setDice (values);
  262.         game.checkThreeOfAKind ();
  263.         assertEquals (valuesToString (values) + " Five of a kind has a  Three of a kind -should increment score by " +
  264.             THREE_OF_A_KIND ,  scoreBefore + THREE_OF_A_KIND , game.getScore());
  265.  
  266.         // a full house has 3 of a kind
  267.         values = genValuesFullHouse ();
  268.         scoreBefore = game.getScore();
  269.         game.setDice (values);
  270.         game.checkThreeOfAKind ();
  271.         assertEquals (valuesToString (values) + " Full house is also three of a kind " +
  272.             THREE_OF_A_KIND , scoreBefore + THREE_OF_A_KIND, game.getScore());
  273.     }
  274.  
  275.     /******************************************************
  276.      * tests testNotOkThreeOfAKind()
  277.      *****************************************************/
  278.     @Test
  279.     public void testNotOkThreeOfAKind()
  280.     {
  281.         int [] values = genValuesStraight (4);
  282.         int scoreBefore = game.getScore();
  283.         game.setDice (values);
  284.         game.checkThreeOfAKind ();
  285.         assertEquals ( valuesToString (values) + "Not threeOfAKind should not increment score "
  286.         , scoreBefore, game.getScore()  );
  287.  
  288.         values = genValuesStraight (5);
  289.         scoreBefore = game.getScore();
  290.         game.setDice (values);
  291.         game.checkThreeOfAKind ();
  292.         assertEquals ( valuesToString (values) + "Not threeOfAKind should not increment score "
  293.         , scoreBefore, game.getScore() );
  294.     }
  295.  
  296.     /******************************************************
  297.      * tests FOUR_OF_A_KIND
  298.      *****************************************************/
  299.     @Test
  300.     public void testOkFourOfAKind()
  301.     {
  302.         int [] values = genValuesOfAKind (4);
  303.         int scoreBefore = game.getScore();
  304.         game.setDice (values);
  305.         game.checkFourOfAKind ();
  306.         assertEquals (valuesToString (values) + " Four of a kind should increment score by " +
  307.             FOUR_OF_A_KIND , scoreBefore + FOUR_OF_A_KIND, game.getScore() );
  308.  
  309.         // five of a kind has a four of a kind
  310.         values = genValuesOfAKind (5);
  311.         scoreBefore = game.getScore();
  312.         game.setDice (values);
  313.         game.checkFourOfAKind ();
  314.         assertEquals (valuesToString (values) + " Five of a kind has a four of a kind - should increment score by " +
  315.             FOUR_OF_A_KIND ,  scoreBefore + FOUR_OF_A_KIND , game.getScore());
  316.     }
  317.  
  318.     /******************************************************
  319.      * tests testNotOkFourOfAKind()
  320.      *****************************************************/
  321.     @Test
  322.     public void testNotOkFourOfAKind()
  323.     {
  324.         int [] values = genValuesStraight (4);
  325.         int scoreBefore = game.getScore();
  326.         game.setDice (values);
  327.         game.checkFourOfAKind ();
  328.         assertEquals ( valuesToString (values) + "Not FourOfAKind should not increment score "
  329.         , scoreBefore, game.getScore() );
  330.  
  331.         values = genValuesStraight (5);
  332.         scoreBefore = game.getScore();
  333.         game.setDice (values);
  334.         game.checkFourOfAKind ();
  335.         assertEquals ( valuesToString (values) + "Not FourOfAKind should not increment score "
  336.         , scoreBefore, game.getScore() );
  337.  
  338.         values = genValuesFullHouse ();
  339.         scoreBefore = game.getScore();
  340.         game.setDice (values);
  341.         game.checkFourOfAKind ();
  342.         assertEquals ( valuesToString (values) + "Not FourOfAKind should not increment score "
  343.         , scoreBefore, game.getScore() );
  344.     }
  345.  
  346.     /******************************************************
  347.      * tests FIVE_OF_A_KIND
  348.      *****************************************************/
  349.     @Test
  350.     public void testOkFiveOfAKind()
  351.     {
  352.         int [] values = genValuesOfAKind (5);
  353.         int scoreBefore = game.getScore();
  354.         game.setDice (values);
  355.         game.checkFiveOfAKind ();
  356.         assertEquals (valuesToString (values) +" Five of a kind should increment score by " +
  357.             FIVE_OF_A_KIND , scoreBefore + FIVE_OF_A_KIND, game.getScore());
  358.     }
  359.  
  360.     /******************************************************
  361.      * tests not FIVE_OF_A_KIND
  362.      *****************************************************/
  363.     @Test
  364.     public void testNotOkFiveOfAKind()
  365.     {
  366.         int [] values = genValuesStraight(4);
  367.         int scoreBefore = game.getScore();
  368.         game.setDice (values);      
  369.         game.checkFiveOfAKind ();
  370.         assertEquals (valuesToString (values) +" Not Five of a kind should increment score by "
  371.         , scoreBefore, game.getScore()  );
  372.  
  373.         values = genValuesStraight (5);
  374.         scoreBefore = game.getScore();
  375.         game.setDice (values);
  376.         game.checkFiveOfAKind ();
  377.         assertEquals ( valuesToString (values) + "Not five OfAKind should not increment score "
  378.         , scoreBefore, game.getScore() );
  379.        
  380.          values = genValuesFullHouse ();
  381.         scoreBefore = game.getScore();
  382.         game.setDice (values);
  383.         game.checkFiveOfAKind ();
  384.         assertEquals ( valuesToString (values) + "Not Five OfAKind should not increment score "
  385.         , scoreBefore, game.getScore() );
  386.     }
  387.  
  388.     /******************************************************
  389.      * tests FULL_HOUSE
  390.      *****************************************************/
  391.     @Test
  392.     public void testOkFullHouse()
  393.     {
  394.         int [] values = genValuesFullHouse();
  395.         int scoreBefore = game.getScore();
  396.         game.setDice (values);
  397.         game.checkFullHouse ();
  398.         assertEquals   (valuesToString (values)+ " Full house (3 of a kind and a pair) increment score by " +
  399.             FULL_HOUSE , scoreBefore + FULL_HOUSE, game.getScore());
  400.  
  401.         // five of a kind is also a full house
  402.         values = genValuesOfAKind (5);
  403.         scoreBefore = game.getScore();
  404.         game.setDice (values);
  405.         game.checkFullHouse ();
  406.         assertEquals (valuesToString (values)+ " Five of a kind is also a full House increment score by " +
  407.             FULL_HOUSE , scoreBefore + FULL_HOUSE, game.getScore());
  408.     }
  409.  
  410.     /******************************************************
  411.      * tests testNotOK full house()
  412.      *****************************************************/
  413.     @Test
  414.     public void testNotOkFullHouse()
  415.     {
  416.         int [] values = genValuesStraight (4);
  417.         int scoreBefore = game.getScore();
  418.         game.setDice (values);
  419.         game.checkFullHouse ();
  420.         assertEquals ( valuesToString (values) + "Not Full house - should not increment score "
  421.         , scoreBefore, game.getScore());
  422.  
  423.         values = genValuesStraight (5);
  424.         scoreBefore = game.getScore();
  425.         game.setDice (values);
  426.         game.checkFullHouse ();
  427.         assertEquals ( valuesToString (values) + "Not Full house -  should not increment score "
  428.         , scoreBefore, game.getScore() );
  429.     }
  430.  
  431.     /******************************************************
  432.      * tests SMALL_STRAIGHT
  433.      *****************************************************/
  434.     @Test
  435.     public void testOkSmallStraight()
  436.     {
  437.         int [] values = genValuesStraight (4);
  438.         int scoreBefore = game.getScore();
  439.         game.setDice (values);
  440.         game.checkSmallStraight ();
  441.         assertEquals (valuesToString (values) +" Small straight should increment score by " +
  442.             SMALL_STRAIGHT , scoreBefore + SMALL_STRAIGHT, game.getScore());
  443.  
  444.         // large straight is also a small straight
  445.         values = genValuesStraight (5);
  446.         scoreBefore = game.getScore();
  447.         game.setDice (values);
  448.         game.checkSmallStraight ();
  449.         assertEquals (valuesToString (values) +" Large straight is also a Small straight - should increment score by " +
  450.             SMALL_STRAIGHT , scoreBefore + SMALL_STRAIGHT, game.getScore());
  451.     }
  452.  
  453.     /******************************************************
  454.      * tests Not Ok - small straight
  455.      *****************************************************/
  456.     @Test
  457.     public void testNotOkSmallStraight()
  458.     {
  459.         int [] values = genValuesFullHouse();
  460.         int scoreBefore = game.getScore();
  461.         game.setDice (values);
  462.         game.checkSmallStraight ();
  463.         assertEquals   (valuesToString (values)+ " Not a small straight - should not increment score by "
  464.         , scoreBefore, game.getScore());
  465.  
  466.         values = genValuesOfAKind (5);
  467.         scoreBefore = game.getScore();
  468.         game.setDice (values);
  469.         game.checkSmallStraight();
  470.         assertEquals (valuesToString (values)+ " Not a small straight - should not increment score by "
  471.         , scoreBefore, game.getScore());
  472.        
  473.         values = genValuesOfAKind (4);
  474.         scoreBefore = game.getScore();
  475.         game.setDice (values);
  476.         game.checkSmallStraight();
  477.         assertEquals (valuesToString (values)+ " Not a small straight - should not increment score by "
  478.         , scoreBefore, game.getScore());
  479.        
  480.         values = genValuesOfAKind (3);
  481.         scoreBefore = game.getScore();
  482.         game.setDice (values);
  483.         game.checkSmallStraight();
  484.         assertEquals (valuesToString (values)+ " Not a small straight - should not increment score by "
  485.         , scoreBefore, game.getScore());
  486.     }
  487.  
  488.     /******************************************************
  489.      * tests Not Ok - Large straight
  490.      *****************************************************/
  491.     @Test
  492.     public void testNotOkLargeStraight()
  493.     {
  494.         int [] values = genValuesFullHouse();
  495.         int scoreBefore = game.getScore();
  496.         game.setDice (values);
  497.         game.checkLargeStraight ();
  498.         assertEquals   (valuesToString (values)+ " Not a large straight - should not increment score by "
  499.         , scoreBefore, game.getScore());
  500.  
  501.         values = genValuesOfAKind (5);
  502.         scoreBefore = game.getScore();
  503.         game.setDice (values);
  504.         game.checkLargeStraight();
  505.         assertEquals (valuesToString (values)+ " Not a large straight - should not increment score by "
  506.         , scoreBefore, game.getScore());
  507.        
  508.         values = genValuesOfAKind (4);
  509.         scoreBefore = game.getScore();
  510.         game.setDice (values);
  511.         game.checkLargeStraight();
  512.         assertEquals (valuesToString (values)+ " Not a large straight - should not increment score by "
  513.         , scoreBefore, game.getScore());
  514.        
  515.         values = genValuesOfAKind (3);
  516.         scoreBefore = game.getScore();
  517.         game.setDice (values);
  518.         game.checkLargeStraight();
  519.         assertEquals (valuesToString (values)+ " Not a large straight - should not increment score by "
  520.         , scoreBefore, game.getScore());
  521.     }
  522.  
  523.     /******************************************************
  524.      * tests LARGE_STRAIGHT
  525.      *****************************************************/
  526.     @Test
  527.     public void testOkLargeStraight()
  528.     {
  529.         int [] values = genValuesStraight (5);      
  530.         game.setDice (values);
  531.         int scoreBefore = game.getScore();
  532.         game.checkLargeStraight ();
  533.         assertEquals ( valuesToString (values) + "Large straight should increment score by " +
  534.             LARGE_STRAIGHT , scoreBefore + LARGE_STRAIGHT, game.getScore());
  535.     }
  536.  
  537.     /******************************************************
  538.      * tests CHANCE
  539.      *****************************************************/
  540.     @Test
  541.     public void testChance()
  542.     {
  543.         int [] values = generateRandomVals();
  544.         int scoreBefore;
  545.         int sum = 0;
  546.         for (int v : values)
  547.             sum += v;
  548.  
  549.         game.setDice (values);
  550.         scoreBefore = game.getScore();
  551.         game.checkChance ();
  552.         assertEquals (valuesToString (values) + " Chance should increment score by " +
  553.             sum , scoreBefore + sum, game.getScore());
  554.     }
  555.  
  556.     /******************************************************
  557.      * Converts an array to a String
  558.      * @param int [] - array of values
  559.      * @return String -
  560.      *****************************************************/
  561.     private String valuesToString (int [] values){
  562.         String s = "[";
  563.         s += values [0];
  564.         for (int i = 1 ; i <= values.length - 1 ; i++)
  565.             s += "," + values [i];
  566.         s += "]";
  567.         return s;
  568.     }
  569. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement