Advertisement
junebug217

Drop Dead - Dice Game

Apr 16th, 2013
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 44.46 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /**
  7.  * @Author: Joshua Farrow
  8.  * @Program: DropDead.java
  9.  * @Date: April 11, 2013
  10.  * @TimeCompleted: 08:00am
  11.  * @Version: 1.01
  12.  */
  13. package dropdead;
  14.  
  15. import java.io.*;
  16. import java.util.*;
  17.  
  18. public class DropDead
  19. {
  20.     public static Scanner input= new Scanner(System.in);
  21.         public static Random number=new Random();
  22.         public static final int GOAL=10; /*Set the desired point goal here.*/
  23.         public static final int TIE=999;
  24.  
  25.  
  26.     public static void main(String [] args) throws IOException
  27.     {
  28.                 int startingPlayer;
  29.                 introduction();
  30.                 int players = numberOfPlayers();
  31.         int[] dice = new int[players];
  32.                 if(players>1)
  33.                 {
  34.                     startingPlayer=determineStartingPlayer(dice,players);
  35.                 }
  36.                 else
  37.                 {
  38.                     startingPlayer=1;
  39.                 }
  40.                
  41.                 playGame(players,startingPlayer);
  42.     }
  43.        
  44.         /**
  45.          * Allows the player to read the instructions
  46.          * of the game, if they so desire, or bypass them altogether.
  47.          * @return
  48.          * @throws IOException
  49.          */
  50.         public static int introduction() throws IOException
  51.         {
  52.             char decision;
  53.             do
  54.             {
  55.                 System.out.println("Would you like to hear the rules? ");
  56.                 System.out.println("Please type Y or N: ");
  57.                 String yesOrNo=input.next();
  58.                 System.out.println();
  59.                 decision=yesOrNo.charAt(0);
  60.             }while(decision!='y'&&decision!='Y'&&decision!='n'&&decision!='N');
  61.            
  62.             if(decision=='y'||decision=='Y')
  63.             {
  64.                 rules();
  65.             }
  66.             return 1;
  67.         }
  68.        
  69.         /**
  70.          * Ask user to input how many players will be playing the game.
  71.          *
  72.          * @return
  73.          */
  74.         public static int numberOfPlayers()
  75.         {
  76.             int players;
  77.             System.out.println("How many people will be playing? ");
  78.             do
  79.             {
  80.                 System.out.println("Please enter a number between 1 & 4: ");
  81.                 players = input.nextInt();
  82.             }while(players<1||players>4); /* Limits amount of players */
  83.            
  84.             return players;
  85.         }
  86.        
  87.        
  88.        
  89.         /**
  90.          * Determines who the starting player will be.
  91.          * Makes a call to beginningRoll(int[],int).
  92.          * @param dice
  93.          * @param players
  94.          * @return
  95.          */
  96.         public static int determineStartingPlayer(int[] dice, int players)
  97.                 throws IOException
  98.         {
  99.             int[] playerRolls=new int[players];
  100.             System.out.println();
  101.             System.out.println("Press any key to roll and see which player "
  102.                     + "begins the game: ");
  103.             System.in.read();               /* waits for a key press. */
  104.             clearScreen(7);
  105.            
  106.             rollDice(dice);
  107.             showRoll(dice,players);
  108.            
  109.             System.arraycopy(dice, 0, playerRolls, 0, playerRolls.length);
  110.             int startingPlayer=beginningRoll(playerRolls,players)+1;
  111.             System.out.println("Player " + startingPlayer + " will be "
  112.                     + "starting.");
  113.             return startingPlayer;
  114.            
  115.         }
  116.        
  117.         /**
  118.          * Changes the current player to the next player in line.
  119.          * @param currentPlayer
  120.          * @param players
  121.          * @return
  122.          */
  123.         public static int setCurrentPlayer(int currentPlayer, int players)
  124.         {
  125.            if((currentPlayer+1)<=players)
  126.             {
  127.                 currentPlayer+=1;
  128.             }
  129.             else if((currentPlayer+1)>players)
  130.             {
  131.                 currentPlayer=1;
  132.             }
  133.             return currentPlayer;
  134.        }
  135.        
  136.         /**
  137.          * Completes dice roll to determine the starting player.
  138.          * Method uses recursive tail call.
  139.          * @param playerRolls
  140.          * @param players
  141.          * @return
  142.          */
  143.         public static int beginningRoll(int playerRolls[],int players)
  144.         {
  145.             int tie=0;
  146.             int highestPlayer=0;
  147.             int highestDice=playerRolls[0];
  148.             for(int i=1;i<playerRolls.length;i++)
  149.             {
  150.                 if(highestDice<playerRolls[i])
  151.                 {
  152.                  playerRolls[highestPlayer]=0;      
  153.                  highestPlayer=i;
  154.                  highestDice=playerRolls[i];
  155.                  tie=0;
  156.                
  157.                 }
  158.                 else if(highestDice>playerRolls[i])
  159.                 {
  160.                     playerRolls[i]=0;
  161.                 }
  162.                 else if(highestDice==playerRolls[i]&&highestPlayer!=i)
  163.                 {
  164.                     tie++;
  165.                 }
  166.             }
  167.             if(tie>0)
  168.                 {
  169.                   playerRolls=tieBreaker(playerRolls,players);
  170.                  
  171.                   //Recursive call to resolve the new playerRolls array
  172.                   highestPlayer=beginningRoll(playerRolls,players);
  173.                  
  174.                 }
  175.            
  176.             return highestPlayer;
  177.            
  178.         }
  179.        
  180.         /**
  181.          * Breaks any ties that might occur when determining the starting player.
  182.          * @param playerRolls
  183.          * @param players
  184.          * @return
  185.          */
  186.         public static int[] tieBreaker(int playerRolls[],int players)
  187.         {
  188.          
  189.           for(int i=0;i<playerRolls.length;i++)
  190.           {
  191.                if(playerRolls[i]!=0)
  192.                {
  193.                   playerRolls[i]=1+number.nextInt(5);
  194.                   System.out.print(" Player " + (i+1));
  195.                   System.out.print(" had to re-roll.");
  196.                 }
  197.                
  198.                
  199.           }
  200.                System.out.println();
  201.                showRoll(playerRolls,players);
  202.        
  203.                
  204.            
  205.           return playerRolls;
  206.         }
  207.        
  208.         /**
  209.          * Entirety of game is ran through this method.
  210.          * @param players
  211.          * @param currentPlayer
  212.          * @throws IOException
  213.          */
  214.         public static void playGame(int players, int currentPlayer)
  215.                 throws IOException
  216.         {
  217.             int winner=0;
  218.             int round=1;
  219.             int liveDice,reRoll,playerCurrentScore,turnCount;
  220.             char decision;
  221.             int[] scoreBoard=new int[players]; //Running total of scores
  222.             System.out.println("Press any key to begin:");
  223.             System.in.read();
  224.             clearScreen(7);
  225.             do
  226.             {
  227.                 System.out.println("Round " + round);
  228.                 clearScreen(3);
  229.                 turnCount=1;
  230.                 do
  231.                 {
  232.                     //Resets all the variables for the next player.
  233.                     int[] dice=new int[5];
  234.                     liveDice=5;
  235.                     reRoll=1;
  236.                     playerCurrentScore=0;
  237.                     System.out.println("Player " + currentPlayer + "'s " +
  238.                         "turn.");
  239.                     System.out.println("Press any key to roll:");
  240.                     System.in.read();
  241.                     do
  242.                     {
  243.                         rollDice(dice);
  244.                         showRoll(dice,dice.length);
  245.                         playerCurrentScore+=calculateScore(dice);
  246.                         dice=determineDeadDice(dice);
  247.                         liveDice=dice.length;
  248.                         System.out.println();
  249.                        
  250.                         System.out.println("Current score: " +
  251.                                 playerCurrentScore);
  252.                         System.out.println("Live Dice Remaining: " + liveDice);
  253.                         System.out.println("Score if you don't re-roll: " +
  254.                                 (playerCurrentScore*liveDice));
  255.                         System.out.println();
  256.                        
  257.                         if(liveDice==0)
  258.                         {
  259.                             break;      /*Makes sure the player isn't
  260.                                                    asked to re-roll.*/
  261.                         }
  262.                        
  263.                         //Ask player if they want to re-roll.
  264.                         do
  265.                         {
  266.                             System.out.println("Would you like to "
  267.                                     + "keep rolling?");
  268.                             System.out.println("Y or N: ");
  269.                             String yesOrNo=input.next();
  270.                             decision=yesOrNo.charAt(0);
  271.                         }while(decision!='y'&&decision!='Y'&&decision!='n'
  272.                                 &&decision!='N');
  273.  
  274.                         if(decision=='y'||decision=='Y')
  275.                         {
  276.                             reRoll=1;
  277.                         }
  278.                         else if(decision=='n'||decision=='N')
  279.                         {
  280.                             reRoll=0;
  281.                         }
  282.  
  283.                     }while(liveDice>=1&&reRoll!=0);
  284.                    
  285.                    
  286.                     clearScreen(10);
  287.                     playerCurrentScore*=liveDice;
  288.                     scoreBoard[currentPlayer-1]+=playerCurrentScore;
  289.                     if(liveDice==0)
  290.                     {
  291.                         showRoll(dice,dice.length);
  292.                         System.out.println("All of your dice are dead.");
  293.                     }
  294.                    
  295.                     System.out.println();
  296.                     System.out.println("Player " + currentPlayer + "'s score: "
  297.                             + scoreBoard[currentPlayer-1]);
  298.                     System.out.println();
  299.                     System.out.println("Press any key to begin next "
  300.                             + "player's turn:");
  301.                     System.in.read();
  302.                     clearScreen(10);
  303.                    
  304.                    
  305.                     currentPlayer=setCurrentPlayer(currentPlayer,players);
  306.                     turnCount++;
  307.                 }while(turnCount<=players);
  308.                
  309.                 //Round ends
  310.                 System.out.println("End of Round " + round);
  311.                 System.out.println();
  312.                 System.out.println("Current scores are as follows: ");
  313.                 scoreBoardPrint(scoreBoard);
  314.                 round++;
  315.             }while(isThereAWinner(scoreBoard)!=true);
  316.            
  317.             int winningPlayer=whoIsWinner(scoreBoard);
  318.             if(winningPlayer!=TIE)
  319.             {
  320.                 System.out.println("The winner of the game is Player "
  321.                         + (winningPlayer+1));
  322.             }
  323.            
  324.         }
  325.        
  326.         /**
  327.          * Generates a new dice roll
  328.          *
  329.          * @param dice Array to dice.
  330.          * @return dice array.
  331.          */
  332.         public static int[] rollDice(int [] dice)
  333.         {
  334.             int i;
  335.             for (i=0; i<dice.length; i++)
  336.                 {
  337.             dice[i]= 1+number.nextInt(5);
  338.                 }
  339.             return dice;
  340.         }
  341.        
  342.         /**
  343.      * Displays rolled dice
  344.      *
  345.      * @param dice  Array to dice.
  346.      * @param n Number of dice to be displayed.
  347.     */
  348.     public static void showRoll(int [] dice, int n)
  349.     {
  350.         int i;
  351.         //System.out.println(n+" dice were rolled ...");
  352.  
  353.         for (i=0; i<n; i++)
  354.                 {
  355.                     System.out.print("  +-----+");
  356.                 }
  357.         System.out.println();
  358.  
  359.         for (i=0; i<n; i++)
  360.                 {
  361.             switch(dice[i])
  362.             {
  363.                 case 4:
  364.                 case 5:
  365.                 case 6: System.out.print("  |*   *|"); break;
  366.                 case 2:
  367.                 case 3: System.out.print("  |*    |"); break;
  368.                 case 1: System.out.print("  |     |"); break;
  369.                                 case 0: System.out.print("  |     |"); break;
  370.             }
  371.                 }
  372.         System.out.println();
  373.  
  374.         for (i=0; i<n; i++)
  375.                 {
  376.             switch(dice[i]) {
  377.                 case 1:
  378.                 case 3:
  379.                 case 5: System.out.print("  |  *  |"); break;
  380.                 case 2:
  381.                 case 4: System.out.print("  |     |"); break;
  382.                 case 6: System.out.print("  |*   *|"); break;
  383.                                 case 0: System.out.print("  |     |"); break;
  384.             }
  385.                 }
  386.         System.out.println();
  387.  
  388.         for (i=0; i<n; i++)
  389.                 {
  390.             switch(dice[i]) {
  391.                 case 4:
  392.                 case 5:
  393.                 case 6: System.out.print("  |*   *|"); break;
  394.                 case 2:
  395.                 case 3: System.out.print("  |    *|"); break;
  396.                 case 1: System.out.print("  |     |"); break;
  397.                                 case 0: System.out.print("  |     |"); break;
  398.             }
  399.                 }
  400.         System.out.println();
  401.  
  402.         for (i=0; i<n; i++)
  403.                 {
  404.             System.out.print("  +-----+");
  405.                 }
  406.         System.out.println();
  407.     }
  408.        
  409.         /**
  410.          * Determines which dice are dead, and then removes them by creating
  411.          * and returning  a new array.
  412.          * @param oldDice
  413.          * @return
  414.          */
  415.         public static int[] determineDeadDice(int[] oldDice)
  416.         {
  417.             int[] newDice=new int[0];
  418.             int count=0;
  419.             for(int i=0;i<oldDice.length;i++)
  420.             {
  421.                 if(oldDice[i]==2||oldDice[i]==5)
  422.                 {
  423.                     oldDice[i]=0;
  424.                     count++;
  425.                 }
  426.             }
  427.            
  428.             if(count==0)
  429.             {
  430.                 for(int i=0;i<oldDice.length;i++)
  431.                 {
  432.                     newDice=new int[oldDice.length];
  433.                 }
  434.             }
  435.             else            //Removes the "dead" dice.
  436.             {
  437.                 newDice=new int[oldDice.length-count];
  438.             }
  439.             return newDice;
  440.         }
  441.        
  442.         /**
  443.          * Calculates the score by sorting through the array and determining
  444.          * which dice is a 2 or a 5.
  445.          * @param dice
  446.          * @return
  447.          */
  448.         public static int calculateScore(int[] dice)
  449.         {
  450.             int count=0;
  451.             int score=0;
  452.             for(int i=0;i<dice.length;i++)
  453.             {
  454.                 if(dice[i]==2||dice[i]==5)
  455.                 {
  456.                     count++;        //There's probably a better way to do this.
  457.                            
  458.                 }
  459.             }
  460.            
  461.             if(count==0)
  462.             {
  463.                 for(int i=0;i<dice.length;i++)
  464.                 {
  465.                     score+=dice[i];
  466.                 }
  467.             }
  468.             else
  469.             {
  470.                 score=0;
  471.             }
  472.             return score;
  473.         }
  474.        
  475.         /**
  476.          * Prints the score board at the end of each round.
  477.          * @param scoreBoard
  478.          * @throws IOException
  479.          */
  480.         public static void scoreBoardPrint(int[] scoreBoard) throws IOException
  481.         {
  482.             for(int i=0;i<scoreBoard.length;i++)
  483.             {
  484.                 System.out.println("Player " + (i+1) + ": " + scoreBoard[i]
  485.                         + "\t");
  486.             }
  487.             System.out.println();
  488.             System.out.println();
  489.             System.out.println("Press any key to continue: ");
  490.             System.in.read();
  491.             clearScreen(10);
  492.         }
  493.        
  494.         /**
  495.          * Checks to see if a winner has been found.
  496.          * This method DOES NOT determine who the winner is.
  497.          * @param scoreBoard
  498.          * @return
  499.          */
  500.         public static boolean isThereAWinner(int[] scoreBoard)
  501.         {
  502.             boolean status=false;
  503.             for(int i=0;i<scoreBoard.length;i++)
  504.             {
  505.                 if(scoreBoard[i]>=GOAL)
  506.                 {
  507.                     status=true;
  508.                 }
  509.             }
  510.            
  511.             return status;
  512.         }
  513.        
  514.         /**
  515.          * Determines which player is the winner.
  516.          *
  517.          * @param scoreBoard
  518.          * @return
  519.          */
  520.         public static int whoIsWinner(int[] scoreBoard)
  521.         {
  522.             int winningPlayer=0;
  523.             int count=0;
  524.            
  525.             /**
  526.              * Sorts through array and determines who has the highest score
  527.              *  and sets it as the value of winningPlayer
  528.              */
  529.             for(int i=0;i<scoreBoard.length;i++)
  530.             {
  531.                 if(scoreBoard[i]>=GOAL)
  532.                 {
  533.                     if(scoreBoard[i]>scoreBoard[winningPlayer]
  534.                             &&(winningPlayer!=i))
  535.                     {
  536.                        winningPlayer=i;
  537.                     }
  538.                 }
  539.             }
  540.            
  541.             /**
  542.              * Sorts back through the array and looks to see if any ties
  543.              * have occurred.This is done second so that the values in the
  544.              * array are compared to winningPlayer
  545.              */
  546.            
  547.             for(int i=0;i<scoreBoard.length;i++)
  548.             {
  549.                 if(scoreBoard[i]==scoreBoard[winningPlayer]&&(winningPlayer!=i))
  550.                 {
  551.                     System.out.print("Player " + (i+1) + ", and ");
  552.                     count=TIE;
  553.                 }
  554.             }
  555.            
  556.             if(count==TIE)
  557.             {
  558.                 System.out.println("Player " + (winningPlayer+1) + " have tied!");
  559.                 winningPlayer=count;
  560.             }
  561.             return winningPlayer;
  562.         }
  563.        
  564.         /**
  565.          * A simple method that clears the screen.
  566.          * @param numberOfSpaces
  567.          */
  568.         public static void clearScreen(int numberOfSpaces)
  569.         {
  570.             for(int i=0;i<numberOfSpaces;i++)
  571.             {
  572.                 System.out.print("\n");
  573.             }
  574.         }
  575.        
  576.         /**
  577.          * Simply lists out all the rules of the game.
  578.          * @throws IOException
  579.          */
  580.         public static void rules() throws IOException
  581.         {
  582.             /**
  583.              * Enter in rules for the game.
  584.              */
  585.             clearScreen(15);
  586.             System.out.println(" \t\t\t\t\t\t ------------ Drop Dead "
  587.                     + "------------");
  588.             System.out.println();
  589.             System.out.print("First, figure out how many people will "
  590.                     + "be playing.");
  591.             System.out.println(" Once that has been decided, you will all "
  592.                     + "roll to see who gets to go first.");
  593.             System.out.print("Play will begin with the person who had "
  594.                     + "the highest roll,");
  595.             System.out.println(" with ties re-rolling until a winner "
  596.                     + "is found.");
  597.             System.out.println();
  598.             System.out.print("Play begins by the first player rolling "
  599.                     + "five dice.");
  600.             System.out.println(" If any 2's or 5's are rolled, the player "
  601.                     + "receives a score of zero.");
  602.             System.out.print("These dice are then removed, as they are "
  603.                     + "considered to be \"dead\".");
  604.             System.out.println(" The remaining \"live\" dice will then "
  605.                     + "be re-rolled.");
  606.             System.out.println();
  607.             System.out.print("If NO 2's or 5's are rolled, then all of the dice "
  608.                     + "are added together.");
  609.             System.out.println(" This is known as the player's \"round score\", "
  610.                     + "and is");
  611.             System.out.print("added to with each roll that contains "
  612.                     + "NO 2's or 5's.");
  613.             System.out.println();
  614.             System.out.println();
  615.             System.out.println("Press any key to continue...");
  616.             System.out.println();
  617.             System.in.read();
  618.             clearScreen(10);
  619.             System.out.println(" \t\t\t\t\t\t ---------- Drop Dead (cont.) "
  620.                     + "----------");
  621.             System.out.println();
  622.             System.out.print("After each roll, the player will be asked if "
  623.                     + "he/she would like to stop.");
  624.             System.out.println(" If the player chooses to stop, ");
  625.             System.out.print("their round score will be multiplied by their "
  626.                     + "remaining live dice.");
  627.             System.out.println("\n");
  628.             System.out.print("It is helpful to note that the player's round score will ALWAYS");
  629.             System.out.println(" be multiplied by their remaining live dice,");
  630.             System.out.println("even when all the dice are dead.");
  631.             System.out.println();
  632.             System.out.print("Ex: If the player has a round score of 100, but rolls until there are");
  633.             System.out.println("no more live dice, then the player's round score will be multiplied");
  634.             System.out.print("by zero, resulting in a round score of zero as well.");
  635.             System.out.println("Play continues in this fashion until all the "
  636.                     + "dice are \"dead\".");
  637.             System.out.println();
  638.             System.out.println("Press any key to continue...");
  639.             System.out.println();
  640.             System.in.read();
  641.             clearScreen(10);
  642.             System.out.println(" \t\t\t\t\t\t ---------- Drop Dead (cont.) "
  643.                     + "----------");
  644.             System.out.println();
  645.             System.out.println("Play continues until all players have had a turn, at which time the round ends.");
  646.             System.out.println("The game ends whenever a player reaches " + GOAL + " points.");
  647.             System.out.println();
  648.             System.out.println("If more than one player has reached " + GOAL + " points, then the player with the highest");
  649.             System.out.println("score wins the game.");
  650.             clearScreen(4);
  651.             System.out.println("Press any key to begin the game...");
  652.             System.out.println();
  653.             System.in.read();
  654.             clearScreen(15);
  655.                    
  656.         }
  657.        
  658. }/*
  659.  * To change this template, choose Tools | Templates
  660.  * and open the template in the editor.
  661.  */
  662.  
  663. /**
  664.  * @Author: Joshua Farrow
  665.  * @Program: DropDead.java
  666.  * @Date: April 10, 2013
  667.  * @TimeCompleted: 10:03pm
  668.  * @Version: 1.01
  669.  */
  670. package dropdead;
  671.  
  672. import java.io.*;
  673. import java.util.*;
  674.  
  675. public class DropDead
  676. {
  677.     public static Scanner input= new Scanner(System.in);
  678.         public static Random number=new Random();
  679.         public static final int GOAL=10; /*Set the desired point goal here.*/
  680.         public static final int TIE=999;
  681.  
  682.  
  683.     public static void main(String [] args) throws IOException
  684.     {
  685.                 int startingPlayer;
  686.                 introduction();
  687.                 int players = numberOfPlayers();
  688.         int[] dice = new int[players];
  689.                 if(players>1)
  690.                 {
  691.                     startingPlayer=determineStartingPlayer(dice,players);
  692.                 }
  693.                 else
  694.                 {
  695.                     startingPlayer=1;
  696.                 }
  697.                
  698.                 playGame(players,startingPlayer);
  699.     }
  700.        
  701.         /**
  702.          * Allows the player to read the instructions
  703.          * of the game, if they so desire, or bypass them altogether.
  704.          * @return
  705.          * @throws IOException
  706.          */
  707.         public static int introduction() throws IOException
  708.         {
  709.             char decision;
  710.             do
  711.             {
  712.                 System.out.println("Would you like to hear the rules? ");
  713.                 System.out.println("Please type Y or N: ");
  714.                 String yesOrNo=input.next();
  715.                 System.out.println();
  716.                 decision=yesOrNo.charAt(0);
  717.             }while(decision!='y'&&decision!='Y'&&decision!='n'&&decision!='N');
  718.            
  719.             if(decision=='y'||decision=='Y')
  720.             {
  721.                 rules();
  722.             }
  723.             return 1;
  724.         }
  725.        
  726.         /**
  727.          * Ask user to input how many players will be playing the game.
  728.          *
  729.          * @return
  730.          */
  731.         public static int numberOfPlayers()
  732.         {
  733.             int players;
  734.             System.out.println("How many people will be playing? ");
  735.             do
  736.             {
  737.                 System.out.println("Please enter a number between 1 & 4: ");
  738.                 players = input.nextInt();
  739.             }while(players<1||players>4); /* Limits amount of players */
  740.            
  741.             return players;
  742.         }
  743.        
  744.        
  745.        
  746.         /**
  747.          * Determines who the starting player will be.
  748.          * Makes a call to beginningRoll(int[],int).
  749.          * @param dice
  750.          * @param players
  751.          * @return
  752.          */
  753.         public static int determineStartingPlayer(int[] dice, int players)
  754.                 throws IOException
  755.         {
  756.             int[] playerRolls=new int[players];
  757.             System.out.println();
  758.             System.out.println("Press any key to roll and see which player "
  759.                     + "begins the game: ");
  760.             System.in.read();               /* waits for a key press. */
  761.             clearScreen(7);
  762.            
  763.             rollDice(dice);
  764.             showRoll(dice,players);
  765.            
  766.             System.arraycopy(dice, 0, playerRolls, 0, playerRolls.length);
  767.             int startingPlayer=beginningRoll(playerRolls,players)+1;
  768.             System.out.println("Player " + startingPlayer + " will be "
  769.                     + "starting.");
  770.             return startingPlayer;
  771.            
  772.         }
  773.        
  774.         /**
  775.          * Changes the current player to the next player in line.
  776.          * @param currentPlayer
  777.          * @param players
  778.          * @return
  779.          */
  780.         public static int setCurrentPlayer(int currentPlayer, int players)
  781.         {
  782.            if((currentPlayer+1)<=players)
  783.             {
  784.                 currentPlayer+=1;
  785.             }
  786.             else if((currentPlayer+1)>players)
  787.             {
  788.                 currentPlayer=1;
  789.             }
  790.             return currentPlayer;
  791.        }
  792.        
  793.         /**
  794.          * Completes dice roll to determine the starting player.
  795.          * Method uses recursive tail call.
  796.          * @param playerRolls
  797.          * @param players
  798.          * @return
  799.          */
  800.         public static int beginningRoll(int playerRolls[],int players)
  801.         {
  802.             int tie=0;
  803.             int highestPlayer=0;
  804.             int highestDice=playerRolls[0];
  805.             for(int i=1;i<playerRolls.length;i++)
  806.             {
  807.                 if(highestDice<playerRolls[i])
  808.                 {
  809.                  playerRolls[highestPlayer]=0;      
  810.                  highestPlayer=i;
  811.                  highestDice=playerRolls[i];
  812.                  tie=0;
  813.                
  814.                 }
  815.                 else if(highestDice>playerRolls[i])
  816.                 {
  817.                     playerRolls[i]=0;
  818.                 }
  819.                 else if(highestDice==playerRolls[i]&&highestPlayer!=i)
  820.                 {
  821.                     tie++;
  822.                 }
  823.             }
  824.             if(tie>0)
  825.                 {
  826.                   playerRolls=tieBreaker(playerRolls,players);
  827.                  
  828.                   //Recursive call to resolve the new playerRolls array
  829.                   highestPlayer=beginningRoll(playerRolls,players);
  830.                  
  831.                 }
  832.            
  833.             return highestPlayer;
  834.            
  835.         }
  836.        
  837.         /**
  838.          * Breaks any ties that might occur when determining the starting player.
  839.          * @param playerRolls
  840.          * @param players
  841.          * @return
  842.          */
  843.         public static int[] tieBreaker(int playerRolls[],int players)
  844.         {
  845.          
  846.           for(int i=0;i<playerRolls.length;i++)
  847.           {
  848.                if(playerRolls[i]!=0)
  849.                {
  850.                   playerRolls[i]=1+number.nextInt(5);
  851.                   System.out.print(" Player " + (i+1));
  852.                   System.out.print(" had to re-roll.");
  853.                 }
  854.                
  855.                
  856.           }
  857.                System.out.println();
  858.                showRoll(playerRolls,players);
  859.        
  860.                
  861.            
  862.           return playerRolls;
  863.         }
  864.        
  865.         /**
  866.          * Entirety of game is ran through this method.
  867.          * @param players
  868.          * @param currentPlayer
  869.          * @throws IOException
  870.          */
  871.         public static void playGame(int players, int currentPlayer)
  872.                 throws IOException
  873.         {
  874.             int winner=0;
  875.             int round=1;
  876.             int liveDice,reRoll,playerCurrentScore,turnCount;
  877.             char decision;
  878.             int[] scoreBoard=new int[players]; //Running total of scores
  879.             System.out.println("Press any key to begin:");
  880.             System.in.read();
  881.             clearScreen(7);
  882.             do
  883.             {
  884.                 System.out.println("Round " + round);
  885.                 clearScreen(3);
  886.                 turnCount=1;
  887.                 do
  888.                 {
  889.                     //Resets all the variables for the next player.
  890.                     int[] dice=new int[5];
  891.                     liveDice=5;
  892.                     reRoll=1;
  893.                     playerCurrentScore=0;
  894.                     System.out.println("Player " + currentPlayer + "'s " +
  895.                         "turn.");
  896.                     System.out.println("Press any key to roll:");
  897.                     System.in.read();
  898.                     do
  899.                     {
  900.                         rollDice(dice);
  901.                         showRoll(dice,dice.length);
  902.                         playerCurrentScore+=calculateScore(dice);
  903.                         dice=determineDeadDice(dice);
  904.                         liveDice=dice.length;
  905.                         System.out.println();
  906.                        
  907.                         System.out.println("Current score: " +
  908.                                 playerCurrentScore);
  909.                         System.out.println("Live Dice Remaining: " + liveDice);
  910.                         System.out.println("Score if you don't re-roll: " +
  911.                                 (playerCurrentScore*liveDice));
  912.                         System.out.println();
  913.                        
  914.                         if(liveDice==0)
  915.                         {
  916.                             break;      /*Makes sure the player isn't
  917.                                                    asked to re-roll.*/
  918.                         }
  919.                        
  920.                         //Ask player if they want to re-roll.
  921.                         do
  922.                         {
  923.                             System.out.println("Would you like to "
  924.                                     + "keep rolling?");
  925.                             System.out.println("Y or N: ");
  926.                             String yesOrNo=input.next();
  927.                             decision=yesOrNo.charAt(0);
  928.                         }while(decision!='y'&&decision!='Y'&&decision!='n'
  929.                                 &&decision!='N');
  930.  
  931.                         if(decision=='y'||decision=='Y')
  932.                         {
  933.                             reRoll=1;
  934.                         }
  935.                         else if(decision=='n'||decision=='N')
  936.                         {
  937.                             reRoll=0;
  938.                         }
  939.  
  940.                     }while(liveDice>=1&&reRoll!=0);
  941.                    
  942.                    
  943.                     clearScreen(10);
  944.                     playerCurrentScore*=liveDice;
  945.                     scoreBoard[currentPlayer-1]+=playerCurrentScore;
  946.                     if(liveDice==0)
  947.                     {
  948.                         showRoll(dice,dice.length);
  949.                         System.out.println("All of your dice are dead.");
  950.                     }
  951.                    
  952.                     System.out.println();
  953.                     System.out.println("Player " + currentPlayer + "'s score: "
  954.                             + scoreBoard[currentPlayer-1]);
  955.                     System.out.println();
  956.                     System.out.println("Press any key to begin next "
  957.                             + "player's turn:");
  958.                     System.in.read();
  959.                     clearScreen(10);
  960.                    
  961.                    
  962.                     currentPlayer=setCurrentPlayer(currentPlayer,players);
  963.                     turnCount++;
  964.                 }while(turnCount<=players);
  965.                
  966.                 //Round ends
  967.                 System.out.println("End of Round " + round);
  968.                 System.out.println();
  969.                 System.out.println("Current scores are as follows: ");
  970.                 scoreBoardPrint(scoreBoard);
  971.                 round++;
  972.                 for(int i=0;i<scoreBoard.length;i++)
  973.                 {
  974.                     scoreBoard[i]=20;
  975.                 }
  976.             }while(isThereAWinner(scoreBoard)!=true);
  977.            
  978.             int winningPlayer=whoIsWinner(scoreBoard);
  979.             if(winningPlayer!=TIE)
  980.             {
  981.                 System.out.println("The winner of the game is Player "
  982.                         + (winningPlayer+1));
  983.             }
  984.            
  985.         }
  986.        
  987.         /**
  988.          * Generates a new dice roll
  989.          *
  990.          * @param dice Array to dice.
  991.          * @return dice array.
  992.          */
  993.         public static int[] rollDice(int [] dice)
  994.         {
  995.             int i;
  996.             for (i=0; i<dice.length; i++)
  997.                 {
  998.             dice[i]= 1+number.nextInt(5);
  999.                 }
  1000.             return dice;
  1001.         }
  1002.        
  1003.         /**
  1004.      * Displays rolled dice
  1005.      *
  1006.      * @param dice  Array to dice.
  1007.      * @param n Number of dice to be displayed.
  1008.     */
  1009.     public static void showRoll(int [] dice, int n)
  1010.     {
  1011.         int i;
  1012.         //System.out.println(n+" dice were rolled ...");
  1013.  
  1014.         for (i=0; i<n; i++)
  1015.                 {
  1016.                     System.out.print("  +-----+");
  1017.                 }
  1018.         System.out.println();
  1019.  
  1020.         for (i=0; i<n; i++)
  1021.                 {
  1022.             switch(dice[i])
  1023.             {
  1024.                 case 4:
  1025.                 case 5:
  1026.                 case 6: System.out.print("  |*   *|"); break;
  1027.                 case 2:
  1028.                 case 3: System.out.print("  |*    |"); break;
  1029.                 case 1: System.out.print("  |     |"); break;
  1030.                                 case 0: System.out.print("  |     |"); break;
  1031.             }
  1032.                 }
  1033.         System.out.println();
  1034.  
  1035.         for (i=0; i<n; i++)
  1036.                 {
  1037.             switch(dice[i]) {
  1038.                 case 1:
  1039.                 case 3:
  1040.                 case 5: System.out.print("  |  *  |"); break;
  1041.                 case 2:
  1042.                 case 4: System.out.print("  |     |"); break;
  1043.                 case 6: System.out.print("  |*   *|"); break;
  1044.                                 case 0: System.out.print("  |     |"); break;
  1045.             }
  1046.                 }
  1047.         System.out.println();
  1048.  
  1049.         for (i=0; i<n; i++)
  1050.                 {
  1051.             switch(dice[i]) {
  1052.                 case 4:
  1053.                 case 5:
  1054.                 case 6: System.out.print("  |*   *|"); break;
  1055.                 case 2:
  1056.                 case 3: System.out.print("  |    *|"); break;
  1057.                 case 1: System.out.print("  |     |"); break;
  1058.                                 case 0: System.out.print("  |     |"); break;
  1059.             }
  1060.                 }
  1061.         System.out.println();
  1062.  
  1063.         for (i=0; i<n; i++)
  1064.                 {
  1065.             System.out.print("  +-----+");
  1066.                 }
  1067.         System.out.println();
  1068.     }
  1069.        
  1070.         /**
  1071.          * Determines which dice are dead, and then removes them by creating
  1072.          * and returning  a new array.
  1073.          * @param oldDice
  1074.          * @return
  1075.          */
  1076.         public static int[] determineDeadDice(int[] oldDice)
  1077.         {
  1078.             int[] newDice=new int[0];
  1079.             int count=0;
  1080.             for(int i=0;i<oldDice.length;i++)
  1081.             {
  1082.                 if(oldDice[i]==2||oldDice[i]==5)
  1083.                 {
  1084.                     oldDice[i]=0;
  1085.                     count++;
  1086.                 }
  1087.             }
  1088.            
  1089.             if(count==0)
  1090.             {
  1091.                 for(int i=0;i<oldDice.length;i++)
  1092.                 {
  1093.                     newDice=new int[oldDice.length];
  1094.                 }
  1095.             }
  1096.             else            //Removes the "dead" dice.
  1097.             {
  1098.                 newDice=new int[oldDice.length-count];
  1099.             }
  1100.             return newDice;
  1101.         }
  1102.        
  1103.         /**
  1104.          * Calculates the score by sorting through the array and determining
  1105.          * which dice is a 2 or a 5.
  1106.          * @param dice
  1107.          * @return
  1108.          */
  1109.         public static int calculateScore(int[] dice)
  1110.         {
  1111.             int count=0;
  1112.             int score=0;
  1113.             for(int i=0;i<dice.length;i++)
  1114.             {
  1115.                 if(dice[i]==2||dice[i]==5)
  1116.                 {
  1117.                     count++;        //There's probably a better way to do this.
  1118.                            
  1119.                 }
  1120.             }
  1121.            
  1122.             if(count==0)
  1123.             {
  1124.                 for(int i=0;i<dice.length;i++)
  1125.                 {
  1126.                     score+=dice[i];
  1127.                 }
  1128.             }
  1129.             else
  1130.             {
  1131.                 score=0;
  1132.             }
  1133.             return score;
  1134.         }
  1135.        
  1136.         /**
  1137.          * Prints the score board at the end of each round.
  1138.          * @param scoreBoard
  1139.          * @throws IOException
  1140.          */
  1141.         public static void scoreBoardPrint(int[] scoreBoard) throws IOException
  1142.         {
  1143.             for(int i=0;i<scoreBoard.length;i++)
  1144.             {
  1145.                 System.out.println("Player " + (i+1) + ": " + scoreBoard[i]
  1146.                         + "\t");
  1147.             }
  1148.             System.out.println();
  1149.             System.out.println();
  1150.             System.out.println("Press any key to continue: ");
  1151.             System.in.read();
  1152.             clearScreen(10);
  1153.         }
  1154.        
  1155.         /**
  1156.          * Checks to see if a winner has been found.
  1157.          * This method DOES NOT determine who the winner is.
  1158.          * @param scoreBoard
  1159.          * @return
  1160.          */
  1161.         public static boolean isThereAWinner(int[] scoreBoard)
  1162.         {
  1163.             boolean status=false;
  1164.             for(int i=0;i<scoreBoard.length;i++)
  1165.             {
  1166.                 if(scoreBoard[i]>=GOAL)
  1167.                 {
  1168.                     status=true;
  1169.                 }
  1170.             }
  1171.            
  1172.             return status;
  1173.         }
  1174.        
  1175.         /**
  1176.          * Determines which player is the winner.
  1177.          *
  1178.          * @param scoreBoard
  1179.          * @return
  1180.          */
  1181.         public static int whoIsWinner(int[] scoreBoard)
  1182.         {
  1183.             int winningPlayer=0;
  1184.             int count=0;
  1185.            
  1186.             /**
  1187.              * Sorts through array and determines who has the highest score
  1188.              *  and sets it as the value of winningPlayer
  1189.              */
  1190.             for(int i=0;i<scoreBoard.length;i++)
  1191.             {
  1192.                 if(scoreBoard[i]>=GOAL)
  1193.                 {
  1194.                     if(scoreBoard[i]>scoreBoard[winningPlayer]
  1195.                             &&(winningPlayer!=i))
  1196.                     {
  1197.                        winningPlayer=i;
  1198.                     }
  1199.                 }
  1200.             }
  1201.            
  1202.             /**
  1203.              * Sorts back through the array and looks to see if any ties
  1204.              * have occurred.This is done second so that the values in the
  1205.              * array are compared to winningPlayer
  1206.              */
  1207.            
  1208.             for(int i=0;i<scoreBoard.length;i++)
  1209.             {
  1210.                 if(scoreBoard[i]==scoreBoard[winningPlayer]&&(winningPlayer!=i))
  1211.                 {
  1212.                     System.out.print("Player " + (i+1) + ", and ");
  1213.                     count=TIE;
  1214.                 }
  1215.             }
  1216.            
  1217.             if(count==TIE)
  1218.             {
  1219.                 System.out.println("Player " + (winningPlayer+1) + " have tied!");
  1220.                 winningPlayer=count;
  1221.             }
  1222.             return winningPlayer;
  1223.         }
  1224.        
  1225.         /**
  1226.          * A simple method that clears the screen.
  1227.          * @param numberOfSpaces
  1228.          */
  1229.         public static void clearScreen(int numberOfSpaces)
  1230.         {
  1231.             for(int i=0;i<numberOfSpaces;i++)
  1232.             {
  1233.                 System.out.print("\n");
  1234.             }
  1235.         }
  1236.        
  1237.         /**
  1238.          * Simply lists out all the rules of the game.
  1239.          * @throws IOException
  1240.          */
  1241.         public static void rules() throws IOException
  1242.         {
  1243.             /**
  1244.              * Enter in rules for the game.
  1245.              */
  1246.             clearScreen(15);
  1247.             System.out.println(" \t\t\t\t\t\t ------------ Drop Dead "
  1248.                     + "------------");
  1249.             System.out.println();
  1250.             System.out.print("First, figure out how many people will "
  1251.                     + "be playing.");
  1252.             System.out.println(" Once that has been decided, you will all "
  1253.                     + "roll to see who gets to go first.");
  1254.             System.out.print("Play will begin with the person who had "
  1255.                     + "the highest roll,");
  1256.             System.out.println(" with ties re-rolling until a winner "
  1257.                     + "is found.");
  1258.             System.out.println();
  1259.             System.out.print("Play begins by the first player rolling "
  1260.                     + "five dice.");
  1261.             System.out.println(" If any 2's or 5's are rolled, the player "
  1262.                     + "receives a score of zero.");
  1263.             System.out.print("These dice are then removed, as they are "
  1264.                     + "considered to be \"dead\".");
  1265.             System.out.println(" The remaining \"live\" dice will then "
  1266.                     + "be re-rolled.");
  1267.             System.out.println();
  1268.             System.out.print("If NO 2's or 5's are rolled, then all of the dice "
  1269.                     + "are added together.");
  1270.             System.out.println(" This is known as the player's \"round score\", "
  1271.                     + "and is");
  1272.             System.out.print("added to with each roll that contains "
  1273.                     + "NO 2's or 5's.");
  1274.             System.out.println();
  1275.             System.out.println();
  1276.             System.out.println("Press any key to continue...");
  1277.             System.out.println();
  1278.             System.in.read();
  1279.             clearScreen(10);
  1280.             System.out.println(" \t\t\t\t\t\t ---------- Drop Dead (cont.) "
  1281.                     + "----------");
  1282.             System.out.println();
  1283.             System.out.print("After each roll, the player will be asked if "
  1284.                     + "he/she would like to stop.");
  1285.             System.out.println(" If the player chooses to stop, ");
  1286.             System.out.print("their round score will be multiplied by their "
  1287.                     + "remaining live dice.");
  1288.             System.out.println("\n");
  1289.             System.out.print("It is helpful to note that the player's round score will ALWAYS");
  1290.             System.out.println(" be multiplied by their remaining live dice,");
  1291.             System.out.println("even when all the dice are dead.");
  1292.             System.out.println();
  1293.             System.out.print("Ex: If the player has a round score of 100, but rolls until there are");
  1294.             System.out.println("no more live dice, then the player's round score will be multiplied");
  1295.             System.out.print("by zero, resulting in a round score of zero as well.");
  1296.             System.out.println("Play continues in this fashion until all the "
  1297.                     + "dice are \"dead\".");
  1298.             System.out.println();
  1299.             System.out.println("Press any key to continue...");
  1300.             System.out.println();
  1301.             System.in.read();
  1302.             clearScreen(10);
  1303.             System.out.println(" \t\t\t\t\t\t ---------- Drop Dead (cont.) "
  1304.                     + "----------");
  1305.             System.out.println();
  1306.             System.out.println("Play continues until all players have had a turn, at which time the round ends.");
  1307.             System.out.println("The game ends whenever a player reaches " + GOAL + " points.");
  1308.             System.out.println();
  1309.             System.out.println("If more than one player has reached " + GOAL + " points, then the player with the highest");
  1310.             System.out.println("score wins the game.");
  1311.             clearScreen(4);
  1312.             System.out.println("Press any key to begin the game...");
  1313.             System.out.println();
  1314.             System.in.read();
  1315.             clearScreen(15);
  1316.                    
  1317.         }
  1318.        
  1319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement