CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2011
    Posts
    19

    Removing Bubbles on my grid

    Hi,

    So my program consists of a grid (currently working only on 8x8 grid till i get it working) and it works like a bubble breaker game, whereby if you click a bubble it removes it along with all connected bubbles of the same colour. my issue comes when I click a cluster of bubbles, and it begins removing them, it wont remove bubbles which are above and to the left or right of it as it should... it works for below and to the left or right but cant figure out how to implement it for left and right.

    Hope fully the images below help clarify...

    screen 1: If i was to click where the black dot is, the yellow outlined bubbles should be removed...



    Screen 2 : but as you can see... it does not remove the one above to the right and would do the same for left too as i have tried many combinations and have tried lots of different coding:



    All my code is posted below for viewing and I will provide a link to all my files with bubble images etc.. for running yourself if need be... sorry if the code is a bit unreadable in its current state.

    GameInterface.java
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.*;
    import javax.swing.JComponent.*;
    
    public class GameInterface extends JFrame implements ActionListener{
    
    	GridForGame gridForGame = new GridForGame();
    
    
    	public GameInterface(){
    		super("Bubble Breaker");
    		buildGUI();
    		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    		pack();
    		setVisible(true);
    		setResizable(false);
    	}
    	
    	private void buildGUI(){
    		JPanel main = new JPanel(new BorderLayout());
    		add(main, BorderLayout.CENTER);
    		//JLabel statusbar;
    		
    		JPanel menu = new JPanel();
    		//menu.setLayout(new BoxLayout(menu, BoxLayout.Y_AXIS));
    		menu.setLayout(new GridLayout(2,3));
    		menu.setPreferredSize(new Dimension(150,100));
    		Dimension buttonSize = new Dimension(120,40);
    		
    		JButton small = new JButton();
    		small = addButton("8 x 8 Grid", "small", buttonSize);
    		JButton medium = new JButton();
    		medium = addButton("12 x 12 Grid", "medium", buttonSize);
    		JButton large = new JButton();
    		large = addButton("16 x 16 Grid", "large", buttonSize);
    		
    		JButton howTo = new JButton();
    		howTo = addButton("How To Play", "howToPlay", buttonSize);
    		JButton options = new JButton();
    		options = addButton("Options", "options", buttonSize);
    		JButton quit = new JButton();
    		quit = addButton("Quit", "quitGame", buttonSize);
    		
    		menu.add(small);
    		menu.add(medium);
    		menu.add(large);
    		menu.add(howTo);
    		menu.add(options);
    		menu.add(quit);
    		menu.setBorder(new TitledBorder(new EtchedBorder(4, Color.blue, Color.black), "Menu"));
    		/*JPanel buttons = new JPanel(new GridLayout());
    		buttons.add(menu);
    
    		menu.add(new JButton(""))
    
    
    		content.add(new JButton("Button 1"));
    		content.add(new JButton("2"));
    		content.add(new JLabel(""));  // for empty cell
    		content.add(new JButton("This is button three"));*/
    
    		
    		main.add(menu, BorderLayout.SOUTH);
    		main.add(gridForGame, BorderLayout.CENTER);
    		//statusbar = new JLabel("default");
    		//add(statusbar, BorderLayout.EAST);
    		
    	}
    	
    	public JButton addButton(String label, String command, Dimension size){
    		
    		JButton button = new JButton(label);
    		button.setPreferredSize(size);
    		button.setMaximumSize(size);
    		Dimension seperatorSize = new Dimension(0,1);
    		button.addActionListener(this);
    		button.setActionCommand(command);
    		//panel.add(Box.createRigidArea(seperatorSize));
    		//panel.add(button);
    
    		return button;
    	}
    	
    	public void actionPerformed(ActionEvent ae){
    		
    		String command = ae.getActionCommand();
    		if (command == "small"){
    			gridForGame.small();
    		}
    		
    		if (command == "medium"){
    			gridForGame.medium();
    		}
    		
    		if (command == "large"){
    			gridForGame.large();
    		}
    		
    		if(command.equals("howToPlay")){
    			// check source code for URL that goes here
    		}
    		
    		if(command.equals("options")){
    			// check source image for code that goes here
    		}
    		
    		if(command == "quitGame"){
    			int selectedValue = JOptionPane.showConfirmDialog(null, "If Your Quit The Game All Data Will Be Lost" +
    				"\n" + "Are You Sure You Want To Quit?", "Attention", JOptionPane.YES_NO_OPTION);
    			// Might be issues above due to splitting brackets onto lines
    			
    			if(selectedValue == JOptionPane.NO_OPTION)
    				return;
    			
    			else System.exit(0);
    			
    		}
    	}
    	
    	public static void main( String args[]){
    		GameInterface i = new GameInterface();
    		i.addWindowListener(new WindowAltering());
    	}
    	
    	static class WindowAltering extends WindowAdapter{
    		public void windowClosing(WindowEvent e ){
    			int selectedValue = JOptionPane.showConfirmDialog(null, "Closing This Window Will End the Game." +
    				"\n" + "Click Yes To Exit", "Exit Bubble Breaker Game?", JOptionPane.YES_NO_OPTION);
    			if(selectedValue == JOptionPane.NO_OPTION)
    				return;
    			
    			else System.exit(0);
    		}
    	}
    }
    GridForGame.java
    Code:
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.util.Random;
    import java.awt.Color.*;
    import java.awt.Cursor.*;
    import javax.swing.border.*;
    import java.awt.event.*;
    
    public class GridForGame extends JPanel {
    	JTextField myNumberArray[] = new JTextField[16];
    	MouseHandling handler = new MouseHandling();
    
    	JLabel statusbar = new JLabel("default");
    	JLabel tempLabel = new JLabel();
    
    	JPanel squareContainer = new JPanel();
    	Boolean handlerOn = false;
    	boolean colourChanged = false;
    
    	ImageIcon redBubble;
    	ImageIcon blueBubble;
    	ImageIcon yellowBubble;
    	ImageIcon orangeBubble;
    	ImageIcon greenBubble;
    	ImageIcon pinkBubble;
    	ImageIcon purpleBubble;
    	ImageIcon whiteBubble;
    
    	int pointCounter = 0;
    
    	Icon tempIconTwo;
    	JLabel tempFirst;
    
    
    
    	public void smallIcons(){
    		redBubble = new ImageIcon("images/redSmall.jpg");
    		blueBubble = new ImageIcon("images/blueSmall.jpg");
    		yellowBubble = new ImageIcon("images/yellowSmall.jpg");
    		orangeBubble = new ImageIcon("images/orangeSmall.jpg");
    		greenBubble = new ImageIcon("images/greenSmall.jpg");
    		pinkBubble = new ImageIcon("images/pinkSmall.jpg");
    		purpleBubble = new ImageIcon("images/purpleSmall.jpg");
    		whiteBubble = new ImageIcon("images/whiteSmall.jpg");
    	}
    
    	public void mediumIcons(){
    		redBubble = new ImageIcon("images/redMedium.jpg");
    		blueBubble = new ImageIcon("images/blueMedium.jpg");
    		yellowBubble = new ImageIcon("images/yellowMedium.jpg");
    		orangeBubble = new ImageIcon("images/orangeMedium.jpg");
    		greenBubble = new ImageIcon("images/greenMedium.jpg");
    		pinkBubble = new ImageIcon("images/pinkMedium.jpg");
    		purpleBubble = new ImageIcon("images/purpleMedium.jpg");
    	}
    
    	public void largeIcons(){
    		redBubble = new ImageIcon("images/redLarge.jpg");
    		blueBubble = new ImageIcon("images/blueLarge.jpg");
    		yellowBubble = new ImageIcon("images/yellowLarge.jpg");
    		orangeBubble = new ImageIcon("images/orangeLarge.jpg");
    		greenBubble = new ImageIcon("images/greenLarge.jpg");
    		pinkBubble = new ImageIcon("images/pinkLarge.jpg");
    		purpleBubble = new ImageIcon("images/purpleLarge.jpg");
    	}
    
    	public GridForGame(){
    		setLayout(new BorderLayout());
    		setBackground(Color.gray);	
    		setBorder(new EtchedBorder(4, Color.blue, Color.blue));
    		setPreferredSize(new Dimension(600,600));
    	}
    
    	public void small(){
    
    		smallIcons();
    		/*
    		 * Below empties the main game view so that
    		 * more can be 'painted' on it.
    		 */
    		squareContainer.removeAll();
    		revalidate();
    
    		// Set the grid for 8 x 8
            squareContainer.setLayout(new GridLayout(8,8));
            squareContainer.setPreferredSize(new Dimension(500,500));
            squareContainer.setMaximumSize(new Dimension(500,500));
    
            /*
    		 * Checks if a Mouse handler has already been
    		 * assigned to game view, if not, Add one!
    		 */
            if(!handlerOn){
            	squareContainer.addMouseListener(handler);
            	handlerOn = true;
       		}
    
            //statusbar.setText("default");
            //add(statusbar, BorderLayout.NORTH);
    
            for(int i=0; i<8; i++){
     
                for(int j=0; j<8; j++){
                    JLabel number = new JLabel();
    
                    number.setEnabled(true);
                    number.setOpaque(true);
                    number.setBorder(new MatteBorder(1, 1, 1, 1, Color.black));
    
                    for(int k=0; k<8; k++){
                        int n = new Random().nextInt(7);
                            switch(n){
                                case 0: number.setBackground(Color.red);
                                		number.setIcon(redBubble); break;
                                case 1: number.setBackground(Color.cyan);
                                		number.setIcon(orangeBubble); break;
                                case 2: number.setBackground(Color.blue);
                                		number.setIcon(blueBubble); break;
                                case 3: number.setBackground(Color.green);
                                		number.setIcon(greenBubble); break;
                                case 4: number.setBackground(Color.yellow);
                                		number.setIcon(yellowBubble); break;
                                case 5: number.setBackground(Color.magenta);
                                		number.setIcon(pinkBubble); break;
                                default:
                            }   
                        }
    
                         squareContainer.add(number);
                    }
            
                    //squareContainer.add(squares);
                }
            add(squareContainer);
            revalidate();
        }
    	
    	public void medium(){
    
    		mediumIcons();
    
    		/*
    		 * Below empties the main game view so that
    		 * more can be 'painted' on it.
    		 */
    		squareContainer.removeAll();
    		revalidate();
    
    		// Set the grid for 12 x 12
    		squareContainer.setLayout(new GridLayout(12,12));
    		squareContainer.setPreferredSize(new Dimension(500,500));
    		squareContainer.setMaximumSize(new Dimension(500,500));
    		
    		/*
    		 * Checks if a Mouse handler has already been
    		 * assigned to game view, if not, Add one!
    		 */
    		if(!handlerOn){
            	squareContainer.addMouseListener(handler);
            	handlerOn = true;
       		}
    
    		statusbar.setText("default");
    		add(statusbar, BorderLayout.NORTH);
    
    		for(int i=0; i<12; i++){
    			JPanel squares = new JPanel();
    			squares.setLayout(new GridLayout(1,1));
    			for(int j=0; j<12; j++){
    				JLabel number = new JLabel();
    				number.setEnabled(true);
    				number.setOpaque(true);
    				number.setBorder(new MatteBorder(1, 1, 1, 1, Color.black));
    
    				squares.add(number);
    				for(int k=0; k<12; k++){
    					int n = new Random().nextInt(7);
    						switch(n){
    							case 0: number.setBackground(Color.red);
                                		number.setIcon(redBubble); break;
                                case 1: number.setBackground(Color.cyan);
                                		number.setIcon(orangeBubble); break;
                                case 2: number.setBackground(Color.blue);
                                		number.setIcon(blueBubble); break;
                                case 3: number.setBackground(Color.green);
                                		number.setIcon(greenBubble); break;
                                case 4: number.setBackground(Color.yellow);
                                		number.setIcon(yellowBubble); break;
                                case 5: number.setBackground(Color.magenta);
                                		number.setIcon(pinkBubble); break;
    							default:
    						}	
    					}
    				}
    		
    				squareContainer.add(squares);
    			}
    		add(squareContainer);
    		revalidate();
    	}
    
    	public void large(){
    
    		largeIcons();
    
    		/*
    		 * Below empties the main game view so that
    		 * more can be 'painted' on it.
    		 */
    		squareContainer.removeAll();
    		revalidate();
    
    		// Set the grid for 16 x 16
    		squareContainer.setLayout(new GridLayout(16,16));
    		squareContainer.setPreferredSize(new Dimension(500,500));
    		squareContainer.setMaximumSize(new Dimension(500,500));
    		
    		/*
    		 * Checks if a Mouse handler has already been
    		 * assigned to game view, if not, Add one!
    		 */
    		if(!handlerOn){
            	squareContainer.addMouseListener(handler);
            	handlerOn = true;
       		}
    
    		statusbar.setText("default");
    		add(statusbar, BorderLayout.NORTH);
    
    		for(int i=0; i<16; i++){
    			JPanel squares = new JPanel();
    			squares.setLayout(new GridLayout(1,1));
    			for(int j=0; j<16; j++){
    				JLabel number = new JLabel();
    				number.setEnabled(true);
    				number.setOpaque(true);
    				number.setBorder(new MatteBorder(1, 1, 1, 1, Color.black));
    
    				squares.add(number);
    				for(int k=0; k<16; k++){
    					int n = new Random().nextInt(7);
    						switch(n){
    							case 0: number.setBackground(Color.red);
                                		number.setIcon(redBubble); break;
                                case 1: number.setBackground(Color.cyan);
                                		number.setIcon(orangeBubble); break;
                                case 2: number.setBackground(Color.blue);
                                		number.setIcon(blueBubble); break;
                                case 3: number.setBackground(Color.green);
                                		number.setIcon(greenBubble); break;
                                case 4: number.setBackground(Color.yellow);
                                		number.setIcon(yellowBubble); break;
                                case 5: number.setBackground(Color.magenta);
                                		number.setIcon(pinkBubble); break;
                                default:
    						}	
    					}
    				}
    		
    				squareContainer.add(squares);
    			}
    		add(squareContainer);
    		revalidate();
    	}
    
    	private class MouseHandling implements MouseListener{
    		public void mouseClicked(MouseEvent event){
    
    			statusbar.setText(String.format("Clicked at &#37;d,%d", event.getX(), event.getY()));
    			System.out.println(String.format("Component clicked was " + findComponentAt(event.getX(), event.getY()).getBackground()));
    
    
    			int x = event.getX();
        		int y = event.getY();
    
    			Component comp =  findComponentAt(x, y);
        		if (!(comp instanceof JLabel)) {
           			return;   
        		}
    
    
        		JLabel temp = (JLabel) comp;
    
        		Icon tempIcon = temp.getIcon();
    
        		go(x, y, tempIcon);
    
        		System.out.println("finished clicked methods...");
    
    		}	
    
    		public void mousePressed(MouseEvent event){
    			statusbar.setText("anything");
    		}
    
    		public void mouseReleased(MouseEvent event){
    			statusbar.setText(String.format("Released at %d,%d", event.getX(), event.getY()));
    		}
    
    		public void mouseEntered(MouseEvent event){
    			statusbar.setText("Entered");
    		}
    
    		public void mouseExited(MouseEvent event){
    			statusbar.setText("Exited");
    		}
    	}
    
    
    	public void goUp(int x, int y, Icon firstIcon){
    
    
    		Component comp =  findComponentAt(x, y);
        		if (!(comp instanceof JLabel)) {
           			return;   // make sure the clicked place is a JLabel
        		}
    
        		JLabel temp = (JLabel) comp;
        		Icon tempIcon = temp.getIcon();
    
        	/*
        	 * if this bubble is equal to the first bubble
        	 * clicked, then move them up / remove this bubble
        	*/
        	if(tempIcon == firstIcon){
        		move(x, y, "go");
        		goLeft(x, y, firstIcon);
        	    goRight(x, y, firstIcon);
        		go(x, y, firstIcon);
        	}
    
    	}
    
    	public void goDown(int x, int y, Icon firstIcon){
    
    		/*
    		 * go to the next Bubble down
    		*/
    		int thisY = y + 72;
    
    		if(thisY > 575){
    			return;
    		}
    
    
    		Component comp =  findComponentAt(x, thisY);
        		if (!(comp instanceof JLabel)) {
           			return;   // if it's not a JLabel get out of this method
        		}
    
        		JLabel temp = (JLabel) comp;
        		Icon tempIcon = temp.getIcon();
    
        	/*
    		 * if the bubble below is the same - Do below
    		*/
        	if(tempIcon == firstIcon){
        		go(x, thisY, firstIcon);
        	}
    
        	if(tempIcon != firstIcon){
        		return;
        	}
    
    	}
    
    	public void goLeft(int x, int y, Icon firstIcon){
    
    		/*
    		 * go to the bubble to the left
    		*/
    		int thisX = x - 72;
    
    		if(thisX < 0){
    			return;
    		}
    
    
    		Component comp =  findComponentAt(thisX, y);
        		if (!(comp instanceof JLabel)) {
           			return;   // if it's not a JLabel get out of this method
        		}
    
        		JLabel temp = (JLabel) comp;
        		Icon tempIcon = temp.getIcon();
        	
        	/*
    		 * if this bubble is the same as the left - DO
    		*/
        	if(tempIcon == firstIcon){
        		go(thisX, y, firstIcon);
        	}
    
        	if(tempIcon != firstIcon){
        		return;
        	}
    
    
    	}
    
    	public void goRight(int x, int y, Icon firstIcon){
    
    		System.out.println("in goRight...");
    
    		/*
    		 * go to the bubble 1 to the right...
    		*/
    		int thisX = x + 72;
    
    		if(thisX > 590){
    			return;
    		}
    
    
    		Component comp =  findComponentAt(thisX, y);
        		if (!(comp instanceof JLabel)) {
           			return;   // if it's not a JLabel get out of this method
        		}
    
        		JLabel temp = (JLabel) comp;
        		Icon tempIcon = temp.getIcon();
        	
        	/*
    		 * if the bubble to the right is the same - Do below
    		*/
    
        	if(tempIcon == firstIcon){
        		System.out.println("in goRight... Icons Match!");
        		go(thisX, y, firstIcon);
        	}
    
        	if(tempIcon != firstIcon){
        		return;
        	}
    	}
    
    	public void move(int x, int y, String move){
    
    		System.out.println("We are in the move Method...");
    
    		/*
    		 * look at the bubble above...
    		*/
    		int thisY = y - 72;
    
    		/*
    		 * if this bubble is the highest one, make it blank
    		 * in other words, remove it and RETURN;
    		*/
    		if(thisY < 0){
    			Component comp =  findComponentAt(x, y);
    			JLabel temp = (JLabel) comp;
    			temp.setIcon(whiteBubble);
    			return;
    		}
    
    
    		Component comp =  findComponentAt(x, y);
    		Component aboveComp = findComponentAt(x, thisY);
    
    
    
        	JLabel temp = (JLabel) comp;
        	JLabel aboveTemp = (JLabel) aboveComp;
    
       		/*
    		 * make this JLabel the bubble of the
    		 * bubble above then call this method again
    		 * to continue moving upward.
    		*/
       		Icon tempIcon = aboveTemp.getIcon();
       		Icon firstIcon = temp.getIcon();
       		temp.setIcon(tempIcon);
    
       		move(x, thisY, "go");
    
    		
    	}
    
    	public void go(int x, int y, Icon firstIcon){
    
    		goUp(x, y, firstIcon);
    		goLeft(x, y, firstIcon);
        	goRight(x, y, firstIcon);
       		goDown(x, y, firstIcon);
    		
    	}
    }
    link to download all the files....:

    www.didierd.co.uk/BubbleBreaker090311.zip

    Thank you for any help you could possibly give.

    Thanks!
    Last edited by DidierD; March 9th, 2011 at 05:54 PM.

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Removing Bubbles on my grid

    The code is a bit unreadable in its current state - the commented-out code confuses matters. If you don't have the time to tidy it up, I don't have the time or inclination to figure it out.

    Programs must be written for people to read, and only incidentally for machines to execute...
    H. Abelson and G. Sussman
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Feb 2011
    Posts
    19

    Re: Removing Bubbles on my grid

    hmmm i've tried neatening it up a bit, it's the way i try new things, add, then just comment out instead of removing the actual code, its silly, but i do it incase I want to backtrack. My bad and hopefully thats a little bit better.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Removing Bubbles on my grid

    I don't understand how the code is supposed to work - there are no comments explaining what the methods do, the methods don't make sense, and the variable names are worse than useless - 'thisX', 'tempIcon', etc...

    There's a 'move' method that takes a String argument that's never used, and recursively replaces the current cell colour with the colour of the cell above, replacing the top cell with white. What's that about?

    Theres a 'goUp' method that takes a cell coordinates and the cell's icon, retrieves the cell component, compares the icon with the icon passed in (it's the same cell, so the icon will always be the same), and calls the 'move' method. Then it retrieves the same component again (why?) checks to see if it's still a label (how could it change?). If the icon wasn't changed by 'move' if calls goLeft, goRight, and go, passing in the original icon... ??

    The 'goDown', 'goLeft' and 'goRight' methods look at the cell below, left, and right respectively, but I can't be arsed to figure out what they do after that.

    Why does the 'move' method always just move up the grid?
    Why doesn't the 'goUp' look anything like the 'goDown', 'goLeft' or 'goRight' methods?

    I can't make any sense of it.

    If I were you, I'd go back to my design algorithm on paper, step through a grid selection by hand and make sure that it works as specified - then go back to the code and make sure the code follows the design. Make sure that each method does only what you specified it to do - and make sure by testing it independently - if not by hand, with a unit test.

    More than the act of testing, the act of designing tests is one of the best bug preventers known. The thinking that must be done to create a useful test can discover and eliminate bugs before they are coded - indeed, test-design thinking can discover and eliminate bugs at every stage in the creation of software, from conception to specification, to design, coding and the rest...
    B. Bezier
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Feb 2011
    Posts
    19

    Re: Removing Bubbles on my grid

    I've changed it all and figured it out. But thanks anyway.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured