import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @author sneak *
*/
public class DiceRoller extends Applet implements ActionListener{
// We need 2 dice
private Die d1,d2;
// a button to roll the dice
private Button btnRoll;
private int bet;
private int score;
private TextField txfGuess; //a field to enter a guess of the addition of the two dice values
private int guess; // a field for the guess to be stored and compared with actual result
private int total; // a total field to find the addition of the two dice values
private boolean win; //a boolean value to display win/loose message.
private TextField userBet = new TextField();
TextField totalScore = new TextField();//set uneditable using totalScore.setEditable(false);
/**
* Initialise the applet
*/
public void init(){
setSize(500,400); //sets the size of the window
setBackground(Color.cyan); //sets the background color
setLayout(null); /*gets rid of the standard layout precedure of placing from centre outwards. Allows
for user to input coordinates for objects to be placed */
// Create the 2 dice
d1 = new Die(50,40); //creates a new dice, dice1, using the attributes from the dice class
d2 = new Die(220,40); // does as before, creates a new dice
// Create the button and add it to the GUI
btnRoll = new Button("ROLL THE DICE"); //This adds a button named roll the dice to the program
btnRoll.addActionListener(this);
btnRoll.setBounds(370, 50, 100, 20); //sets its 'boundaries' or position on the screen.
add(btnRoll); //adds the actual button, 'btnroll', to the program.
txfGuess = new TextField(""); /* adds a textfield to enter the numerical guess. As its stored as
a string it will need to be converted to a integer using the 'Integer parseInt' command. */
txfGuess.setBounds(400,17,40,20); //sets the position of the Guess box.
add(txfGuess); //adds the guess textbox to the program.
}
/**
* Display the current values of the 2 dice
*/
public void paint(Graphics g){ /* in the dice class the graphics g is used to set the dimensions of the dice, set the colour of the dice, set the fonts
used on the numbering for the dice, draws the value from the mathrandom calculation to the dice, */
g.drawString("Please enter a sum of what you think the dice roller will achieve", 25,30);
d1.display(g); //using the paint method, dice1 & 2 are drawn to the applet window
d2.display(g);
//using the boolean command, we can set the win or loose to display based on weather the statement is true or false.
if(win == true){ //display win if equal to true
g.drawString("You Win!", 35,180);
}
else {
g.drawString("You Lose!", 25,180); //otherwise, display lose.
}
}
/**
* When the button is clicked roll the dice.
*
*/
public void actionPerformed(ActionEvent ae) {
d1.roll(); /*this is linked with the actionlistener. when the roll the dice button is clicked, is uses d1.roll() statement.
This is listed in the dice class, as a mathrandom calculation. This means it will work out a random number as displayed it as the result for the dice roll.*/
d2.roll();
total = d1.getValue() + d2.getValue(); /*This is showing that the 'total' is created from the addition of the two dice roll values. using the getValue command. In the dice class the value is declared as 'Value'.
then linked with a mathrandom calculation, to produce a random number on the dice when rolled.*/
guess = Integer.parseInt(txfGuess.getText()); //This converts a string character '2' to an integer numerical value '2'.
try{
bet = Integer.toString(userBet.getText());/*You should validate the user entry somewhere above or below here!*/
}catch (NumberFormatException nfe ){
//let the user know its invalid
}
if(guess == total){
System.out.println("WIN");
win = true;
score +=bet;
userBet.setText( Integer.toString(score/2) );//set the default bet, score/2 but this can be changed to whatever
totalScore.setEditable(true);
totalScore.setText( Integer.toString(score));
totalScore.setEditble(false);
// this is a boolean statement. If the guess input by the user is EQUAL to that of the total of the two dice, it is classed as TRUE, and WIN is displayed as a string on the screen
}
else {
System.out.println("LOSE");
win = false;
score -=bet;
if (score <= 0 ){
}
totalScore.setEditable(true);
totalScore.setText( Integer.toString(score));
totalScore.setEditble(false);
// or else, (guess is not equal to total) the string Lose is displayed as win is returned false.
}
{}
repaint();
}