Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- * @Author: Joshua Farrow
- * @Program: DropDead.java
- * @Date: April 11, 2013
- * @TimeCompleted: 08:00am
- * @Version: 1.01
- */
- package dropdead;
- import java.io.*;
- import java.util.*;
- public class DropDead
- {
- public static Scanner input= new Scanner(System.in);
- public static Random number=new Random();
- public static final int GOAL=10; /*Set the desired point goal here.*/
- public static final int TIE=999;
- public static void main(String [] args) throws IOException
- {
- int startingPlayer;
- introduction();
- int players = numberOfPlayers();
- int[] dice = new int[players];
- if(players>1)
- {
- startingPlayer=determineStartingPlayer(dice,players);
- }
- else
- {
- startingPlayer=1;
- }
- playGame(players,startingPlayer);
- }
- /**
- * Allows the player to read the instructions
- * of the game, if they so desire, or bypass them altogether.
- * @return
- * @throws IOException
- */
- public static int introduction() throws IOException
- {
- char decision;
- do
- {
- System.out.println("Would you like to hear the rules? ");
- System.out.println("Please type Y or N: ");
- String yesOrNo=input.next();
- System.out.println();
- decision=yesOrNo.charAt(0);
- }while(decision!='y'&&decision!='Y'&&decision!='n'&&decision!='N');
- if(decision=='y'||decision=='Y')
- {
- rules();
- }
- return 1;
- }
- /**
- * Ask user to input how many players will be playing the game.
- *
- * @return
- */
- public static int numberOfPlayers()
- {
- int players;
- System.out.println("How many people will be playing? ");
- do
- {
- System.out.println("Please enter a number between 1 & 4: ");
- players = input.nextInt();
- }while(players<1||players>4); /* Limits amount of players */
- return players;
- }
- /**
- * Determines who the starting player will be.
- * Makes a call to beginningRoll(int[],int).
- * @param dice
- * @param players
- * @return
- */
- public static int determineStartingPlayer(int[] dice, int players)
- throws IOException
- {
- int[] playerRolls=new int[players];
- System.out.println();
- System.out.println("Press any key to roll and see which player "
- + "begins the game: ");
- System.in.read(); /* waits for a key press. */
- clearScreen(7);
- rollDice(dice);
- showRoll(dice,players);
- System.arraycopy(dice, 0, playerRolls, 0, playerRolls.length);
- int startingPlayer=beginningRoll(playerRolls,players)+1;
- System.out.println("Player " + startingPlayer + " will be "
- + "starting.");
- return startingPlayer;
- }
- /**
- * Changes the current player to the next player in line.
- * @param currentPlayer
- * @param players
- * @return
- */
- public static int setCurrentPlayer(int currentPlayer, int players)
- {
- if((currentPlayer+1)<=players)
- {
- currentPlayer+=1;
- }
- else if((currentPlayer+1)>players)
- {
- currentPlayer=1;
- }
- return currentPlayer;
- }
- /**
- * Completes dice roll to determine the starting player.
- * Method uses recursive tail call.
- * @param playerRolls
- * @param players
- * @return
- */
- public static int beginningRoll(int playerRolls[],int players)
- {
- int tie=0;
- int highestPlayer=0;
- int highestDice=playerRolls[0];
- for(int i=1;i<playerRolls.length;i++)
- {
- if(highestDice<playerRolls[i])
- {
- playerRolls[highestPlayer]=0;
- highestPlayer=i;
- highestDice=playerRolls[i];
- tie=0;
- }
- else if(highestDice>playerRolls[i])
- {
- playerRolls[i]=0;
- }
- else if(highestDice==playerRolls[i]&&highestPlayer!=i)
- {
- tie++;
- }
- }
- if(tie>0)
- {
- playerRolls=tieBreaker(playerRolls,players);
- //Recursive call to resolve the new playerRolls array
- highestPlayer=beginningRoll(playerRolls,players);
- }
- return highestPlayer;
- }
- /**
- * Breaks any ties that might occur when determining the starting player.
- * @param playerRolls
- * @param players
- * @return
- */
- public static int[] tieBreaker(int playerRolls[],int players)
- {
- for(int i=0;i<playerRolls.length;i++)
- {
- if(playerRolls[i]!=0)
- {
- playerRolls[i]=1+number.nextInt(5);
- System.out.print(" Player " + (i+1));
- System.out.print(" had to re-roll.");
- }
- }
- System.out.println();
- showRoll(playerRolls,players);
- return playerRolls;
- }
- /**
- * Entirety of game is ran through this method.
- * @param players
- * @param currentPlayer
- * @throws IOException
- */
- public static void playGame(int players, int currentPlayer)
- throws IOException
- {
- int winner=0;
- int round=1;
- int liveDice,reRoll,playerCurrentScore,turnCount;
- char decision;
- int[] scoreBoard=new int[players]; //Running total of scores
- System.out.println("Press any key to begin:");
- System.in.read();
- clearScreen(7);
- do
- {
- System.out.println("Round " + round);
- clearScreen(3);
- turnCount=1;
- do
- {
- //Resets all the variables for the next player.
- int[] dice=new int[5];
- liveDice=5;
- reRoll=1;
- playerCurrentScore=0;
- System.out.println("Player " + currentPlayer + "'s " +
- "turn.");
- System.out.println("Press any key to roll:");
- System.in.read();
- do
- {
- rollDice(dice);
- showRoll(dice,dice.length);
- playerCurrentScore+=calculateScore(dice);
- dice=determineDeadDice(dice);
- liveDice=dice.length;
- System.out.println();
- System.out.println("Current score: " +
- playerCurrentScore);
- System.out.println("Live Dice Remaining: " + liveDice);
- System.out.println("Score if you don't re-roll: " +
- (playerCurrentScore*liveDice));
- System.out.println();
- if(liveDice==0)
- {
- break; /*Makes sure the player isn't
- asked to re-roll.*/
- }
- //Ask player if they want to re-roll.
- do
- {
- System.out.println("Would you like to "
- + "keep rolling?");
- System.out.println("Y or N: ");
- String yesOrNo=input.next();
- decision=yesOrNo.charAt(0);
- }while(decision!='y'&&decision!='Y'&&decision!='n'
- &&decision!='N');
- if(decision=='y'||decision=='Y')
- {
- reRoll=1;
- }
- else if(decision=='n'||decision=='N')
- {
- reRoll=0;
- }
- }while(liveDice>=1&&reRoll!=0);
- clearScreen(10);
- playerCurrentScore*=liveDice;
- scoreBoard[currentPlayer-1]+=playerCurrentScore;
- if(liveDice==0)
- {
- showRoll(dice,dice.length);
- System.out.println("All of your dice are dead.");
- }
- System.out.println();
- System.out.println("Player " + currentPlayer + "'s score: "
- + scoreBoard[currentPlayer-1]);
- System.out.println();
- System.out.println("Press any key to begin next "
- + "player's turn:");
- System.in.read();
- clearScreen(10);
- currentPlayer=setCurrentPlayer(currentPlayer,players);
- turnCount++;
- }while(turnCount<=players);
- //Round ends
- System.out.println("End of Round " + round);
- System.out.println();
- System.out.println("Current scores are as follows: ");
- scoreBoardPrint(scoreBoard);
- round++;
- }while(isThereAWinner(scoreBoard)!=true);
- int winningPlayer=whoIsWinner(scoreBoard);
- if(winningPlayer!=TIE)
- {
- System.out.println("The winner of the game is Player "
- + (winningPlayer+1));
- }
- }
- /**
- * Generates a new dice roll
- *
- * @param dice Array to dice.
- * @return dice array.
- */
- public static int[] rollDice(int [] dice)
- {
- int i;
- for (i=0; i<dice.length; i++)
- {
- dice[i]= 1+number.nextInt(5);
- }
- return dice;
- }
- /**
- * Displays rolled dice
- *
- * @param dice Array to dice.
- * @param n Number of dice to be displayed.
- */
- public static void showRoll(int [] dice, int n)
- {
- int i;
- //System.out.println(n+" dice were rolled ...");
- for (i=0; i<n; i++)
- {
- System.out.print(" +-----+");
- }
- System.out.println();
- for (i=0; i<n; i++)
- {
- switch(dice[i])
- {
- case 4:
- case 5:
- case 6: System.out.print(" |* *|"); break;
- case 2:
- case 3: System.out.print(" |* |"); break;
- case 1: System.out.print(" | |"); break;
- case 0: System.out.print(" | |"); break;
- }
- }
- System.out.println();
- for (i=0; i<n; i++)
- {
- switch(dice[i]) {
- case 1:
- case 3:
- case 5: System.out.print(" | * |"); break;
- case 2:
- case 4: System.out.print(" | |"); break;
- case 6: System.out.print(" |* *|"); break;
- case 0: System.out.print(" | |"); break;
- }
- }
- System.out.println();
- for (i=0; i<n; i++)
- {
- switch(dice[i]) {
- case 4:
- case 5:
- case 6: System.out.print(" |* *|"); break;
- case 2:
- case 3: System.out.print(" | *|"); break;
- case 1: System.out.print(" | |"); break;
- case 0: System.out.print(" | |"); break;
- }
- }
- System.out.println();
- for (i=0; i<n; i++)
- {
- System.out.print(" +-----+");
- }
- System.out.println();
- }
- /**
- * Determines which dice are dead, and then removes them by creating
- * and returning a new array.
- * @param oldDice
- * @return
- */
- public static int[] determineDeadDice(int[] oldDice)
- {
- int[] newDice=new int[0];
- int count=0;
- for(int i=0;i<oldDice.length;i++)
- {
- if(oldDice[i]==2||oldDice[i]==5)
- {
- oldDice[i]=0;
- count++;
- }
- }
- if(count==0)
- {
- for(int i=0;i<oldDice.length;i++)
- {
- newDice=new int[oldDice.length];
- }
- }
- else //Removes the "dead" dice.
- {
- newDice=new int[oldDice.length-count];
- }
- return newDice;
- }
- /**
- * Calculates the score by sorting through the array and determining
- * which dice is a 2 or a 5.
- * @param dice
- * @return
- */
- public static int calculateScore(int[] dice)
- {
- int count=0;
- int score=0;
- for(int i=0;i<dice.length;i++)
- {
- if(dice[i]==2||dice[i]==5)
- {
- count++; //There's probably a better way to do this.
- }
- }
- if(count==0)
- {
- for(int i=0;i<dice.length;i++)
- {
- score+=dice[i];
- }
- }
- else
- {
- score=0;
- }
- return score;
- }
- /**
- * Prints the score board at the end of each round.
- * @param scoreBoard
- * @throws IOException
- */
- public static void scoreBoardPrint(int[] scoreBoard) throws IOException
- {
- for(int i=0;i<scoreBoard.length;i++)
- {
- System.out.println("Player " + (i+1) + ": " + scoreBoard[i]
- + "\t");
- }
- System.out.println();
- System.out.println();
- System.out.println("Press any key to continue: ");
- System.in.read();
- clearScreen(10);
- }
- /**
- * Checks to see if a winner has been found.
- * This method DOES NOT determine who the winner is.
- * @param scoreBoard
- * @return
- */
- public static boolean isThereAWinner(int[] scoreBoard)
- {
- boolean status=false;
- for(int i=0;i<scoreBoard.length;i++)
- {
- if(scoreBoard[i]>=GOAL)
- {
- status=true;
- }
- }
- return status;
- }
- /**
- * Determines which player is the winner.
- *
- * @param scoreBoard
- * @return
- */
- public static int whoIsWinner(int[] scoreBoard)
- {
- int winningPlayer=0;
- int count=0;
- /**
- * Sorts through array and determines who has the highest score
- * and sets it as the value of winningPlayer
- */
- for(int i=0;i<scoreBoard.length;i++)
- {
- if(scoreBoard[i]>=GOAL)
- {
- if(scoreBoard[i]>scoreBoard[winningPlayer]
- &&(winningPlayer!=i))
- {
- winningPlayer=i;
- }
- }
- }
- /**
- * Sorts back through the array and looks to see if any ties
- * have occurred.This is done second so that the values in the
- * array are compared to winningPlayer
- */
- for(int i=0;i<scoreBoard.length;i++)
- {
- if(scoreBoard[i]==scoreBoard[winningPlayer]&&(winningPlayer!=i))
- {
- System.out.print("Player " + (i+1) + ", and ");
- count=TIE;
- }
- }
- if(count==TIE)
- {
- System.out.println("Player " + (winningPlayer+1) + " have tied!");
- winningPlayer=count;
- }
- return winningPlayer;
- }
- /**
- * A simple method that clears the screen.
- * @param numberOfSpaces
- */
- public static void clearScreen(int numberOfSpaces)
- {
- for(int i=0;i<numberOfSpaces;i++)
- {
- System.out.print("\n");
- }
- }
- /**
- * Simply lists out all the rules of the game.
- * @throws IOException
- */
- public static void rules() throws IOException
- {
- /**
- * Enter in rules for the game.
- */
- clearScreen(15);
- System.out.println(" \t\t\t\t\t\t ------------ Drop Dead "
- + "------------");
- System.out.println();
- System.out.print("First, figure out how many people will "
- + "be playing.");
- System.out.println(" Once that has been decided, you will all "
- + "roll to see who gets to go first.");
- System.out.print("Play will begin with the person who had "
- + "the highest roll,");
- System.out.println(" with ties re-rolling until a winner "
- + "is found.");
- System.out.println();
- System.out.print("Play begins by the first player rolling "
- + "five dice.");
- System.out.println(" If any 2's or 5's are rolled, the player "
- + "receives a score of zero.");
- System.out.print("These dice are then removed, as they are "
- + "considered to be \"dead\".");
- System.out.println(" The remaining \"live\" dice will then "
- + "be re-rolled.");
- System.out.println();
- System.out.print("If NO 2's or 5's are rolled, then all of the dice "
- + "are added together.");
- System.out.println(" This is known as the player's \"round score\", "
- + "and is");
- System.out.print("added to with each roll that contains "
- + "NO 2's or 5's.");
- System.out.println();
- System.out.println();
- System.out.println("Press any key to continue...");
- System.out.println();
- System.in.read();
- clearScreen(10);
- System.out.println(" \t\t\t\t\t\t ---------- Drop Dead (cont.) "
- + "----------");
- System.out.println();
- System.out.print("After each roll, the player will be asked if "
- + "he/she would like to stop.");
- System.out.println(" If the player chooses to stop, ");
- System.out.print("their round score will be multiplied by their "
- + "remaining live dice.");
- System.out.println("\n");
- System.out.print("It is helpful to note that the player's round score will ALWAYS");
- System.out.println(" be multiplied by their remaining live dice,");
- System.out.println("even when all the dice are dead.");
- System.out.println();
- System.out.print("Ex: If the player has a round score of 100, but rolls until there are");
- System.out.println("no more live dice, then the player's round score will be multiplied");
- System.out.print("by zero, resulting in a round score of zero as well.");
- System.out.println("Play continues in this fashion until all the "
- + "dice are \"dead\".");
- System.out.println();
- System.out.println("Press any key to continue...");
- System.out.println();
- System.in.read();
- clearScreen(10);
- System.out.println(" \t\t\t\t\t\t ---------- Drop Dead (cont.) "
- + "----------");
- System.out.println();
- System.out.println("Play continues until all players have had a turn, at which time the round ends.");
- System.out.println("The game ends whenever a player reaches " + GOAL + " points.");
- System.out.println();
- System.out.println("If more than one player has reached " + GOAL + " points, then the player with the highest");
- System.out.println("score wins the game.");
- clearScreen(4);
- System.out.println("Press any key to begin the game...");
- System.out.println();
- System.in.read();
- clearScreen(15);
- }
- }/*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- * @Author: Joshua Farrow
- * @Program: DropDead.java
- * @Date: April 10, 2013
- * @TimeCompleted: 10:03pm
- * @Version: 1.01
- */
- package dropdead;
- import java.io.*;
- import java.util.*;
- public class DropDead
- {
- public static Scanner input= new Scanner(System.in);
- public static Random number=new Random();
- public static final int GOAL=10; /*Set the desired point goal here.*/
- public static final int TIE=999;
- public static void main(String [] args) throws IOException
- {
- int startingPlayer;
- introduction();
- int players = numberOfPlayers();
- int[] dice = new int[players];
- if(players>1)
- {
- startingPlayer=determineStartingPlayer(dice,players);
- }
- else
- {
- startingPlayer=1;
- }
- playGame(players,startingPlayer);
- }
- /**
- * Allows the player to read the instructions
- * of the game, if they so desire, or bypass them altogether.
- * @return
- * @throws IOException
- */
- public static int introduction() throws IOException
- {
- char decision;
- do
- {
- System.out.println("Would you like to hear the rules? ");
- System.out.println("Please type Y or N: ");
- String yesOrNo=input.next();
- System.out.println();
- decision=yesOrNo.charAt(0);
- }while(decision!='y'&&decision!='Y'&&decision!='n'&&decision!='N');
- if(decision=='y'||decision=='Y')
- {
- rules();
- }
- return 1;
- }
- /**
- * Ask user to input how many players will be playing the game.
- *
- * @return
- */
- public static int numberOfPlayers()
- {
- int players;
- System.out.println("How many people will be playing? ");
- do
- {
- System.out.println("Please enter a number between 1 & 4: ");
- players = input.nextInt();
- }while(players<1||players>4); /* Limits amount of players */
- return players;
- }
- /**
- * Determines who the starting player will be.
- * Makes a call to beginningRoll(int[],int).
- * @param dice
- * @param players
- * @return
- */
- public static int determineStartingPlayer(int[] dice, int players)
- throws IOException
- {
- int[] playerRolls=new int[players];
- System.out.println();
- System.out.println("Press any key to roll and see which player "
- + "begins the game: ");
- System.in.read(); /* waits for a key press. */
- clearScreen(7);
- rollDice(dice);
- showRoll(dice,players);
- System.arraycopy(dice, 0, playerRolls, 0, playerRolls.length);
- int startingPlayer=beginningRoll(playerRolls,players)+1;
- System.out.println("Player " + startingPlayer + " will be "
- + "starting.");
- return startingPlayer;
- }
- /**
- * Changes the current player to the next player in line.
- * @param currentPlayer
- * @param players
- * @return
- */
- public static int setCurrentPlayer(int currentPlayer, int players)
- {
- if((currentPlayer+1)<=players)
- {
- currentPlayer+=1;
- }
- else if((currentPlayer+1)>players)
- {
- currentPlayer=1;
- }
- return currentPlayer;
- }
- /**
- * Completes dice roll to determine the starting player.
- * Method uses recursive tail call.
- * @param playerRolls
- * @param players
- * @return
- */
- public static int beginningRoll(int playerRolls[],int players)
- {
- int tie=0;
- int highestPlayer=0;
- int highestDice=playerRolls[0];
- for(int i=1;i<playerRolls.length;i++)
- {
- if(highestDice<playerRolls[i])
- {
- playerRolls[highestPlayer]=0;
- highestPlayer=i;
- highestDice=playerRolls[i];
- tie=0;
- }
- else if(highestDice>playerRolls[i])
- {
- playerRolls[i]=0;
- }
- else if(highestDice==playerRolls[i]&&highestPlayer!=i)
- {
- tie++;
- }
- }
- if(tie>0)
- {
- playerRolls=tieBreaker(playerRolls,players);
- //Recursive call to resolve the new playerRolls array
- highestPlayer=beginningRoll(playerRolls,players);
- }
- return highestPlayer;
- }
- /**
- * Breaks any ties that might occur when determining the starting player.
- * @param playerRolls
- * @param players
- * @return
- */
- public static int[] tieBreaker(int playerRolls[],int players)
- {
- for(int i=0;i<playerRolls.length;i++)
- {
- if(playerRolls[i]!=0)
- {
- playerRolls[i]=1+number.nextInt(5);
- System.out.print(" Player " + (i+1));
- System.out.print(" had to re-roll.");
- }
- }
- System.out.println();
- showRoll(playerRolls,players);
- return playerRolls;
- }
- /**
- * Entirety of game is ran through this method.
- * @param players
- * @param currentPlayer
- * @throws IOException
- */
- public static void playGame(int players, int currentPlayer)
- throws IOException
- {
- int winner=0;
- int round=1;
- int liveDice,reRoll,playerCurrentScore,turnCount;
- char decision;
- int[] scoreBoard=new int[players]; //Running total of scores
- System.out.println("Press any key to begin:");
- System.in.read();
- clearScreen(7);
- do
- {
- System.out.println("Round " + round);
- clearScreen(3);
- turnCount=1;
- do
- {
- //Resets all the variables for the next player.
- int[] dice=new int[5];
- liveDice=5;
- reRoll=1;
- playerCurrentScore=0;
- System.out.println("Player " + currentPlayer + "'s " +
- "turn.");
- System.out.println("Press any key to roll:");
- System.in.read();
- do
- {
- rollDice(dice);
- showRoll(dice,dice.length);
- playerCurrentScore+=calculateScore(dice);
- dice=determineDeadDice(dice);
- liveDice=dice.length;
- System.out.println();
- System.out.println("Current score: " +
- playerCurrentScore);
- System.out.println("Live Dice Remaining: " + liveDice);
- System.out.println("Score if you don't re-roll: " +
- (playerCurrentScore*liveDice));
- System.out.println();
- if(liveDice==0)
- {
- break; /*Makes sure the player isn't
- asked to re-roll.*/
- }
- //Ask player if they want to re-roll.
- do
- {
- System.out.println("Would you like to "
- + "keep rolling?");
- System.out.println("Y or N: ");
- String yesOrNo=input.next();
- decision=yesOrNo.charAt(0);
- }while(decision!='y'&&decision!='Y'&&decision!='n'
- &&decision!='N');
- if(decision=='y'||decision=='Y')
- {
- reRoll=1;
- }
- else if(decision=='n'||decision=='N')
- {
- reRoll=0;
- }
- }while(liveDice>=1&&reRoll!=0);
- clearScreen(10);
- playerCurrentScore*=liveDice;
- scoreBoard[currentPlayer-1]+=playerCurrentScore;
- if(liveDice==0)
- {
- showRoll(dice,dice.length);
- System.out.println("All of your dice are dead.");
- }
- System.out.println();
- System.out.println("Player " + currentPlayer + "'s score: "
- + scoreBoard[currentPlayer-1]);
- System.out.println();
- System.out.println("Press any key to begin next "
- + "player's turn:");
- System.in.read();
- clearScreen(10);
- currentPlayer=setCurrentPlayer(currentPlayer,players);
- turnCount++;
- }while(turnCount<=players);
- //Round ends
- System.out.println("End of Round " + round);
- System.out.println();
- System.out.println("Current scores are as follows: ");
- scoreBoardPrint(scoreBoard);
- round++;
- for(int i=0;i<scoreBoard.length;i++)
- {
- scoreBoard[i]=20;
- }
- }while(isThereAWinner(scoreBoard)!=true);
- int winningPlayer=whoIsWinner(scoreBoard);
- if(winningPlayer!=TIE)
- {
- System.out.println("The winner of the game is Player "
- + (winningPlayer+1));
- }
- }
- /**
- * Generates a new dice roll
- *
- * @param dice Array to dice.
- * @return dice array.
- */
- public static int[] rollDice(int [] dice)
- {
- int i;
- for (i=0; i<dice.length; i++)
- {
- dice[i]= 1+number.nextInt(5);
- }
- return dice;
- }
- /**
- * Displays rolled dice
- *
- * @param dice Array to dice.
- * @param n Number of dice to be displayed.
- */
- public static void showRoll(int [] dice, int n)
- {
- int i;
- //System.out.println(n+" dice were rolled ...");
- for (i=0; i<n; i++)
- {
- System.out.print(" +-----+");
- }
- System.out.println();
- for (i=0; i<n; i++)
- {
- switch(dice[i])
- {
- case 4:
- case 5:
- case 6: System.out.print(" |* *|"); break;
- case 2:
- case 3: System.out.print(" |* |"); break;
- case 1: System.out.print(" | |"); break;
- case 0: System.out.print(" | |"); break;
- }
- }
- System.out.println();
- for (i=0; i<n; i++)
- {
- switch(dice[i]) {
- case 1:
- case 3:
- case 5: System.out.print(" | * |"); break;
- case 2:
- case 4: System.out.print(" | |"); break;
- case 6: System.out.print(" |* *|"); break;
- case 0: System.out.print(" | |"); break;
- }
- }
- System.out.println();
- for (i=0; i<n; i++)
- {
- switch(dice[i]) {
- case 4:
- case 5:
- case 6: System.out.print(" |* *|"); break;
- case 2:
- case 3: System.out.print(" | *|"); break;
- case 1: System.out.print(" | |"); break;
- case 0: System.out.print(" | |"); break;
- }
- }
- System.out.println();
- for (i=0; i<n; i++)
- {
- System.out.print(" +-----+");
- }
- System.out.println();
- }
- /**
- * Determines which dice are dead, and then removes them by creating
- * and returning a new array.
- * @param oldDice
- * @return
- */
- public static int[] determineDeadDice(int[] oldDice)
- {
- int[] newDice=new int[0];
- int count=0;
- for(int i=0;i<oldDice.length;i++)
- {
- if(oldDice[i]==2||oldDice[i]==5)
- {
- oldDice[i]=0;
- count++;
- }
- }
- if(count==0)
- {
- for(int i=0;i<oldDice.length;i++)
- {
- newDice=new int[oldDice.length];
- }
- }
- else //Removes the "dead" dice.
- {
- newDice=new int[oldDice.length-count];
- }
- return newDice;
- }
- /**
- * Calculates the score by sorting through the array and determining
- * which dice is a 2 or a 5.
- * @param dice
- * @return
- */
- public static int calculateScore(int[] dice)
- {
- int count=0;
- int score=0;
- for(int i=0;i<dice.length;i++)
- {
- if(dice[i]==2||dice[i]==5)
- {
- count++; //There's probably a better way to do this.
- }
- }
- if(count==0)
- {
- for(int i=0;i<dice.length;i++)
- {
- score+=dice[i];
- }
- }
- else
- {
- score=0;
- }
- return score;
- }
- /**
- * Prints the score board at the end of each round.
- * @param scoreBoard
- * @throws IOException
- */
- public static void scoreBoardPrint(int[] scoreBoard) throws IOException
- {
- for(int i=0;i<scoreBoard.length;i++)
- {
- System.out.println("Player " + (i+1) + ": " + scoreBoard[i]
- + "\t");
- }
- System.out.println();
- System.out.println();
- System.out.println("Press any key to continue: ");
- System.in.read();
- clearScreen(10);
- }
- /**
- * Checks to see if a winner has been found.
- * This method DOES NOT determine who the winner is.
- * @param scoreBoard
- * @return
- */
- public static boolean isThereAWinner(int[] scoreBoard)
- {
- boolean status=false;
- for(int i=0;i<scoreBoard.length;i++)
- {
- if(scoreBoard[i]>=GOAL)
- {
- status=true;
- }
- }
- return status;
- }
- /**
- * Determines which player is the winner.
- *
- * @param scoreBoard
- * @return
- */
- public static int whoIsWinner(int[] scoreBoard)
- {
- int winningPlayer=0;
- int count=0;
- /**
- * Sorts through array and determines who has the highest score
- * and sets it as the value of winningPlayer
- */
- for(int i=0;i<scoreBoard.length;i++)
- {
- if(scoreBoard[i]>=GOAL)
- {
- if(scoreBoard[i]>scoreBoard[winningPlayer]
- &&(winningPlayer!=i))
- {
- winningPlayer=i;
- }
- }
- }
- /**
- * Sorts back through the array and looks to see if any ties
- * have occurred.This is done second so that the values in the
- * array are compared to winningPlayer
- */
- for(int i=0;i<scoreBoard.length;i++)
- {
- if(scoreBoard[i]==scoreBoard[winningPlayer]&&(winningPlayer!=i))
- {
- System.out.print("Player " + (i+1) + ", and ");
- count=TIE;
- }
- }
- if(count==TIE)
- {
- System.out.println("Player " + (winningPlayer+1) + " have tied!");
- winningPlayer=count;
- }
- return winningPlayer;
- }
- /**
- * A simple method that clears the screen.
- * @param numberOfSpaces
- */
- public static void clearScreen(int numberOfSpaces)
- {
- for(int i=0;i<numberOfSpaces;i++)
- {
- System.out.print("\n");
- }
- }
- /**
- * Simply lists out all the rules of the game.
- * @throws IOException
- */
- public static void rules() throws IOException
- {
- /**
- * Enter in rules for the game.
- */
- clearScreen(15);
- System.out.println(" \t\t\t\t\t\t ------------ Drop Dead "
- + "------------");
- System.out.println();
- System.out.print("First, figure out how many people will "
- + "be playing.");
- System.out.println(" Once that has been decided, you will all "
- + "roll to see who gets to go first.");
- System.out.print("Play will begin with the person who had "
- + "the highest roll,");
- System.out.println(" with ties re-rolling until a winner "
- + "is found.");
- System.out.println();
- System.out.print("Play begins by the first player rolling "
- + "five dice.");
- System.out.println(" If any 2's or 5's are rolled, the player "
- + "receives a score of zero.");
- System.out.print("These dice are then removed, as they are "
- + "considered to be \"dead\".");
- System.out.println(" The remaining \"live\" dice will then "
- + "be re-rolled.");
- System.out.println();
- System.out.print("If NO 2's or 5's are rolled, then all of the dice "
- + "are added together.");
- System.out.println(" This is known as the player's \"round score\", "
- + "and is");
- System.out.print("added to with each roll that contains "
- + "NO 2's or 5's.");
- System.out.println();
- System.out.println();
- System.out.println("Press any key to continue...");
- System.out.println();
- System.in.read();
- clearScreen(10);
- System.out.println(" \t\t\t\t\t\t ---------- Drop Dead (cont.) "
- + "----------");
- System.out.println();
- System.out.print("After each roll, the player will be asked if "
- + "he/she would like to stop.");
- System.out.println(" If the player chooses to stop, ");
- System.out.print("their round score will be multiplied by their "
- + "remaining live dice.");
- System.out.println("\n");
- System.out.print("It is helpful to note that the player's round score will ALWAYS");
- System.out.println(" be multiplied by their remaining live dice,");
- System.out.println("even when all the dice are dead.");
- System.out.println();
- System.out.print("Ex: If the player has a round score of 100, but rolls until there are");
- System.out.println("no more live dice, then the player's round score will be multiplied");
- System.out.print("by zero, resulting in a round score of zero as well.");
- System.out.println("Play continues in this fashion until all the "
- + "dice are \"dead\".");
- System.out.println();
- System.out.println("Press any key to continue...");
- System.out.println();
- System.in.read();
- clearScreen(10);
- System.out.println(" \t\t\t\t\t\t ---------- Drop Dead (cont.) "
- + "----------");
- System.out.println();
- System.out.println("Play continues until all players have had a turn, at which time the round ends.");
- System.out.println("The game ends whenever a player reaches " + GOAL + " points.");
- System.out.println();
- System.out.println("If more than one player has reached " + GOAL + " points, then the player with the highest");
- System.out.println("score wins the game.");
- clearScreen(4);
- System.out.println("Press any key to begin the game...");
- System.out.println();
- System.in.read();
- clearScreen(15);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement