Simple Slot Machine

SAMPLE APPLET:

INFORMATION: This a really simple game with a REALLY simple scoring mechanism. You will get to see the full source code for this assignment, but you will have to get get your own pictures!!! You pics should all be set to 90x120. The pictures were used in this applet were taken with a digital camera and then fixed up in GIMP.

Inspect the souce code carefully!!! Notice the use of the Random class to generate Random numbers. Also carefully inspect the makeSet and scoreSet methods. Notice what happens when the total score exceeds 99. Be ready to answer questions about how the code works when you are ready to show your work to your instructor.

The aspect of this game in most need of improvement is the scoring mechanism. Be ready to explain how scoring works in this sample and how this could be improved to be more similar to the way real slot machines work.

import java.awt.*; import java.applet.*; import java.awt.event.*; import java.util.*; public class slot extends Applet{ Image offI; Graphics offG; int c; //current image String[] pic = {"pumpkin.jpg", "guitar.jpg", "kenny.jpg", "penguin.jpg", "flag.jpg"}; Image[] set = new Image[5]; Image[] bank = new Image[5]; Dimension d; Random r; int score, total; int[] SCORE= {0,0,0,0,0}; public void init(){ setBackground(Color.blue); d=getSize(); for(int i=0; i<bank.length; i++){ bank[i]=getImage(getDocumentBase(), pic[i]); } makeSet(); offI=createImage(d.width, d.height); requestFocus(); this.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent k){ if(total<99) makeSet(); repaint(); } }); } public void update(Graphics g){ paint(g); } public void makeSet(){ r = new Random(); int x; for(int i=0; i<bank.length; i++){ x = Math.abs(r.nextInt()%bank.length); set[i]=bank[x]; SCORE[i]=x; } scoreSet(); } public void scoreSet(){ score=0; for(int i=0; i<SCORE.length; i++){ score+=SCORE[i]; } total+=score; } public void paint(Graphics g){ offG=offI.getGraphics(); offG.setColor(getBackground()); offG.fillRect(0,0,d.width, d.height); if(total<100){ for(int i=0; i<bank.length; i++){ offG.drawImage(set[i],10+i*100,35,this); } } else{ for(int i=0; i<bank.length; i++){ offG.drawImage(bank[4], 10+i*100, 35, this); } } offG.setColor(Color.white); offG.setFont(new Font("Serif", Font.BOLD, 32)); offG.drawString("Simple Slot Machine", 110, 26); if(total<100){ offG.setFont(new Font("Serif", Font.PLAIN, 12)); offG.drawString("SCORE: "+score, 10, 170); offG.drawString("TOTAL: "+total, 10, 190); offG.drawString("Press any key for another round", 10, 210); } else{ offG.drawString("WINNER!!!", 160, 200); } g.drawImage(offI,0,0,this); offG.dispose(); } }