/* Pawel Olas Poker Chip Applet 2 (Program 5) Fully functional. You can select one chip at a time from the piles (any chip - important for the green ones which have different values) and then a box pops up around your center pile allowing you to move the chip. You can then move */ import javax.swing.*; import java.awt.*; import java.lang.Math; import java.awt.event.*; import java.applet.Applet; public class PokerChipsApplet extends JApplet implements ActionListener, MouseListener { Pile redPile=new Pile(); Pile bluePile=new Pile(); Pile greenPile=new Pile(); Pile myCenterPile=new Pile(); Pile computerCenterPile= new Pile(); Pile selectedPile=new Pile(); JPanel topPanel=new JPanel(); JPanel centerPanel=new JPanel(); JPanel bottomPanel=new JPanel(); JPanel dicePanel= new JPanel(); rollPanel myRollPanel=new rollPanel(); rollPanel computerRollPanel=new rollPanel(); int myDie=0; int computerDie=0; private JButton rollDice=new JButton("Roll the Dice!"); int panelWidth; int panelHeight; int centerWidth; Dimension panelSize=new Dimension(); Dimension centerSize=new Dimension(); int movedChipNumber=-1; Color movedChipColor=new Color(0); int movedChipValue; boolean isChipSelected=false; int selx=0; int sely=0; public void init() { redPile=new Pile(getParameter("nRedChips"), Color.red, 100); bluePile=new Pile(getParameter("nBlueChips"), Color.blue, 500); greenPile=new Pile(getParameter("nGreenChips"), Color.green, -1); Dimension frameSize=this.getSize(); panelWidth=(int)frameSize.getWidth()/3; panelHeight=(int)frameSize.getHeight()/2; panelSize=new Dimension(panelWidth, panelHeight); dicePanel.setLayout(new GridLayout(1,3,15,15)); topPanel.setLayout(new BorderLayout()); centerPanel.setLayout(new GridLayout(1,2,1,1)); bottomPanel.setLayout(new GridLayout(1,3,1,1)); getContentPane().setLayout(new GridLayout(2,1,1,1)); dicePanel.add(myRollPanel); dicePanel.add(rollDice); dicePanel.add(computerRollPanel); centerPanel.add(myCenterPile); centerPanel.add(computerCenterPile); topPanel.add(centerPanel, "Center"); topPanel.add(dicePanel, "North"); bottomPanel.add(redPile); bottomPanel.add(bluePile); bottomPanel.add(greenPile); getContentPane().add(topPanel); getContentPane().add(bottomPanel); redPile.addMouseListener(this); bluePile.addMouseListener(this); greenPile.addMouseListener(this); myCenterPile.addMouseListener(this); rollDice.addActionListener(this); } public void paintComponent( Graphics g ) { //g.drawString(x + " " + y, 10, 10); } public void actionPerformed(ActionEvent e){ myDie=myRollPanel.roll(); computerDie=computerRollPanel.roll(); while(myDie==computerDie){ myDie=myRollPanel.roll(); computerDie=computerRollPanel.roll(); } if(myDie>computerDie){ movedChipNumber=myCenterPile.getnChips(); while(movedChipNumber != -1){ movedChipColor=myCenterPile.getChipColor(movedChipNumber); movedChipValue=myCenterPile.getChipValue(movedChipNumber); if(movedChipValue==100) redPile.addChip(movedChipColor, movedChipValue); if(movedChipValue==500) bluePile.addChip(movedChipColor, movedChipValue); if(movedChipValue!=100&&movedChipValue!=500) greenPile.addChip(movedChipColor, movedChipValue); myCenterPile.removeChip(); movedChipNumber=myCenterPile.getnChips(); } movedChipNumber=computerCenterPile.getnChips(); while(movedChipNumber != -1){ movedChipColor=computerCenterPile.getChipColor(movedChipNumber); movedChipValue=computerCenterPile.getChipValue(movedChipNumber); if(movedChipValue==100) redPile.addChip(movedChipColor, movedChipValue); if(movedChipValue==500) bluePile.addChip(movedChipColor, movedChipValue); if(movedChipValue!=100&&movedChipValue!=500) greenPile.addChip(movedChipColor, movedChipValue); computerCenterPile.removeChip(); movedChipNumber=computerCenterPile.getnChips(); } } if(myDie-1){ movedChipColor=redPile.getChipColor(movedChipNumber); movedChipValue=redPile.getChipValue(movedChipNumber); isChipSelected=true; selx=x; sely=y; myCenterPile.enclosePile(true); }//if movedChipNumber>-1 }//if x-1){ //has end movedChipColor=bluePile.getChipColor(movedChipNumber); movedChipValue=bluePile.getChipValue(movedChipNumber); isChipSelected=true; selx=x; sely=y; myCenterPile.enclosePile(true); }//if movedChipNumber>-1 }//if x <( redPileSize.getWidth() + bluePileSize.getWidth() ) if(e.getSource()==greenPile){ selectedPile=greenPile; greenPile.clickAt(x,y); movedChipNumber=greenPile.getClickedChip(); if(movedChipNumber>-1){ //has end movedChipColor=greenPile.getChipColor(movedChipNumber); movedChipValue=greenPile.getChipValue(movedChipNumber); isChipSelected=true; selx=x; sely=y; myCenterPile.enclosePile(true); }//if }//if greenPile if(e.getSource()==myCenterPile){ selectedPile=myCenterPile; myCenterPile.clickAt(x,y); movedChipNumber=myCenterPile.getClickedChip(); if(movedChipNumber>-1){ movedChipColor=myCenterPile.getChipColor(movedChipNumber); movedChipValue=myCenterPile.getChipValue(movedChipNumber); isChipSelected=true; selx=x; sely=y; redPile.enclosePile(true); greenPile.enclosePile(true); bluePile.enclosePile(true); }//if } } //continues if isChipSelected is not false else { if(e.getSource()==redPile){ if(selectedPile==redPile){ redPile.clickAt(x,y); if(redPile.getClickedChip()==-1){ selectedPile.clickAt(selx, sely); isChipSelected=false; myCenterPile.enclosePile(false); } } if(selectedPile==myCenterPile){ if(movedChipValue==100){ myCenterPile.clickAt(selx, sely); redPile.addChip(movedChipColor, movedChipValue); myCenterPile.removeChip(movedChipNumber); computerCenterPile.removeChip(movedChipNumber); isChipSelected=false; movedChipNumber=-1; redPile.enclosePile(false); greenPile.enclosePile(false); bluePile.enclosePile(false);} } }//if selected==redPile if(e.getSource()==bluePile){ if(selectedPile==bluePile){ bluePile.clickAt(x,y); if(bluePile.getClickedChip()==-1){ isChipSelected=false; myCenterPile.enclosePile(false); } }//if selected==bluePile if(selectedPile==myCenterPile){ if(movedChipValue==500){ myCenterPile.clickAt(selx, sely); bluePile.addChip(movedChipColor, movedChipValue); myCenterPile.removeChip(movedChipNumber); computerCenterPile.removeChip(movedChipNumber); isChipSelected=false; movedChipNumber=-1; redPile.enclosePile(false); greenPile.enclosePile(false); bluePile.enclosePile(false); } } } //if source==bluePile if(e.getSource()==greenPile){ if(selectedPile==greenPile){ greenPile.clickAt(x,y); if(greenPile.getClickedChip()==-1){ isChipSelected=false; myCenterPile.enclosePile(false); } }//if selected==greenPile if(selectedPile==myCenterPile){ if(movedChipValue!=100&&movedChipValue!=500){ myCenterPile.clickAt(selx, sely); greenPile.addChip(movedChipColor, movedChipValue); myCenterPile.removeChip(movedChipNumber); computerCenterPile.removeChip(movedChipNumber); isChipSelected=false; movedChipNumber=-1; redPile.enclosePile(false); greenPile.enclosePile(false); bluePile.enclosePile(false); } } }//if source==greenPile if(e.getSource()==myCenterPile&&movedChipNumber!=-1){ if(selectedPile!=myCenterPile){ selectedPile.clickAt(selx, sely); myCenterPile.addChip(movedChipColor, movedChipValue); computerCenterPile.addChip(movedChipColor, movedChipValue); selectedPile.removeChip(movedChipNumber); isChipSelected=false; myCenterPile.enclosePile(false); movedChipNumber=-1; } else { selectedPile.clickAt(selx,sely); redPile.enclosePile(false); bluePile.enclosePile(false); greenPile.enclosePile(false); isChipSelected=false; } } } repaint(); }//mousePressed }