Results 1 to 7 of 7
  1. #1
    thesonyuh is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Problem with Memory Matching Game

    Java Code:
    import java.awt.*;
    import java.lang.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.event.*;
    import java.math.*;
    
    public class test extends JFrame implements ActionListener{
    	JButton x []= new JButton [12];
    	int [] arr=new int[15];
    	int count=0;
    	int f=0,l=0,f1=0,l1=0;
    	int dist=0,fg=0;
        public test() {
        	setLayout(new GridLayout(3,4));
        	int randomNumbers = 12;
        	Container con=getContentPane();
    		ArrayList<Integer> numbers = new ArrayList<Integer>();
    		int number = (int)(Math.random() * randomNumbers);
    		numbers.add(number);
    		for(int i = 0; i < randomNumbers-1; i++){
    			do{
    				number = (int)(Math.random() * 12);
    				
    			}while(numbers.indexOf(number)!=-1);
    			numbers.add(number);
    		}
    		for(Integer i: numbers){
    			System.out.println(i);
    			x[i] = new JButton("1", new ImageIcon ("q2.jpg"));
    			con.add(x[i]);
    			x[i].addActionListener(this);
    		}
    			
    		
        	setTitle("Bahay Kubling Matching Game");
        	setSize(300,400);
        	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	setLocationRelativeTo(null);
        	setVisible(true);
        }
        
        public void actionPerformed (ActionEvent e){
        	
        	
        	ImageIcon img1 = new ImageIcon ("kalabasa.jpg");
        	ImageIcon img2 = new ImageIcon ("mangga.jpg");
        	ImageIcon img3 = new ImageIcon ("mani.jpg");
        	ImageIcon img4 = new ImageIcon ("sigarilyas.jpg");
        	ImageIcon img5 = new ImageIcon ("singkamas.jpg");
        	ImageIcon img6 = new ImageIcon ("sitaw.jpg");
        	ImageIcon q = new ImageIcon ("q2.jpg");
        	
        	if (e.getSource()==x[0])
        	{
        		x[0].setIcon(img1);
        		count=count+1;
        		if (count==1)
        			f=11;
        		if (count==2)
        			l=11;
        	}
        	
        	else if (e.getSource()==x[1])
        	{
        		x[1].setIcon(img1);
        		count++;
        		if (count==1)
        			f=12;
        		if (count==2)
        			l=12;
        	}
        	
        	else if (e.getSource()==x[2])
        	{
        		x[2].setIcon(img2);
        		count++;
        		if (count==1)
        			f=21;
        		if (count==2)
        			l=21;	
        	}
        	
        	else if (e.getSource()==x[3])
        	{
        		x[3].setIcon(img2);
        		count++;
        		if (count==1)
        			f=22;
        		if (count==2)
        			l=22;
        	}
        	
        	else if (e.getSource()==x[4])
        	{
        		x[4].setIcon(img3);
        		count++;
        		if (count==1)
        			f=31;
        		if (count==2)
        			l=31;
        	}
        	
        	else if (e.getSource()==x[5])
        	{
        		x[5].setIcon(img3);
        		count++;
        		if (count==1)
        			f=32;
        		if (count==2)
        			l=32;	
        	}
        	
        	else if (e.getSource()==x[6])
        	{
        		x[6].setIcon(img4);
        		count++;
        		if (count==1)
        			f=41;
        		if (count==2)
        			l=41;
        	}
        	
        	else if (e.getSource()==x[7])
        	{
        		x[7].setIcon(img4);
        		count++;
        		if (count==1)
        			f=42;
        		if (count==2)
        			l=42;
        	}
        	
        	else if (e.getSource()==x[8])
        	{
        		x[8].setIcon(img5);
        		count++;
        		if (count==1)
        			f=51;
        		if (count==2)
        			l=51;
        	}
        	
        	else if (e.getSource()==x[9])
        	{
        		x[9].setIcon(img5);
        		count++;
        		if (count==1)
        			f=52;
        		if (count==2)
        			l=52;
        	}
        	
        	else if (e.getSource()==x[10])
        	{
        		x[10].setIcon(img6);
        		count++;
        		if (count==1)
        			f=61;
        		if (count==2)
        			l=61;
        	}
        	
        	else if (e.getSource()==x[11])
        	{
        		x[11].setIcon(img6);
        		count++;
        		if (count==1)
        			f=62;
        		if (count==2)
        			l=62;
        	}
        	//
        	
        	if (count==2)
        	{
        			f1=f%10;
        			l1=l%10;
        			f=f/10;
        			l=l/10;
        			
        		if (l==f)
        		{
        			x[(f*2)-1].setEnabled(false);
        			x[(l*2)-2].setEnabled(false);
        			count=0;
        			
        		}
        		else
        		{
        			f=f*10;
        			l=l*10;
        			f=f+f1;
        			l=l+l1;
        			if (f==11)
        				dist=0;
        			if (f==12)
        				dist=1;
        			if (f==21)
        				dist=2;
        			if (f==22)
        				dist=3;
        			if (f==31)
        				dist=4;
        			if (f==32)
        				dist=5;
        			if (f==41)
        				dist=6;
        			if (f==42)
        				dist=7;
        			if (f==51)
        				dist=8;
        			if (f==52)
        				dist=9;
        			if (f==61)
        				dist=10;
        			if (f==62)
        				dist=11;
        				
        			if (l==11)
        				fg=0;
        			if (l==12)
        				fg=1;
        			if (l==21)
        				fg=2;
        			if (l==22)
        				fg=3;
        			if (l==31)
        				fg=4;
        			if (l==32)
        				fg=5;
        			if (l==41)
        				fg=6;
        			if (l==42)
        				fg=7;
        			if (l==51)
        				fg=8;
        			if (l==52)
        				fg=9;
        			if (l==61)
        				fg=10;
        			if (l==62)
        				fg=11;
        			
        			System.out.println(dist +" "+fg);
        			x[dist].setIcon(q);
        			x[fg].setIcon(q);
        			x[dist].setEnabled(true);
    This is my code. My problem is when the user clicks 2 different boxes; The image in the first box will be seen but as soon as he clicks on a different box he won't see the image in it... Please try the program (because it's somewhat different to explain.)

    Thanks in advance.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default Re: Problem with Memory Matching Game

    The end of your posted code is missing.

    Your code looks like you need to learn how to use classes to hold and process the code and data that you have in all the if / else if statements.

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

    Default Re: Problem with Memory Matching Game

    There's a lot of unnecessary duplication of code there. If you simplify it, it will be a lot easier to debug. And as Norm states, your code is incomplete.

  4. #4
    thesonyuh is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Problem with Memory Matching Game

    Quote Originally Posted by Norm View Post
    The end of your posted code is missing.

    Your code looks like you need to learn how to use classes to hold and process the code and data that you have in all the if / else if statements.
    What do you mean classes? ? Can you show me how??
    BTW, this is the continuation:
    Java Code:
    ....
    x[fg].setIcon(q);
        			x[dist].setEnabled(true);
        			x[fg].setEnabled(true);
        			count=0;
        		}
        	}
        	
        	
        }
        
        public static void main (String[] args){
        	//new splasher();
        	new test();
        }
        
        
    }

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

    Default Re: Problem with Memory Matching Game

    Sorry but that code again has a ton of needless redundancy. All those else if blocks could be handled in one small block of code. Also, there's no need to create new ImageIcons with each call of the ActionListener. Why not instead create all your images and ImageIcons once in the class's constructor?

    Anyway, when the second button is pressed, sure you set the Image of the button to the icon, but then immediately the code runs to this spot:

    Java Code:
                x[dist].setIcon(q);
                x[fg].setIcon(q);
                x[dist].setEnabled(true);
                x[fg].setIcon(q);
                x[dist].setEnabled(true);
                x[fg].setEnabled(true);
    where you reset everything back. Since there's no delay, you will not see the second button's icon. A solution would be to use a Swing Timer to pause all for a bit before you reset the buttons back to their previous default iconized state.

  6. #6
    thesonyuh is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Problem with Memory Matching Game

    Quote Originally Posted by Fubarable View Post
    Sorry but that code again has a ton of needless redundancy. All those else if blocks could be handled in one small block of code. Also, there's no need to create new ImageIcons with each call of the ActionListener. Why not instead create all your images and ImageIcons once in the class's constructor?

    Anyway, when the second button is pressed, sure you set the Image of the button to the icon, but then immediately the code runs to this spot:

    Java Code:
                x[dist].setIcon(q);
                x[fg].setIcon(q);
                x[dist].setEnabled(true);
                x[fg].setIcon(q);
                x[dist].setEnabled(true);
                x[fg].setEnabled(true);
    where you reset everything back. Since there's no delay, you will not see the second button's icon. A solution would be to use a Swing Timer to pause all for a bit before you reset the buttons back to their previous default iconized state.
    Yup there are redundancies but I'll fix them in furnishing.. I'm just getting the ideas before I furnish them.. Could yuo give me some codes for the Swing Timer?? I'm new to it. Thanks!

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

    Default Re: Problem with Memory Matching Game

    Quote Originally Posted by thesonyuh View Post
    Yup there are redundancies but I'll fix them in furnishing.. I'm just getting the ideas before I furnish them.. Could yuo give me some codes for the Swing Timer?? I'm new to it. Thanks!
    No. Since you are new, you're far better off checking out the tutorials (just Google Java Swing Timer Tutorial), and working through this. You'll learn much more than we can show you here.

Similar Threads

  1. Matching picture game in java
    By iswan in forum New To Java
    Replies: 1
    Last Post: 09-08-2011, 02:02 AM
  2. Need help with matching game
    By soundymcsoundsound in forum New To Java
    Replies: 2
    Last Post: 11-02-2010, 11:09 PM
  3. Memory game
    By ronyyy in forum New To Java
    Replies: 9
    Last Post: 04-15-2010, 11:00 AM
  4. Picture Matching Game
    By schnappi in forum AWT / Swing
    Replies: 3
    Last Post: 08-09-2009, 01:38 PM
  5. help with memory game!
    By rac in forum New To Java
    Replies: 6
    Last Post: 04-13-2009, 11:39 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
  •