Forums Register Login

Slot Machine

+Pie Number of slices to send: Send
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
setSize(450,300);
setLayout(null);

nameField=new TextField ("",20);
add (nameField);
nameField.addActionListener(this);
nameField.setBounds(90,50,100,20);

tester = new Button("Press here");
add(tester);
tester.addActionListener(this);
tester.setBounds(200,50,70,20);

slider = new Scrollbar (Scrollbar.HORIZONTAL, 0, 1, 0, 100);
add(slider);
slider.addAdjustmentListener(this);
slider.setBounds(90,90,70,20);
}//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="win";
else
result = "sorry you lose" ;

}//end of if statement

repaint();
}//end of action performed

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

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

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

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

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

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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Please don't post the same question twice.
+Pie Number of slices to send: Send
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.
Enjoy the full beauty of the english language. Embedded in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1004 times.
Similar Threads
Problem with writing code.
Need help with drawing in a JPanel
help on Yahtzee
My Games Flashing!!
Custom AWT Component not rendering more than one instance
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:59:23.