Results 1 to 5 of 5
  1. #1
    SnarkKnuckle is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default Dice game help and arrays

    I'm unsure about what to do with this section of code, errors are on lines 368 - 388, it's underlined in red using Eclipse and says, pointDie*JLabel1 (* is 1-5) as it does this on all lines through this section, cannot be resolved. I have a feeling it has something to do with the arrays and variables above, I need some help and any other issues you may come across. I know rollFives and moveDice is undefined at this time, and change player as this was going to be assigned for two players only but thought I'd try to let it decide 1-5 players. The scores are also suppose to populate in the JTextField on the right depending on how many players were selected....so lost. Any help will be appreciated.

    BTW the other errors are at 463 in the else if , 479 with the turnpoints undefined


    Java Code:
    // Name
    // Play a basic game of Zonk, 10,000
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.text.*;
    
    public class zonk extends JFrame 
    {
       // JPanel and TitledBorder to contain dice
       private JPanel pointDiceJPanel;
       private TitledBorder pointDiceTitledBorder;
       
       // JLabels to display the die images in pointDiceJPanel
       private JLabel pointDie1JLabel;
       private JLabel pointDie2JLabel;
       private JLabel pointDie3JLabel;
       private JLabel pointDie4JLabel;
       private JLabel pointDie5JLabel;
    
       
       // JLabels to display the die images from the rolls of the dice
       private JLabel die1JLabel;
       private JLabel die2JLabel;
       private JLabel die3JLabel;
       private JLabel die4JLabel;
       private JLabel die5JLabel;
    
       private JCheckBox die1KeepJCheckBox;
       private JCheckBox die2KeepJCheckBox;
       private JCheckBox die3KeepJCheckBox;
       private JCheckBox die4KeepJCheckBox;
       private JCheckBox die5KeepJCheckBox;
    
       private JTextArea rollPointsJTextField;
       private JComboBox selectScoreJComboBox;
       private JLabel pointsJLabel;
       private JTextField pointsJTextField;
       
             
       // JButtons to allow user to interact with game
       private JButton playJButton;
       private JButton addScoreJButton;   
       private JButton rollJButton;
       private JButton stopJButton;
       private JButton buildScoreCardJButton;
       
       // JLabel and JTextField to show results of game
       private JLabel resultJLabel;
       private JTextField resultJTextField;
    
    
    
       // instance variables
       private int numberOfPlayers = 1;
       private int numberRolls = 0;
       private boolean newGame = true; 
       private int[] scores = {0,0,0,0,0};
       private String[] scoreNames = {"", "", "", "", ""};
       private String[] playerNames = {"Player1", "Player2", "Player3", "Player4", "Player5"};
       private boolean[] scoresUsed = new boolean[scores.length];
       private int[] diceFace = {1, 2, 3, 4, 5};
       private int turnPoints = 0;
       
       
       private int myPoint = 0;
       private int[] roll = {1, 2, 3, 4, 5}; 
       private int sumOfDice = 0;
       private Random randomObject = new Random();
       
       // no-argument constructor
       public zonk()
       {
          createUserInterface();
       }
       
       // create and position GUI components; register event handlers
       private void createUserInterface()
       {
          // get content pane for attaching GUI components
          Container contentPane = getContentPane();
    
          // enable explicit positioning of GUI components
          contentPane.setLayout( null );
    
          // set up pointDiceTitledBorder for use with pointDiceJPanel
          pointDiceTitledBorder = new TitledBorder( "Keep" );
         
          // set up pointDiceJPanel
          pointDiceJPanel = new JPanel();
          pointDiceJPanel.setBounds( 10, 16, 490, 116 );
          pointDiceJPanel.setLayout( null );
          pointDiceJPanel.setBorder( pointDiceTitledBorder );
          contentPane.add( pointDiceJPanel );
          
          // set up pointDie1JLabel
          pointDie1JLabel = new JLabel();
          pointDie1JLabel.setBounds( 14, 34, 64, 56 );
          pointDiceJPanel.add( pointDie1JLabel );
          
          // set up pointDie2JLabel
          pointDie2JLabel = new JLabel();
          pointDie2JLabel.setBounds( 105, 34, 64, 56 );
          pointDiceJPanel.add( pointDie2JLabel );
      
                // set up pointDie2JLabel
          pointDie3JLabel = new JLabel();
          pointDie3JLabel.setBounds( 200, 34, 64, 56 );
          pointDiceJPanel.add( pointDie3JLabel );
          
               // set up pointDie2JLabel
          pointDie4JLabel = new JLabel();
          pointDie4JLabel.setBounds( 292, 34, 64, 56 );
          pointDiceJPanel.add( pointDie4JLabel );
          
                // set up pointDie2JLabel
          pointDie5JLabel = new JLabel();
          pointDie5JLabel.setBounds( 383, 34, 64, 56 );
          pointDiceJPanel.add( pointDie5JLabel );
          
          // set up die1JLabel
          die1JLabel = new JLabel();
          die1JLabel.setBounds( 14, 150, 64, 64 );
          contentPane.add( die1JLabel );
     
                // set up JCheckBox
          die1KeepJCheckBox = new JCheckBox();
          die1KeepJCheckBox.setBounds( 14, 220, 90, 24 );
          die1KeepJCheckBox.setText( "Keep die 1" );
          contentPane.add( die1KeepJCheckBox );
          
          die2KeepJCheckBox = new JCheckBox();
          die2KeepJCheckBox.setBounds( 105, 220, 90, 24 );
          die2KeepJCheckBox.setText( "Keep die 2" );
          contentPane.add( die2KeepJCheckBox );
          
          die3KeepJCheckBox = new JCheckBox();
          die3KeepJCheckBox.setBounds( 200, 220, 90, 24 );
          die3KeepJCheckBox.setText( "Keep die 3" );
          contentPane.add( die3KeepJCheckBox );
          
          die4KeepJCheckBox = new JCheckBox();
          die4KeepJCheckBox.setBounds( 292, 220, 90, 24 );
          die4KeepJCheckBox.setText( "Keep die 4" );
          contentPane.add( die4KeepJCheckBox );
          
          die5KeepJCheckBox = new JCheckBox();
          die5KeepJCheckBox.setBounds(383, 220, 90, 24 );
          die5KeepJCheckBox.setText( "Keep die 5" );
          contentPane.add( die5KeepJCheckBox );
     
          
          // set up die2JLabel
          die2JLabel = new JLabel();
          die2JLabel.setBounds( 105, 150, 64, 56 );
          contentPane.add( die2JLabel );
     
                // set up die2JLabel
          die3JLabel = new JLabel();
          die3JLabel.setBounds( 200, 150, 64, 56 );
          contentPane.add( die3JLabel );
          
                // set up die2JLabel
          die4JLabel = new JLabel();
          die4JLabel.setBounds( 292, 150, 64, 56 );
          contentPane.add( die4JLabel );
          
                // set up die2JLabel
          die5JLabel = new JLabel();
          die5JLabel.setBounds( 383, 150, 64, 56 );
          contentPane.add( die5JLabel );
     
        
          
          // set up playJButton
          playJButton = new JButton();
          playJButton.setBounds( 20, 256, 88, 23 );
          playJButton.setText( "Play" );
          contentPane.add( playJButton );
          playJButton.addActionListener(
             
             new ActionListener() // anonymous inner class
             {
                 // event handler called when playJButton is clicked
                 public void actionPerformed ( ActionEvent event )
                 {
                    playJButtonActionPerformed( event );
                 }
    
             } // end anonymous inner class
    
          ); // end call to addActionListener      
     
          selectScoreJComboBox = new JComboBox();
          selectScoreJComboBox.setBounds(286,320,135,21);
          selectScoreJComboBox.setMaximumRowCount(8);
        
        
          contentPane.add(selectScoreJComboBox);
          
           
          
         
     
          /*// set up resultJLabel
          pointsJLabel = new JLabel();
          pointsJLabel.setBounds( 200, 320, 48, 16 );
          pointsJLabel.setText( "Points from roll:" );
          contentPane.add( pointsJLabel );*/
                
         /* // set up resultJTextField
          pointsJTextField = new JTextField();
          pointsJTextField.setBounds( 250, 320, 188, 24 );
          pointsJTextField.setHorizontalAlignment( JTextField.CENTER );
          contentPane.add( pointsJTextField );*/
          
       
          // set up rollJButton
          rollJButton = new JButton();
          rollJButton.setBounds( 220, 256, 88, 23 );
          rollJButton.setText( "Roll" );
          rollJButton.setEnabled( false );
          contentPane.add( rollJButton );
          rollJButton.addActionListener(
                
             new ActionListener() // anonymous inner class
             {
                // event handler called when rollJButton is clicked
                public void actionPerformed ( ActionEvent event )
                {
                   rollJButtonActionPerformed( event );
                }
             
             } // end anonymous inner class
    
          ); // end call to addActionListener
          // set up stopJButton
          stopJButton = new JButton();
          stopJButton.setBounds( 400, 256, 88, 23 );
          stopJButton.setText( "Stop" );
          stopJButton.setEnabled( false );
          contentPane.add( stopJButton );
          stopJButton.addActionListener(
                
             new ActionListener() // anonymous inner class
             {
                // event handler called when stopJButton is clicked
                public void actionPerformed ( ActionEvent event )
                {
                	stopJButtonActionPerformed( event );
                }
             
             } // end anonymous inner class
    
          ); // end call to addActionListener
          // set up resultJLabel
          resultJLabel = new JLabel();
          resultJLabel.setBounds( 20, 290, 48, 16 );
          resultJLabel.setText( "Result:" );
          contentPane.add( resultJLabel );
          
          // set up resultJTextField
          resultJTextField = new JTextField();
          resultJTextField.setBounds( 20, 320, 188, 24 );
          resultJTextField.setHorizontalAlignment( JTextField.CENTER );
          resultJTextField.setEditable( false );
          contentPane.add( resultJTextField );
     
          // set up resultJTextField
          rollPointsJTextField = new JTextArea();
          rollPointsJTextField.setBounds(550, 16, 238, 440 );
          contentPane.add( rollPointsJTextField);
          
          buildScoreCard();
        
          // set properties of application's window
          setTitle( "Zonk a dice game" ); // set title bar string
          setSize( 800, 550 );      // set window size
          setVisible( true );       // display window
          
       } // end method createUserInterface
       
       // start new game of craps
       private void buildScoreCard()
       {
        
           rollPointsJTextField.setText("           PLAYERS AND THEIR SCORES \n" );
       for(int players = 0; players < numberOfPlayers; players++)
    	   { 
    		   rollPointsJTextField.append("   " + String.valueOf(players + 1) + "\t" + String.valueOf(scores[players]) + "\n");   
    	   }
       
       }
       private void playJButtonActionPerformed( ActionEvent event )
       {
          if(newGame)
          {
        	 String playersAnswer =  JOptionPane.showInputDialog("How many players will be participating, limit of five?");
        	 numberOfPlayers = Integer.parseInt(playersAnswer.toString());
        	 selectScoreJComboBox.removeAll();
        	 rollPointsJTextField.setText("" );
        	// pointsJTextField.setText("");
        	 for (int x=0; x < 5; x++)
        	 {
        		scoreNames[x] = "";
        		scores[x] = 0;
        	 }
        	 
        	 for (int x=0; x < numberOfPlayers; x++)
        	 {
        		scoreNames[x] = playerNames[x];
        		selectScoreJComboBox.insertItemAt(scoreNames[x], x);
        	 }
        	 selectScoreJComboBox.setSelectedIndex(0);
          }
      
    
    	   resultJTextField.setText("New Game!!!");
               pointDiceJPanel.repaint();
    
    
               playJButton.setEnabled( false );
               rollJButton.setEnabled( true );
               stopJButton.setEnabled(true);
               // change the state of the JButtons
           
    
       } // end method playJButtonActionPerformed
    
       private void stopJButtonActionPerformed( ActionEvent event )
       {
    	   sumOfDice = 0;
    	   if(selectScoreJComboBox.getSelectedIndex()< numberOfPlayers -1)
    	   {
    		   selectScoreJComboBox.setSelectedIndex(selectScoreJComboBox.getSelectedIndex()+1);
    	   }
    	   else if(selectScoreJComboBox.getSelectedIndex()== numberOfPlayers -1 )
    	   {
    		   selectScoreJComboBox.setSelectedIndex(0);   
    	   }
      
    
           
    
       } // end method playJButtonActionPerformed
       private void addScoreJButtonActionPerformed( ActionEvent event )
       {
    		numberRolls ++;
    		addScoreJButton.setEnabled( false );
    		
    		 // reset roll array to zero
    		for ( int y=1; y<6; y++ )
    		{
    			roll [y]= 0;
    		}
    		
    		int rollPoints = 0;
    		int countOne = 0;
    		int countTwo = 0;
    		int countThree = 0;
    		int countFour = 0;
    		int countFive = 0;
    		int countSix = 0;
    		
    		// load roll array with dice kept for score
    		if (die1KeepJCheckBox.isSelected() && pointDie1JLabel1.getIcon() == null)
    		{
    			roll [1] = diceFace[1];
    		}
    		else roll [1] = 0;
    		if (die2KeepJCheckBox.isSelected() && pointDie2JLabel1.getIcon() == null)
    		{
    			roll [2] = diceFace [2];
    		}
    		else roll [2] = 0;
    		if (die3KeepJCheckBox.isSelected() && pointDie3JLabel1.getIcon() == null)
    		{
    			roll [3] = diceFace [3];
    		}
    		else roll [3] = 0;
    		if (die4KeepJCheckBox.isSelected() && pointDie4JLabel1.getIcon() == null)
    		{
    			roll [4] = diceFace [4];
    		}
    		else roll [4] = 0;
    		if (die5KeepJCheckBox.isSelected() && pointDie5JLabel1.getIcon() == null)
    		{
    			roll [5] = diceFace [5];
    		}
    		else roll [5] = 0;
    		
    		// determine dice faces rolled
    		int x = 1;
    		do
    		{
    			if (roll[x] == 1)
    				countOne +=1;
    			if (roll[x] == 2)
    				countTwo += 1;
    			if (roll[x] == 3)
    				countThree += 1;
    			if (roll[x] == 4)
    				countFour += 1;
    			if (roll[x] == 5)
    				countFive += 1;
    			if (roll[x] == 6)
    				countSix += 1;
    			x++;
    		}while (x < 6);
    		
    		// determine points for selected dice
    		if (countOne == 3)
    			rollPoints += 1000; // Three 1's
    		
    		if (countTwo == 3)
    			rollPoints += 200; // Three 2's
    		
    		if (countThree == 3)
    			rollPoints += 300; // Three 3's
    		
    		if (countFour == 3)
    			rollPoints += 400; // Three 4's
    		
    		if (countFive >= 3)
    			rollPoints += 500; // Three 5's
    		
    		if (countSix == 3)
    			rollPoints += 600; // Three 6's
    		
    		if (countOne > 3)
    			rollPoints += (countOne - 3) * 100; // more than three 1's (extra's 100 points each)
    		
    		if (countOne > 0 && countOne < 3)
    			rollPoints += countOne * 100; // less than three 1's (100 points each )
    		
    		if (countFive > 3)
    			rollPoints += (countFive - 3) * 50; // more than three 5's (extra's 50 points each)
    		
    		if (countFive > 0 && countFive < 3)
    			rollPoints += countFive * 50; // less than three 5's (50 points each)
    		
    		// score fives and add to roll points without moving them
    		rollFives();
    		rollPoints += scoreFives;
    		
    		// move dice after scoring them
    		moveDice();
    		
    		rollPointsJTextField.setText( String.valueOf(rollPoints)); //display roll points
    		
    		// roll points zero *ZONK*
    		if (rollPoints == 0)
    		{
    			resultJTextField.setText( "Zonk!!!" );
    			resultJTextField.setForeground(Color.red );
    			rollPoints = 0;
    			rollPointsJTextField.setText( "0" );
    			turnPoints = 0;
    			changePlayer();
    		}
    		else if  (scores[indexForArrays] == 0 && turnPoints < 500 && numberRolls >= 3 ) 
    			// 500 points to get on board in 3 rolls
    		{
    			resultJTextField.setText( "ZONK!!!" );
    			resultJTextField.setForeground(Color.red );
    			rollPoints = 0;
    			rollPointsJTextField.setText( "0" );
    			turnPoints = 0;
    			changePlayer();
    		}
    		else 
    			{
    			turnPoints + rollPoints;
    			stopJButton.setEnabled(true);
    			}
    		
    		turnPointsJTextField.setText( String.valueOf(turnPoints)); // display turn points
    		
      } //end addScore method
    
    
       
       // continue the game
       private void rollJButtonActionPerformed( ActionEvent event )
       {
          if(die1KeepJCheckBox.isSelected() && pointDie1JLabel.getIcon() == null)
          {
                pointDie1JLabel.setIcon( die1JLabel.getIcon() );
                die1JLabel.setIcon(null);
          }
          if (die2KeepJCheckBox.isSelected() && pointDie2JLabel.getIcon() == null)
          {
                pointDie2JLabel.setIcon( die2JLabel.getIcon() );
                 die2JLabel.setIcon(null);
          }
          if (die3KeepJCheckBox.isSelected() && pointDie3JLabel.getIcon() == null)
          {
                pointDie3JLabel.setIcon( die3JLabel.getIcon() );
                 die3JLabel.setIcon(null);
          }
          if (die4KeepJCheckBox.isSelected() && pointDie4JLabel.getIcon() == null)
          {
                pointDie4JLabel.setIcon( die4JLabel.getIcon() );
                 die4JLabel.setIcon(null);
          }
          if (die5KeepJCheckBox.isSelected() && pointDie5JLabel.getIcon() == null)
          {
                pointDie5JLabel.setIcon( die5JLabel.getIcon() );
                 die5JLabel.setIcon(null);
          }
          //make the dice reappear if the user unselects a kept dice
          if(!die1KeepJCheckBox.isSelected() && die1JLabel.getIcon()==null)
          {
              die1JLabel.setIcon( die1JLabel.getIcon() );
              pointDie1JLabel.setIcon(null);
          }
          if(!die2KeepJCheckBox.isSelected() && die2JLabel.getIcon()==null)
          {
              die2JLabel.setIcon( die2JLabel.getIcon() );
              pointDie2JLabel.setIcon(null);          
          }
          if(!die3KeepJCheckBox.isSelected() && die3JLabel.getIcon()==null)
          {
              die3JLabel.setIcon( die3JLabel.getIcon() );
              pointDie3JLabel.setIcon(null);          
          }
          if(!die4KeepJCheckBox.isSelected() && die4JLabel.getIcon()==null)
          {
              die4JLabel.setIcon( die4JLabel.getIcon() );
              pointDie4JLabel.setIcon(null);          
          }
          if(!die5KeepJCheckBox.isSelected() && die5JLabel.getIcon()==null)
          {
              die5JLabel.setIcon( die5JLabel.getIcon() );
              pointDie5JLabel.setIcon(null);          
          }
       
                 sumOfDice += rollDice();       
          
          
       }
       
    // generate random die rolls
       private int rollDice()
       {
    	   if (!(die1KeepJCheckBox.isSelected()))
    	   {
    		   int die1 = 1 + randomObject.nextInt(6);
    		   displayDie(die1JLabel, die1);
    	   }
    
    	   if (!(die2KeepJCheckBox.isSelected()))
    	   {
    		   int die2 = 1 + randomObject.nextInt(6);
    		   displayDie(die2JLabel, die2);
    	   }
    
    	   if (!(die3KeepJCheckBox.isSelected()))
    	   {
    		   int die3 = 1 + randomObject.nextInt(6);
    		   displayDie(die3JLabel, die3);
    	   }
    
    	   if (!(die4KeepJCheckBox.isSelected()))
    	   {
    		   int die4 = 1 + randomObject.nextInt(6);
    		   displayDie(die4JLabel, die4);
    	   }
    
    	   if (!(die5KeepJCheckBox.isSelected()))
    	   {
    		   int die5 = 1 + randomObject.nextInt(6);
    		   displayDie(die5JLabel, die5);
    	   }  
          
          
    
    	  
    
          return 100; // return sum of dice values
          
       } // end method rollDice
       
       // displays the die image
       private void displayDie( JLabel picDieJLabel, int face )
       {
          ImageIcon image = 
             new ImageIcon( "G:/Java Programming/Dice/die" + face + ".png" );
    
          // display die images in picDieJLabel      
          picDieJLabel.setIcon( image );
    
       } // end method displayDie
       
       // main method
       public static void main( String args[] )
       {
          zonk application = new zonk();
          application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    
       } // end method main
    
    } // end class zonk

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    TL DR

    I'm not wading thorugh all that code. Post the EXACT error message(s) you get and the relevant line of code.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You are lucky there is a search feature.

    Java Code:
    private JLabel pointDie1JLabel;
    pointDie1JLabel1.getIcon()

  4. #4
    SnarkKnuckle is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    indexForArrays canot be resolved to a variable

    else if (scores[indexForArrays] == 0 && turnPoints < 500 && numberRolls >= 3 )
    // 500 points to get on board in 3 rolls


    I got the previous issues figured out as it was just a typing mistake on my part.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Do you plan on posting every compiler message you get here instead of trying to think it through and solve it yourself?

    Please show me where in you 10 gazillion lines of code you have declared the "indexForArrays" variable.

Similar Threads

  1. Unreachable statement Pig Dice Game
    By noturn10 in forum New To Java
    Replies: 2
    Last Post: 11-22-2009, 01:36 AM
  2. Help with a dice game.
    By hero in forum AWT / Swing
    Replies: 14
    Last Post: 07-26-2009, 12:50 PM
  3. Help with dice game...student seeking advice
    By waparson in forum New To Java
    Replies: 3
    Last Post: 07-21-2008, 04:31 PM
  4. Help debugging a dice game
    By Windoze in forum New To Java
    Replies: 7
    Last Post: 11-22-2007, 02:01 AM
  5. help debugging a dice game
    By Windoze in forum Advanced Java
    Replies: 0
    Last Post: 11-16-2007, 11:28 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
  •