DiceDemo.java Code

/*
 * DiceDemo.java
 *
 * Created on September 10, 2000, 7:39 AM
 */
 import java.awt.*;
 import java.applet.*;
 import java.awt.event.*;
 import java.util.*;
 

/**
 *
 * @author  hunt, jan
 * @version 
 */
public class DiceDemo extends java.applet.Applet implements ActionListener {

    private Button btnRoll;  //declare button variables
    private Button btnRules;
    private Button btnNewGame;
    die leftDie = new die(102,102,64,4); //declare die variables and define them at same time
    die rightDie = new die(202,102,64,4);
    public int figure = 0;                  
    Random diceValues = new Random();  //random number generator
    public int rolls = 0; 
    public int point = 0;
    public String status = "";
    private int flag = 0;
    
    public void init() {
        //Create the Roll button, add it to the container, set up listening;
        btnRoll = new Button("Roll Me");
        add(btnRoll);
        btnRoll.addActionListener(this);
        
        btnNewGame = new Button("New Game");
        add(btnNewGame);
        btnNewGame.addActionListener(this);
        
        btnRules = new Button("Rules for Craps");
        add(btnRules);
        btnRules.addActionListener(this);
        //leftDie.x=102; leftDie.y=102; leftDie.size=64; leftDie.points=4;
        //rightDie.x=202; rightDie.y=102; rightDie.size=64; rightDie.points=4;
    }//end init
    
    public void actionPerformed(ActionEvent event) {
        if(event.getSource() == btnRoll) {
            if(flag == 1) {
               status = "CLICK NEW GAME BUTTON";
            }//end if for flag
            else if(flag == 0) {
            leftDie.points=1+Math.abs(diceValues.nextInt())%6;
            rightDie.points=1+Math.abs(diceValues.nextInt())%6;
            figure=1;
            rolls = rolls + 1;
            calculateScore(leftDie.points, rightDie.points);
        }//end else if for flag
        }//end if statement
        else if(event.getSource() == btnRules) {
			status = "";
            figure=2;
        }//end else if
        else if(event.getSource() == btnNewGame) {
            rolls = 0;
            figure = 0;
            status = "";
            point = 0;
            flag = 0;
        }//end second else if
        repaint(); //call paint()
    }//end actionPerformed
    
    public void calculateScore(int left, int right) {
        int combDie = left + right;
        if(rolls == 1) {
        switch(combDie){
            case 2:
            case 3:
            case 12:
                status = "You Lose Immediately!";
                flag = 1;
                break;
            case 7:
            case 11:
                status = "You WIN Immediately!";
                flag = 1;
                break;
            case 4:
            case 5:
            case 6:
            case 8:
            case 9:    
            case 10: 
                status = "Point Number, keep rolling";
                point = combDie;
                flag = 0;
                break;
            }//end switch
        }//end if for rolls == 1  
        else if(rolls > 1){
            switch(point){
                case 4:
                case 5:
                case 6:
                case 8:
                case 9:
                case 10:
                    status = "Continue rolling until you get a 7 or the point number";
                    flag = 0;
                    break;
                }//end switch for point
                if(combDie == point) {
                    status = "Point number you WIN!";
                    flag = 1;
                }//end nested if
            switch(combDie) {
                case 7:
                   status = "You lose, you rolled a 7";
                   flag = 1;
            }//end switch
            }//end else if for counter > 1
    }//end calculate Score
    
    public class die {
        //instance variables in class die;
        int x; //coordinate from the top of the applet;
        int y; //coordinate from the left of applet;
        int size; //size of the die - width and height;
        int points; //pips on the die - can have 1 thru 6;
        
    
    die(int left, int top, int dieSize, int numPoints){
        //constructor for initialized object
        x=left; y=top; size=dieSize; points=numPoints;
           }//end die method for initialized object
   
    public void draw(Graphics g) {
        int q = size/4;
        g.setColor(Color.blue);
        g.drawString("Rolls : " + rolls, 5, 50);
        g.drawString("Point # : " + point, 5, 70);
        g.drawString("" + status, 5, 90);
        //Draw dice figures
        if(figure==0) {
        g.setColor(Color.black);
        g.drawRect(x,y,size,size);
        //g.setColor(new Color(255,255,228)); //pale yellow 
        g.setColor(Color.red);
        g.fillRect(x,y,size,size);
        g.setColor(Color.black);
        g.fillOval((x-4)+(2*q), (y-4)+(2*q), size/8, size/8);
        }//end if
        else if(figure ==1){
        g.setColor(Color.black);
        g.drawRect(x,y,size,size);
        //g.setColor(new Color(255,255,228)); //pale yellow 
        g.setColor(Color.red);
        g.fillRect(x,y,size,size);
        
        g.setColor(Color.black);
            switch(points) {
            case 1:
                g.fillOval((x-4)+(2*q), (y-4)+(2*q), size/8, size/8);
                break;
            case 2:
                g.fillOval((x-4)+ q, (y-4) + q, size/8, size/8);
                g.fillOval((x-4)+(3*q), (y-4)+(3*q), size/8, size/8); 
                break;
            case 3:
                g.fillOval((x-4)+ q, (y-4) + q, size/8, size/8);
                g.fillOval((x-4)+(2*q), (y-4)+(2*q), size/8, size/8);
                g.fillOval((x-4)+(3*q), (y-4)+(3*q), size/8, size/8);
                break;
            case 4:
                g.fillOval((x-4)+ q, (y-4) + q, size/8, size/8);
                g.fillOval((x-4)+(3*q), (y-4)+(3*q), size/8, size/8); 
                g.fillOval((x-4)+ q, (y-4) + (3*q), size/8, size/8);
                g.fillOval((x-4)+ (3*q), (y-4)+ q, size/8, size/8); 
                break;
            case 5:
                g.fillOval((x-4)+ q, (y-4) + q, size/8, size/8);
                g.fillOval((x-4)+(3*q), (y-4)+(3*q), size/8, size/8); 
                g.fillOval((x-4)+(2*q), (y-4)+(2*q), size/8, size/8);
                g.fillOval((x-4)+ q, (y-4) + (3*q), size/8, size/8);
                g.fillOval((x-4)+ (3*q), (y-4)+ q, size/8, size/8); 
                break;
            case 6:
                g.fillOval((x-4)+ q, (y-4) + q, size/8, size/8);
                g.fillOval((x-4)+(3*q), (y-4)+(3*q), size/8, size/8); 
                g.fillOval((x-4)+ q, (y-4)+(2*q), size/8, size/8); 
                g.fillOval((x-4)+ (3*q), (y-4)+(2*q), size/8, size/8);
                g.fillOval((x-4)+ q, (y-4) + (3*q), size/8, size/8);
                g.fillOval((x-4)+ (3*q), (y-4)+ q, size/8, size/8); 
                break;
            }//end switch statement
        }//end else if
        else if(figure ==2){
            g.setColor(Color.red);
            g.drawString("The rules for craps as I understand them", 50, 95);
            g.setColor(Color.blue);
            g.drawString("-If you throw a 2,3,12 on the first roll the game is over with a loss.", 0, 115);
            g.drawString("-If you throw a 4,5,6,8,9,10 you get that number as a point and", 0, 135);
            g.drawString("continue to roll until you get a 7 (loss) or the point number.", 5, 155);
            g.drawString("You win if the point number is thrown!", 5, 175);
            g.drawString("-If you throw a 7 or 11 on the first roll you WIN!", 0, 195);
         }//end second else if
        }//end draw method
 }//end class die

    public void paint(Graphics g){
        leftDie.draw(g);
        rightDie.draw(g);
    }//end paint method

}//end class DiceDemo