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
}
}