Solved

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 105
servlet  URL Rewriting 1 42
login jsp example 24 68
Java Eclipse Loop 3 21
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…
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…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

828 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