Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Major Help, SAVE ME!! Java Dice Game - Keep Score?

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Major Help, SAVE ME!! Java Dice Game - Keep Score?

    I have got my code working 100%, I took bits and pieces from other open source java files to help build this up.. but the issue I am having is this.. I need the code to keep track of the scores once the dice is rolled and do so for 2 players and for 5 rounds.. make sense? This is set up to let each player roll 3 dice, highest score wins ... but I can only do this once.. I need up to 5 rounds and all the while the score is kept and it accumulates.. Any help to get me started or what I can do.. Here is the full code I have...

    Dice_Game.java
    import javax.swing.JFrame;
     
    public class Dice_Game
    {
    	public static void main(String args[] )
    	{
    		GUI GCO = new GUI();
    		GCO.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		GCO.setSize(250,223);
    		GCO.setVisible(true);
    	}
    }


    Guts/GUI/ETC

    GUI.java
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.Color;
    import javax.swing.JOptionPane;
     
    public class GUI extends JFrame
    {
    	private static final long serialVersionUID = 1l;
     
    	private String[]    bStatus     = { "Roll", "Roll", "Rematch" };
    	private String[]    filename    = { "1.png", "2.png", "3.png", "4.png", "5.png", "6.png" };
    	private Icon[]      dice        = { new ImageIcon(getClass().getResource(filename[0])),
    										new ImageIcon(getClass().getResource(filename[1])),
    										new ImageIcon(getClass().getResource(filename[2])),
    										new ImageIcon(getClass().getResource(filename[3])),
    										new ImageIcon(getClass().getResource(filename[4])),
    										new ImageIcon(getClass().getResource(filename[5]))  };
     
    	private String[]    Player = { "Player 1", "Player 2" };
     
    	public int bState = 0;
     
    	public JLabel die1;
    	public JLabel die2;
    	public JLabel die3;
    	public JLabel die4;
    	public JLabel die5;
    	public JLabel die6;
     
    	public JLabel sStatusBar;
     
    	public JButton bButton;
     
    	public Component s1;
    	public Component s2;
     
    	public int p1Total = 0, p2Total = 0;
     
    	public GUI()
    	{
    		super("Dice Game");
    		Container c = getContentPane();
    		c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
    		c.setBackground(new Color(0, 115, 0));
     
    		Player[0] = JOptionPane.showInputDialog("Enter Player 1's Name");
    		Player[1] = JOptionPane.showInputDialog("Enter Player 2's Name");
     
    		//Player 1 (Label)
    		JLabel Player1 = new JLabel(Player[0]);
    		Player1.setForeground(Color.WHITE);
    		Player1.setAlignmentX(Component.CENTER_ALIGNMENT);
     
    		//Player 2 (Label)
    		JLabel Player2 = new JLabel(Player[1]);
    		Player2.setForeground(Color.WHITE);
    		Player2.setAlignmentX(Component.CENTER_ALIGNMENT);
     
    		//Roll / New Game Button
    		bButton = new JButton(bStatus[0]);
    		bButton.setAlignmentX(Component.CENTER_ALIGNMENT);
     
    		//Panel
    		JPanel p = new JPanel();
    		p.setAlignmentY(BOTTOM_ALIGNMENT);
     
    		//Green Panels
    		JPanel gp = new JPanel();
    		gp.setBackground(new Color(0, 115, 0));
     
    		JPanel gp2 = new JPanel();
    		gp2.setBackground(new Color(0, 115, 0));
     
    		//Status Bar (Label)
    		sStatusBar = new JLabel(Player[0] + ", it's your turn.");
    		sStatusBar.setAlignmentX(Component.LEFT_ALIGNMENT);
     
    		die1 = new JLabel();
    		die1.setAlignmentX(Component.CENTER_ALIGNMENT);
    		die2 = new JLabel();
    		die2.setAlignmentX(Component.CENTER_ALIGNMENT);
    		die3 = new JLabel();
    		die3.setAlignmentX(Component.CENTER_ALIGNMENT);
    		die4 = new JLabel();
    		die4.setAlignmentX(Component.CENTER_ALIGNMENT);
    		die5 = new JLabel();
    		die5.setAlignmentX(Component.CENTER_ALIGNMENT);
    		die6 = new JLabel();
    		die6.setAlignmentX(Component.CENTER_ALIGNMENT);
     
    		die1.setIcon(dice[0]);
    		die2.setIcon(dice[1]);
    		die3.setIcon(dice[2]);
    		die4.setIcon(dice[3]);
    		die5.setIcon(dice[4]);
    		die6.setIcon(dice[5]);
     
    		s1 = Box.createVerticalStrut(31);
    		s2 = Box.createVerticalStrut(31);
     
    		c.add(Player1);
    		c.add(gp);
    		gp.add(s1);
    		gp.add(die1);
    		gp.add(die2);
    		gp.add(die3);
    		die1.setVisible(false);
    		die2.setVisible(false);
    		die3.setVisible(false);
     
    		c.add(Player2);
    		c.add(gp);
    		gp.add(s2);
    		gp.add(die4);
    		gp.add(die5);
    		gp.add(die6);		
    		die4.setVisible(false);
    		die5.setVisible(false);
    		die6.setVisible(false);
     
    		c.add(bButton);
    		c.add(Box.createVerticalStrut(20));
     
    		c.add(p);
    		p.add(sStatusBar);
     
    		bButton.addActionListener(
     
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						bButtonActionPerformed(event);
    					}
     
    					private void bButtonActionPerformed(ActionEvent event) {
    						// TODO Auto-generated method stub
    						Random randomizer = new Random();
    						if (bState == 0)
    						{
    							int n1 = 0, n2 = 0, n3 = 0;
    							n1 = randomizer.nextInt(6);
    							n2 = randomizer.nextInt(6);
    							n3 = randomizer.nextInt(6);
    							p1Total = n1+n2+n3;
     
    							die1.setIcon(dice[n1]);
    							die2.setIcon(dice[n2]);
    							die3.setIcon(dice[n3]);
     
    							s1.setVisible(false);
    							die1.setVisible(true);
    							die2.setVisible(true);
    							die3.setVisible(true);
    							sStatusBar.setText(Player[1] + ", it's your turn.");
    							bButton.setText(bStatus[1]);
    						}
    						if (bState == 1)
    						{
    							int n1 = 0, n2 = 0, n3 = 0;
    							n1 = randomizer.nextInt(6);
    							n2 = randomizer.nextInt(6);
    							n3 = randomizer.nextInt(6);
    							p2Total = n1+n2+n3;
     
    							die4.setIcon(dice[n1]);
    							die5.setIcon(dice[n2]);
    							die6.setIcon(dice[n3]);
     
    							s2.setVisible(false);
    							die4.setVisible(true);
    							die5.setVisible(true);
    							die6.setVisible(true);
    							if (p1Total > p2Total)
    								sStatusBar.setText(Player[0] + ", won!  Play again?");
    							if (p1Total < p2Total)
    								sStatusBar.setText(Player[1] + ", won!  Play again?");
    							if (p1Total == p2Total)
    								sStatusBar.setText("Draw, Play again?");
    							bButton.setText(bStatus[2]);
    						}
    						bState++;
    						if (bState == 3 )
    						{
    							die1.setVisible(false);
    							die2.setVisible(false);
    							die3.setVisible(false);
    							die4.setVisible(false);
    							die5.setVisible(false);
    							die6.setVisible(false);
    							s1.setVisible(true);
    							s2.setVisible(true);
    							sStatusBar.setText(Player[0] + ", it's your turn!");
    							bButton.setText(bStatus[0]);
    							bState = 0;
    						}
     
    					}
     
     
    				}
    				);
    	}
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Major Help, SAVE ME!! Java Dice Game - Keep Score?

    Start by adding a display to the GUI which show what the round of play is and what the scores are.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Dice score program
    By ksahakian21 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 23rd, 2012, 05:25 PM
  2. Java Programming - Dice Game Help!
    By blackvelvet in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 19th, 2012, 11:47 AM
  3. Dice game code creating help!
    By kodols in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 22nd, 2011, 07:01 PM
  4. Dice Game help and Arrays
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 9th, 2011, 10:08 PM
  5. Dice game help, a little cleanup
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2011, 12:28 PM