Results 1 to 8 of 8
  1. #1
    King8654's Avatar
    King8654 is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default Roll 2-Dice "Pig" Game Help

    Alright, been working on this game for quite a while, and couple things just cant get straight.

    MyApp.java
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;

    public class MyApp extends JFrame
    {
    private JLabel _aLabel, _comp, _comproll, _comptotal;
    private JLabel _user, _userroll, _usertotal;
    private JPanel _buttonPanel, _labelPanel1, _labelPanel2, _diePanel;
    private JButton roll;
    private JButton pass;
    private Die _leftDie; // component for one die
    private Die _rightDie;
    int roll;

    public MyApp(String title)
    { super(title);
    this.setSize(600,450);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    _aLabel = new JLabel("Pig 2-Dice Java Game");
    this.add(_aLabel, java.awt.BorderLayout.NORTH);
    _aLabel.setHorizontalAlignment(JLabel.CENTER);

    _leftDie = new Die();
    _rightDie = new Die();

    _diePanel = new JPanel();
    _diePanel.setLayout(new GridLayout(0,1));
    _diePanel.add(_leftDie);
    _diePanel.add(_rightDie);
    this.add(_diePanel, java.awt.BorderLayout.CENTER);

    _comp = new JLabel("Computer");
    _comproll = new JLabel("Round Score: " + roll);
    _comptotal = new JLabel("Total Score: ");

    _labelPanel1 = new JPanel();
    _labelPanel1.setLayout(new GridLayout(0,1));
    _labelPanel1.add(_comp);
    _labelPanel1.add(_comproll);
    _labelPanel1.add(_comptotal);
    // put JPanel into ContentPane of Frame
    this.add(_labelPanel1, java.awt.BorderLayout.WEST);

    _user = new JLabel("User");
    _userroll = new JLabel("Round Score: " + roll);
    _usertotal = new JLabel("Total Score: " );

    // instantiate JPanel and put buttons on JPanel
    _labelPanel2 = new JPanel();
    _labelPanel2.setLayout(new GridLayout(0,1));
    _labelPanel2.add(_user);
    _labelPanel2.add(_userroll);
    _labelPanel2.add(_usertotal);
    // put JPanel into ContentPane of Frame
    this.add(_labelPanel2, java.awt.BorderLayout.EAST);


    JButton roll = new JButton("Roll");
    roll.addActionListener(new RollListener());

    JButton pass = new JButton("Pass");

    _buttonPanel = new JPanel();
    _buttonPanel.setLayout(new GridLayout(1,0));
    _buttonPanel.add(roll);
    _buttonPanel.add(pass);

    this.add(_buttonPanel, java.awt.BorderLayout.SOUTH);


    this.setVisible(true);
    }

    private class RollListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    _leftDie.roll();
    _rightDie.roll();


    }
    }

    public static void main(String [] args) {
    MyApp myApp = new MyApp("A Game Of Fookin Pig");
    }
    }
    Die.java

    import java.awt.*;
    import javax.swing.*;


    public class Die extends JComponent {

    private static final int SPOT_DIAM = 12;
    private static final int a = 120;
    private int _faceValue, die1, die2, roll, score;


    public Die() {

    setPreferredSize(new Dimension(120,120));

    roll();
    }


    public int roll() {
    die = (int)(6*Math.random() + 1);
    roll = die1 + die2;
    repaint();
    return roll;

    }


    public void paintComponent(Graphics g) {

    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
    RenderingHints.VALUE_ANTIALIAS_ON);


    g2.setColor(Color.RED);
    g2.fillRect(0, 0, a, a);
    g2.setColor(Color.BLACK);
    g2.drawRect(0, 0, a-1, a-1);

    switch (die1) {
    case 1:
    drawSpot(g2, a/2, a/2);
    break;
    case 3:
    drawSpot(g2, a/2, a/2);
    case 2:
    drawSpot(g2, a/4, a/4);
    drawSpot(g2, 3*a/4, 3*a/4);
    break;
    case 5:
    drawSpot(g2, a/2, a/2);
    case 4:
    drawSpot(g2, a/4, a/4);
    drawSpot(g2, 3*a/4, 3*a/4);
    drawSpot(g2, 3*a/4, a/4);
    drawSpot(g2, a/4, 3*a/4);
    break;
    case 6:
    drawSpot(g2, a/4, a/4);
    drawSpot(g2, 3*a/4, 3*a/4);
    drawSpot(g2, 3*a/4, a/4);
    drawSpot(g2, a/4, 3*a/4);
    drawSpot(g2, a/4, a/2);
    drawSpot(g2, 3*a/4, a/2);
    break;
    }
    }



    private void drawSpot(Graphics2D g2, int x, int y) {
    g2.fillOval(x-SPOT_DIAM/2, y-SPOT_DIAM/2, SPOT_DIAM, SPOT_DIAM);
    }
    }
    So far, the program shows the layout and the dice generated from pressing the Roll button. I still have a long way to go with the game itself, but kinda hit a wall. Im pretty sure its quite simple and im just missing something.

    1. Cant seem to figure out how to place the dice next to each other in the center of their panel.

    2. Need to add the sum of each roll total, and place the result in the score label, and also keep a running tab of the roll values in the score label.

    3. Im pretty sure this program would benefit from more classes instead of squeezing into two, any advice would be awesome.

    After this is completed, still have to go back and forth between the user and computer rolling the dice, and the first to 100 wins, BUT if you roll a 1, you lose that rounds score, and if you roll two 1's, you lose your entire score.

    Just looking for help with the three objectives i posted little above, any help would be much appreciated. :) Thanks guys
    Last edited by King8654; 04-07-2008 at 04:25 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class DiceApp extends JFrame { 
        private JLabel _aLabel, _comp, _comproll, _comptotal;
        private JLabel _user, _userroll, _usertotal;
        private JPanel _buttonPanel, _labelPanel1, _labelPanel2, _diePanel;
        private JButton roll;
        private JButton pass;
        private Die _leftDie; // component for one die 
        private Die _rightDie;
        // You have a JButton and an [i]int[/i] both named "roll".
    //    int roll;
        int userScore = 0;
        int compScore = 0;
    
        public DiceApp(String title) {
            super(title);
            this.setSize(600,450);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE );
            _aLabel = new JLabel("Pig 2-Dice Java Game");
            this.add(_aLabel, BorderLayout.NORTH);
            _aLabel.setHorizontalAlignment(JLabel.CENTER);
    
            _leftDie = new Die();
            _rightDie = new Die();
    
            _diePanel = new JPanel();
            // Add a border to see what the layout manager is
            // doing with your components:
            _diePanel.setBorder(BorderFactory.createEtchedBorder());
            // Try a different layout manager for more flexibility:
            _diePanel.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            // spacing around components:
            gbc.insets = new Insets(5,5,5,5);
            // Leave the weight constraints at default zero values
            // to keep components clumped together.
            // Or, you could set [i]weighty[/i] to a non-zero to allow
            // components to spread out in vertical dimension:
            //gbc.weighty = 1.0;
            // single column:
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            _diePanel.add(_leftDie, gbc);
            _diePanel.add(_rightDie, gbc);
            this.add(_diePanel, BorderLayout.CENTER);
    
            _comp = new JLabel("Computer");
            _comproll = new JLabel("Round Score: " + roll);
            _comptotal = new JLabel("Total Score: ");
    
            _labelPanel1 = new JPanel();
            // Give labels some breathing room:
            Dimension d = _labelPanel1.getPreferredSize();
            d.width = 200;
            _labelPanel1.setPreferredSize(d);
            _labelPanel1.setLayout(new GridLayout(0,1));
            _labelPanel1.add(_comp);
            _labelPanel1.add(_comproll);
            _labelPanel1.add(_comptotal);
            // put JPanel into ContentPane of Frame
            this.add(_labelPanel1, BorderLayout.WEST);
    
            _user = new JLabel("User");
            _userroll = new JLabel("Round Score: " + roll);
            _usertotal = new JLabel("Total Score: " );
    
            // instantiate JPanel and put buttons on JPanel
            _labelPanel2 = new JPanel();
            // Need room for labels.
            _labelPanel2.setPreferredSize(d);
            _labelPanel2.setLayout(new GridLayout(0,1));
            _labelPanel2.add(_user);
            _labelPanel2.add(_userroll);
            _labelPanel2.add(_usertotal);
            // put JPanel into ContentPane of Frame
            this.add(_labelPanel2, BorderLayout.EAST);
    
            JButton roll = new JButton("Roll");
            roll.addActionListener(new RollListener());
    
            JButton pass = new JButton("Pass");
    
            _buttonPanel = new JPanel();
            _buttonPanel.setLayout(new GridLayout(1,0));
            _buttonPanel.add(roll);
            _buttonPanel.add(pass);
    
            this.add(_buttonPanel, BorderLayout.SOUTH);
    
            this.setVisible(true);
        }
    
        private class RollListener implements ActionListener {
            int turn = 0; // computer: 0  user: 1
    
            public void actionPerformed(ActionEvent e) {
                int left  = _leftDie.roll();
                int right = _rightDie.roll();
                postLabels(left, right, turn);
                turn = (turn+1) % 2;  // toggle turn
            }
        }
    
        private void postLabels(int left, int right, int turn) {
            // Figure out what the numbers mean/how scored.
            // For now:
            int round = left + right;
            switch(turn) {
                case 0:  // computer
                    _comproll.setText("Round Score: " + round);
                    compScore += round;
                    _comptotal.setText("Total Score: " + compScore);
                    break;
                case 1:  // user
                    _userroll.setText("Round Score: " + round);
                    userScore += round;
                    _usertotal.setText("Total Score: " + userScore);
            }
            if(compScore > 100 || userScore > 100) {
                System.out.println("Done");
                // reset variables, labels and "turn"
            }
        }
    
        public static void main(String [] args) {
            new DiceApp("A Game Of Fookin Pig");
        }
    }
    
    class Die extends JComponent {
        private static final int SPOT_DIAM = 12; 
        private static final int a = 120; 
        private int _faceValue, die1, die2, roll, score; 
    
        public Die() {
            setPreferredSize(new Dimension(120,120));
            roll(); 
        }
    
        public int roll() {
    //        die = (int)(6*Math.random() + 1);
            die1 = (int)(6*Math.random() + 1);
            die2 = (int)(6*Math.random() + 1);
            roll = die1 + die2;
            repaint();
            return roll;
        }
    
        public void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D)g; 
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(Color.RED);
            g2.fillRect(0, 0, a, a);
            g2.setColor(Color.BLACK);
            g2.drawRect(0, 0, a-1, a-1); 
    
            switch (die1) {
                case 1:
                    drawSpot(g2, a/2, a/2);
                    break;
                case 3:
                    drawSpot(g2, a/2, a/2);
                case 2:
                    drawSpot(g2, a/4, a/4);
                    drawSpot(g2, 3*a/4, 3*a/4);
                    break;
                case 5:
                    drawSpot(g2, a/2, a/2);
                case 4:
                    drawSpot(g2, a/4, a/4);
                    drawSpot(g2, 3*a/4, 3*a/4);
                    drawSpot(g2, 3*a/4, a/4);
                    drawSpot(g2, a/4, 3*a/4);
                    break;
                case 6:
                    drawSpot(g2, a/4, a/4);
                    drawSpot(g2, 3*a/4, 3*a/4);
                    drawSpot(g2, 3*a/4, a/4);
                    drawSpot(g2, a/4, 3*a/4);
                    drawSpot(g2, a/4, a/2);
                    drawSpot(g2, 3*a/4, a/2);
                    break;
            }
        }
    
        private void drawSpot(Graphics2D g2, int x, int y) {
            g2.fillOval(x-SPOT_DIAM/2, y-SPOT_DIAM/2, SPOT_DIAM, SPOT_DIAM);
        }
    }

  3. #3
    King8654's Avatar
    King8654 is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default awesome

    THANKS!!! made the changes, and now the program works great, switching between the computer and the user, keeping track of the score, and upon reaching 100, ending.

    One more question for the guru who helped me, if at all possible...

    If your playing as the user, you can choose to pass your turn, and click the pass button, and it moves to the computers turn.

    Thanks again for the help, i really tried everything, but the gridbaglayout i was scared to even get into on top of the program and complicate everything, but it worked out great! thanks again!

    Heres the new MyApp.java with the fixes and some other small bits I added
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.Insets.*;

    public class MyApp extends JFrame
    {
    private JLabel _aLabel, _comp, _comproll, _comptotal;
    private JLabel _user, _userroll, _usertotal, _victory, _compWin;
    private JPanel _buttonPanel, _labelPanel1, _labelPanel2, _diePanel;
    //private JButton _roll;
    //private JButton _pass;
    //private JButton _quit;
    private Die _leftDie; // component for one die
    private Die _rightDie;
    int userScore = 0;
    int compScore = 0;


    public MyApp(String title)
    { super(title);
    this.setSize(600,450);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    _aLabel = new JLabel("Pig 2-Dice Java Game");
    this.add(_aLabel, java.awt.BorderLayout.NORTH);
    _aLabel.setHorizontalAlignment(JLabel.CENTER);

    _leftDie = new Die();
    _rightDie = new Die();

    JButton quit = new JButton("Quit");
    quit.addActionListener(new QuitListener());

    _victory = new JLabel();
    _victory.setVisible(false);

    _diePanel = new JPanel();
    _diePanel.setBorder(BorderFactory.createEtchedBord er());
    _diePanel.setLayout(new GridBagLayout());

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(5,5,5,5);
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    _diePanel.add(_leftDie,gbc);
    _diePanel.add(_rightDie,gbc);
    _diePanel.add(quit,gbc);
    _diePanel.add(_victory,gbc);
    _victory.setHorizontalAlignment(JLabel.CENTER);
    this.add(_diePanel, BorderLayout.CENTER);

    _comp = new JLabel("Computer");
    _comp.setFont(new Font("Impact", Font.BOLD, 32));
    _comproll = new JLabel();
    _comptotal = new JLabel();

    _labelPanel1 = new JPanel();
    _labelPanel1.setBorder(BorderFactory.createEtchedB order());
    Dimension d = _labelPanel1.getPreferredSize();
    d.width = 200;
    _labelPanel1.setPreferredSize(d);
    _labelPanel1.setLayout(new GridLayout(0,1));
    _labelPanel1.add(_comp);
    _comp.setHorizontalAlignment(JLabel.CENTER);
    _comp.setBorder(BorderFactory.createEtchedBorder() );
    _labelPanel1.add(_comproll);
    _comproll.setHorizontalAlignment(JLabel.CENTER);
    _labelPanel1.add(_comptotal);
    _comptotal.setHorizontalAlignment(JLabel.CENTER);
    this.add(_labelPanel1, BorderLayout.EAST);

    _user = new JLabel("User");
    _user.setFont(new Font("Impact", Font.BOLD, 32));
    _userroll = new JLabel();
    _usertotal = new JLabel();

    _labelPanel2 = new JPanel();
    _labelPanel2.setBorder(BorderFactory.createEtchedB order());
    _labelPanel2.setPreferredSize(d);
    _labelPanel2.setLayout(new GridLayout(0,1));
    _labelPanel2.add(_user);
    _user.setHorizontalAlignment(JLabel.CENTER);
    _user.setBorder(BorderFactory.createEtchedBorder() );
    _labelPanel2.add(_userroll);
    _userroll.setHorizontalAlignment(JLabel.CENTER);
    _labelPanel2.add(_usertotal);
    _usertotal.setHorizontalAlignment(JLabel.CENTER);
    this.add(_labelPanel2, BorderLayout.WEST);

    JButton roll = new JButton("Roll");
    roll.addActionListener(new RollListener());

    JButton pass = new JButton("Pass");

    _buttonPanel = new JPanel();
    _buttonPanel.setLayout(new GridLayout(1,0));
    _buttonPanel.add(roll);
    _buttonPanel.add(pass);

    this.add(_buttonPanel, BorderLayout.SOUTH);


    this.setVisible(true);
    }

    private class RollListener implements ActionListener {
    int turn = 0;
    public void actionPerformed(ActionEvent e) {
    int left = _leftDie.roll();
    int right = _rightDie.roll();
    postLabels(left, right, turn);
    turn = (turn+1) % 2;


    }
    }

    private class QuitListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    System.exit(0);


    }
    }

    private class PassListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {

    }
    }
    private void postLabels(int left, int right, int turn) {

    int round = left + right;
    switch(turn) {
    case 0: // user
    _userroll.setText("Round Score: " + round);
    userScore += round;
    _usertotal.setText("Total Score: " + userScore);
    break;
    case 1: // computer
    _comproll.setText("Round Score: " + round);
    compScore += round;
    _comptotal.setText("Total Score: " + compScore);
    }
    if(userScore > 100) {
    _victory.setText(" User Wins " + userScore + " to " + compScore);
    _victory.setVisible(true);

    }
    else if(compScore > 100) {
    _victory.setText("Computer Wins " + compScore + " to " + userScore);
    _victory.setVisible(true);

    }
    // reset variables, labels and "turn"
    }



    public static void main(String [] args) {
    MyApp myApp = new MyApp("A Game Of Fookin Pig");
    }
    }
    Last edited by King8654; 04-01-2008 at 11:32 AM.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Some more ideas:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class DiceApp2 extends JFrame
    { 
        private JLabel _aLabel, _comp, _comproll, _comptotal;
        private JLabel _user, _userroll, _usertotal, _victory, _compWin;
        private JPanel _buttonPanel, _labelPanel1, _labelPanel2, _diePanel;
        private Die _leftDie; // component for one die 
        private Die _rightDie;
        int userScore = 0;
        int compScore = 0;
        TurnManager turnManager = new TurnManager();
    
        public DiceApp2(String title)
        {
            super(title);
            this.setSize(600,450);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE );
            _aLabel = new JLabel("Pig 2-Dice Java Game");
            this.add(_aLabel, java.awt.BorderLayout.NORTH);
            _aLabel.setHorizontalAlignment(JLabel.CENTER);
    
            _leftDie = new Die();
            _rightDie = new Die();
    
            JButton quit = new JButton("Quit"); 
            quit.addActionListener(new QuitListener());
    
            _victory = new JLabel();
            _victory.setVisible(false);
    
            _diePanel = new JPanel();
            // This was for diagnostic visibility; not needed.
            _diePanel.setBorder(BorderFactory.createEtchedBorder());
            _diePanel.setLayout(new GridBagLayout());
    
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(5,5,5,5);
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            _diePanel.add(_leftDie,gbc);
            _diePanel.add(_rightDie,gbc);
            _diePanel.add(quit,gbc);
            _diePanel.add(_victory,gbc);
            _victory.setHorizontalAlignment(JLabel.CENTER);
            this.add(_diePanel, BorderLayout.CENTER);
    
            _comp = new JLabel("Computer");
            _comp.setFont(new Font("Impact", Font.BOLD, 32));
            _comproll = new JLabel();
            _comptotal = new JLabel();
    
            _labelPanel1 = new JPanel();
            _labelPanel1.setBorder(BorderFactory.createEtchedBorder());
            Dimension d = _labelPanel1.getPreferredSize();
            d.width = 200;
            _labelPanel1.setPreferredSize(d);
            _labelPanel1.setLayout(new GridLayout(0,1));
            _labelPanel1.add(_comp);
            _comp.setHorizontalAlignment(JLabel.CENTER);
            _comp.setBorder(BorderFactory.createEtchedBorder() );
            _labelPanel1.add(_comproll);
            _comproll.setHorizontalAlignment(JLabel.CENTER);
            _labelPanel1.add(_comptotal);
            _comptotal.setHorizontalAlignment(JLabel.CENTER);
            this.add(_labelPanel1, BorderLayout.EAST);
    
            _user = new JLabel("User");
            _user.setFont(new Font("Impact", Font.BOLD, 32));
            _userroll = new JLabel();
            _usertotal = new JLabel();
    
            _labelPanel2 = new JPanel();
            _labelPanel2.setBorder(BorderFactory.createEtchedBorder());
            _labelPanel2.setPreferredSize(d);
            _labelPanel2.setLayout(new GridLayout(0,1));
            _labelPanel2.add(_user);
            _user.setHorizontalAlignment(JLabel.CENTER);
            _user.setBorder(BorderFactory.createEtchedBorder() );
            _labelPanel2.add(_userroll);
            _userroll.setHorizontalAlignment(JLabel.CENTER);
            _labelPanel2.add(_usertotal);
            _usertotal.setHorizontalAlignment(JLabel.CENTER);
            this.add(_labelPanel2, BorderLayout.WEST);
    
            JButton roll = new JButton("Roll");
            roll.addActionListener(new RollListener());
    
            JButton pass = new JButton("Pass");
            pass.addActionListener(new PassListener());
    
            _buttonPanel = new JPanel();
            _buttonPanel.setLayout(new GridLayout(1,0));
            _buttonPanel.add(roll);
            _buttonPanel.add(pass);
    
            this.add(_buttonPanel, BorderLayout.SOUTH);
    
            this.setVisible(true);
        }
    
        private class RollListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                int left = _leftDie.roll();
                int right = _rightDie.roll();
                postLabels(left, right);
                turnManager.next();
            }
        }
    
        private class QuitListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        }
    
        private class PassListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                turnManager.next();
            }
        }
    
        private class TurnManager {
            final static int USER     = 0;
            final static int COMPUTER = 1;
            int turn = USER;
    
            public void next() {
                turn = (turn+1) % 2;
            }
    
            public int getTurn() { return turn; };
    
            public void reset() { turn = USER; };
        }
    
        private void postLabels(int left, int right) {
            int round = left + right;
            int turn = turnManager.getTurn();
            switch(turn) {
                case TurnManager.USER:
                    _userroll.setText("Round Score: " + round);
                    userScore += round;
                    _usertotal.setText("Total Score: " + userScore);
                    break;
                case TurnManager.COMPUTER:
                    _comproll.setText("Round Score: " + round);
                    compScore += round;
                    _comptotal.setText("Total Score: " + compScore);
            }
            if(userScore > 100 || compScore > 100)
                gameOver();
        }
    
        private void gameOver() {
            if(userScore > 100) {
                _victory.setText(" User Wins " + userScore + " to " + compScore);
                _victory.setVisible(true);
            } else if(compScore > 100) {
                _victory.setText("Computer Wins " + compScore + " to " + userScore);
                _victory.setVisible(true);
            }
            // reset variables, labels and "turn"
        }
    
        public static void main(String [] args) {
            new DiceApp2("A Game Of Fookin Pig");
        }
    }

  5. #5
    King8654's Avatar
    King8654 is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default About turnmanager

    Thanks once again for the help wired, but I seem to still be having a problem, maybe it was my mis explaining the objective.

    After adding the revised code, and running, clicking on roll multiple times keeps switching between the user and computer, instead of staying with the same player and adding the sum of the rolls until the pass button is pressed, which changes the turn over to the computer.

    The pass button now at least functions, but instead of switching to the other players turn, it actually passes over their turn, like skipping them in line, so with successive clicks of the pass button and roll button the turn can stay with one person until 100, instead of switching.

    Sorry to keep bothering you, and thanks for all the awesome help you've been dishing out.

  6. #6
    King8654's Avatar
    King8654 is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default

    ACTUALLY, just fixed that problem by removing:

    public void actionPerformed(ActionEvent e) {
    int left = _leftDie.roll();
    int right = _rightDie.roll();
    postLabels(left, right);
    //turnManager.next();
    }
    }
    the turnmanager.next(); from the roll function, so indeed it does work good. Im going to go through this, and try now to implement if a one is rolled, the round score is set to 0, and if two ones are rolled, the total score is set to 0. Both of these switch turns, but with the manager now, should be alot easier. Thanks!

  7. #7
    King8654's Avatar
    King8654 is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default About rolling 1's, and changing comp's turn after one roll

    Sorry, but im back. Just got over catching something bad, havent been able to do much work since last post, with other exams eating me alive. feel bad for me...jk lol

    wondering where to start for a method to handle if a one or two ones is rolled, and the actions taken, for a one changes turn and doesnt count the score from that roll, and two rolls of a one wipes out the total score and changes turns. Guessing this would use a switch loop, where the cases would be a sum of 2(roll 2 ones), and a roll 1 || roll 2 equal one, then call the turn manager, but just dont know where to put it. :(

    Also, wondering for hint on how to use the turn manager next turn call after one roll for the computers turn. thanks a million for all the other help received, just a tad bit more and the prog will work beautifully. thanks again!

    bryan

  8. #8
    King8654's Avatar
    King8654 is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Talking Finished!!!!!!!

    Guess i doubted myself too much, so went ahead, wrote a few methods, messed around with some loops and function calls, and bam, all done!

    Wrote two new functions, for rolling the single one and double ones, and works!

    heres the updated code if anyone needs it in the future, or for some help!

    MyApp2.java - handles actual game

    // MyApp.java

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.Insets.*;

    public class DiceApp2 extends JFrame
    {
    private JLabel _aLabel, _comp, _comproll, _comptotal;
    private JLabel _user, _userroll, _usertotal, _victory, _compWin;
    private JPanel _buttonPanel, _labelPanel1, _labelPanel2, _diePanel;
    private Die _leftDie;
    private Die _rightDie;
    int userScore = 0;
    int compScore = 0;
    TurnManager turnManager = new TurnManager();


    public DiceApp2(String title)
    { super(title);
    this.setSize(600,450);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    _aLabel = new JLabel("Pig 2-Dice Java Game");
    this.add(_aLabel, java.awt.BorderLayout.NORTH);
    _aLabel.setHorizontalAlignment(JLabel.CENTER);

    _leftDie = new Die();
    _rightDie = new Die();

    JButton quit = new JButton("Quit");
    quit.addActionListener(new QuitListener());

    _victory = new JLabel();
    _victory.setVisible(false);

    _diePanel = new JPanel();
    _diePanel.setBorder(BorderFactory.createEtchedBord er());
    _diePanel.setBackground(Color.DARK_GRAY);
    _diePanel.setLayout(new GridBagLayout());

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(5,5,5,5);
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    _diePanel.add(_leftDie,gbc);
    _diePanel.add(_rightDie,gbc);
    _diePanel.add(quit,gbc);
    _diePanel.add(_victory,gbc);
    _victory.setHorizontalAlignment(JLabel.CENTER);
    this.add(_diePanel, BorderLayout.CENTER);

    _comp = new JLabel("Computer");
    _comp.setFont(new Font("Impact", Font.BOLD, 32));
    _comproll = new JLabel();
    _comptotal = new JLabel();

    _labelPanel1 = new JPanel();
    _labelPanel1.setBorder(BorderFactory.createEtchedB order());
    Dimension d = _labelPanel1.getPreferredSize();
    d.width = 200;
    _labelPanel1.setPreferredSize(d);
    _labelPanel1.setLayout(new GridLayout(0,1));
    _labelPanel1.add(_comp);
    _comp.setHorizontalAlignment(JLabel.CENTER);
    _comp.setBorder(BorderFactory.createEtchedBorder() );
    _labelPanel1.add(_comproll);
    _comproll.setHorizontalAlignment(JLabel.CENTER);
    _labelPanel1.add(_comptotal);
    _comptotal.setHorizontalAlignment(JLabel.CENTER);
    this.add(_labelPanel1, BorderLayout.EAST);

    _user = new JLabel("User");
    _user.setFont(new Font("Impact", Font.BOLD, 32));
    _userroll = new JLabel();
    _usertotal = new JLabel();

    _labelPanel2 = new JPanel();
    _labelPanel2.setBorder(BorderFactory.createEtchedB order());
    _labelPanel2.setPreferredSize(d);
    _labelPanel2.setLayout(new GridLayout(0,1));
    _labelPanel2.add(_user);
    _user.setHorizontalAlignment(JLabel.CENTER);
    _user.setBorder(BorderFactory.createEtchedBorder() );
    _labelPanel2.add(_userroll);
    _userroll.setHorizontalAlignment(JLabel.CENTER);
    _labelPanel2.add(_usertotal);
    _usertotal.setHorizontalAlignment(JLabel.CENTER);
    this.add(_labelPanel2, BorderLayout.WEST);

    JButton roll = new JButton("Roll");
    roll.addActionListener(new RollListener());

    JButton pass = new JButton("Pass");
    pass.addActionListener(new PassListener());

    _buttonPanel = new JPanel();
    _buttonPanel.setLayout(new GridLayout(1,0));
    _buttonPanel.add(roll);
    _buttonPanel.add(pass);

    this.add(_buttonPanel, BorderLayout.SOUTH);

    this.setVisible(true);
    }

    private class RollListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
    int left = _leftDie.roll();
    int right = _rightDie.roll();
    rollDoubleOnes(left,right);



    }
    }

    private class QuitListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    System.exit(0);
    }
    }

    private class PassListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    turnManager.next();

    }
    }

    private class TurnManager {
    final static int USER = 0;
    final static int COMPUTER = 1;
    int turn = USER;

    public void next() {
    turn = (turn+1) %2;
    }

    public int getTurn() { return turn; };

    public void reset() { turn = USER; };
    }

    private void postLabels(int left, int right) {

    int round = left + right;
    int turn = turnManager.getTurn();
    switch(turn) {
    case TurnManager.USER: // user

    _userroll.setText("Round Score: " + round);
    userScore += round;
    _usertotal.setText("Total Score: " + userScore);
    //turnManager.next();
    break;
    case TurnManager.COMPUTER: // computer

    _comproll.setText("Round Score: " + round);
    compScore += round;
    _comptotal.setText("Total Score: " + compScore);
    turnManager.next();
    }



    if(userScore > 100 || compScore > 100)
    gameOver();


    }

    private void rollDoubleOnes(int left, int right) {

    int round = left + right;

    int turn = turnManager.getTurn();

    if (turn == turnManager.USER && round == 2) {

    round = round *0;
    _userroll.setText("Round Score: " + round);
    userScore = userScore *0;
    _usertotal.setText("Total Score: " + userScore);
    turnManager.next();


    }
    else if(turn == turnManager.COMPUTER && round == 2) {

    round = round *0;
    _comproll.setText("Round Score: " + round);
    compScore = compScore *0;
    _comptotal.setText("Total Score: " + compScore);
    turnManager.next();


    } else {
    rollOnes(left,right); }
    }

    private void rollOnes(int left, int right) {


    int turn = turnManager.getTurn();
    int round = left + right;

    if (turn == turnManager.USER && left == 1 || right == 1) {

    round = round *0;
    _userroll.setText("Round Score: " + round);
    userScore = userScore + 0;
    _usertotal.setText("Total Score: " + userScore);
    turnManager.next();
    }

    else if (turn == turnManager.COMPUTER && left == 1 || right == 1) {

    round = round *0;
    _comproll.setText("Round Score: " + round);
    compScore = compScore + 0;
    _comptotal.setText("Total Score: " + compScore);
    turnManager.next();
    }
    else {
    postLabels(left, right);

    }

    }

    private void gameOver() {
    if(userScore > 100) {
    _victory.setText(" User Wins " + userScore + " to " + compScore);
    _victory.setFont(new Font("Ariel", Font.BOLD, 16));
    _victory.setForeground(new Color(0xffffff));
    _victory.setVisible(true);

    }
    else if(compScore > 100) {
    _victory.setText(" Comp Wins " + compScore + " to " + userScore);
    _victory.setFont(new Font("Ariel", Font.BOLD, 16));
    _victory.setForeground(new Color(0xffffff));
    _victory.setVisible(true);

    }
    // reset variables, labels and "turn"
    }



    public static void main(String [] args) {
    new DiceApp2("A Game Of Fookin Pig");
    }
    }
    And Die.java - handles the drawing of die and defines some functions

    import java.awt.*;
    import javax.swing.*;


    public class Die extends JComponent {

    private static final int SPOT_DIAM = 12;
    private static final int a = 120;
    private int die, _faceValue, _roll = 0, _sum = 0, roll;


    public Die() {

    setPreferredSize(new Dimension(120,120));
    roll();

    }


    public int roll() {
    die = (int)(6*Math.random() + 1);
    setValue(die);
    return die;

    }


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

    }

    /** Sets the value of the Die. Causes repaint().
    * @param spots Number from 1-6.
    */
    public void setValue(int spots) {
    _faceValue = spots;
    repaint();
    }


    /** Draws spots of die face. */
    public void paintComponent(Graphics g) {

    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
    RenderingHints.VALUE_ANTIALIAS_ON);


    g2.setColor(Color.RED);
    g2.fillRect(0, 0, a, a);
    g2.setColor(Color.BLACK);

    g2.drawRect(0, 0, a-1, a-1);

    switch (die) {
    case 1:
    drawSpot(g2, a/2, a/2);
    break;
    case 3:
    drawSpot(g2, a/2, a/2);
    case 2:
    drawSpot(g2, a/4, a/4);
    drawSpot(g2, 3*a/4, 3*a/4);
    break;
    case 5:
    drawSpot(g2, a/2, a/2);
    case 4:
    drawSpot(g2, a/4, a/4);
    drawSpot(g2, 3*a/4, 3*a/4);
    drawSpot(g2, 3*a/4, a/4);
    drawSpot(g2, a/4, 3*a/4);
    break;
    case 6:
    drawSpot(g2, a/4, a/4);
    drawSpot(g2, 3*a/4, 3*a/4);
    drawSpot(g2, 3*a/4, a/4);
    drawSpot(g2, a/4, 3*a/4);
    drawSpot(g2, a/4, a/2);
    drawSpot(g2, 3*a/4, a/2);
    break;
    }
    }


    /** Utility method used by paintComponent(). */
    private void drawSpot(Graphics2D g2, int x, int y) {
    g2.fillOval(x-SPOT_DIAM/2, y-SPOT_DIAM/2, SPOT_DIAM, SPOT_DIAM);
    }
    }
    thanks everyone for the snippets of help, and for actually giving me WAY more confidence in programming having to figure most of it on my own. thanks again guys!! :D

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 09:26 AM
  2. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  3. Replies: 1
    Last Post: 03-14-2008, 02:51 PM
  4. Replies: 1
    Last Post: 07-27-2007, 10:59 PM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •