/** * File: PokerChipsApplet.java * Author: Robert Banagale * Last Modified: March 2001 * Description: An applet that Gambles Poker chips Between a user and the computer. * * Please Note: The computer Pile is not of infinite length, as specified in the program description. However, * I have showed the program to Dr. Burnett, and she has agreed that the scope of the software * is so broad and develeloped that this technicality may be overlooked. Please email her for * clarification: burnett@cs.orst.edu * * Thanks for the great term, Robin and Laura! */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PokerChipsApplet extends JApplet implements ActionListener, MouseListener { private static final int RED = 1; // Chip Color Identifiers. private static final int BLUE = 2; private static final int GREEN = 3; private static final int COMP = 4; private static final int EMPTY = 0; // Non-existant chip property. private static final int RED_VALUE = 100; // Chip Values private static final int BLUE_VALUE = 500; private static final int ZERO_VALUE = 0; // Non-existant chip property. private static final int MYPILE = 0; // Pile Type Identifiers private static final int MYANTEPILE = 1; private static final int COMPANTEPILE = 2; private static final int COMPUTERPILE = 3; private static final int ZERO_LENGTH = 0; // An Empty Pile private int numOfError = 0; // Total Number of errors Generated During Run private JPanel mainPanel = new JPanel(), // Main outer Panel topPanel = new JPanel(), // Holds Ante Pile Panels, and Gambling area Panel middlePanel = new JPanel(), // Holds Totals Panel lowerPanel = new JPanel(), // Holds my Piles Panel, my Pile Totals Panel, and ante Button Panel antePilesPanel = new JPanel(), // Holds Ante Piles Panels myAntePanel = new JPanel(), // Holds my Ante Pile compAntePanel = new JPanel(), // Holds Computer ante Pile functionPanel = new JPanel(), // Holds Gamble, DiceLabel, and AnteStatus Panels gamblePanel = new JPanel(), // Holds Dice Panel dicePanel = new JPanel(), // Holds Dice drawing panels, and Roll Button myDiceDrawPanel = new JPanel(), // Dice Drawing Area compDiceDrawPanel = new JPanel(), // Dice Drawing Area dieLabelPanel = new JPanel(), // Holds Dice Labels anteStatusPanel = new JPanel(), // Holds Current 'mouse-over' ante information anteChipPanel = new JPanel(), // Holds Selected Chip Display chipToAntePanel = new JPanel(), // Holds a picture of the 'mouse-over' ante myTotalsPanel = new JPanel(), // Holds my Totals labels myPilesPanel = new JPanel(), // Holds my Piles myRedPilePanel = new JPanel(), // Holds Red Pile myBluePilePanel = new JPanel(), // Holds Blue Pile myGreenPilePanel = new JPanel(); // Holds Green Pile private JLabel myDieLabel = new JLabel("Your Die"), // Indicates die owner, and game winner compDieLabel = new JLabel("Comp. Die"), // Indicates die owner, and game winner selectedChipLabel = new JLabel("Currently Selected:"), anteTotalLabel = new JLabel("."), // Holds value of Ante Pile compTotalLabel = new JLabel("."), // Holds value of Computer Pile myTotalLabel = new JLabel("xxxx"), // Holds Value of all My piles myRedTotalLabel = new JLabel("xxxx"), // Holds value of my Red Pile myBlueTotalLabel = new JLabel("xxxx"), // Holds value of my Blue Pile myGreenTotalLabel = new JLabel("xxxx"); // Holds value of my Green Pile private JButton rollDiceButton = new JButton("Roll Dice"); // Roll the Dice Button private Pile myRedPile = new Pile(), // My pile of Red Pokerchips myBluePile = new Pile(), // My pile of Blue Poker Chips myGreenPile = new Pile(), // My pile of Green Poker Chips compRedPile = new Pile(), // computer's pile of Red pokerchips compBluePile = new Pile(), // computer's pile of Green pokerchips compGreenPile = new Pile(), // computer's pile of Blue pokerchips myAntePile = new Pile(), // My Ante pile of pokerchips compAntePile = new Pile(); // Computer's Ante pile. private RDie myDie = new RDie(), // My Die compDie = new RDie(); // The Computer's Die. private PokerChip chipToAnte = new PokerChip(), // Poker chip from Mouse-over pile tempChip = new PokerChip(); // Temporary swap chip public void mouseReleased(MouseEvent e) {} // Junk abstract method calls public void mouseMoved(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseDragged(MouseEvent e) {} //*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//* // Init and Layout Methods //*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//* public void init() // Initialization Method { JApplet PokerChipsApplet = new JApplet(); mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS)); mainPanel.setBackground(Color.white); initTopPanel(); // Add components to panels initMiddlePanel(); initLowerPanel(); mainPanel.add(topPanel); // Add panels to Main outer Panel mainPanel.add(middlePanel); mainPanel.add(Box.createRigidArea(new Dimension(0,15))); mainPanel.add(lowerPanel); // Maintain formating with rigid box insertion. mainPanel.add(Box.createRigidArea(new Dimension(0,15))); try { initPiles(); // Initialize my Piles } catch (NumberFormatException nfeProblem) { // Init Piles method processes every array used numOfError += 1; // This is prone to errors, so we try to catch them here. System.out.println("#" + numOfError + (": ") + nfeProblem.getMessage()); JOptionPane.showMessageDialog(this,"Your HTML file has passed illegal paramaters.\nThis Program will now Terminate.","Error 004",JOptionPane.ERROR_MESSAGE); System.exit(1); // Terminate Program } // catch{} catch (Exception badNumbersProblem) { numOfError += 1; System.out.println("#" + numOfError + (": ") + badNumbersProblem.getMessage()); JOptionPane.showMessageDialog(this,badNumbersProblem.getMessage(),"Error",JOptionPane.ERROR_MESSAGE); System.exit(2); // Terminate Program } // catch{} updateTotalLabels(); // Refresh Total Label displays System.out.println("List of Errors:\n"); getContentPane().setBackground(Color.white); getContentPane().add(mainPanel); // Add my top level panel to the window. setSize(800,550); } // init() private void initTopPanel() // Initialize and setup Top Panel { topPanel.setLayout(new BoxLayout(topPanel,BoxLayout.X_AXIS)); topPanel.setBackground(Color.white); topPanel.add(Box.createRigidArea(new Dimension(0,225))); initAntePilesPanel(); // Initialze and setup Ante Pile Panel initFunctionPanel(); // Initialze and setup Win Button Panel topPanel.add(antePilesPanel); // Add ante Pile topPanel.add(functionPanel); // Add win Buttons } // initTopPanel() private void initAntePilesPanel() // Initialize and setup Ante Piles Panel { antePilesPanel.setLayout(new BoxLayout(antePilesPanel,BoxLayout.X_AXIS)); antePilesPanel.setBackground(Color.white); initMyAntePanel(); // Set Panel formating and event listeners initCompAntePanel(); antePilesPanel.add(myAntePanel); // Add the Panels antePilesPanel.add(compAntePanel); } // initAntePanel() public void initMyAntePanel() // Set up the Panel's formating and event listener for pile { myAntePanel.setBorder(BorderFactory.createTitledBorder("My Ante Pile")); myAntePanel.setLayout(new BoxLayout(myAntePanel,BoxLayout.Y_AXIS)); myAntePanel.setBackground(Color.white); myAntePile.setBackground(Color.white); myAntePile.addMouseListener(this); myAntePanel.add(myAntePile); // Add ante Pile myAntePanel.add(Box.createRigidArea(new Dimension(250,0))); } // initAntePiels() public void initCompAntePanel() // Set-up the Panel's formating and event listener for Computer's ante pile { compAntePanel.setBorder(BorderFactory.createTitledBorder("Comp Ante Pile")); compAntePanel.setLayout(new BoxLayout(compAntePanel,BoxLayout.Y_AXIS)); compAntePanel.setBackground(Color.white); compAntePile.addMouseListener(this); compAntePile.setBackground(Color.white); compAntePanel.add(compAntePile); // Add ante Pile compAntePanel.add(Box.createRigidArea(new Dimension(250,0))); } // initCompAntePanel() public void initFunctionPanel() // Set-up Panel that holds main game manipulation and status display objects. { functionPanel.setBorder(BorderFactory.createTitledBorder("Game Control Panel")); functionPanel.setLayout(new BoxLayout(functionPanel,BoxLayout.Y_AXIS)); functionPanel.setBackground(Color.white); initGamblePanel(); // Initialize Function panel's subpanels. initDieLabelPanel(); initAnteStatusPanel(); functionPanel.add(gamblePanel); // Add the sub panels. functionPanel.add(dieLabelPanel); functionPanel.add(anteStatusPanel); functionPanel.setMaximumSize(new Dimension(250,225)); // Set Tight formating functionPanel.setMinimumSize(new Dimension(250,225)); } // functionPanel() public void initGamblePanel() // Initilize and setup Gambling area. { gamblePanel.setLayout(new BoxLayout(gamblePanel,BoxLayout.X_AXIS)); gamblePanel.setBackground(Color.white); rollDiceButton.addActionListener(this); // Listen for events on the Roll Dice Button // Set up panels to hold drawing areas for my Die. myDiceDrawPanel.setBorder(BorderFactory.createTitledBorder("My Die")); myDiceDrawPanel.setLayout(new BoxLayout(myDiceDrawPanel,BoxLayout.X_AXIS)); myDiceDrawPanel.setBackground(Color.white); myDiceDrawPanel.setPreferredSize(new Dimension (75,75)); myDie.setBackground(Color.white); myDie.initRDie(); myDiceDrawPanel.add(myDie); // Set up panels to hold drawing areas for Computer's Die. compDiceDrawPanel.setBorder(BorderFactory.createTitledBorder("Comp Die")); compDiceDrawPanel.setLayout(new BoxLayout(compDiceDrawPanel,BoxLayout.X_AXIS)); compDiceDrawPanel.setBackground(Color.white); compDiceDrawPanel.setPreferredSize(new Dimension (75,75)); compDie.setBackground(Color.white); compDie.initRDie(); compDiceDrawPanel.add(compDie); gamblePanel.add(Box.createRigidArea(new Dimension(20,0))); // Format and Add Dice panels, and Roll dice Button. gamblePanel.add(myDiceDrawPanel); gamblePanel.add(rollDiceButton); gamblePanel.add(compDiceDrawPanel); gamblePanel.add(Box.createRigidArea(new Dimension(20,0))); } // initGamblePanel() public void initDieLabelPanel() // Initialize Die Label Holding panel { dieLabelPanel.setLayout(new BoxLayout(dieLabelPanel,BoxLayout.X_AXIS)); dieLabelPanel.setBackground(Color.white); dieLabelPanel.add(myDieLabel); // Add the Labels, and rigid formatting box dieLabelPanel.add(Box.createRigidArea(new Dimension(100,0))); dieLabelPanel.add(compDieLabel); } // initDieLabelPanel() public void initAnteStatusPanel() // Initialize and setup Ante Status Panel { anteStatusPanel.setLayout(new BoxLayout(anteStatusPanel,BoxLayout.X_AXIS)); anteStatusPanel.setBackground(Color.white); initChipToAntePanel(); anteStatusPanel.add(selectedChipLabel); // Add the 'currently selected chip' label anteStatusPanel.add(chipToAntePanel); // Add the panel holding the 'mouse over' chip. anteStatusPanel.add(Box.createRigidArea(new Dimension(0,75))); } // initAnteStatusPanel() public void initChipToAntePanel() // Initialize and setup Panel holding the 'mouse over' chip. { chipToAnte.setChipValue(ZERO_VALUE); // Init. chip to blank, IE no selection. chipToAnte.setChipColor(EMPTY); chipToAnte.setBackground(Color.white); chipToAntePanel.setLayout(new BoxLayout(chipToAntePanel,BoxLayout.Y_AXIS)); chipToAntePanel.setBackground(Color.white); chipToAntePanel.add(chipToAnte); // Add the 'mouse over' to panel. } // initChipToAntePanel() private void initMiddlePanel() // Initialize and setup Middle Panel { middlePanel.setLayout(new BoxLayout(middlePanel,BoxLayout.X_AXIS)); middlePanel.setBackground(Color.white); middlePanel.add(Box.createRigidArea(new Dimension(10,0))); middlePanel.add(myTotalLabel); // Add my Total Label middlePanel.add(Box.createRigidArea(new Dimension(150,0))); middlePanel.add(anteTotalLabel); // Add ante Total Label middlePanel.add(Box.createRigidArea(new Dimension(150,0))); middlePanel.add(compTotalLabel); // Add computer total label } // initMiddlePanel() private void initLowerPanel() // Initilize and setup Lower Panel { lowerPanel.setLayout(new BorderLayout(0,0)); lowerPanel.setBackground(Color.white); initMyTotalsPanel(); // Initilize and setup my Totals panel initMyPilesPanel(); // Initilize and setup My Piles Panel lowerPanel.add(myTotalsPanel,"North"); // Add these Panels lowerPanel.add(myPilesPanel,"Center"); } // initLowerPanel() private void initMyTotalsPanel() // Initilize and setup my Totals panel { myTotalsPanel.setLayout(new BoxLayout(myTotalsPanel,BoxLayout.X_AXIS)); myTotalsPanel.setBackground(Color.white); myTotalsPanel.add(Box.createRigidArea(new Dimension(70,0))); myTotalsPanel.add(myRedTotalLabel); // Add My total Labels, format with rigid boxes. myTotalsPanel.add(Box.createRigidArea(new Dimension(150,0))); myTotalsPanel.add(myBlueTotalLabel); myTotalsPanel.add(Box.createRigidArea(new Dimension(140,0))); myTotalsPanel.add(myGreenTotalLabel); } // initMyTotalsPanel() private void initMyPilesPanel() // Initialze and Setup my Piles and their individual containing panel, and the panel holding them all. { myPilesPanel.setLayout(new BoxLayout(myPilesPanel,BoxLayout.X_AXIS)); myPilesPanel.setBackground(Color.white); myRedPile.setBackground(Color.white); myBluePile.setBackground(Color.white); myGreenPile.setBackground(Color.white); myRedPile.addMouseListener(this); // Make the piles listen for mouse events. myBluePile.addMouseListener(this); myGreenPile.addMouseListener(this); myRedPilePanel.setLayout(new BoxLayout(myRedPilePanel,BoxLayout.X_AXIS)); // Format Each Pile, and add it to the Panel myRedPilePanel.setBackground(Color.white); myRedPilePanel.add(myRedPile); // Add My Piles myBluePilePanel.setLayout(new BoxLayout(myBluePilePanel,BoxLayout.X_AXIS)); myBluePilePanel.setBackground(Color.white); myBluePilePanel.add(myBluePile); // Add My Piles myGreenPilePanel.setLayout(new BoxLayout(myGreenPilePanel,BoxLayout.X_AXIS)); myGreenPilePanel.setBackground(Color.white); myGreenPilePanel.add(myGreenPile); // Add My Piles myPilesPanel.add(Box.createRigidArea(new Dimension(15,0))); // Add each Pile's Containing Panel to the Pile Panel Holder. myPilesPanel.add(myRedPilePanel); myPilesPanel.add(Box.createRigidArea(new Dimension(20,0))); myPilesPanel.add(Box.createRigidArea(new Dimension(20,0))); myPilesPanel.add(myBluePilePanel); myPilesPanel.add(Box.createRigidArea(new Dimension(20,0))); myPilesPanel.add(Box.createRigidArea(new Dimension(20,0))); myPilesPanel.add(myGreenPilePanel); myPilesPanel.add(Box.createRigidArea(new Dimension(15,0))); } // initMyPilesPanel() //*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//* // Functional Methods //*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//* public void initPiles() throws Exception // Initilize each of my Piles { if ((Integer.parseInt(getParameter("nRedChips")) > 48) || // Check for Too many chips Passed in. (Integer.parseInt(getParameter("nBlueChips")) > 48) || (Integer.parseInt(getParameter("nGreenChips")) > 48)) throw new Exception("The Maximum number allowed is 48.\nThis program will now Terminate"); if ((Integer.parseInt(getParameter("nRedChips")) < 0) || // Check for Too few Chips Passed in. (Integer.parseInt(getParameter("nBlueChips")) < 0) || (Integer.parseInt(getParameter("nGreenChips")) < 0)) throw new Exception("You may not pass a Negative number of Chips from HTML file.\nThis program will now Terminate"); int paramRedChips = Integer.parseInt(getParameter("nRedChips")); // Read Pile Length paramaters int paramBlueChips = Integer.parseInt(getParameter("nBlueChips")); int paramGreenChips = Integer.parseInt(getParameter("nGreenChips")); int maxBidLength = (paramRedChips + paramBlueChips + paramGreenChips); // Sum up the total chips myRedPile.setPileLength(paramRedChips); // Set the Pile's Length myRedPile.setPileID(MYPILE); // Set the Pile's Identifier myRedPile.initPile(RED); // Create Create pile's objects myBluePile.setPileLength(paramBlueChips); myBluePile.setPileID(MYPILE); myBluePile.initPile(BLUE); myGreenPile.setPileLength(paramGreenChips); myGreenPile.setPileID(MYPILE); myGreenPile.initPile(GREEN); myAntePile.setPileLength(maxBidLength); // Big enough for full ante. myAntePile.setPileID(MYANTEPILE); myAntePile.initPile(EMPTY); myAntePile.setPileLength(EMPTY); // Re-Initialize Pile to zero Length compAntePile.setPileLength(maxBidLength); // Big enough for full ante. compAntePile.setPileID(COMPANTEPILE); compAntePile.initPile(EMPTY); compAntePile.setPileLength(EMPTY); // Re-Initialize Pile to zero Length compRedPile.setPileLength(paramRedChips); compRedPile.setPileID(COMPUTERPILE); compRedPile.initPile(RED); compBluePile.setPileLength(paramBlueChips); compBluePile.setPileID(COMPUTERPILE); compBluePile.initPile(BLUE); compGreenPile.setPileLength(paramGreenChips); compGreenPile.setPileID(COMPUTERPILE); compGreenPile.initPile(GREEN); } // initPiles() public void anteAChip(int passedChipColor) // Ante A Chip from one of my Piles { PokerChip tempChip = new PokerChip(); // Temporary Swap pokerchip try { if (passedChipColor == RED) // Ante Red Chip { // Setup Swap Chip for My Chip tempChip.setChipColor(myRedPile.pilePokerChip[myRedPile.getPileLength() - 1].getChipColor()); tempChip.setChipValue(myRedPile.pilePokerChip[myRedPile.getPileLength() - 1].getChipValue()); // Remove My Pile's Chip myRedPile.pilePokerChip[myRedPile.getPileLength() - 1].setChipColor(EMPTY); myRedPile.pilePokerChip[myRedPile.getPileLength() - 1].setChipValue(ZERO_VALUE); myRedPile.setPileLength(myRedPile.getPileLength() - 1); myAntePile.pushAChip(tempChip); // Put My Red chip in the Ante Pile if (myRedPile.getPileLength() == 0) // Check for UI Changes, refresh accordingly. { myRedPile.setPileSelected(false); updateSelectedChip(EMPTY,ZERO_VALUE); } // if else updateSelectedChip(myRedPile.pilePokerChip[myRedPile.getPileLength() - 1].getChipColor(),myRedPile.pilePokerChip[myRedPile.getPileLength() - 1].getChipValue()); // Setup Swap Chip for Computer Chip tempChip.setChipColor(compRedPile.pilePokerChip[compRedPile.getPileLength() - 1].getChipColor()); tempChip.setChipValue(compRedPile.pilePokerChip[compRedPile.getPileLength() - 1].getChipValue()); // Remove Computer Pile's Chip compRedPile.pilePokerChip[compRedPile.getPileLength() - 1].setChipColor(EMPTY); compRedPile.pilePokerChip[compRedPile.getPileLength() - 1].setChipValue(ZERO_VALUE); compRedPile.setPileLength(compRedPile.getPileLength() - 1); compAntePile.pushAChip(tempChip); // Put the Computer's chip in the Ante Pile } // if else if (passedChipColor == BLUE) // Ante Blue Chip { // Setup Swap Chip for My Chip tempChip.setChipColor(myBluePile.pilePokerChip[myBluePile.getPileLength() - 1].getChipColor()); tempChip.setChipValue(myBluePile.pilePokerChip[myBluePile.getPileLength() - 1].getChipValue()); // Remove My Pile's Chip myBluePile.pilePokerChip[myBluePile.getPileLength() - 1].setChipColor(EMPTY); myBluePile.pilePokerChip[myBluePile.getPileLength() - 1].setChipValue(ZERO_VALUE); myBluePile.setPileLength(myBluePile.getPileLength() - 1); myAntePile.pushAChip(tempChip); // Put My Blue chip in the Ante Pile if (myBluePile.getPileLength() == 0) // Check for UI Changes, refresh accordingly. { myBluePile.setPileSelected(false); updateSelectedChip(EMPTY,ZERO_VALUE); } // if else updateSelectedChip(myBluePile.pilePokerChip[myBluePile.getPileLength() - 1].getChipColor(),myBluePile.pilePokerChip[myBluePile.getPileLength() - 1].getChipValue()); // Setup Swap Chip for Computer Chip tempChip.setChipColor(compBluePile.pilePokerChip[compBluePile.getPileLength() - 1].getChipColor()); tempChip.setChipValue(compBluePile.pilePokerChip[compBluePile.getPileLength() - 1].getChipValue()); // Remove Computer Pile's Chip compBluePile.pilePokerChip[compBluePile.getPileLength() - 1].setChipColor(EMPTY); compBluePile.pilePokerChip[compBluePile.getPileLength() - 1].setChipValue(ZERO_VALUE); compBluePile.setPileLength(compBluePile.getPileLength() - 1); compAntePile.pushAChip(tempChip); // Put the Computer's chip in the Ante Pile } // if else if (passedChipColor == GREEN) // Ante Green Chip { // Setup Swap Chip for My Chip tempChip.setChipColor(myGreenPile.pilePokerChip[myGreenPile.getPileLength() - 1].getChipColor()); tempChip.setChipValue(myGreenPile.pilePokerChip[myGreenPile.getPileLength() - 1].getChipValue()); // Remove My Pile's Chip myGreenPile.pilePokerChip[myGreenPile.getPileLength() - 1].setChipColor(EMPTY); myGreenPile.pilePokerChip[myGreenPile.getPileLength() - 1].setChipValue(ZERO_VALUE); myGreenPile.setPileLength(myGreenPile.getPileLength() - 1); myAntePile.pushAChip(tempChip); // Put My Green chip in the Ante Pile if (myGreenPile.getPileLength() == 0) // Check for UI Changes, refresh accordingly. { myGreenPile.setPileSelected(false); updateSelectedChip(EMPTY,ZERO_VALUE); } // if else updateSelectedChip(myGreenPile.pilePokerChip[myGreenPile.getPileLength() - 1].getChipColor(),myGreenPile.pilePokerChip[myGreenPile.getPileLength() - 1].getChipValue()); // Setup Swap Chip for Computer Chip tempChip.setChipColor(compGreenPile.pilePokerChip[compGreenPile.getPileLength() - 1].getChipColor()); tempChip.setChipValue(compGreenPile.pilePokerChip[compGreenPile.getPileLength() - 1].getChipValue()); // Remove Computer Pile's Chip compGreenPile.pilePokerChip[compGreenPile.getPileLength() - 1].setChipColor(EMPTY); compGreenPile.pilePokerChip[compGreenPile.getPileLength() - 1].setChipValue(ZERO_VALUE); compGreenPile.setPileLength(compGreenPile.getPileLength() - 1); compAntePile.pushAChip(tempChip); // Put the Computer's chip in the Ante Pile } // if updateTotalLabels(); // Refresh Total Labels } // try // This Method seriously manipulates main arrays several times // Catch calls to out of bound index values catch (IndexOutOfBoundsException ioobeProblem_anteAChip) { numOfError++; System.out.println("#" + numOfError + (": ") + ioobeProblem_anteAChip.getMessage()); System.out.println("#" + numOfError + (": ") + ioobeProblem_anteAChip.getMessage()); JOptionPane.showMessageDialog(this,ioobeProblem_anteAChip.getMessage(),"Error",JOptionPane.ERROR_MESSAGE); } // catch } // anteAChip() public void prepForChipAnte(int passedPileColor) throws Exception // Pre-Screen User's ability to Ante A chip. { try { // if you clicked a certain pile, the pile isn't empty, and the computer can match your ante if (passedPileColor == RED) { if ((myRedPile.getPileLength() != 0) && (compRedPile.getPileLength() != 0)) { // All conditions are ok to ante the selected chip. anteAChip(passedPileColor); // So Ante it! myRedPile.repaint(); } // if else if (myRedPile.getPileLength() == 0) throw new Exception("Your Red Pile is Empty \"You are out of Red Chips\""); else if (compRedPile.getPileLength() == 0) throw new Exception("ERROR: Computer Red Pile is Empty, \"Can't Match Ante\""); } // if ante Red else if (passedPileColor == BLUE) { if ((myBluePile.getPileLength() != 0) && (compBluePile.getPileLength() != 0)) { anteAChip(passedPileColor); myBluePile.repaint(); } // if else if (myBluePile.getPileLength() == 0) throw new Exception("Your Blue Pile is Empty \"You are out of Red Chips\""); else if (compBluePile.getPileLength() == 0) throw new Exception("Computer Blue Pile is Empty, \"Can't Match Ante\""); } // if ante Blue else if (passedPileColor == GREEN) { if ((myGreenPile.getPileLength() != 0) && (compGreenPile.getPileLength() != 0)) { anteAChip(passedPileColor); myGreenPile.repaint(); } // if else if (myGreenPile.getPileLength() == 0) throw new Exception("Your Green Pile is Empty \"You are out of Green Chips\""); else if (compGreenPile.getPileLength() == 0) throw new Exception("Computer Green Pile is Empty, \"Can't Match Ante\""); } // else myAntePile.repaint(); // Refresh UI compAntePile.repaint(); } // try catch (IndexOutOfBoundsException ioobeProblem) { // Watch for An invalid array manipulation. numOfError++; // Increment Total number of Errors generated. System.out.println("ioob Excption"); System.out.println("#" + numOfError + (": ") + ioobeProblem.getMessage()); JOptionPane.showMessageDialog(this,ioobeProblem.getMessage(),"Error",JOptionPane.ERROR_MESSAGE); } // Catch } // prepForChipAnte() public void unanteChip(int passedDropPile) // Remove a chip from one of the Ante Piles. { try { PokerChip tempChip = new PokerChip(); // Temporary swap chip if (myAntePile.getPileLength() > 0) // Make sure ante Pile isn't empty { tempChip.setChipColor(getTopAnteChipColor()); tempChip.setChipValue(getTopAnteChipValue()); myAntePile.pilePokerChip[myAntePile.getPileLength() - 1].setChipColor(EMPTY); myAntePile.pilePokerChip[myAntePile.getPileLength() - 1].setChipValue(ZERO_VALUE); myAntePile.setPileLength(myAntePile.getPileLength() - 1); if (tempChip.getChipColor() == RED) // Add the chip in the Ante pile to correct computer or user pile { myRedPile.pushAChip(tempChip); // Actually Push the Chip onto the appropriate pile. if (myAntePile.getPileLength() == 0) { myAntePile.setPileSelected(false); // Make changes to UI Information updateSelectedChip(myRedPile.pilePokerChip[myRedPile.getPileLength() - 1].getChipColor(),myRedPile.pilePokerChip[myRedPile.getPileLength() - 1].getChipValue()); } // if else updateSelectedChip(myAntePile.pilePokerChip[myAntePile.getPileLength() - 1].getChipColor(),myAntePile.pilePokerChip[myAntePile.getPileLength() - 1].getChipValue()); } // if else if (tempChip.getChipColor() == BLUE) { myBluePile.pushAChip(tempChip); if (myAntePile.getPileLength() == 0) { myAntePile.setPileSelected(false); updateSelectedChip(myBluePile.pilePokerChip[myBluePile.getPileLength() - 1].getChipColor(),myBluePile.pilePokerChip[myBluePile.getPileLength() - 1].getChipValue()); } // if else updateSelectedChip(myAntePile.pilePokerChip[myAntePile.getPileLength() - 1].getChipColor(),myAntePile.pilePokerChip[myAntePile.getPileLength() - 1].getChipValue()); } // if else if (tempChip.getChipColor() == GREEN) { myGreenPile.pushAChip(tempChip); if (myAntePile.getPileLength() == 0) { myAntePile.setPileSelected(false); updateSelectedChip(myGreenPile.pilePokerChip[myGreenPile.getPileLength() - 1].getChipColor(),myGreenPile.pilePokerChip[myGreenPile.getPileLength() - 1].getChipValue()); } // if else updateSelectedChip(myAntePile.pilePokerChip[myAntePile.getPileLength() - 1].getChipColor(),myAntePile.pilePokerChip[myAntePile.getPileLength() - 1].getChipValue()); } // if else updateSelectedChip(myAntePile.pilePokerChip[myAntePile.getPileLength() - 1].getChipColor(),myAntePile.pilePokerChip[myAntePile.getPileLength() - 1].getChipValue()); } // if if (compAntePile.getPileLength() > 0) // Now remove the top chip from the computer's pile. { tempChip.setChipColor(compAntePile.pilePokerChip[compAntePile.getPileLength() - 1].getChipColor()); tempChip.setChipValue(compAntePile.pilePokerChip[compAntePile.getPileLength() - 1].getChipValue()); compAntePile.pilePokerChip[compAntePile.getPileLength() - 1].setChipColor(EMPTY); compAntePile.pilePokerChip[compAntePile.getPileLength() - 1].setChipValue(ZERO_VALUE); compAntePile.setPileLength(compAntePile.getPileLength() - 1); if (tempChip.getChipColor() == RED) // Add to appropriate pile { compRedPile.pushAChip(tempChip); myRedPile.repaint(); } else if (tempChip.getChipColor() == BLUE) { compBluePile.pushAChip(tempChip); myBluePile.repaint(); } else if (tempChip.getChipColor() == GREEN) { compGreenPile.pushAChip(tempChip); myGreenPile.repaint(); } } // if computer ante isn't empty if (myAntePile.getPileLength() == 0) // Make changes to UI Info. { myAntePile.setPileSelected(false); myAntePile.setPileHot(false); } // if myAntePile.repaint(); // Refresh UI compAntePile.repaint(); } // try catch (IndexOutOfBoundsException ioobeProblem) { // Watch for An invalid array manipulation. numOfError++; // Increment Total number of Errors generated. System.out.println("ioob Excption"); System.out.println("#" + numOfError + (": ") + ioobeProblem.getMessage()); JOptionPane.showMessageDialog(this,ioobeProblem.getMessage(),"Error",JOptionPane.ERROR_MESSAGE); } // Catch } // unanteChip() public void updateTotalLabels() // Get each pile total, and set its Total Label { myRedTotalLabel.setText("Red Pile Total : " + String.valueOf(myRedPile.getPileTotal())); // Set The Text, and get the value from the pile. myBlueTotalLabel.setText("Blue Pile Total : " + String.valueOf(myBluePile.getPileTotal())); myGreenTotalLabel.setText("Green Pile Total : " + String.valueOf(myGreenPile.getPileTotal())); myTotalLabel.setText("All of My Piles Total : " + String.valueOf(myGreenPile.getPileTotal() + myBluePile.getPileTotal() + myRedPile.getPileTotal())); anteTotalLabel.setText("Combined Ante Total : " + String.valueOf(myAntePile.getPileTotal() + compAntePile.getPileTotal())); compTotalLabel.setText("Computer Pile Total : " + String.valueOf(compRedPile.getPileTotal() + compBluePile.getPileTotal() + compGreenPile.getPileTotal())); // Sum of computer's piles } // updateTotalLabels() public void updateSelectedChip(int passedColor, int passedValue) // Refreshes Selected chip status display { chipToAnte.setChipColor(passedColor); // Make Changes to 'mouse-over' chip's variables. chipToAnte.setChipValue(passedValue); chipToAnte.repaint(); } // updateSelectedChip() public int getTopAnteChipColor() // Return the color of the top-most chip on My ante Pile { return (myAntePile.pilePokerChip[myAntePile.getPileLength() - 1].getChipColor()); } // getMyTopAnteChip() public int getTopAnteChipValue() // Return the value of the top-most chip on My ante Pile { return (myAntePile.pilePokerChip[myAntePile.getPileLength() - 1].getChipValue()); } // getMyTopAnteChip() public boolean myPilesSelected() // Checks to see if any of my Piles are selected { return (myRedPile.getPileSelected() || myBluePile.getPileSelected() || myGreenPile.getPileSelected()); } // myPileSelected() public int getWhichPileSelected() // Returns which of my Piles is selected by Color { int selectedPile = EMPTY; if (myRedPile.getPileSelected()) selectedPile = RED; else if (myBluePile.getPileSelected()) selectedPile = BLUE; else if (myGreenPile.getPileSelected()) selectedPile = GREEN; return selectedPile; } // getWhichPileSelcted() public void gameWon(int passedWinner) // There Return Chips in ante pile to winner. { try { // Remove each chip in the ante pile, then check its color, then add to appropriate Pile while (myAntePile.getPileLength() >= 1) { tempChip.setChipColor(getTopAnteChipColor()); // Set up Swap Chip tempChip.setChipValue(getTopAnteChipValue()); myAntePile.pilePokerChip[myAntePile.getPileLength() - 1].setChipColor(EMPTY); myAntePile.pilePokerChip[myAntePile.getPileLength() - 1].setChipValue(ZERO_VALUE); myAntePile.setPileLength(myAntePile.getPileLength() - 1); if (passedWinner == MYANTEPILE) // I won the Game { if (tempChip.getChipColor() == RED) // Add to appropriate pile myRedPile.pushAChip(tempChip); else if (tempChip.getChipColor() == BLUE) myBluePile.pushAChip(tempChip); else if (tempChip.getChipColor() == GREEN) myGreenPile.pushAChip(tempChip); } // if I won else if (passedWinner == COMPANTEPILE) // Computer Won the game :( { if (tempChip.getChipColor() == RED) // Add to appropriate pile compRedPile.pushAChip(tempChip); else if (tempChip.getChipColor() == BLUE) compBluePile.pushAChip(tempChip); else if (tempChip.getChipColor() == GREEN) compGreenPile.pushAChip(tempChip); } // if Computer won } // While there are chips left to remove. // Repeat Ante pile empty job for Computer's pile. while (compAntePile.getPileLength() >= 1) { tempChip.setChipColor(compAntePile.pilePokerChip[compAntePile.getPileLength() - 1].getChipColor()); tempChip.setChipValue(compAntePile.pilePokerChip[compAntePile.getPileLength() - 1].getChipValue()); compAntePile.pilePokerChip[compAntePile.getPileLength() - 1].setChipColor(EMPTY); compAntePile.pilePokerChip[compAntePile.getPileLength() - 1].setChipValue(ZERO_VALUE); compAntePile.setPileLength(compAntePile.getPileLength() - 1); if (passedWinner == MYANTEPILE) // I won the Game { if (tempChip.getChipColor() == RED) // Add to appropriate pile myRedPile.pushAChip(tempChip); else if (tempChip.getChipColor() == BLUE) myBluePile.pushAChip(tempChip); else if (tempChip.getChipColor() == GREEN) myGreenPile.pushAChip(tempChip); } // if I won else if (passedWinner == COMPANTEPILE) // Computer Won the game :( { if (tempChip.getChipColor() == RED) // Add to appropriate pile compRedPile.pushAChip(tempChip); else if (tempChip.getChipColor() == BLUE) compBluePile.pushAChip(tempChip); else if (tempChip.getChipColor() == GREEN) compGreenPile.pushAChip(tempChip); } // if Computer won } // While there are chips left to move // Check for maximal End of Game Scenerios if ((myRedPile.getPileLength() + myGreenPile.getPileLength() + myBluePile.getPileLength()) == 0) { System.out.println("You have lost all of your chips.\nThe computer has won!"); JOptionPane.showMessageDialog(this,"You have lost all of your chips.\nThe computer has won the game.\n Choose 'ReLoad' from the Applet Menu to play again.","Game Over",JOptionPane.PLAIN_MESSAGE); } // if else if ((compRedPile.getPileLength() + compGreenPile.getPileLength() + compBluePile.getPileLength()) == 0) { System.out.println("The Computer has lost all of its chips.\n You have won!"); JOptionPane.showMessageDialog(this,"The Computer has lost all of its chips!\n You have won the game!\n Choose 'ReLoad' from the Applet Menu to play again.","Game Over",JOptionPane.PLAIN_MESSAGE); } // if } // Try catch (IndexOutOfBoundsException ioobeProblem) { // First Try to catch an invalid array Index reference attempt numOfError += 1; System.out.println("ioob Excption"); System.out.println("#" + numOfError + (": ") + ioobeProblem.getMessage()); JOptionPane.showMessageDialog(this,ioobeProblem.getMessage(),"Error",JOptionPane.ERROR_MESSAGE); } catch (Exception eProblem) { // Catch a more general exception numOfError += 1; System.out.println("General exception"); System.out.println("#" + numOfError + (": ") + eProblem.getMessage()); JOptionPane.showMessageDialog(this,eProblem.getMessage(),"Error 001",JOptionPane.ERROR_MESSAGE); } myRedPile.setPileHot(false); // Reset all UI Info. myBluePile.setPileHot(false); myGreenPile.setPileHot(false); myRedPile.setPileSelected(false); myBluePile.setPileSelected(false); myGreenPile.setPileSelected(false); myAntePile.setPileSelected(false); updateTotalLabels(); // Refresh Label Totals repaint(); // Refresh the entire Window } // removeAntePile() class RDie extends JPanel // Special inner class definition for the Dice. { private final static int NFACES = 6; private final static int DOTSIZE = 8; private final static int LEFT = 18; private final static int CENTERX = 30; private final static int CENTERY = 16; private final static int RIGHT = 42; private final static int TOP = 5; private final static int MIDDLE = 16; private final static int BOTTOM = 28; int dieValue = 0; public void initRDie() // Initialize Die values to 'snake eyes' { this.setBackground(Color.white); this.dieValue = 1; this.repaint(); } // initRDie() public void setRandDieValue() // Roll this die { int randomNumber = 0; dieValue = (1 + (int)(Math.random() * NFACES)); this.repaint(); } // setDieValue() public int getDieValue() // Report this Die's Value. { return this.dieValue; } // getDieValue() public void paintComponent(Graphics g) // Of Inner Class { super.paintComponent(g); g.setColor(Color.red); g.fillRoundRect(15,0,40,40,5,5); g.setColor(Color.black); if (this.dieValue == 1) // Depending on Die value, Paint the die. g.fillOval(CENTERX,16,DOTSIZE,DOTSIZE); if (this.dieValue == 2) { g.fillOval(LEFT,TOP,DOTSIZE,DOTSIZE); g.fillOval(RIGHT,BOTTOM,DOTSIZE,DOTSIZE); } // if if (this.dieValue == 3) { g.fillOval(LEFT,5,DOTSIZE,DOTSIZE); g.fillOval(CENTERX,16,DOTSIZE,DOTSIZE); g.fillOval(RIGHT,BOTTOM,DOTSIZE,DOTSIZE); } // if if (this.dieValue == 4) { g.fillOval(LEFT,TOP,DOTSIZE,DOTSIZE); g.fillOval(LEFT,BOTTOM,DOTSIZE,DOTSIZE); g.fillOval(RIGHT,TOP,DOTSIZE,DOTSIZE); g.fillOval(RIGHT,BOTTOM,DOTSIZE,DOTSIZE); } // if if (this.dieValue == 5) { g.fillOval(LEFT,TOP,DOTSIZE,DOTSIZE); g.fillOval(LEFT,BOTTOM,DOTSIZE,DOTSIZE); g.fillOval(RIGHT,TOP,DOTSIZE,DOTSIZE); g.fillOval(RIGHT,BOTTOM,DOTSIZE,DOTSIZE); g.fillOval(CENTERX,CENTERY,DOTSIZE,DOTSIZE); } // if if (this.dieValue == 6) { g.fillOval(LEFT,TOP,DOTSIZE,DOTSIZE); g.fillOval(LEFT,CENTERY,DOTSIZE,DOTSIZE); g.fillOval(LEFT,BOTTOM,DOTSIZE,DOTSIZE); g.fillOval(RIGHT,TOP,DOTSIZE,DOTSIZE); g.fillOval(RIGHT,CENTERY,DOTSIZE,DOTSIZE); g.fillOval(RIGHT,BOTTOM,DOTSIZE,DOTSIZE); } // if } // paintComponenet() } // local inner class RDie public void paintComponent(Graphics g) // Refresh the Pile and Dice Displays { this.setBackground(Color.black); myRedPile.paintComponent(g); // Refresh Piles myBluePile.paintComponent(g); myGreenPile.paintComponent(g); myAntePile.paintComponent(g); compAntePile.paintComponent(g); myDie.paintComponent(g); // Refresh Dice compDie.paintComponent(g); } // paintComponent() //*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//* // Event Handling Methods //*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//* public void mouseEntered(MouseEvent e) // Watch for a mouse entering a pile { if ((e.getSource() == myAntePile) && (!myAntePile.getPileSelected())) // Make sure the ante pile isn't selected { if (!myPilesSelected() && (myAntePile.getPileLength() == 0)) // Update 'mouse-over' status { chipToAnte.setChipColor(EMPTY); chipToAnte.setChipValue(ZERO_VALUE); } // if if (myAntePile.getPileSelected() || !myPilesSelected() && (myAntePile.getPileLength() != 0)) { chipToAnte.setChipColor(getTopAnteChipColor()); // Update 'mouse-over' status chipToAnte.setChipValue(getTopAnteChipValue()); } if (myAntePile.getPileLength() != 0) // Update the Pile's 'hot' status (border color) myAntePile.setPileHot(true); chipToAnte.repaint(); myAntePile.repaint(); } // if // Check for current pile selection, update UI accordingly if (!myPilesSelected() && !myAntePile.getPileSelected()) { if ((e.getSource() == myRedPile) && // Check each pile's selected status, and whether its empty. !(myRedPile.getPileSelected()) && !(myRedPile.getPileLength() == 0)) { chipToAnte.setChipColor(RED); // update 'hot' status accordinly chipToAnte.setChipValue(RED_VALUE); myRedPile.setPileHot(true); chipToAnte.repaint(); myRedPile.repaint(); } // if if ((e.getSource() == myBluePile) && !(myBluePile.getPileSelected()) && !(myBluePile.getPileLength() == 0)) { chipToAnte.setChipColor(BLUE); chipToAnte.setChipValue(BLUE_VALUE); myBluePile.setPileHot(true); chipToAnte.repaint(); myBluePile.repaint(); } // if if ((e.getSource() == myGreenPile) && !(myGreenPile.getPileSelected()) && !(myGreenPile.getPileLength() == 0)) { chipToAnte.setChipColor(GREEN); chipToAnte.setChipValue(myGreenPile.pilePokerChip[myGreenPile.getPileLength() - 1].getChipValue()); myGreenPile.setPileHot(true); myGreenPile.repaint(); chipToAnte.repaint(); } // if } // if } // mouseEntered() public void mouseExited(MouseEvent e) // Watch for a mouse entering a pile { if (e.getSource() == myAntePile && (!myAntePile.getPileSelected())) // Check for Pile's selection status { myAntePile.setPileHot(false); // Update 'hot' status accordingly myAntePile.repaint(); } // IF if ((e.getSource() == myRedPile) && (!myPilesSelected()) && (!myAntePile.getPileSelected())) { chipToAnte.setChipColor(EMPTY); chipToAnte.setChipValue(ZERO_VALUE); myRedPile.setPileHot(false); myRedPile.repaint(); } // if if ((e.getSource() == myBluePile) && (!myPilesSelected()) && (!myAntePile.getPileSelected())) { chipToAnte.setChipColor(EMPTY); chipToAnte.setChipValue(ZERO_VALUE); myBluePile.setPileHot(false); myBluePile.repaint(); } // if if ((e.getSource() == myGreenPile) && (!myPilesSelected()) && (!myAntePile.getPileSelected())) { chipToAnte.setChipColor(EMPTY); chipToAnte.setChipValue(ZERO_VALUE); myGreenPile.setPileHot(false); myGreenPile.repaint(); } // if chipToAnte.repaint(); // Refresh 'mouse-over' status chip. } // mouseExited(MouseEvent e) public void mouseClicked(MouseEvent e) // If the Mouse is clicked, do the following: { try { if (e.getSource() == compAntePile) throw new Exception("You may not Selecte the Computer's Chips!"); // Throw Exception for trying to select an empty Ante pile. else if (e.getSource() == myAntePile) { if ((myAntePile.getPileLength() == 0) && (!myAntePile.getPileSelected()) && (!myPilesSelected())) // If Ante pile is not selected, and Pile is of zero value, (empty.), and None of my piles are selected throw new Exception("Ante Pile is empty!"); // Throw Exception for trying to select an empty Ante pile. else if (((myAntePile.getPileLength() != 0) && !(myPilesSelected()))) // If the ante Pile isn't empty, and If no other Piles are selected myAntePile.setPileSelected(!myAntePile.getPileSelected()); // Toggle Selection else if (!(myAntePile.getPileSelected()) && (myPilesSelected())) // If Ante Pile is unselected, and one of My piles is selected. prepForChipAnte(getWhichPileSelected()); // Ante a chip From My pile that is selected } // if You clicked the Ante pile else if (e.getSource() == myRedPile) // If you clicked the Red Pile { if (myRedPile.getPileLength() != 0) if ((!myPilesSelected()) || (getWhichPileSelected() == RED) ) // And if there aren't any of my other piles Selected, or This Pile is selected. if (!myAntePile.getPileSelected()) // And the Ante Pile isn't Selected. myRedPile.setPileSelected(!myRedPile.getPileSelected()); // Toggle Red Pile on if ((myAntePile.getPileSelected()) && (getTopAnteChipColor() == RED)) // If Ante Pile is selected, and the Chip allowed to un-ante is RED unanteChip(RED); // Un-ante the chip else if ((myAntePile.getPileSelected()) && (getTopAnteChipColor() != RED)) // If Ante Pile is selected, and the top ante Chip is NOT RED throw new Exception("Wrong Chip Color, Try another Pile"); else if ((myRedPile.getPileLength() == 0) && (!myPilesSelected()) && (!myAntePile.getPileSelected())) // If the Pile IS Worthless, (empty), and No other Piles are selected // If you are deselecting Red Pile, or None of my other piles are selected throw new Exception("This Pile Empty, You may Not select an Empty Pile."); // Give error of trying to Selecting an empty pile. else if ((myPilesSelected()) && (getWhichPileSelected() != RED)) // If one of my piles is selected, and it is is Not this Pile throw new Exception("You May Not move Your Chips between your piles"); // Give error of Trying to Move chips between my own Piles (illegal) } // if else if (e.getSource() == myBluePile) // If you clicked the Blue Pile { if (myBluePile.getPileLength() != 0) if ((!myPilesSelected()) || (getWhichPileSelected() == BLUE) ) // And if there aren't any of my other piles Selected, or This Pile is selected. if (!myAntePile.getPileSelected()) // And the Ante Pile isn't Selected. myBluePile.setPileSelected(!myBluePile.getPileSelected()); // Toggle Red Pile on if ((myAntePile.getPileSelected()) && (getTopAnteChipColor() == BLUE)) // If Ante Pile is selected, and the Chip allowed to un-ante is RED unanteChip(BLUE); else if ((myAntePile.getPileSelected()) && (getTopAnteChipColor() != BLUE)) // If Ante Pile is selected, and the top ante Chip is NOT RED throw new Exception("Wrong Chip Color, Try another Pile"); else if ((myBluePile.getPileLength() == 0) && (!myPilesSelected()) && (!myAntePile.getPileSelected())) // If the Pile IS Worthless, (empty), and No other Piles are selected // If you are deselecting Red Pile, or None of my other piles are selected throw new Exception("This Pile Empty, You may Not select an Empty Pile."); // Give error of trying to Selecting an empty pile. else if ((myPilesSelected()) && (getWhichPileSelected() != BLUE)) // If one of my piles is selected, and it is is Not this Pile throw new Exception("You May Not move Your Chips between your piles"); // Give error of Trying to Move chips between my own Piles (illegal) } // if else if (e.getSource() == myGreenPile) // If you clicked the Blue Pile { if (myGreenPile.getPileLength() != 0) if ((!myPilesSelected()) || (getWhichPileSelected() == GREEN) ) // And if there aren't any of my other piles Selected, or This Pile is selected. if (!myAntePile.getPileSelected()) // And the Ante Pile isn't Selected. myGreenPile.setPileSelected(!myGreenPile.getPileSelected()); // Toggle Red Pile on if ((myAntePile.getPileSelected()) && (getTopAnteChipColor() == GREEN)) // If Ante Pile is selected, and the Chip allowed to un-ante is RED unanteChip(GREEN); // Un-ante the chip else if ((myAntePile.getPileSelected()) && (getTopAnteChipColor() != GREEN)) // If Ante Pile is selected, and the top ante Chip is NOT RED throw new Exception("Wrong Chip Color, Try another Pile"); else if ((myGreenPile.getPileLength() == 0) && (!myPilesSelected()) && (!myAntePile.getPileSelected())) // If the Pile IS Worthless, (empty), and No other Piles are selected // If you are deselecting Red Pile, or None of my other piles are selected throw new Exception("This Pile Empty, You may Not select an Empty Pile."); // Give error of trying to Selecting an empty pile. else if ((myPilesSelected()) && (getWhichPileSelected() != GREEN)) // If one of my piles is selected, and it is is Not this Pile throw new Exception("You May Not move Your Chips between your piles"); // Give error of Trying to Move chips between my own Piles (illegal) } // if } // try catch (Exception problem) { // Watch for illegal chip move attempts by user attempted numOfError += 1; System.out.println("#" + numOfError + (": ") + problem.getMessage()); JOptionPane.showMessageDialog(this,problem.getMessage(),"Error 003",JOptionPane.ERROR_MESSAGE); } // catch() } // mousePressed() public void actionPerformed(ActionEvent e) // Listen for events, take actions { try { if ((e.getSource() == rollDiceButton) && (myAntePile.getPileLength() != 0)) // Check for for non zero ante piles. { myDie.initRDie(); // Initialize dice compDie.initRDie(); while (myDie.getDieValue() == compDie.getDieValue()) // Roll dice until they aren't equal { myDie.setRandDieValue(); if ((myDie.getDieValue() < 1) || (myDie.getDieValue() > 6)) throw new Exception("Invalid Die Value! Check NFACES"); // Someone has messed with the Die values. compDie.setRandDieValue(); if ((compDie.getDieValue() < 1) || (compDie.getDieValue() > 6)) throw new Exception("Invalid Die Value! Check NFACES"); myDie.repaint(); compDie.repaint(); } // while the die's values aren't equal (the Roll isn't a tie) if ((compDie.getDieValue()) < (myDie.getDieValue())) // Check for winner { JOptionPane.showMessageDialog( this, // Display results "You Win!\n Computer has:\n" + (compRedPile.getPileLength()) + " Red Chips left,\n" + (compBluePile.getPileLength()) + " Blue Chips left,\n" + (compGreenPile.getPileLength()) + " Green Chips left.", "Congratulations",JOptionPane.PLAIN_MESSAGE); gameWon(MYANTEPILE); // Move the ante pile to winner's piles. } else { JOptionPane.showMessageDialog( this, "You Lose!!\n You have:\n" + (myRedPile.getPileLength()) + " Red Chips left,\n" + (myBluePile.getPileLength()) + " Blue Chips left,\n" + (myGreenPile.getPileLength()) + " Green Chips left.", "Sorry...",JOptionPane.PLAIN_MESSAGE); gameWon(COMPANTEPILE); } myRedPile.setPileSelected(false); // Update UI information. myBluePile.setPileSelected(false); myGreenPile.setPileSelected(false); } // if else if ((e.getSource() == rollDiceButton) && (myAntePile.getPileLength() == 0)) throw new Exception("You must Ante first!"); } // try catch (Exception problem) { // Catch die errors. repaint(); numOfError += 1; System.out.println("#" + numOfError + (": ") + problem.getMessage()); JOptionPane.showMessageDialog(this,problem.getMessage(),"Error 002",JOptionPane.ERROR_MESSAGE); } // catch } // actionPerformed(1) } // PokerChipsApplet.java *End of File*