Hi,

I am having issues while comparing of cards.As i starts the card game and clicks on first button the card opens and when i click on the any other button if the button i have clicked matches with the image on the first button both the cards stays open but if the second card does not matches with the first card the first card closes back.Up to this position it works fine, the problem comes when i have been able to match any pair and after that as i try to click any other button to make the next pair then the first pair of cards closes back.I believe that i am not been able to come up with the correct logic. please advice. My coding in given below.

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import java.util.*;

public class arraytest1 implements ActionListener {

	JButton jButton[],b2;
	int count=0,count1,record,record2;
	int i,z,h,x,
	String tl;
	JPanel p1;
	JMenuBar mb;
	JMenu file,help;
	JMenuItem newgame,exit,about,rules;
	ImageIcon image[];
	ArrayList list;
	public arraytest1(){
		
		JFrame jframe = new JFrame();
		
		mb=new JMenuBar();
		jframe.setJMenuBar(mb);
		file=new JMenu("File");
		newgame=new JMenuItem("New Game");
		exit=new JMenuItem("Exit");
		mb.add(file);
		file.add(newgame);
		file.add(exit);
		newgame.addActionListener(this);
		exit.addActionListener(this);
		help=new JMenu("Help");
		about=new JMenuItem("About");
		rules=new JMenuItem("Game Rules");
		mb.add(help);
		help.add(rules);
		help.add(about);
		
		p1=new JPanel();
		jButton=new JButton[4];
		p1.setLayout(new GridLayout(4,2,5,5));
		list=new ArrayList();
		for(int i=0;i<jButton.length;i++){
jButton[i] = new JButton(Integer.toString(i+1),new ImageIcon("D:/WorkSpace/images.gif"));
		list.add(jButton[i]);
		jButton[i].addActionListener(this);
		jButton[i].addActionListener(new NewListener());
		p1.add(jButton[i]);
		System.out.println("i:"+i);
		p1.setVisible(false);
		}
		

		  jframe.add(p1);
		  jframe.pack();
	      jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	      jframe.setVisible(true);
	      jframe.setSize(500, 500);
	      
	      
	      	      
	}
		
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource().equals(exit)){
			System.exit(0);
		}
		
		if(e.getSource().equals(newgame)){
			try{
				Collections.shuffle(list);
				for(int i=0;i<list.size();i++){
					p1.add((JButton)list.get(i));
				}
				p1.setVisible(true);
				z*=0;
				count*=0;
				count1*=0;
				
				
			jButton[0].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
		    	i++;
		    	
		    	jButton[1].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
		    	i++;
		    	
		    	jButton[2].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
		    	i++;
		    	
		    	jButton[3].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
				
		    	
		    	
		    	
			}catch(Exception ex){
			System.err.println("Exception occurred :" + ex.toString());
				ex.printStackTrace();
			}
			
			}
	}
	private class NewListener implements ActionListener
	
	{
		/* (non-Javadoc)
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		
		
		public void actionPerformed(ActionEvent e){
						
			try{
				String tl=((JButton)e.getSource()).getText();
				System.out.println("Value of text " + tl);
				i=Integer.parseInt(tl);
                                i--;
                                
                                if(i==0)
			       {
            	   
			jButton[i].setIcon(new ImageIcon("D:/WorkSpace/cards/ah.gif"));
				count++;
				record=i;
				x=i;
				a++;
				j++;
				System.out.println("Value of record:"+record);
				System.out.println("Value of count:"+count);
				System.out.println("Value of record2:"+record2);
				System.out.println("Value of x"+x);
				}
                              
               
               
                               if(i==1)
			{
				
                         jButton[i].setIcon(new ImageIcon("D:/WorkSpace/cards/ad.gif"));
	    		        count++;
				record=i;
	    		        x=i;
	    		
	    			System.out.println("Value of record"+record);
				System.out.println("Value of count:"+count);
				System.out.println("Value of record2:"+record2);
				System.out.println("Value of x"+x);
			}
			 
		 			
		 	if(i==2)
			{	
				
			 jButton[i].setIcon(new ImageIcon("D:/WorkSpace/cards/ac.gif"));
			  count++;
			  record2=i;
			  x=i;
			  k++;
			  b++;
			  System.out.println("Value of record2:"+record2);
			  System.out.println("Value of count:"+count);
			  System.out.println("Value of record"+record);
			  System.out.println("Value of x"+x);
			}
			 

			if(i==3)
			{
		
                jButton[i].setIcon(new ImageIcon("D:/workSpace/cards/as.gif"));
		count++;
                record2=i;
                x=i;
                k++;
                b++;
                System.out.println("Value of record2:"+record2);
                System.out.println("Value of count:"+count);
                System.out.println("Value of record:"+record);
                System.out.println("Value of x"+x);
		}
			


               	 if(record!=x){

		jButton[0].setIcon(new ImageIcon("D:/workSpace/images.gif"));
		jButton[1].setIcon(new ImageIcon("D:/workSpace/images.gif"));
		count*=0;
    	        record*=0;
		    	
		
		System.out.println("Value of record:"+record);

		}
			
			
         	     
		}
		

          	if(record2!=x){
	
         	jButton[2].setIcon(new ImageIcon("D:/workSpace/images.gif"));
		jButton[3].setIcon(new ImageIcon("D:/workSpace/images.gif"));
		
         	count*=0;
	
		record2*=0;
	
				
				
		System.out.println("Value of record2:"+record2);
				
		}
			
								
		if(count==2){
		
                System.out.println("Score:"+count);
		
                }
				
				
				
		}catch(Exception ex){
		System.err.println("Exception occurred :"+ex.toString());
		ex.printStackTrace();
		}
		
		}
			
	}
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		
				
		new arraytest1();
		// TODO Auto-generated method stub

	}

	}

If u find coding a bit messed up i am very sorry for that and have re-posted the thread.

Hi,

I am having issues while comparing of cards.As i starts the card game and clicks on first button the card opens and when i click on the any other button if the button i have clicked matches with the image on the first button both the cards stays open but if the second card does not matches with the first card the first card closes back.Up to this position it works fine, the problem comes when i have been able to match any pair and after that as i try to click any other button to make the next pair then the first pair of cards closes back.I believe that i am not been able to come up with the correct logic. please advice. My coding in given below.

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import java.util.*;

public class arraytest1 implements ActionListener {

	JButton jButton[],b2;
	int count=0,count1,record,record2;
	int i,z,h,x,
	String tl;
	JPanel p1;
	JMenuBar mb;
	JMenu file,help;
	JMenuItem newgame,exit,about,rules;
	ImageIcon image[];
	ArrayList list;
	public arraytest1(){
		
	JFrame jframe = new JFrame();
		
	mb=new JMenuBar();
	jframe.setJMenuBar(mb);
	file=new JMenu("File");
	newgame=new JMenuItem("New Game");
	exit=new JMenuItem("Exit");
	mb.add(file);
	file.add(newgame);
	file.add(exit);
	newgame.addActionListener(this);
	exit.addActionListener(this);
	help=new JMenu("Help");
	about=new JMenuItem("About");
	rules=new JMenuItem("Game Rules");
	mb.add(help);
	help.add(rules);
	help.add(about);
		
	p1=new JPanel();
	jButton=new JButton[4];
	p1.setLayout(new GridLayout(4,2,5,5));
	list=new ArrayList();
	for(int i=0;i<jButton.length;i++){
jButton[i] = new JButton(Integer.toString(i+1),new ImageIcon("D:/WorkSpace/images.gif"));
	
        list.add(jButton[i]);
	jButton[i].addActionListener(this);
	jButton[i].addActionListener(new NewListener());
	p1.add(jButton[i]);
	System.out.println("i:"+i);
	p1.setVisible(false);
	}
		

	jframe.add(p1);
	jframe.pack();
	jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	jframe.setVisible(true);
	jframe.setSize(500, 500);
	      
	      
	      	      
	}
		
	public void actionPerformed(ActionEvent e)
	{
	if(e.getSource().equals(exit)){
	System.exit(0);
	}
		
	if(e.getSource().equals(newgame)){
	try{
	Collections.shuffle(list);
	for(int i=0;i<list.size();i++){
	p1.add((JButton)list.get(i));
	}
	p1.setVisible(true);
	z*=0;
	count*=0;
	count1*=0;
				
				
	jButton[0].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
	i++;
		    	
	jButton[1].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
	i++;
		    	
	jButton[2].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
	i++;
		    	
	jButton[3].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
				
		    	
		    	
		    	
	}catch(Exception ex){
	System.err.println("Exception occurred :" + ex.toString());
	ex.printStackTrace();
	}
			
	}
	}
	private class NewListener implements ActionListener
	
	{
		/* (non-Javadoc)
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		
		
	public void actionPerformed(ActionEvent e){
						
	try{
	String tl=((JButton)e.getSource()).getText();
	System.out.println("Value of text " + tl);
	i=Integer.parseInt(tl);
        i--;
                                
        if(i==0)
	{
            	   
	jButton[i].setIcon(new ImageIcon("D:/WorkSpace/cards/ah.gif"));
	count++;
	record=i;
	x=i;
	
        System.out.println("Value of record:"+record);
	System.out.println("Value of count:"+count);
	System.out.println("Value of x"+x);
	}
                              
               
               
        if(i==1)
	{
				
        jButton[i].setIcon(new ImageIcon("D:/WorkSpace/cards/ad.gif"));
	count++;
	record=i;
	x=i;
	    		
	System.out.println("Value of record"+record);
	System.out.println("Value of count:"+count);
	System.out.println("Value of x"+x);
	}
			 
		 			
	if(i==2)
	{	
				
	jButton[i].setIcon(new ImageIcon("D:/WorkSpace/cards/ac.gif"));
	count++;
	record2=i;
	x=i;
	
        System.out.println("Value of record2:"+record2);
	System.out.println("Value of count:"+count);
	System.out.println("Value of x"+x);
	}
			 
	if(i==3)
			{
		
        jButton[i].setIcon(new ImageIcon("D:/workSpace/cards/as.gif"));
	count++;
        record2=i;
        x=i;

        System.out.println("Value of record2:"+record2);
        System.out.println("Value of count:"+count);
        System.out.println("Value of record:"+record);
        System.out.println("Value of x"+x);
        }
			

        if(record!=x){

	jButton[0].setIcon(new ImageIcon("D:/workSpace/images.gif"));
	jButton[1].setIcon(new ImageIcon("D:/workSpace/images.gif"));
	count*=0;
    	record*=0;
		    	
		
	System.out.println("Value of record:"+record);

	}
			
			
       	if(record2!=x){
	
       	jButton[2].setIcon(new ImageIcon("D:/workSpace/images.gif"));
	jButton[3].setIcon(new ImageIcon("D:/workSpace/images.gif"));
		
       	count*=0;
	
	record2*=0;
	
	System.out.println("Value of record2:"+record2);
				
	}
			
								
	if(count==2){
		
        System.out.println("Score:"+count);
		
        }
				
				
				
	}catch(Exception ex){
	System.err.println("Exception occurred :"+ex.toString());
	ex.printStackTrace();
	}
		
	}
			
	}
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		
				
		new arraytest1();
		// TODO Auto-generated method stub

	}

	}

he problem comes when i have been able to match any pair and after that as i try to click any other button to make the next pair then the first pair of cards closes back

Before I try debugging your code, have you tried debugging your code by adding println() statements to show execution flow and changing variables? Some well place println()s will help you understand what is happening in your code.

What is this code supposed to do:

z*=0;
  count*=0;
  count1*=0;

Use more descriptive variable names than the single letters you are using:

int i,z,h,x,
	String tl;
	JPanel p1;

Edited 6 Years Ago by NormR1: n/a

Hi,

As you have asked i have used println() statement and what i have found is that the value of x is actually the prime cause of the problem i am facing.When ever the value of x differ from the value of record or record2 the existing pair of cards gets closed.I am still not been able to come up with the solution so that i can controlled the changing nature of x. The int z,count,count1 i was using earlier in the program and now i have removed them from the coding.

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import java.util.*;

public class arraytest1 implements ActionListener {

	JButton jButton[],b2;
	int record,record2;
	int i,x;
	String tl;
	JPanel p1;
	JMenuBar mb;
	JMenu file,help;
	JMenuItem newgame,exit,about,rules;
	ImageIcon image[];
	ArrayList list;
	public arraytest1(){
		
	JFrame jframe = new JFrame();
		
	mb=new JMenuBar();
	jframe.setJMenuBar(mb);
	file=new JMenu("File");
	newgame=new JMenuItem("New Game");
	exit=new JMenuItem("Exit");
	mb.add(file);
	file.add(newgame);
	file.add(exit);
	newgame.addActionListener(this);
	exit.addActionListener(this);
	help=new JMenu("Help");
	about=new JMenuItem("About");
	rules=new JMenuItem("Game Rules");
	mb.add(help);
	help.add(rules);
	help.add(about);
	
	p1=new JPanel();
	jButton=new JButton[4];
	p1.setLayout(new GridLayout(4,2,5,5));
	list=new ArrayList();
	for(int i=0;i<jButton.length;i++){
    jButton[i] = new JButton(Integer.toString(i+1),new ImageIcon("D:/WorkSpace/images.gif"));
	list.add(jButton[i]);
	jButton[i].addActionListener(this);
	jButton[i].addActionListener(new NewListener());
	p1.add(jButton[i]);
	System.out.println("i:"+i);
	p1.setVisible(false);
	}
	

       jframe.add(p1);
       jframe.pack();
       jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       jframe.setVisible(true);
       jframe.setSize(500, 500);
	      
	           	      
       }
		
	public void actionPerformed(ActionEvent e)
	{
	if(e.getSource().equals(exit)){
	System.exit(0);
	}
		
	if(e.getSource().equals(newgame)){
	try{
	Collections.shuffle(list);
	for(int i=0;i<list.size();i++){
	p1.add((JButton)list.get(i));
	}
	p1.setVisible(true);
												
	jButton[0].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
	i++;
		    	
	jButton[1].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
	i++;
		    	
	jButton[2].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
	i++;
		    	
	jButton[3].setIcon(new ImageIcon("D:/WorkSpace/images.gif"));
				
		    		    	
		    	
	}catch(Exception ex){
	System.err.println("Exception occurred :" + ex.toString());
	ex.printStackTrace();
	}
			
	}
	}
	private class NewListener implements ActionListener
	
	{
		/* (non-Javadoc)
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		
		
	public void actionPerformed(ActionEvent e){
						
	try{
	String tl=((JButton)e.getSource()).getText();
	System.out.println("Value of text " + tl);
	i=Integer.parseInt(tl);
        i--;
        if(i==0)
	{
            	   
	jButton[i].setIcon(new ImageIcon("D:/WorkSpace/cards/ah.gif"));
	record=i;
	x=i;
		
	System.out.println("Value of record:"+record);
	System.out.println("Value of record2:"+record2);
	System.out.println("Value of x"+x);
	}
                              
                           
        if(i==1)
	{
	jButton[i].setIcon(new ImageIcon("D:/WorkSpace/cards/ad.gif"));
	record=i;
	x=i;
	    		
	System.out.println("Value of record"+record);
	System.out.println("Value of record2:"+record2);
	System.out.println("Value of x"+x);
	}
			 
		 			
	if(i==2)
	{	
				
	jButton[i].setIcon(new ImageIcon("D:/WorkSpace/cards/ac.gif"));
	record2=i;
	x=i;
			  
	System.out.println("Value of record2:"+record2);
	System.out.println("Value of record"+record);
	System.out.println("Value of x"+x);
	}
			 
	if(i==3)
	{
	jButton[i].setIcon(new ImageIcon("D:/workSpace/cards/as.gif"));
	record2=i;
        x=i;
                
        System.out.println("Value of record2:"+record2);
        System.out.println("Value of record:"+record);
        System.out.println("Value of x"+x);
	}
			 
	if(record!=x){
	jButton[0].setIcon(new ImageIcon("D:/workSpace/images.gif"));
	jButton[1].setIcon(new ImageIcon("D:/workSpace/images.gif"));
	record*=0;
						    					
	System.out.println("Value of record:"+record);
	System.out.println("Value of record2:"+record2);
	System.out.println("Value of x"+x);
	}
			
					 
	if(record2!=x){
	jButton[2].setIcon(new ImageIcon("D:/workSpace/images.gif"));
	jButton[3].setIcon(new ImageIcon("D:/workSpace/images.gif"));
	
        record2*=0;								
				
	System.out.println("Value of record2:"+record2);
	System.out.println("Value of record:"+record);
	System.out.println("Value of x"+x);
	}
			
			
	}catch(Exception ex){
	System.err.println("Exception occurred :"+ex.toString());
	ex.printStackTrace();
	}
			
	}
			
	}
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		
				
		new arraytest1();
		// TODO Auto-generated method stub

	}

	}

Before I try debugging your code, have you tried debugging your code by adding println() statements to show execution flow and changing variables? Some well place println()s will help you understand what is happening in your code.

What is this code supposed to do:

z*=0;
  count*=0;
  count1*=0;

Use more descriptive variable names than the single letters you are using:

int i,z,h,x,
	String tl;
	JPanel p1;

I am still not been able to come up with the solution so that i can controlled the changing nature of x

I can't understand your problem. Your code sets and changes the value of x.
There must be a reason for each time you change x's value.
What are those reasons? Look at your program design and see why you need to have the variable x and how you want to use it.

What is the reason for the following line of code:
record2*=0;

Edited 6 Years Ago by NormR1: n/a

This article has been dead for over six months. Start a new discussion instead.