/* * A simple slot machine game. * Feel free to re-use any part of this code. * author * Jamie M. Hall, jamihall@mhv.net 1/9/96 * Web page: http://www1.mhv.net/~jamihall * Email: jamihall@mhv.net --> */ import java.applet.*; import java.awt.*; public class Slot extends Applet implements Runnable { Image displayLeft, displayCenter, displayRight, slotImages[]; Label score; boolean spinMe = false; Thread animator = null; int spinSequence = 0, endSequence = 0; public void init() { int i, slotImagesLen = 0; Panel p, q, r; Button spin; setFont(new Font("TimesRoman", Font.BOLD, 24)); setBackground(Color.cyan); setForeground(Color.magenta); setLayout(new BorderLayout()); slotImages = new Image[4]; for (i = 1; i < 5; i++) { Image im = getImage(getCodeBase(), "T" + i + ".gif"); if (im == null) { break; } slotImages[slotImagesLen++] = im; } add("South", p = new Panel()); p.setLayout(new BorderLayout()); p.add("North", new Label("")); p.add("Center", q = new Panel()); q.setLayout(new FlowLayout()); q.add(spin = new Button(" Spin ")); spin.setBackground(Color.blue); p.add("South", r = new Panel()); r.setLayout(new FlowLayout()); r.add(new Label("Total:")); r.add(score = new Label()); score.setForeground(Color.red); newGame(); } public void start () { if (animator == null) { animator = new Thread(this); animator.start(); } } public void stop() { if (animator != null) { animator.stop(); animator = null; } } public void run () { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while (size().width > 0 && size().height > 0 && animator != null) { repaint(); try { Thread.sleep(300); } catch (InterruptedException e) {}; } } public void update(Graphics g) { int i; if (Integer.valueOf((score.getText()).trim()).intValue() < 1) { endGame(g); } else if (spinMe) { spinAnimate(); paint(g); } else { paint(g); } } public void paint(Graphics g) { g.drawImage(displayLeft, (int)(size().width * .05), (int)(size().height * .1), ((size().width/3)-30), (int)(size().height * .5), this); g.drawImage(displayCenter, (int)(size().width * .35), (int)(size().height * .1), ((size().width/3)-30), (int)(size().height * .5), this); g.drawImage(displayRight, (int)(size().width * .65), (int)(size().height * .1), ((size().width/3)-30), (int)(size().height * .5), this); } public boolean action(Event evt, Object arg) { if (evt.target instanceof Button) spinMe = true; return true; } public void newGame() { displayLeft = slotImages[0]; displayCenter = slotImages[0]; displayRight = slotImages[0]; score.setText(" 10 "); } public void endGame(Graphics g) { if (++endSequence < 4) { g.clearRect(0, 0, size().width, size().height); g.drawString("Sorry", (size().width - 75)/2, (size().height - 50)/2); } else if (endSequence % 2 == 0) { g.clearRect(0, 0, size().width, size().height); g.drawString("You Lose!", (int)((Math.random() * 1000) % (size().width - 130)), (int)(((Math.random() * 1000) % (size().height - 140)) + 25)); } else if (endSequence == 41) { g.clearRect(0, 0, size().width, size().height); endSequence = 0; newGame(); } } public void doSpin() { int newImage, special = 0; newImage = (int)(((Math.random() * 1000) % 3) + .7) ; displayLeft = slotImages[newImage]; if (displayLeft == slotImages[0]) { special += 1; } newImage = (int)(((Math.random() * 1000) % 3) + .7); displayCenter = slotImages[newImage]; if (displayCenter == slotImages[0]) { special += 1; } newImage = (int)(((Math.random() * 1000) % 3) + .7); displayRight = slotImages[newImage]; if (displayRight == slotImages[0]) { special += 1; } if (special == 3) { score.setText(String.valueOf(Integer.valueOf((score.getText()).trim()).intValue() + 1000)); spinWin(1000); } else if (special == 2) { score.setText(String.valueOf(Integer.valueOf((score.getText()).trim()).intValue() + 25)); spinWin(25); } else if ((displayLeft == displayCenter) && (displayLeft == displayRight)) { score.setText(String.valueOf(Integer.valueOf((score.getText()).trim()).intValue() + 5)); spinWin(5); } else { score.setText(String.valueOf(Integer.valueOf((score.getText()).trim()).intValue() - 1)); getAppletContext().showStatus("Sorry, you lose"); } } public void spinAnimate() { int newImage; int i; newImage = (int)(((Math.random() * 1000) % 3) + .7) ; displayLeft = slotImages[newImage]; newImage = (int)(((Math.random() * 1000) % 3) + .7); displayCenter = slotImages[newImage]; newImage = (int)(((Math.random() * 1000) % 3) + .7); displayRight = slotImages[newImage]; if (++spinSequence > 2) { spinMe = false; spinSequence = 0; doSpin(); } } public void spinWin(int points) { getAppletContext().showStatus("You win " + Integer.toString(points) + " points - mazel tov!"); } public String getAppletInfo() { return "Java Virtual Slot Machine by Jamie M. Hall, 1996"; } }