/** * Class: Pile * File: Pile.java * Original Author: Dave Peixotto * Creation Date: 2.24.2001 * Modified by: NULL * Last Modified: 2.28.2001 ************************************************************** * Class Description: This class is used to keep track of a pile * of poker chips. It contains methods for adding and removing * chips to the pile and it is also able to display itself. ************************************************************** * Instance Variables: **numberOfChips - private - number of poker chips in the pile **chips - private - Vector - contains all of the poler chips in the pile **pileValue - private - value of the pile **colorOfChips - private - color of the chips **CHIP_SPACE - the amount of space between each chip in the pile. used for display ************************************************************** * Methods: **addChip() - adds a chip to the pile **removeChip() - removes a chip from the pile **removeAllChips() - removes all the chips from a pile **fillChips() - fills the chips vector with chips **displayPile() - displays the pile on the screen **countPileValue() - counts the value of the pile **getPileValue() - returns the vallue of this pile */ import java.util.Vector; import java.awt.*; public class Pile { //Class Variables public static final int CHIP_SPACE = 5; //Instance Variables************************************** private int numberOfChips; private int pileValue; private Vector chips; private Color colorOfChips; //not currently used, perhaps in the future?? //Methods************************************************** //constructor---------------- public Pile(int num, Color color, int val) { fillChipsVector(num, color, val); numberOfChips = num; colorOfChips = color; pileValue = countPileValue(); } //accessors----------------------------------------------------- public int getPileValue() { return pileValue; } public int getNumberOfChips() { return numberOfChips; } public Color getColorOfChips() { return colorOfChips; } public String getPileValueString() { return (new Integer(pileValue).toString() ); } //chip manipulation----------------------------------------------- /**addChip()**** * Parameters: * pchip - PokerChip - chip to be added to the pile * ***** * Method adds a chip to the pile by adding it to the chips Vector. The chip * is inserted after the last position of the Vector */ public void addChip(PokerChip pchip) { chips.insertElementAt((Object)pchip, numberOfChips); numberOfChips += 1; pileValue = countPileValue(); } /**removeChip**** * Parameters: * index - int - index of the chip that should be removed. 0 is the bottom most * chip. ***** * Method removes the chip from the pile by deleting it from the chips Vector. * The chip to be removed is first stored in a temporary chip object and then * the chip is deleted from the Vector. The temporary chip is then returned to * the calling object. */ public PokerChip removeChip(int index) { //create temporary chip PokerChip tempChip = new PokerChip(Color.black, 0); tempChip = (PokerChip)chips.elementAt(index); //remove the chip chips.removeElementAt(index); numberOfChips -= 1; pileValue = countPileValue(); return tempChip; } /**removeAllChips()**** * This method removes all chips from the pile by deleting all the elements * in the chips Vector */ public void removeAllChips() { chips.clear(); numberOfChips = 0; pileValue = 0; } //graphic methods-------------------------------------------------------- /**displayPile()**** * Parameters: * g2 - handle to the graphics context * x - x position for the pile. indicates the leftmost position of the pile * y - y position for the pile. indicates the bottom position of the pile ***** * Method is used to display the pile on the screen. The method cycles * through each chip in the pile and has it display itself. The first * n-1 chips are displayed in a loop and the last chip is displayed outside * the loop so that its value can also be painted on the chip * The chips are painted upwards, that is the y coordinate passed in * will represent the top of the very first chip painted, all remaining chips * will be painted above this chip */ public void displayPile(Graphics2D g2, int x, int y) { int index; int deltaY = 0; // amount to subtract to y positon of each chip to display the pile correctly // create temporary poker chip PokerChip tempChip = new PokerChip(Color.green, 0); //loop is skipped if there is <= 1 chip in the pile for(index = 0;index < numberOfChips - 1; ++index) { tempChip = (PokerChip) chips.elementAt(index); tempChip.display(g2, x, y - deltaY); deltaY += CHIP_SPACE; } //display the last chip with the value painted in the chip //test to be sure that there is at least one chip to be painted if(numberOfChips > 0) { tempChip = (PokerChip)chips.elementAt(index); tempChip.setDisplayValue(true); tempChip.display(g2, x, y - deltaY); } } //class utilities-------------------------------------------------------- /**fillChipsVector()**** * Parameters: * num - int - number of chips * val - int - value of the chips * color - Color - color of the chips ***** * This method fills out the chips vector with the appropriate amount of chips * using the value and color passes in as parameters. First we test to see * if the chips are green. If they are we create the chips with random * values, otherwise we use the values supplied by the user. */ private void fillChipsVector(int num, Color color, int val) { chips = new Vector(num); //fill with random values if(color.equals(Color.green) ){ for(int i = 0; i < num; ++i) { chips.add(i, (Object) new PokerChip(color, (int)(Math.random() * 1001)) ); } } //fill with supplied value else{ for(int i = 0; i < num; ++i) { chips.add(i, (Object) new PokerChip(color, val) ); } } } /**countPileValue**** * This method counts the value of the pile. * We go through the chips Vector and add the values of all the chips. * */ private int countPileValue() { //reset value pileValue = 0; // create temporary poker chip PokerChip tempChip = new PokerChip(Color.black, 0); //cycle through vector and add the value of all the chips for(int i = 0; i < numberOfChips; ++i) { tempChip = (PokerChip)chips.elementAt(i); pileValue += tempChip.getValue(); } return pileValue; } /**printChipValues**** * Prints the values of the chips in the vector. For debugging purposes only. */ public String printValues() { StringBuffer tmpBuff = new StringBuffer(); // create temporary poker chip PokerChip tempChip = new PokerChip(Color.green, 0); //cycle through array and add the value of all the chips for(int i = 0; i < numberOfChips; ++i) { tempChip = (PokerChip)chips.elementAt(i); tmpBuff.append(tempChip.getValueString() + " " ); } return tmpBuff.toString(); } }