aspose file tools*
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
Author

Slot Machine

ant williams
Greenhorn

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
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
carl sjostrom
Greenhorn

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.

see: http://faq.javaranch.com/view?UseCodeTags
[ March 20, 2007: Message edited by: carl sjostrom ]
Campbell Ritchie
Sheriff

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

Joined: Oct 02, 2003
Posts: 11422
    
  16

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: http://aspose.com/file-tools
 
subject: Slot Machine