/* * 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;iplayerRolls[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=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=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=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;i1) { 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;iplayerRolls[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=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=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=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