Results 1 to 4 of 4
  1. #1
    schnappi is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default Picture Matching Game

    I have to create a 4x4 picture matching game where the objective is for the player to find all matching pairs of pictures.

    How do I store my 8 images in an array and setup a loop for it?

    Java Code:
    class displayHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			for (int i = 0; i < cardButton.length; i++)
    			{
    				if (cardButton[i] == e.getSource())
    				{
    					cardButton[i].setIcon(null);
    					cardButton[i].setEnabled(false);
    					counter++;
    					
    				if (counter == 3)
    				{
    					if (sameValues())
    					{
    						cardButton[btnID[0]].setEnabled(false);
    						cardButton[btnID[1]].setEnabled(false);
    					}
    					else
    					{
    						cardButton[btnID[0]].setEnabled(true);
    						cardButton[btnID[0]].setIcon(null);
    						cardButton[btnID[1]].setEnabled(true);
    						cardButton[btnID[1]].setIcon(null);
    					}
    					counter = 1;
    				}
    				
    				if (counter == 1)
    				{
    					btnID[0] = i;
    					//btnValue[0] = gameList.get(i);
    				}
    				
    				if (counter == 2)
    				{
    					btnID[1] = i;
    					//btnValue[1] = gameList.get(i);
    				}
    								
    			   }
    		    }	
    		}
    				
    	}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What I've done in this situation in the past (and I'm not saying it's the best solution, but it's worked for me), is create a MemoryButton class that actually is a JPanel that holds both a JButton and a JLabel using a CardLayout. When the button is pressed, the cardlayout swaps the button for the label, displaying the label. Each MemoryButton also holds an ImageIcon and an int "value" field (not necessary, I suppose) and has a setIcon method that simply sets the image icon of the JLabel held by these objects and setValue and getValue methods.

    I then create a 4 x 4 grid of MemoryButtons and create an ImageIcon[8] array for my 8 ImageIcons, and do this only once at the beginning of the program. I have a reset method in the main class that creates an ArrayList<Integer> called intList, adds pairs of numbers 1-8 to this list (16 numbers in all), and then uses Collections.shuffle(intList) to randomize the list. Then I iterate through the list assiging to my MemoryButtons values and the ImageIcon from that array position that corresponds to that value.

  3. #3
    schnappi is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    thanks but do you mind going into more detail?

    as of now, i have already set all my 16 buttons to a blank image via a loop.
    how do i add the 8 images to an array and loop through them?

    I also need to declare a method to assign images to the buttons.
    similar to :

    Java Code:
    public void setArrayListText()
    	{
    		for (int i=0; i<2; i++)
    		{
    			for (int ii=1; ii<(cardButton.length/2)+1; ii++)
    			{
    				gameList.add(ii);
    			}	
    	}
    }
    except i need to do this to images instead of integers.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    My way would require you to re-write your program from scratch using the I've described framework that I've described:

    pseudo-structure
    Java Code:
    class MemoryButton 
      holds a CardLayout object
      holds a JPanel mainPanel that uses this cardlayout
      holds a JButton that is initially displayed in the cardlayout
      holds a JLabel that displays an ImageIcon that is held "behind" the JButton by the cardlayout
      
      method setIcon that sets the icon of the JLabel
    
      method showLabel( boolean)  if true it tells the cardlayout to show the label, if false, the button
    
      method addActionListener (ActionListener)  allows other classes to add an actionlistener to the button.  It's the class that holds the buttons that will decide if there's been a match (continue to show the label) or not (show the label for x seconds, then show the button).

Similar Threads

  1. matching and getting xml data
    By Juuno in forum Advanced Java
    Replies: 6
    Last Post: 04-26-2009, 06:25 PM
  2. Regex - matching literal characters
    By racha0601 in forum Advanced Java
    Replies: 3
    Last Post: 04-07-2009, 11:25 PM
  3. Help with signature matching
    By cachi in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:21 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
  •