CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Threaded View

  1. #1
    Join Date
    Feb 2011
    Posts
    19

    Is creating a 'Pathway' possible? / Program is deviating

    I use the term pathway lightly here...

    The issue is such that, basically I have a grid of items. I want my program to go through a loop for each element in the grid, then have my program go onto each element in the grid, simulate clicking it and SIMULATE removing that object from the grid and stores the results somewhere. once it has simulated doing this to all the object, its takes the details of the one with the best result and removes it for real this time.

    it looks like this:

    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.*;
    import java.awt.Robot;
    import java.awt.event.InputEvent;
    import java.awt.event.KeyEvent;
    
    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();
    	JLabel number = new JLabel();
    	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;
    
    	// below is used to calculate the space
    	// to the next bubble (the space changes by size)
    	int bubbleSize = 0;
    	int[][] compBoard = new int [1][1];
    	int highestGroup = 0;
    	int compX = 0;
    	int compY = 0;
    	int original;
    	int removed;
    	int add;
    	int highestX;
    	int highestY;
    	int scLocX;
    	int scLocY;
    
    	String compString;
    
    	Icon tempIconTwo;
    	JLabel tempFirst;
    
    	boolean remove = true;
    	boolean compPlaying = false;
    
    
    
    	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");
    		whiteBubble = new ImageIcon("images/whiteMedium.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");
    		whiteBubble = new ImageIcon("images/whiteLarge.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(){
    
    		compPlaying = false;
    		compString = "";
    		remove = true;
    		bubbleSize = 72;
    		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;
       		}
    
    
            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);
                    }
            
                }
            add(squareContainer);
            revalidate();
        }
    	
    	public void medium(){
    		compPlaying = false;
    		compString = "";
    		remove = true;
    
    		bubbleSize = 48;
    
    		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;
       		}
    
    		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(){
    		compString = "";
    		compPlaying = false;
    		remove = true;
    
    		bubbleSize = 36;
    
    		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;
       		}
    
    		for(int i=0; i<16; i++){
    			JPanel squares = new JPanel();
    			squares.setLayout(new GridLayout(1,1));
    			for(int j=0; j<16; j++){
    				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 %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)) {
        			System.out.println("doesnt work in mouseClicked either");
           			return;   
        		}
    
    
        		JLabel temp = (JLabel) comp;
    
        		Icon tempIcon = temp.getIcon();
    
        		if(tempIcon == whiteBubble){
        			return;
        		}
    
        		System.out.println("location on grid is: "+x+", "+y);
        		go(x, y, tempIcon);
    
        		System.out.println("pointcounter times by = "+pointCounter);
        		System.out.println("Icon ATM  = "+tempIcon);
        		System.out.println("highest X and Y and freq= "+highestX+" "+highestY+" and freq "+highestGroup);
        		pointCounter = 0;
    
        		if(compPlaying){
        			try{
        				Thread.sleep(1200);
        			}	catch(InterruptedException ie){}
        		}
    
    		}	
    
    		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){
    
    		int thisY = y - bubbleSize;
    
    		if(thisY < 0){
    			pointCounter++;
    			//move(x, y, "go");
    			//pointCounter = pointCounter + 1;
    			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){
        		pointCounter++;
        		if(remove == true){
        			System.out.println("highest X and Y and freq= "+highestX+" "+highestY+" and freq "+highestGroup);
        			move(x, y, "go");
        		}
        	}
    
    	}
    
    	public void goDown(int x, int y, Icon firstIcon){
    
    		/*
    		 * go to the next Bubble down
    		*/
    		int thisY = y + bubbleSize;
    
    		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 - bubbleSize;
    
    		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 + bubbleSize;
    
    		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 - bubbleSize;
    
    		/*
    		 * 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);
    		
    	}
    
    	public void computerControlled(String gameType){
    
    
    		if(gameType == "smallFirstLine"){
    			
    		}
    
    		if(gameType == "smallWholeBoard"){
    			
    		}
    
    		if(gameType == "mediumWholeBoard"){
    
    			// set variables to work with this board size
    			medium();
    			compX = 24;
    			compY = 24;
    			original = 24;
    			add = 48;
    			removed = 0;
    			compPlaying = true;
    
    		// Keep going around while there is still bubbles on the board.
    			while(removed!= 144 && compX < 600){
    				System.out.println("In the while loop for Comp Controlled");
    				remove = false;
    				for(int i = 0; i < 13; i++){
    					System.out.println("In FOR loop 1");
    					compX = original; // puts the X mark back to 24 so that it can go around again
    
    					for(int j = 0; j < 13; j++){
    						System.out.println("In FOR loop 2");
    
    						Point tempPoint = squareContainer.getLocationOnScreen();
    						// get the X and Y location of the SquareContainer that holds the grid.
    						scLocX = (int)tempPoint.getX(); //get the X loc of squareContainer
    						scLocY = (int)tempPoint.getY(); //get the Y loc of squareContainer
    
    						System.out.println("squareContainer is at: "+scLocX+", "+scLocY);
    						System.out.println("Mouse should be at: "+compX+", "+compY);
    
    						try{
    
    							/*
    							 * it should do below once, simulate removing it 
    							 * (gets taken to the MouseClicked method), come
    							 * back, then once its done this for all them pick the 
    							 * best one and remove it. However, its doing this many times, without
    							 * simulating directly after as it should, which messes up the whole process.
    							*/
    							
    							Robot robot = new Robot();
    							// below places the mouse on the very top left bubble
    							robot.mouseMove(scLocX+compX+3, scLocY+compY+3);
    							robot.mousePress(InputEvent.BUTTON1_MASK);
    							robot.mouseRelease(InputEvent.BUTTON1_MASK);
    
    							if(pointCounter > highestGroup){
    							    highestGroup = pointCounter;
    							    highestX = compX;
    							    highestY = compY;
    							}
    
    			    			pointCounter = 0;
    
    							
    						} catch(AWTException ae){}
        					compX = compX + add;
        					// end of inner for loop below
    					}
    
    					compY = compY + add;
    					// end of outer for loop
    				}
    				
    				// now allowed to remove bubbles...
    				remove = true;
    
    				try{	
    					Robot robot = new Robot();
    					robot.mouseMove(scLocX+highestX+3, scLocY+highestY+3);
    					robot.mousePress(InputEvent.BUTTON1_MASK);
    					robot.mouseRelease(InputEvent.BUTTON1_MASK);
    
    				} catch(AWTException ae){}
    
    			    removed = removed + highestGroup;
    			    pointCounter = 0;
    			    highestGroup = 0;
    
    
    				try{
        				Thread.sleep(5000);
        			}	catch(InterruptedException ie){}
    
        			remove = false;
    
    			}
    		}
    
    
    
    
    	}
    
    }
    My files if you want to run them yourself is provided below but since its in a slight loop mess, you will have to CTRL + C in terminal or your equivalent in order to stop the program, I have put thread sleeps in to make it easy. When you run the program click "Comp 12 x 12" to see what I'm on about.

    didierd.co.uk/bubble/BubbleBreaker.zip

    as I mention code above....

    it should do as i mentioned once, simulate removing it (gets taken to the MouseClicked method), come back, then once its done this for all them pick the best one and remove it. However, its doing this many times, without simulating directly after as it should, which messes up the whole process.

    any ideas on how i could make it follow the correct path? or maybe im doing something wrong... any ideas are welcome in the WORK IN PROGRESS
    Last edited by DidierD; March 28th, 2011 at 08:06 PM.

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