-1

hi for all,i tried to write the memory game code but i got mistakes,can any body check them for me please?
here is my code:

import java.util.Random;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

//some things i didn't use in this game,i was just tryin ^^
public class Memorycard2 extends JFrame implements ActionListener
{
	JPanel pan = new JPanel();
	JButton btn[] = new JButton[12];
	int x,i;
	int num[] = new int [12];
	boolean faceUp = false;
	boolean bool[] = new boolean [12];
	Integer arr[]={1,2,3,4,5,6,1,2,3,4,5,6};
	Random rand =new Random();
 
	boolean secondbutton = false;
	boolean firstbutton = false;
	boolean check = true;
	private int[] btnClick = new int[2];   // for the Clicked button
	private int[] btnValue = new int[2]; // the shuffeled values inside the button
	JButton exitBtn, resetBtn;
	int counter =0;
	
	
public Memorycard2(){
	buttons();
	panel();
	setSize(400,400);
	setVisible(true);
	setDefaultCloseOperation(3);
	setTitle("Shatha MemoryGame");
}
		
	public void buttons(){
		
	for(i = 0; i<btn.length; i++)
	{
		btn[i]=new JButton("");
		pan.add(btn[i]);
		btn[i].addActionListener(this);
	}
	exitBtn = new JButton("Exit");
	exitBtn.addActionListener(this);
	resetBtn = new JButton("Reset");
	resetBtn.addActionListener(this);
	} 
//////////		/////////////////////////////////////////////////////////////////////////////////////////////////////
	public void panel() {
	
	Panel REPnl = new Panel(); //The Reset & Exit Panel	
		REPnl.add(resetBtn);
		REPnl.add(exitBtn);
	REPnl.setLayout(new GridLayout(1, 0));
	add(REPnl, BorderLayout.NORTH);
	
	
	
	pan.setLayout(new GridLayout(4,3));
	add(pan,BorderLayout.CENTER);
	add(pan);
	}
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	public boolean sameValues() {
		if (btnValue[0] == btnValue[1]) { 
			return true;
		}
		return false;
	}
	
	public void actionPerformed(ActionEvent ae){
		x=rand.nextInt(12);
			if (exitBtn == ae.getSource()) {
			System.exit(0);
		}
		if (resetBtn == ae.getSource()) { 
			new Memorycard2();

		}
		for (int i = 0; i < btn.length; i++) {
			if (btn[i] == ae.getSource()) {
				btn[i].setText("" + num.get(x));
				btn[i].setEnabled(false);
					counter++;
				if (counter == 3) // for the third click
				 {
					if (sameValues()) // 1. if the numbers are the same:
					{
					btn[btnClick[0]].setEnabled(false); //  . set the 1st & 2nd cliked buttons as Unenabled
						btn[btnClick[1]].setEnabled(false);
					}
					else
					{
						btn[btnClick[0]].setEnabled(true);  // a. set the 1st & the 2nd clicked buttons as enabled
						btn[btnClick[0]].setText("");       // b. also can be clicked again
						btn[btnClick[1]].setEnabled(true);
						btn[btnClick[1]].setText("");
						
					}
					counter = 1;
					
			}
			if (counter == 1) {
					btnClick[0] = i;    
					btnValue[0] = arr.get(x);  
				}
				if (counter == 2) {
					btnClick[1] = i;
					btnValue[1] = arr.get(x);
				}
		}
		
	}
	}	
		
	
	//	for (int i = 0; i<num.length; i++)
	//	{
		//	check = true;
		//	while(check)
		//	{
			//	x=rand.nextInt(12);
			//	if(bool[x]==false)
			//	{
				//	num[i] = arr[x];
				//	bool[x]=true;
				//	check=false;
			//	}
		//	}
	//	}
//	if(ae.getActionCommand() == btn(0))
//	{
	  // if(secondbutton ==false)//so it is the first button that is bressed
	 //  {
	 //  	btn(0).setenable(true);
	   //	set.text(num(x));
	   //	faceUp=true;
	  // if else (firstbutton == false)  
	   		  
	   //	{
	   		
	   	
	   		
	   //	if(num[i]==btn(0) )
	   //	{
	   	//	btn[i].setEnabled(false)
	   	//	faceUp=false;
	   //	}	
	   //	}
	  // }
	   
	  
	  	 	
//	}
	public static void main (String[] args) {
		new Memorycard2();
}
}

Edited by peter_budo: Keep it Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

2
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by adams161
1

you should be more specific on mistakes. does it compile? is your action performed working? i notice your panel doesnt implement action listener, your frame does. the button is on the panel.

Mike

0

actually if the frame or panel both arent overriding method actoin listener, and the listener is added directly to the control. i dont think you should implement action listener at all.

Mike

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.