File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Slot Machine Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Slot Machine" Watch "Slot Machine" New topic

Slot Machine

ant williams

Joined: Mar 20, 2007
Posts: 4
I have been given a piece of coursework to do using random numbers. I have decided to do a slot machine for it. I am having trouble and would like help so that the result either adds 2 pound on to there money if they win or takes one off if they lose. I also would like a loop so that when they have no money left they can not play.

//Slot machine Assignment

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class Slot extends Applet implements ActionListener, AdjustmentListener {

private TextField nameField; //start of attributes
private String name="", result="";
private Button tester;
private int count = 0;
private Scrollbar slider;
private int sliderValue = 0;
private int die, die2, die3;
private int money; //end of attributes

public void init() { //start of init

nameField=new TextField ("",20);
add (nameField);

tester = new Button("Press here");

slider = new Scrollbar (Scrollbar.HORIZONTAL, 0, 1, 0, 100);
}//end of init

public void actionPerformed(ActionEvent event) { //start of action performed
name = nameField.getText();
if (event.getSource()==tester){//start of if statement
die = (int)(Math.random() * 3)+1;
die2 = (int)(Math.random() * 3)+1;
die3 = (int)(Math.random() * 3)+1;
if ((die == die2)&&(die == die3))
result = "sorry you lose" ;

}//end of if statement

}//end of action performed

public void adjustmentValueChanged (AdjustmentEvent e) {// start of changed adjustment value
sliderValue =slider.getValue();
money = sliderValue;
}//end of changed adjustment value

public void paint(Graphics g){//start of paint

g.drawRect(100,170,40,40); // draw a green rectangle

g.drawRect(150,170,40,40); // draw a red rectangle

g.drawRect(200,170,40,40); // draw a yellow rectangle

g.setColor(; // set colour of text to black
g.drawString("Slot Machine Assignment",50,10);
g.drawString("Please input your name in the field below",40,40);
g.drawString("Hello " + name, 100, 130);
g.drawString("The amount you started with is �"+ sliderValue, 100, 150);
g.drawString("The amount you have now is �"+ money, 100, 270);
g.drawString("" + die, 120, 190);
g.drawString("" + die2, 170, 190);
g.drawString("" + die3, 220, 190);
g.drawString (result, 100, 250);


Any help you could give would be greatly appreciated
carl sjostrom

Joined: Jan 04, 2006
Posts: 7
It looks to me like you are doing real good, so I don't understand what you are having problems with. Adding two and subtracting one seems so simple, maybe I am missing something?

Also you may want to use code tags to make your code easier to read.

[ March 20, 2007: Message edited by: carl sjostrom ]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46362
Please don't post the same question twice.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

Do not post the same question in more than one forum. You now have two people trying to help you, who may not be aware of what the other is doing. It wastes at least one persons time.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: Slot Machine
It's not a secret anymore!