Results 1 to 3 of 3
  1. #1
    Soufly is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default Card memory game applet.

    Hello, I'm trying to build a card memory game in an 8 by 6 Gridlayout to have 48 cards in total which you have to find the pairs. The cards are in a 2d array which is also 8 by 6. I can get the pairs to appear on the GUI no problem, but the problem is that the pairs appear side by side.

    I know that I can just put a button that that does a collection.shuffle, but I want the cards to be shuffled right when the applet starts. The shuffling parts works, but the pairs still stick together, kinda cute if you ask me.

    Any suggestions? Please let me if you need more details, thanks in advance!

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    import java.util.Collections;
    // ================================================================================
    public class JeuDeCarte extends JApplet
    // ================================================================================
    {
    	//Variables d'instances--------------------------------------------------------
    	private JLabel cartes[][] = new JLabel[8][6];
    	private JPanel canevas = new JPanel();
    	private ArrayList<JLabel[][]> listeCartes = new ArrayList<JLabel[][]>();
    	private ArrayList<String> listePos = new ArrayList<String>();
    	// ----------------------------------------------------------------------------
    	public void init()
    	// ----------------------------------------------------------------------------
    	{
    		initialiserGUI();
    		enregistrerListeners();
    	}
    	//-----------------------------------------------------------------------------
    	//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
    	//-----------------------------------------------------------------------------
    	private void initialiserGUI()
    	// ----------------------------------------------------------------------------
    	{
    		this.add(canevas);
    		canevas.setLayout(new GridLayout( 8, 6, 2, 2 ));
    		afficheCartes();
    		int compteur = 0;
    		//Parcour le tableau de cartes.
    		for ( int i=0; i<8; i++ )
    		{
    			for ( int n=0; n<6; n++ )
    			{
    				//S'assure que seulement 24 carte soit choisit.
    				if ( compteur<24 )
    				{
    					//Choisit une carte au hasard.
    					int nb1 = ( int )( Math.random() *4 );
    					int nb2 = ( int )( Math.random() * 13 ) + 1;
    					if ( nb2 > 9)
    					{
    						//Choisit la position de la carte au hasard.
    						for ( int k=0; k<2; k++)
    						{
    							int posX = ( int )( Math.random() * 8 );
    							int posY = ( int )( Math.random() * 6 );
    							cartes[posX][posY] = new JLabel( new ImageIcon
    									( getClass().getResource( "cartes/carte" + 
    												nb1 + "_" + nb2 + ".png" ) ) );
    							
    							canevas.add( cartes[posX][posY] );
    							
    						}
    					}
    					else
    					{
    						for ( int l=0; l<2; l++)
    						{
    							int posX = ( int )( Math.random() * 8 );
    							int posY = ( int )( Math.random() * 6 );
    							cartes[posX][posY] = new JLabel( new ImageIcon
    									( getClass().getResource( "cartes/carte" + 
    											nb1 + "_0" + nb2 + ".png" ) ) );
    							
    							canevas.add( cartes[posX][posY] );		
    						}
    					}
    				}
    				compteur++;
    			}
    		}
    	}
    	//-----------------------------------------------------------------------------
    	//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
    	//-----------------------------------------------------------------------------
    	private void enregistrerListeners()
    	// ----------------------------------------------------------------------------
    	{
    		
    	}
    	//-----------------------------------------------------------------------------
    	//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
    	//-----------------------------------------------------------------------------
    	private void afficheCartes()
    	// ----------------------------------------------------------------------------
    	{
    		
    	}
    	//-----------------------------------------------------------------------------
    	//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
    	//-----------------------------------------------------------------------------
    }
    // ================================================================================

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    That seems like a lot of code for what could essentially boil down to
    Java Code:
    shuffle 2d array
    loop i
      loop j
        add item in 2d array at i j to panel
      end loop j
    end loop i
    Perhaps I am misunderstanding though, please clarify if I am.

  3. #3
    Soufly is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Thank you for your reply,

    Nah, it's just because of the way I named the cards. like the first ones would be: carte1_01, carte1_02...and so on. Up to carte2_13, thus the weird code with the extra loops. I guess there would be a way to make it simpler, but i just don't know how.

    Coming back to the shuffling...I tried doing : listeCartes.add(cartes) which adds my 2d array to the array list which is listeCartes and then did a Collection.shuffle(ListeCartes) but the pairs still appear side by side...Any suggestions?

Similar Threads

  1. Creating a Card Game
    By Kidd91 in forum New To Java
    Replies: 1
    Last Post: 12-24-2010, 03:25 PM
  2. Card Game
    By abby0910 in forum New To Java
    Replies: 1
    Last Post: 07-24-2010, 01:38 AM
  3. please help me with this card game
    By noobinoo in forum New To Java
    Replies: 13
    Last Post: 03-28-2010, 03:07 PM
  4. Replies: 1
    Last Post: 12-02-2009, 04:39 PM
  5. Replies: 44
    Last Post: 10-19-2009, 09:07 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
  •