Forums Register Login
Slot Machine
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(Color.black); // 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
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.

see: http://faq.javaranch.com/view?UseCodeTags
[ March 20, 2007: Message edited by: carl sjostrom ]
Please don't post the same question twice.
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.
I AM MIGHTY! Especially when I hold this tiny ad:
Why should you try IntelliJ IDEA ?

This thread has been viewed 773 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 18, 2018 02:37:14.