Results 1 to 7 of 7

Thread: Memory Game

  1. #1
    torres is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default Memory Game

    I want to make a Memory Game with 4 x 4 buttons(JButtons - 16 blank buttons) and a "Play Again" and "Exit" button below. The 4 x 4 buttons are blank as the game starts, but when you push one of the buttons it should show a number. You can only click two buttons in a row. And if the two buttons show the same numbers, these buttons shall be disabled.

    If you click "Play Again", the game should restart and if you click "Exit", you exit the application. If you complete the game, a window is suposed to show saying "Congratulations, you made it!".

    Someone knows how to make that? Have to make it for a scool-delivery.

    Thanks inadvance.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Have you read The Swing Tutorial? Once you've been through that make an attempt yourself and then come back with specific questions.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    torres is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default RE:

    The tutorial is nice, but my main problem is how to make the programming for the application. The actual handlings, not the design. Programming with for-loops and math.random and so on. One of the problems is to make 16 JButtons, but in a array. I've tried it many times, but its not working. I propably write something wrong...

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Do you mean in a JButton[], or arranged in a grid? Post your code attempting this and we'll have a look.

    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics) and Math (Java Platform SE 6) will tell you all about for-loops and math.random
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    torres is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

    Default

    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.*;

    public class Memory extends JFrame implements ActionListener
    {
    private JButton btnPlayAgain, btnExit;
    private int [] hiddenNumbers;

    public Memory()
    {
    JButton button[] = new JButton[16];
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(4,4));
    for(int i = 0; i < button.length; i++)
    {
    button[i] = new JButton(Integer.toString(i+1));
    p1.add(button[i]);
    }

    Container c = getContentPane();
    c.add(p1);

    btnPlayAgain = new JButton("Play Again");
    btnExit = new JButton("Exit");

    btnPlayAgain.addActionListener(this);
    btnExit.addActionListener(this);

    JPanel pnlTopp = new JPanel(new GridLayout(1,1));
    add(pnlTopp, BorderLayout.SOUTH);
    pnlTopp.add(btnPlayAgain);
    pnlTopp.add(btnExit);

    setTitle("MEMORY");
    setSize(300,300);
    setVisible(true);
    setResizable(false);

    }

    public static void main(String[]args)
    {
    new Memory();
    }

    public void actionPerformed(ActionEvent event)
    {
    JButton clicked = (JButton)event.getSource();
    if(clicked == btnExit)
    {
    System.exit(0);
    }

    else if(clicked == btnPlayAgain)
    {

    }
    }
    }

    This may not be very good coding, because i've mixed together some few codes. hiddenNumbers are not made proparly, because i don't know how to hide them:P....

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Remember to call validate() or revalidate() when you're done adding and removing components.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  7. #7
    torres is offline Member
    Join Date
    Apr 2009
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  2. help with memory game!
    By rac in forum New To Java
    Replies: 6
    Last Post: 04-14-2009, 12:39 AM
  3. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 07:00 AM
  4. Replies: 2
    Last Post: 08-21-2008, 08:33 PM
  5. Memory
    By mew in forum CLDC and MIDP
    Replies: 0
    Last Post: 12-28-2007, 12:02 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
  •