import java.util.ArrayList; import javax.swing.*; import java.awt.*; /** * The DiceGame class controls the operations of a game played using 10 Chips * and 2 Dice. * * @author Matt Brickner * @version Feb 11, 2003 */ public class DiceGame { private int firstPosition; // the 4 position variables private int secondPosition; // store the array positions private int thirdPosition; // of the chips the user private int fourthPosition; // wants to remove private int playerOneScore; private int playerTwoScore; private int turn = 0; // this variable keeps track of what turn it is // in order to keep player scores and turns separate private boolean rollCheck; // used to check if user has rolled dice before // making selections and removing chips private ArrayList chips; // stores chips with values 1 through 10 private JOptionPane optionPane; private Dice firstDie = new Dice(); private Dice secondDie; private String status; /** * The DiceGame constructor initializes 2 Dice objects and an Array of 10 * chips as well as other variables used throughout the class. */ public DiceGame() { firstDie = new Dice(); secondDie = new Dice(); chips = new ArrayList(); status = "Player one's turn. Roll dice to start game"; firstPosition = -1; secondPosition = -1; thirdPosition = -1; fourthPosition = -1; rollCheck = false; for (int i = 0; i < 10; i++) { chips.add(new Chip(i+1)); } } /** * The rollDice method calls the roll() method from the Dice class * on the two dice objects. */ public void rollDice() { if ( rollCheck == false ) { firstDie.roll(); secondDie.roll(); rollCheck = true; } else status = "You must first remove your selected chips"; } /** * The getDiceTotal method adds the values returned by each dice object. */ public int getDiceTotal() { int firstValue = firstDie.getValue(); int secondValue = secondDie.getValue(); return firstValue + secondValue; } /** * The getFirstDie method returns the first Die object for use * in the GameGui class. * @return Dice object */ public Dice getFirstDie() { return firstDie; } /** * The getSecondDie method returns the first Die object for use * in the GameGui class. * @return Dice object */ public Dice getSecondDie() { return secondDie; } /** * The getScoreString method returns total of non-removed chip values. * as a String */ public String getScoreString() { String scoreString; int finalScore = 0; for (int i = 0; i < 10; i++) { if ( getChipStatus(i) == false ) finalScore = finalScore + getChipValue(i); else finalScore = finalScore + 0; } scoreString = finalScore + ""; return scoreString; } /** * The setSelection method marks user selected chips to be deleted by the * removeChips class. * @param Array position of chip object to mark for removal. */ public void setSelection(int position) { if ( rollCheck == true ) { if( firstPosition > -1 && secondPosition > -1 && thirdPosition > -1 ) { fourthPosition = position; status = "4 Chips Selected"; } else if (firstPosition > -1 && secondPosition > -1 ) { thirdPosition = position; status = "3 Chips Selected"; } else if (firstPosition > -1) { secondPosition = position; status = "2 Chips Selected"; } else { firstPosition = position; status = "1 Chip Selected"; } } else status = "You must roll the dice before you can make anymore selections"; } /** * The removeChips method removes chips selected by the user from the array. */ public void removeChips() { if ( rollCheck == true ) { int firstChipValue; int secondChipValue; int thirdChipValue; int fourthChipValue; boolean equal; if (firstPosition > -1) firstChipValue = ((Chip)chips.get(firstPosition)).getValue(); else firstChipValue = 0; if (secondPosition > -1) secondChipValue = ((Chip)chips.get(secondPosition)).getValue(); else secondChipValue = 0; if (thirdPosition > -1) thirdChipValue = ((Chip)chips.get(thirdPosition)).getValue(); else thirdChipValue = 0; if (fourthPosition > -1) fourthChipValue = ((Chip)chips.get(fourthPosition)).getValue(); else fourthChipValue = 0; int ChipTotal = firstChipValue + secondChipValue + thirdChipValue + fourthChipValue; if ( getDiceTotal() == ChipTotal ) equal = true; else equal = false; if( equal == true) { if (firstPosition > -1) ((Chip)chips.get(firstPosition)).setSelectedStatus(true); else status = "Program Error"; if (secondPosition > -1) ((Chip)chips.get(secondPosition)).setSelectedStatus(true); else status = "Program Error"; if (thirdPosition > -1) ((Chip)chips.get(thirdPosition)).setSelectedStatus(true); else status = "Program Error"; if (fourthPosition > -1) ((Chip)chips.get(fourthPosition)).setSelectedStatus(true); else status = "Program Error"; firstPosition = -1; secondPosition = -1; thirdPosition = -1; fourthPosition = -1; status = "Selected chips removed, roll dice again"; } else { firstPosition = -1; secondPosition = -1; thirdPosition = -1; fourthPosition = -1; status = "invalid selection"; } if ( equal == true ) // prevents user from having to roll dice again rollCheck = false; // if they make invalid selections } else status = "You must roll the dice before you can remove more chips"; } /** * The getStatus method returns status variable String which will be used * to update the status label in the GameGui class. */ public String getStatus() { return status; } /** * The getChipValue method returns the value of a specified chip * @param Array position of chip object. */ public int getChipValue(int position) { return ((Chip)chips.get(position)).getValue(); } /** * The getChipStatus method returns true if chip has been removed and false * if the chip is still active. * @param Array position of chip object */ public boolean getChipStatus(int position) { return ((Chip)chips.get(position)).getSelectedStatus(); } /** * The getPlayerOneScore method returns a String containing the total score * for all rounds played */ public String getPlayerOneScore() { String newScore = playerOneScore + ""; while (newScore.length() < 3) newScore = "0" + newScore; String playerOne = "Player One: " + newScore; return playerOne; } /** * The getPlayerTwoScore method returns a String containing the total score * for all rounds played */ public String getPlayerTwoScore() { String newScore = playerTwoScore + ""; while (newScore.length() < 3) newScore = "0" + newScore; String playerTwo = "Player Two: " + newScore; return playerTwo; } /** * The endTurn method ends a player's turn by adding their final score * for that round to the total running score for the game, and resets game * board for next player's turn. */ public void endTurn() // { rollCheck = false; if (turn == 0 || turn == 2 || turn == 4 || turn == 6 || turn == 8) { playerOneScore = playerOneScore + Integer.parseInt(getScoreString()); status = "Player two's turn. Roll the dice to begin."; turn++; } else if (turn == 1 || turn == 3 || turn == 5 || turn == 7) { playerTwoScore = playerTwoScore + Integer.parseInt(getScoreString()); status = "Player One's turn. Roll the dice to begin."; turn++; } else if (turn == 9) { playerTwoScore = playerTwoScore + Integer.parseInt(getScoreString()); if (playerOneScore < playerTwoScore) status = "Player one wins the game! Press 'End Turn' again to start a new game"; else if (playerTwoScore < playerOneScore) status = "Player two wins the game! Press 'End Turn' again to start a new game"; else status = "Player one and two end in a tie. Press 'End Turn' again to start a new game"; JOptionPane.showMessageDialog(optionPane, status, "Game Over", JOptionPane.PLAIN_MESSAGE); turn++; } else { newGame(); } chips = new ArrayList(); for (int i = 0; i < 10; i++) { chips.add(new Chip(i+1)); } } /** * The newGame method resets player scores and other attributes to start * a new game. */ public void newGame() { turn = 0; chips = new ArrayList(); for (int i = 0; i < 10; i++) { chips.add(new Chip(i+1)); } playerOneScore = 0; playerTwoScore = 0; status = "New game started. Player one, roll dice to begin."; } }