Solved

Cannot get a MineField Java program to stop at a set number of clicks

Posted on 2012-04-01
5
627 Views
Last Modified: 2012-04-01
Hello Experts,
I have written a program for class that allows a user to randomly click up to 10 JPanel objects turning each one white as they click them. After the total of 10 clicks the user wins with a message at the top of the frame and the game is over. There is a total of 20 JPanel squares. If they hit the wrong JPanel it turns that JPanel red and the game is over.  My problem is I am unable to write the correct if statements to cause the user to no longer be able to click any other JPanels after 10.
Everything else in the program appears to work fine.
Thank You

package mineField;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MineField extends JFrame implements ActionListener, MouseListener
{
	private final int ROWS = 4;
	private final int COLS = 5;
	private int NUM = ROWS * COLS;
	private JLabel cong = new JLabel("Play Mine Sweeper, Click the squares!", (int) CENTER_ALIGNMENT);	
	private JPanel congrats = new JPanel(new BorderLayout());
	private JPanel gamePanel = new JPanel(new GridLayout(ROWS, COLS, 3, 3));
	private JPanel[] gameSections = new JPanel[NUM];
	private Container topCon = getContentPane();
	private Container centerGame = getContentPane();
	private Color white1 = Color.WHITE;
	private Color blue1 = Color.BLUE;
	private Color red1 = Color.RED;	
	private int x;
	
	public MineField()
	{
		setTitle("Mine Field Game");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);						
		topCon.add(congrats, BorderLayout.NORTH);
		centerGame.add(gamePanel, BorderLayout.CENTER);
		congrats.add(cong);		
	    setSize(300, 400);
	    //Creates grid of 20 blue squares
	    for(x = 0; x < NUM; ++x)
	      {
	    	 gameSections[x] = new JPanel();
	         gamePanel.add(gameSections[x]);
	         gameSections[x].addMouseListener(this);
	         gameSections[x].setBackground(blue1);
	         
	      }
	   }
	
	 public void mouseClicked(MouseEvent e)
	   {
		    Object panelClick = e.getSource();
		    int y = 0;		    
		    for(y = 0; y < NUM; ++y)
		      {		    	 
			   if(panelClick == gameSections[y])
		        {
			       gameSections[y].setBackground(white1);
				   cong.setText("Excellent, Keep Going");
		        }
			   else if(e.getClickCount() == 10)
		        {
				   e.setSource(null);
			       cong.setText("You WON!");   	   
		        }			    
			   else if(panelClick == gameSections[12])
			    {				   
				   gameSections[12].setBackground(red1);
			       cong.setText("You FAILED");
			    }			   
		     }		     		    	 			   
}

	   public void mouseEntered(MouseEvent e)
	   {

	   }
	   public void mouseExited(MouseEvent e)
	   {

	   }

	   public void mousePressed(MouseEvent e)
	   {
	   }

	   public void mouseReleased(MouseEvent e)
	   {
	   }

	public static void main(String[] args) 
	{
		MineField frame = new MineField();
	    frame.setVisible(true);
	}
}

Open in new window

0
Comment
Question by:robertben
  • 4
5 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37793946
You should not do e.getClickCount() - this counds clicks in this one event.
You should creeate variable instance variavle clicks set it to zero first tand then count each time use clicks on good panel-
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37793954
Something like that:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MineField extends JFrame implements  MouseListener
{

    int clicks = 0;
	private final int ROWS = 4;
	private final int COLS = 5;
	private int NUM = ROWS * COLS;
	private JLabel cong = new JLabel("Play Mine Sweeper, Click the squares!", (int) CENTER_ALIGNMENT);
	private JPanel congrats = new JPanel(new BorderLayout());
	private JPanel gamePanel = new JPanel(new GridLayout(ROWS, COLS, 3, 3));
	private JPanel[] gameSections = new JPanel[NUM];
	private Container topCon = getContentPane();
	private Container centerGame = getContentPane();
	private Color white1 = Color.WHITE;
	private Color blue1 = Color.BLUE;
	private Color red1 = Color.RED;
	private int x;

	public MineField()
	{
		setTitle("Mine Field Game");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		topCon.add(congrats, BorderLayout.NORTH);
		centerGame.add(gamePanel, BorderLayout.CENTER);
		congrats.add(cong);
	    setSize(300, 400);
	    //Creates grid of 20 blue squares
	    for(x = 0; x < NUM; ++x)
	      {
	    	 gameSections[x] = new JPanel();
	         gamePanel.add(gameSections[x]);
	         gameSections[x].addMouseListener(this);
	         gameSections[x].setBackground(blue1);

	      }
	   }

	 public void mouseClicked(MouseEvent e)
	   {
              clicks++;
		    Object panelClick = e.getSource();
		    int y = 0;
		    for(y = 0; y < NUM; ++y)
		      {
                  if(clicks == 10){
                    cong.setText("You WON!");
                      return;
                  }
			   if(panelClick == gameSections[y])
		        {

			       gameSections[y].setBackground(white1);
				   cong.setText("Excellent, Keep Going");
		        }
			   else if(clicks == 10)
		        {
				   e.setSource(null);
			       cong.setText("You WON!");
		        }
			   else if(panelClick == gameSections[12])
			    {
				   gameSections[12].setBackground(red1);
			       cong.setText("You FAILED");
			    }
		     }
}

	   public void mouseEntered(MouseEvent e)
	   {

	   }
	   public void mouseExited(MouseEvent e)
	   {

	   }

	   public void mousePressed(MouseEvent e)
	   {
	   }

	   public void mouseReleased(MouseEvent e)
	   {
	   }

	public static void main(String[] args)
	{
		MineField frame = new MineField();
	    frame.setVisible(true);
	}
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37793961
This is a little bit better - it disables the mouse listeners after winning or losing so that user cannot click further after the end of game

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MineField extends JFrame implements  MouseListener
{

    int clicks = 0;
	private final int ROWS = 4;
	private final int COLS = 5;
	private int NUM = ROWS * COLS;
	private JLabel cong = new JLabel("Play Mine Sweeper, Click the squares!", (int) CENTER_ALIGNMENT);
	private JPanel congrats = new JPanel(new BorderLayout());
	private JPanel gamePanel = new JPanel(new GridLayout(ROWS, COLS, 3, 3));
	private JPanel[] gameSections = new JPanel[NUM];
	private Container topCon = getContentPane();
	private Container centerGame = getContentPane();
	private Color white1 = Color.WHITE;
	private Color blue1 = Color.BLUE;
	private Color red1 = Color.RED;
	private int x;

	public MineField()
	{
		setTitle("Mine Field Game");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		topCon.add(congrats, BorderLayout.NORTH);
		centerGame.add(gamePanel, BorderLayout.CENTER);
		congrats.add(cong);
	    setSize(300, 400);
	    //Creates grid of 20 blue squares
	    for(x = 0; x < NUM; ++x)
	      {
	    	 gameSections[x] = new JPanel();
	         gamePanel.add(gameSections[x]);
	         gameSections[x].addMouseListener(this);
	         gameSections[x].setBackground(blue1);

	      }
	   }

	 public void mouseClicked(MouseEvent e)
	   {

		    Object panelClick = e.getSource();
		    int y = 0;
		    for(y = 0; y < NUM; ++y)
		      {
                  if(clicks == 10){
                    cong.setText("You WON!");
                          for(x = 0; x < NUM; ++x){
                              gameSections[x].removeMouseListener(this);
                          }

                      return;
                  }
			   if(panelClick == gameSections[y])
		        {

			       gameSections[y].setBackground(white1);
                      clicks++;
				   cong.setText("Excellent, Keep Going");
		        }

			   else if(panelClick == gameSections[12])
			    {
				   gameSections[12].setBackground(red1);
			       cong.setText("You FAILED");
                                          for(x = 0; x < NUM; ++x){
                              gameSections[x].removeMouseListener(this);
                          }
                    return;

			    }
		     }
}

	   public void mouseEntered(MouseEvent e)
	   {

	   }
	   public void mouseExited(MouseEvent e)
	   {

	   }

	   public void mousePressed(MouseEvent e)
	   {
	   }

	   public void mouseReleased(MouseEvent e)
	   {
	   }

	public static void main(String[] args)
	{
		MineField frame = new MineField();
	    frame.setVisible(true);
	}
}

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 400 total points
ID: 37793971
I am also guessing you want to use random generator
 to place a mine each time, otherwise it is no fun

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MineField extends JFrame implements  MouseListener
{

    int clicks = 0;
	private final int ROWS = 4;
	private final int COLS = 5;
	private int NUM = ROWS * COLS;
	private JLabel cong = new JLabel("Play Mine Sweeper, Click the squares!", (int) CENTER_ALIGNMENT);
	private JPanel congrats = new JPanel(new BorderLayout());
	private JPanel gamePanel = new JPanel(new GridLayout(ROWS, COLS, 3, 3));
	private JPanel[] gameSections = new JPanel[NUM];
	private Container topCon = getContentPane();
	private Container centerGame = getContentPane();
	private Color white1 = Color.WHITE;
	private Color blue1 = Color.BLUE;
	private Color red1 = Color.RED;
	private int x;
    Random r = new Random();
    int mine;

	public MineField()
	{
		setTitle("Mine Field Game");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		topCon.add(congrats, BorderLayout.NORTH);
		centerGame.add(gamePanel, BorderLayout.CENTER);
		congrats.add(cong);
	    setSize(300, 400);
        mine = r.nextInt(20);
	    //Creates grid of 20 blue squares
	    for(x = 0; x < NUM; ++x)
	      {
	    	 gameSections[x] = new JPanel();
	         gamePanel.add(gameSections[x]);
	         gameSections[x].addMouseListener(this);
	         gameSections[x].setBackground(blue1);

	      }
	   }

	 public void mouseClicked(MouseEvent e)
	   {

		    Object panelClick = e.getSource();
		    int y = 0;
		    for(y = 0; y < NUM; ++y)
		      {
                  if(clicks == 10){
                    cong.setText("You WON!");
                          for(x = 0; x < NUM; ++x){
                              gameSections[x].removeMouseListener(this);
                          }

                      return;
                  }
			   if(panelClick == gameSections[y])
		        {

			       gameSections[y].setBackground(white1);
                      clicks++;
				   cong.setText("Excellent, Keep Going");
		        }

			   else if(panelClick == gameSections[mine])
			    {
				   gameSections[mine].setBackground(red1);
			       cong.setText("You FAILED");
                                          for(x = 0; x < NUM; ++x){
                              gameSections[x].removeMouseListener(this);
                          }
                    return;

			    }
		     }
}

	   public void mouseEntered(MouseEvent e)
	   {

	   }
	   public void mouseExited(MouseEvent e)
	   {

	   }

	   public void mousePressed(MouseEvent e)
	   {
	   }

	   public void mouseReleased(MouseEvent e)
	   {
	   }

	public static void main(String[] args)
	{
		MineField frame = new MineField();
	    frame.setVisible(true);
	}
}

Open in new window

0
 

Author Closing Comment

by:robertben
ID: 37794139
Excellent, Thank you. It looks like I wasn't to far off from getting it to work. I got burned out.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Free Alternative to JIRA 4 90
Java. Convert method from recursion based to iteration based ( loop based ) 6 72
bitbucket vs gitbucket 3 55
maven project error 5 47
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now