import java.awt.*; import java.applet.*; import java.util.*; import java.awt.event.*; import java.lang.String; public class DiceGame extends Applet { Graphics offScreenG; Image offScreenImg, backImg, chipImg, chipImgPlaced; Image[] diceImage; Font small = new Font("Times New Roman", Font.BOLD, 20); Font medium = new Font("Times New Roman", Font.BOLD, 35); Font big = new Font("Times New Roman", Font.BOLD, 50); int X, Y; Choice GameChoice; Button Reset; int[] chipX; int[] chipY; int[] chipPlaced; boolean rolling; boolean gameOver; boolean printErrorMessage; int selectedChip; int[] numChips; int game; int dice1; int dice2; int diceTotal; int numRolls; int totalNumChips; int[] simResults; int[] graphResults; int numRuns; int totalNumRolls; int averageNumRolls; boolean simGameOver; int startPix; public void init() { backImg = getImage(getCodeBase(),"back.gif"); chipImg = getImage(getCodeBase(),"chip2.gif"); chipImgPlaced = getImage(getCodeBase(), "chip.gif"); offScreenImg = createImage(this.size().width, this.size().height); offScreenG = offScreenImg.getGraphics(); repaint(); setLayout(null); GameChoice = new Choice(); GameChoice.add("Play The Game"); GameChoice.add("Simulate 10 Games"); GameChoice.add("Simulate 100 Games"); GameChoice.add("Simulate 1000 Games"); GameChoice.setBounds(15, 65, 150, 30); add(GameChoice); Reset = new Button(); Reset.setLabel("Reset"); Reset.setBounds(530, 340, 75, 25); add(Reset); chipX = new int[10]; chipY = new int[10]; chipPlaced = new int[10]; diceImage = new Image[6]; numChips = new int[11]; simResults = new int[1000]; graphResults = new int[20]; rolling = false; selectedChip = -1; game = 1; startPix = 0; for (int i = 0; i < 10; i++) { chipX[i] = 263; chipY[i] = 11; chipPlaced[i] = 0; numChips[i] = 0; } numChips[10] = 0; diceImage[0] = getImage(getCodeBase(),"dice1.gif"); diceImage[1] = getImage(getCodeBase(),"dice2.gif"); diceImage[2] = getImage(getCodeBase(),"dice3.gif"); diceImage[3] = getImage(getCodeBase(),"dice4.gif"); diceImage[4] = getImage(getCodeBase(),"dice5.gif"); diceImage[5] = getImage(getCodeBase(),"dice6.gif"); } public void paint (Graphics g){ offScreenG.drawImage(backImg,0,0,this); g.setFont(small); g.setColor(new Color(255, 255, 255)); g.drawImage(offScreenImg,0,0,this); for (int i = 0; i < 10; i++) if (chipPlaced[i] > 0) g.drawImage(chipImgPlaced, chipX[i], chipY[i], this); else g.drawImage(chipImg, chipX[i], chipY[i], this); if (rolling) { g.drawImage(diceImage[dice1], 385, 20, this); g.drawImage(diceImage[dice2], 430, 20, this); g.drawString("Rolls: " + numRolls, 388, 80); } for (int i = 0; i < 11; i++) { g.setColor(new Color(1, 1, 203)); if (numChips[i] > 0 && numChips[i] < 10) { if (i % 2 == 0) g.drawString("" + numChips[i], (i/2)*95 + 62, 184); else g.drawString("" + numChips[i], ((i-1)/2)*95 + 112, 280); } else if (numChips[i] == 10) if (i % 2 == 0) g.drawString("" + numChips[i], (i/2)*95 + 56, 184); else g.drawString("" + numChips[i], ((i-1)/2)*95 + 106, 280); } if (gameOver) { g.setFont(big); g.setColor(Color.white); g.fillRect(0, 135, 609, 150); g.setColor(new Color(203, 12, 13)); g.fillRect(0, 130, 609, 10); g.fillRect(0, 280, 609, 10); g.setColor(new Color(1, 1, 203)); g.drawString("You used " + numRolls, 20, 200); g.drawString("rolls to clear your chips!", 20, 250); } if (simGameOver) { g.setFont(big); g.setColor(Color.white); g.fillRect(0, 135, 609, 150); g.setColor(new Color(203, 12, 13)); g.fillRect(0, 130, 609, 10); g.fillRect(0, 280, 609, 10); g.setColor(new Color(1, 1, 203)); g.drawString("You averaged " + averageNumRolls, 20, 200); g.drawString("rolls to clear your chips!", 20, 250); } if (printErrorMessage) { g.setFont(medium); g.setColor(Color.white); g.fillRect(0, 165, 609, 75); g.setColor(new Color(203, 12, 13)); g.fillRect(0, 160, 609, 10); g.fillRect(0, 235, 609, 10); g.setColor(new Color(1, 1, 203)); g.drawString("Place all 10 chips before rolling.", 35, 215); } } public void rollDice() { if (totalNumChips == 10 || rolling) { if (GameChoice.getSelectedItem() == "Play The Game") { numRolls++; rolling = true; dice1 = (int)(Math.random()*6); dice2 = (int)(Math.random()*6); diceTotal = dice1 + dice2 + 2; for (int i = 0; i < 10; i++) if (chipPlaced[i] == diceTotal) { chipX[i] = 263; chipY[i] = 11; chipPlaced[i] = 0; numChips[diceTotal - 2]--; i = 10; totalNumChips--; if (totalNumChips == 0) gameOver = true; } } else if (GameChoice.getSelectedItem() == "Simulate 10 Games") runSim(10); else if (GameChoice.getSelectedItem() == "Simulate 100 Games") runSim(100); else if (GameChoice.getSelectedItem() == "Simulate 1000 Games") runSim(1000); } else printErrorMessage = true; } public void runSim(int runs) { numRuns = runs; int[] tempChipPlaced = new int[10]; int[] tempNumChips = new int[11]; for (int i = 0; i < runs; i++) { totalNumChips = 10; numRolls = 0; for (int j = 0; j < 10; j++) { tempChipPlaced[j] = chipPlaced[j]; tempNumChips[j] = numChips[j]; } tempNumChips[10] = numChips[10]; while (totalNumChips != 0) { numRolls++; dice1 = (int)(Math.random()*6); dice2 = (int)(Math.random()*6); diceTotal = dice1 + dice2 + 2; for (int k = 0; k < 10; k++) if (tempChipPlaced[k] == diceTotal) { tempChipPlaced[k] = 0; tempNumChips[diceTotal - 2]--; k = 10; totalNumChips--; if (totalNumChips == 0) { simResults[i] = numRolls; totalNumRolls += numRolls; } } } } averageNumRolls = totalNumRolls/runs; simGameOver = true; } public void update (Graphics g) { paint(g); } public boolean mouseDown(Event evt, int x, int y){ printErrorMessage = false; if (!gameOver && !simGameOver) { int X = x; int Y = y; if (X >= 490 && X<=580 && Y >=17 && Y<=91) { rollDice(); } if (!rolling) down(x,y); repaint(); } return true; } public boolean mouseDrag(Event evt, int x, int y){ if (!rolling && !gameOver && !simGameOver) { drag(x,y); repaint(); } return true; } public boolean mouseUp(Event evt, int x, int y){ if (!rolling && !gameOver && !simGameOver) { up(x,y); repaint(); } return true; } public void down(int x, int y){ X = x; Y = y; for (int i = 0; i < 10; i++) if ( (X - chipX[i] > 0) && (X - chipX[i] < 53) && (Y - chipY[i] > 0) && (Y - chipY[i] < 53)) { selectedChip = i; i = 10; } } public void drag(int x, int y){ X = x; Y = y; if (selectedChip >= 0 && selectedChip < 10) { chipX[selectedChip] = X-26; chipY[selectedChip] = Y-26; } } public void up(int x, int y){ if (selectedChip >= 0 && selectedChip < 10) { if (Math.abs(chipY[selectedChip] - 120) < Math.abs(chipY[selectedChip] - 280) ) { if (chipPlaced[selectedChip] > 0) { numChips[chipPlaced[selectedChip]-2]--; totalNumChips--; } chipY[selectedChip] = 151; chipX[selectedChip] = ((x-40)/95)*95 + 41; chipPlaced[selectedChip] = ((x-40)/95 + 1)*2; numChips[chipPlaced[selectedChip]-2]++; totalNumChips++; } else { if (chipPlaced[selectedChip] > 0) { numChips[chipPlaced[selectedChip]-2]--; totalNumChips--; } chipY[selectedChip] = 247; chipX[selectedChip] = ((x-90)/95)*95 + 91; chipPlaced[selectedChip] = ((x-90)/95)*2 + 3; numChips[chipPlaced[selectedChip]-2]++; totalNumChips++; } selectedChip = -1; } } public void Reset(){ gameOver = false; rolling = false; selectedChip = -1; for (int i = 0; i < 10; i++) { chipX[i] = 263; chipY[i] = 11; chipPlaced[i] = 0; numChips[i] = 0; } numChips[10] = 0; dice1 = dice2 = diceTotal = 0; numRolls = 0; totalNumChips = 0; int numRuns = 0; totalNumRolls = 0; averageNumRolls = 0; simGameOver = false; repaint(); } public boolean action(Event evt, Object arg) { if (arg.equals("Reset")) Reset(); return true; } }