Solved

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

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
word0 challenge 3 58
mockito example issue 8 35
VB Script to add site to Java Exception List 4 13
Java Timer (static) 9 15
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

758 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

21 Experts available now in Live!

Get 1:1 Help Now