Solved

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

Posted on 2012-04-01
5
639 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

717 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