Results 1 to 3 of 3
Thread: Card memory game applet.
- 04-21-2011, 02:57 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
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() // ---------------------------------------------------------------------------- { } //----------------------------------------------------------------------------- //||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| //----------------------------------------------------------------------------- } // ================================================================================
- 04-21-2011, 03:10 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
That seems like a lot of code for what could essentially boil down to
Perhaps I am misunderstanding though, please clarify if I am.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
- 04-21-2011, 04:02 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
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
-
Creating a Card Game
By Kidd91 in forum New To JavaReplies: 1Last Post: 12-24-2010, 02:25 PM -
Card Game
By abby0910 in forum New To JavaReplies: 1Last Post: 07-24-2010, 12:38 AM -
please help me with this card game
By noobinoo in forum New To JavaReplies: 13Last Post: 03-28-2010, 02:07 PM -
Memory card game using JOption... Need help flipping cards...more info inside, thanks
By moe123 in forum New To JavaReplies: 1Last Post: 12-02-2009, 03:39 PM -
Have been stuck up coding the GUI for Memory Card Game
By Galore in forum New To JavaReplies: 44Last Post: 10-19-2009, 08:07 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks