0 Replies - 10336 Views - Last Post: 12 November 2010 - 12:22 PM Rate Topic: -----

#1 doychin_90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 15-March 10

I have great Problems with Simple Dice Game

Posted 12 November 2010 - 12:22 PM

:stupid: :helpsmilie: :helpsmilie: :helpsmilie: :stupid:

My task is to create simple dice game that is consists of:
•A dice for the player and a dice for the computer
•A button for rolling the dice
•A button for starting a new game
•A score
•A message
The game should proceed as follows:
•On pressing the new game button, both dice are removed from view, any message is removed from view and the roll the dice button is enabled
•On pressing the roll the dice button once after the new game started the computer’s dice will be rolled and the result displayed
•On pressing the roll the dice button a second time, the player’s dice will be rolled and the result will be displayed; the roll the dice button will then be disabled. If the player’s dice is higher than the computer’s dice, the window will display “Winner!” to the user and the score will increase by 2 points. If the player loses, the window will display “You lose” and the score will decrease by 1 point.

I created 3 class: Dice(that creates graphically a dice); Panel(that I used to store and call my Labels and Buttons); Main (that I used it as a main method)

I reached a middle of nowhere with all this classes because I am a beginner and It is difficult a lot.

I am quite desperate and I would be grateful if anyone who can help me with any solutions how to develop my code further gives me a hand.
  public class Dice extends JComponent {
    //================================================================ constants
    private static final int SPOT_DIAM = 9;  // Diameter of spots

    //======================================================= instance variables
    private int value;     // value that shows on face of die

    //============================================================== constructor
    /** Initialize to white background and 60x60 pixels. Initial roll.*/
    public Dice() {
        //-- Preferred size is set, but layout may change it.
        setPreferredSize(new Dimension(55,55));
        roll();  // Set to random initial value

    //============================================================== method roll
    /** Produce random roll in range 1-6.  Causes repaint().
     * @return Result of roll (1-6).
    public int roll() {
        int randomNumber = (int)(6*Math.random() + 1);   // Range 1-6
        return randomNumber;

    //========================================================== method getValue
    /** Returns result of last roll.*/
    public int getValue() {
        return value;

    //========================================================== method setValue
    /** Sets the value of the Die.  Causes repaint().
     * @param spots Number from 1-6.
    public void setValue(int spots) {
        value = spots;
        repaint();    // Value has changed, must repaint

    //==================================================== method paintComponent
    /** Draws spots of die face. */
    @Override public void paintComponent(Graphics g) {
        int w = getWidth();  // Get height and width
        int h = getHeight();

        //... Change to Graphic2D for smoother spots.
        Graphics2D graphics2D = (Graphics2D)g;  // See note below

        //... Paint background
        graphics2D.fillRect(0, 0, w, h);

        graphics2D.drawRect(0, 0, w-1, h-1);  // Draw border

        switch (value) {
            case 1:
                drawSpot(graphics2D, w/2, h/2);
            case 3:
                drawSpot(graphics2D, w/2, h/2);
                // Fall thru to next case
            case 2:
                drawSpot(graphics2D, w/4, h/4);
                drawSpot(graphics2D, 3*w/4, 3*h/4);
            case 5:
                drawSpot(graphics2D, w/2, h/2);
                // Fall thru to next case
            case 4:
                drawSpot(graphics2D, w/4, h/4);
                drawSpot(graphics2D, 3*w/4, 3*h/4);
                drawSpot(graphics2D, 3*w/4, h/4);
                drawSpot(graphics2D, w/4, 3*h/4);
            case 6:
                drawSpot(graphics2D, w/4, h/4);
                drawSpot(graphics2D, 3*w/4, 3*h/4);
                drawSpot(graphics2D, 3*w/4, h/4);
                drawSpot(graphics2D, w/4, 3*h/4);
                drawSpot(graphics2D, w/4, h/2);
                drawSpot(graphics2D, 3*w/4, h/2);

    //========================================================== method drawSpot
    /** Utility method used by paintComponent(). */
    private void drawSpot(Graphics2D graphics2D, int x, int y) {
        graphics2D.fillOval(x-SPOT_DIAM/2, y-SPOT_DIAM/2, SPOT_DIAM, SPOT_DIAM);

 * @author Doychin Sakutov
public class Panel extends JPanel {
    JLabel playersName;
    JLabel computer;
    JLabel score;
    JLabel myJLabel;
    JButton roll;
    JButton newGame;
    private Dice computerDice;
    private Dice playerDice;
    int computerResult;
    int playerResult;
    int a;


        computerDice = new Dice();
        playerDice = new Dice();

        roll = new JButton("ROLL");
        roll.setFont(new Font("Black Arial",Font.PLAIN,14));
        newGame = new JButton ("New Game");
        newGame.setFont(new Font("Black Arial",Font.BOLD,14));

        playersName = new JLabel();
        computer = new JLabel();

        score = new JLabel("Score");

        roll.addActionListener(new RollListener());
        this.setLayout(new BorderLayout(500,500));
       this.add(roll, BorderLayout.PAGE_END);
      this.add(playerDice , BorderLayout.BEFORE_LINE_BEGINS);
       this.add(computerDice, BorderLayout.EAST);
        //this.add(score, BorderLayout.BEFORE_FIRST_LINE);
        //this.add(computer, BorderLayout.BEFORE_LINE_BEGINS);
        //this.add(playersName, BorderLayout.PAGE_START);

        public class RollListener implements ActionListener {
           public void actionPerformed(ActionEvent e) {
if (computerDice.roll()>playerDice.roll()){
             JOptionPane.showMessageDialog(null,"Winner! :)/>","Winner!",JOptionPane.PLAIN_MESSAGE);
             a = a + 2;
             myJLabel = new JLabel(Integer.toString(a));
            JOptionPane.showMessageDialog(null,"You Lost :(/>","Lose",JOptionPane.PLAIN_MESSAGE);
            a = a-1;
            myJLabel =new JLabel (Integer.toString(a));


public class Main extends JApplet{
    public Main() {
        this.setContentPane(new Panel());

     * @param args the command line arguments
       public static void main(String[] args) {
        JFrame window = new JFrame("Dice Game");
        window.setContentPane(new Panel());

Is This A Good Question/Topic? 0
  • +

Page 1 of 1