Solved

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

Posted on 2012-04-01
5
631 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 77
recursion example 16 124
stackato and cloud 4 85
Java Restore security prompts not working 10 9
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

785 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