10 Replies - 708 Views - Last Post: 23 June 2012 - 12:06 PM Rate Topic: -----

#1 MissQT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-June 12

Help with memory game

Posted 22 June 2012 - 01:57 PM

Hi everyone,

I am trying to create a java memory/ matching game and I am completely stuck.
I created the grid with buttons and it looks fine but I have no idea how to make the game work.
the first problem is that I do not know how to make java wait before unflipping the cards.
The second problem is that I am not sure how to flip the cards. I was thinking of sitting the icon to null and
then saving the icons in an array so that I could know which icon belongs to each button.

the third and most important problem is that I so not know how to use the ActionListener.
I do not know how to use it for two buttons at a time.

This is my code. I'm sorry if it seems so messy but it is only because i have experimented a lot.
import javax.swing.Icon;
import javax.swing.ImageIcon;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.util.ArrayList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Memory extends JFrame
{
   private JPanel panel1;
   private JPanel panel;
   private JPanel panel2;
   private JButton cards[];
   private ArrayList<Icon> icons;
   private Icon[] icon;
   private JButton newGame;
   
   private int numOfTries = 0;
   private int pairsFound = 0;
   private int numCards = 16;
   
    private Icon icon1;
	private Icon icon2;
	private Icon icon3;
	private Icon icon4;
	private Icon icon5;
	private Icon icon6;
	private Icon icon7;
	private Icon icon8;
	
	private int num;
	
   public Memory()
   {
		super( "Memory" );
		cards = new JButton[16];
		panel = new JPanel();
		panel.setLayout(new GridLayout(4, 4));
//		setLayout(new GridLayout(4, 4));
		
		icons = new ArrayList<Icon>();
		icon1 = new ImageIcon(getClass().getResource("1.gif"));
		icon2 = new ImageIcon(getClass().getResource("2.gif"));
		icon3 = new ImageIcon(getClass().getResource("3.gif"));
		icon4 = new ImageIcon(getClass().getResource("4.gif"));
		icon5 = new ImageIcon(getClass().getResource("5.gif"));
		icon6 = new ImageIcon(getClass().getResource("6.gif"));
		icon7 = new ImageIcon(getClass().getResource("7.gif"));
		icon8 = new ImageIcon(getClass().getResource("8.gif"));

		icons.add(icon1);
		icons.add(icon1);
		icons.add(icon2);
		icons.add(icon2);
		icons.add(icon3);
		icons.add(icon3);
		icons.add(icon4);
		icons.add(icon4);
		icons.add(icon5);
		icons.add(icon5);
		icons.add(icon6);
		icons.add(icon6);
		icons.add(icon7);
		icons.add(icon7);
		icons.add(icon8);
		icons.add(icon8);
		

		for ( int count = 0; count < cards.length; count++ ) 
	    {
	    	num = (int)((16-count)*Math.random());
	        cards[ count ] = new JButton();
//	        cards[ count ].setIcon(icons.get(num));
			cards[ count ].setPressedIcon(icons.get(num));
	        panel.add( cards[ count ] ); 
//	        add( cards[ count ] ); 
	        icons.remove(num);
	    } 

    	add( panel, BorderLayout.CENTER); // add panel to JFrame
//////    	
//    	panel2 = new JPanel();
//    	newGame = new JButton("New Game");
//    	panel2.add(newGame);
//    	add( panel, BorderLayout.SOUTH);
//		
//		wait(1);

		
//		for ( int count = 0; count < cards.length; count++ ) 
//	    {
//	    	icon = cards[ count ].getIcon();
//			cards[count].setIcon(null);
//	         
//	    } 
		
   } 
   	
  	 public static void wait(int n)
  	 {
        
        long t0, t1;

        t0 =  System.currentTimeMillis();

        do{
            t1 = System.currentTimeMillis();
        }
        while ((t1 - t0) < (n * 1000));
     }
     

//	private class ButtonHandler implements ActionListener
//	{
//		public void actionPerformed(ActionEvent event)
//		{
//		
//		}
//	}

} 



I really appreciate your help. I have only 2 days to submit my project.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with memory game

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2000
  • View blog
  • Posts: 4,866
  • Joined: 10-September 10

Re: Help with memory game

Posted 22 June 2012 - 05:20 PM

Quote

the third and most important problem is that I so not know how to use the ActionListener. I do not know how to use it for two buttons at a time.

You could have a separate actionListener for each button, OR you could use a single actionListener that identifies which button was pressed and provides branching code to act appropriately. You can determine which button was pressed by using the ActionEvent.getSource() method.

Since your code was machine generated, it's atrocious to work with. It's not bad code necessarily, but few want to take the time to figure out the purpose of several variables with names differentiated by a single number. It takes time, is poor practice that no one here would encourage, and it's painful. Even so, you may find someone here who will help you, because a few here are very supportive of graphical GUI builders. Unfortunately, I don't see those same supporters responding to questions like yours that request help with them.

Good luck on your project!
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8065
  • View blog
  • Posts: 31,308
  • Joined: 06-March 08

Re: Help with memory game

Posted 22 June 2012 - 06:51 PM

Better to learn how to use array before writing such a program.
What will you do when you will want a game 10X10 ?
Was This Post Helpful? 0
  • +
  • -

#4 MissQT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-June 12

Re: Help with memory game

Posted 22 June 2012 - 09:46 PM

View PostGregBrannon, on 22 June 2012 - 05:20 PM, said:

Quote

the third and most important problem is that I so not know how to use the ActionListener. I do not know how to use it for two buttons at a time.

You could have a separate actionListener for each button, OR you could use a single actionListener that identifies which button was pressed and provides branching code to act appropriately. You can determine which button was pressed by using the ActionEvent.getSource()


I was thinking of using only one listener but didnt know to make it work on two cards at a time.
I like your first idea but I am not sure how to code this.

I am sorry but can you explain more

View Postpbl, on 22 June 2012 - 06:51 PM, said:

Better to learn how to use array before writing such a program.
What will you do when you will want a game 10X10 ?

Since I am completelt stuck improving my code will be helpful. How do I do that?
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2000
  • View blog
  • Posts: 4,866
  • Joined: 10-September 10

Re: Help with memory game

Posted 23 June 2012 - 12:15 AM

You appear to be starting a rather complex project with less time to complete than most experts would give themselves, yet you lack the most basic skills required to even start the project. My opinion is that you have chosen a project that is beyond your current capabilities, and we don't have the resources to teach you what you need to know.

I suggest you fallback and regroup. Go to your instructor or a TA, explain the situation, and ask for help.
Was This Post Helpful? 0
  • +
  • -

#6 abhyudaya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-June 12

Re: Help with memory game

Posted 23 June 2012 - 12:21 AM

You could probably make as many listener classes as many buttons you have and link each button to each listener or if you are using a single action listener you would find it easy to use an else if ladder that checks for each value of button at each if. Good luck for the project!
Was This Post Helpful? 0
  • +
  • -

#7 MissQT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-June 12

Re: Help with memory game

Posted 23 June 2012 - 01:33 AM

Thank you for your help. I will try to finish the project because I dont have the time to change it.

If you could just pleade help me with this part of code that I am having trouble with
How do I scheck that the icons match. I tried getting the icons from each button but I do
not know how to compare them.

Please help me with this part of code.
Was This Post Helpful? 0
  • +
  • -

#8 MissQT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-June 12

Re: Help with memory game

Posted 23 June 2012 - 02:47 AM

My twacher actually told me that this project is too easy for me. I only have a 3 weeks experience in GUI and I feel new to everything.
Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2000
  • View blog
  • Posts: 4,866
  • Joined: 10-September 10

Re: Help with memory game

Posted 23 June 2012 - 03:41 AM

Your latest plea asking for help to determine if icons match is different (I think) than an actionListener handling multiple buttons, but I'll stay with the latter.

Let us know how it turns out. Something like:

public void actionPerformed( ActionEvent e )
{
	JButton buttonPressed = (JButton)e.getSource();
	
	if ( buttonPressed == cards[0] )
	{
		// do actions for button cards[0]
	}
	else if ( buttonPressed == cards[1] )
	{
		// do actions for button cards[1]
	}
	else if ( ) // etc . . . 
}

Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4948
  • View blog
  • Posts: 11,353
  • Joined: 16-October 07

Re: Help with memory game

Posted 23 June 2012 - 04:49 AM

View PostMissQT, on 22 June 2012 - 04:57 PM, said:

I'm sorry if it seems so messy but it is only because i have experimented a lot.


Good! Excellent. Never stop doing that. However, you may want to stop and clean up from time to time, lest you confuse yourself.

You do not need two buttons. What you need is to change the icon based on it's state. The setPressedIcon method is a distraction; you need only setIcon.

View PostMissQT, on 22 June 2012 - 04:57 PM, said:

the first problem is that I do not know how to make java wait before unflipping the cards.


Think about how forms work. It's waiting for you already, to do something. Clicking a button is something. An actionListerner is the code your write to react to that click. So, then, how should you react?

First, you find the button clicked. Has it already been uncovered? If so, ignore it. Is there already a button waiting to be matched? If there is another button waiting, check for match and react. If there is no other button waiting, then this is the first button; uncover it.

View PostMissQT, on 22 June 2012 - 04:57 PM, said:

the third and most important problem is that I so not know how to use the ActionListener.


Start here: http://docs.oracle.c...onlistener.html


I looked at your code. It looks reasonable. The arrayList is clever. But so much repetion and magic numbers and object level variables...

Here's how I'd start:
import javax.swing.Icon;
import javax.swing.ImageIcon;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.util.ArrayList;
import java.util.Random;

public class Memory extends JFrame {
	private final int ROWS = 4, COLS = 4;
	private final int iconCount, cardCount;
	private final JButton cards[];
	private final Icon[] icons;
	
	// you're missing this
	private final Icon coveredIcon;

	// this will be needed
	private JButton uncoveredCard;

	public Memory() {
		super("Memory");

		this.cardCount = ROWS * COLS;
		this.iconCount = this.cardCount / 2;
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(ROWS, COLS));

		coveredIcon = new ImageIcon(getClass().getResource("covered.gif"));

		cards = new JButton[this.cardCount];
		icons = new Icon[this.iconCount];
		ArrayList<Integer> iconList = new ArrayList<Integer>();
		for(int i=0; i<iconCount; i++) {
			icons[i] = new ImageIcon(getClass().getResource((i+1) + ".gif"));
			iconList.add(i);
			iconList.add(i);
		}

		Random rnd = new Random();
		for (int i = 0; i < this.cardCount; i++) {
			int index = rnd.nextInt(iconList.size());
			int iconNum = iconList.remove(index);
			cards[i] = getButton(panel, iconNum);
		}

		add(panel, BorderLayout.CENTER);
	}

	private JButton getButton(JPanel panel, int iconNum) {
		JButton b = new JButton();

		b.setIcon(coveredIcon);
		// you want to associate the button with an icon
		// I'd extent JButton for this, but for the sake of example
		// you can use action command to store an arbitrary string in the button
		b.setActionCommand("" + iconNum);
		// you would want to add an action listener here

		panel.add(B)/>;
		return b;
	}
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#11 MissQT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-June 12

Re: Help with memory game

Posted 23 June 2012 - 12:06 PM

Thank you. Your reply was very helpful to me.
However, I have to admit that time may not be in my favor.
Do you have any project ideas that could be fone within such a short period of time?

Thanks for your help in advance
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1