/*
Programmer:	
Date:		March 30th, 2011
Filename:	DiceGame2.java
Purpose:	Java program that lets the player roll 5 normal 6 sided die.
		    The player gets to choose which of the die to keep or which to reroll - explains it in messages to the player.
		    Reroll the selected die not kept. Multiply any duplicate die's together add the total and display the result.
		    have some way for them to quit the game or to keep all 5 after a roll. I have not started the multiplication yet just trying to get the the dice to roll and be saved through the checkboxes.
*/

import java.util.*;
import java.util.Random;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import javax.swing.JCheckBox;


	public class DiceGame2 extends JFrame implements ActionListener
	{

			//Declare output stream
			 DataOutputStream output;

			 //Construct a panel for each row
			JPanel firstRow = new JPanel();
			JPanel secondRow = new JPanel();
			JPanel thirdRow = new JPanel();
			JPanel fouthRow = new JPanel();
			JPanel fifthRow = new JPanel();
			JPanel sixthRow = new JPanel();
			JPanel seventhRow = new JPanel();

			//Construct a panel for the fields and buttons.
			JPanel fieldPanel = new JPanel();
			JPanel buttonPanel = new JPanel();
			JPanel checkBoxPanel = new JPanel();

			//Construct labels, text boxes for Game.
			JLabel dice1Label = new JLabel("Dice One");
				JTextField dice1 = new JTextField(5);
			JLabel dice2Label = new JLabel("Dice Two");
				JTextField dice2 = new JTextField(5);
			JLabel dice3Label = new JLabel("Dice Three");
				JTextField dice3 = new JTextField(5);
			JLabel dice4Label = new JLabel("Dice Four");
		    	JTextField dice4 = new JTextField(5);
			JLabel dice5Label = new JLabel("Dice Five");
				JTextField dice5 = new JTextField(5);
			JLabel totalLabel = new JLabel("Total Accumulated");
				JTextField total = new JTextField(15);

			// Construct checboxes for dice that need to be Re-Rolled
			JCheckBox cb1 = new JCheckBox("Click checkbox to save die");
			JCheckBox cb2 = new JCheckBox();
			JCheckBox cb3 = new JCheckBox();
			JCheckBox cb4 = new JCheckBox();
			JCheckBox cb5 = new JCheckBox();



		//Construct buttons for Roll Dice, Re-Roll all, and Exit
	   JButton rollDiceButton = new JButton("Roll Dice");
	   JButton reRollButton = new JButton("Re-Roll All");
	   JButton quitButton = new JButton("Quit Game");

	   public class RollDice{

	   public static void main(String[] args) //main method
	   {

	   	         // Create a Method to roll the Dice
					    public static void RollTheDice()
					    {
							// Create Random number generator and Dice variables
										Random rand = new Random();

										int roll1 = rand.nextInt(6) + 1;
										roll1 = dice1;
										int roll2 = rand.nextInt(6) + 1;
										roll2 = dice2;
										int roll3 = rand.nextInt(6) + 1;
										roll3 = dice3;
										int roll4 = rand.nextInt(6) + 1;
										roll4 = dice4;
										int roll5 = rand.nextInt(6) + 1;
										roll5 = dice5;

											if(cb1 == true)// Check for saved Numbers
											roll1 = dice1;
											else
											{
												int roll1 = rand.nextInt(6) + 1;
												roll1 = dice1;
											}
											if(cb2 == true)
											roll2 = dice2;
											else
											{
												int roll2 = rand.nextInt(6) + 1;
												roll2 = dice2;
											}
											if(cb3 == true)
											roll3 = dice3;
											else
											{
												int roll3 = rand.nextInt(6) + 1;
												roll3 = dice3;
											}
											if(cb4 == true)
											roll4 = dice4;
											else
											{
												int roll4 = rand.nextInt(6) + 1;
												roll4 = dice4;
											}
											if(cb5 == true)
											roll5 = dice5;
											else
											{
												int roll5 = rand.nextInt(6) + 1;
												roll5 = dice5;
											}// End checkbox search
                               }

	   	        //set the look and feel of the interface
	   	         try
	   	         {
	   	                UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

	   	         }
	            catch(Exception e)
	   	         {
	   	         JOptionPane.showMessageDialog(null, "The UIManager could not set the Look and Feel.","error",JOptionPane.INFORMATION_MESSAGE);
	   	         }

	   	//main method executes at run time
	   	DiceGame2 f = new DiceGame2();
	   	f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	   	f.setSize(450,300);
	   	f.setTitle("Welcome to My Dice game");
	   	f.setResizable(false);
	   	f.setLocation(200,200);
	   	f.setVisible(true);
	   	JOptionPane.showMessageDialog(null, "Click Roll Dice to start the game!.\n" +
      "Then click the checkbox next to the numbers you wish to save and Roll Dice agian!.\n" + "You can also choose Re-Roll all dice.","Welcome", JOptionPane .INFORMATION_MESSAGE);
	}
}
	    public DiceGame2()
		{
		        Container c = getContentPane();//content pane assigned
		        c.setLayout((new BorderLayout()));//layout methods
		        fieldPanel.setLayout(new GridLayout(7,1));
		        FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,3);
		            firstRow.setLayout(rowSetup);//layout methods
		            secondRow.setLayout(rowSetup);
		            thirdRow.setLayout(rowSetup);
		            fourthRow.setLayout(rowSetup);
		            fifthRow.setLayout(rowSetup);
		            sixthRow.setLayout(rowSetup);
		    		seventhRow.setLayout(new FlowLayout(FlowLayout.CENTER));//layout methods

		    		//Add fields to rows
		    		fistRow.add (dice1Label);
					firstRow.add (cb1Label);

				    secondRow.add (dice1);
					secondRow.add (cb1);

					thirdRow.add(dice2);
					thirdRow.add(cb2);


					fourthRow.add(dice3);
				    fourthRow.add(cb3);

					fifthRow.add(dice4);
					fifthRow.add(cb4);

				    sixthRow.add(dice5);
				    sixthRow.add(cb5);

				    seventhRow.add(total);

				  //Add rows to panel
				fieldPanel.add(firstRow);
				fieldPanel.add(secondRow);
				fieldPanel.add(thirdRow);
				fieldPanel.add(fourthRow);
				fieldPanel.add(fifthRow);
				fieldPanel.add(sixthRow);
				fieldPanel.add(seventhRow);

				//Add buttons to the panel
				buttonPanel.add(rollDiceButton);
				buttonPanel.add(reRollButton);
	            buttonPanel.add(quitButton);

	            //Add panels to frame
				c.add(fieldPanel, BorderLayout.WEST);
				c.add(checkBoxPanel, BorderLayout.EAST);
	            c.add(buttonPanel, BorderLayout.SOUTH);

                //Add functionality to buttons
                rollDiceButton.addActionListener(this);
                reRollButton.addActionListener(this);
	            quitButton.addActionListener(this);

	            // Add functionality to the checkboxes
	            cb1.addActionListener(this);
	            cb2.addActionListener(this);
	            cb3.addActionListener(this);
	            cb4.addActionListener(this);
	            cb5.addActionListener(this);



public void actionPerformed(ActionEvent e)
{
	String arg = e.getActionCommand();

		if(arg == rollDice) // User clicks Roll Dice button
		{
			RollTheDice();
		}

			if(arg == reRoll) // User clicks Roll All Dice button
					{
						RollTheDice();
				    }
				    if(arg == quit)// User clicks quit button
				    {
						System.exit(0);
					}
		}// End Action Performed
	}
}

Recommended Answers

All 2 Replies

You haven't stated what you are having trouble with.

Aside: I'd recommend changing your indentation to no more than 4 spaces and use it consistently. You've got code pushed all over the place there.

Sorry about the mess when I pasted code it moved it all around. I thought I had fixed. Anyway my problem is I need to call the method RollTheDice when the player starts the game and clicks rollDice and when they choose re-roll all. When I try to compile the program it gives me several errors. illegal start of expression : public static void RollTheDice(),and my public void actionPerformed(ActionEvent e) has errors says it needs a ; which I imagine is from the earlier error. Any help would be appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.