Results 1 to 3 of 3
  1. #1
    phb5004 is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default Difficulties randomizing images in an array

    I am creating a Memory game in java, however I am having some problems with some last minute things. I have 8 pairs of images in an array and I am getting them to be selected randomly, however I need each image to be selected exactly 2 times. As of now the images are just being selected at random, which is creating an uneven amount of pairs... here is my code, I appreciate any help.

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

    public class GamePanel extends JPanel
    {
    private int[][] gameBoard = new int[4][4];
    private JButton[][] buttons = new JButton[4][4];
    private JPanel picturePanel, buttonPanel;
    private JButton quitButton;
    private boolean isFirstButton;
    private int firstPictureIndex;
    private JButton firstButton;
    private int secondPictureIndex;
    private JButton secondButton;

    private ImageIcon cardBack = new ImageIcon("card.gif");

    private ImageIcon[] images = {new ImageIcon("cheetah.gif"),
    new ImageIcon("giraffe.gif"),
    new ImageIcon("panther.gif"),
    new ImageIcon("tiger.gif"),
    new ImageIcon("koohala.gif"),
    new ImageIcon("monkey.gif"),
    new ImageIcon("dog.gif"),
    new ImageIcon("zebra.gif")};

    public GamePanel()
    {
    int rnd;
    System.out.println("How good is your Memory?!");

    quitButton = new JButton("Quit");
    buttonPanel = new JPanel();
    quitButton.addActionListener(new ButtonListener());
    buttonPanel.add(quitButton);
    picturePanel = new JPanel();
    picturePanel.setLayout (new GridLayout (4,4));
    for (int i = 0; i < images.length; i++)
    {
    for (int j = 0; j < images.length; j++)
    {
    rnd = (int)(Math.random()*16);
    gameBoard[i][j] = rnd;
    buttons[i][j] = new JButton(cardBack);
    picturePanel.add(buttons[i][j]);
    buttons[i][j].addActionListener(new ButtonListener());
    }
    }



    isFirstButton = true;

    setLayout(new BorderLayout());
    add(picturePanel, BorderLayout.CENTER);
    add(buttonPanel, BorderLayout.SOUTH);
    setBackground (Color.red);
    setPreferredSize(new Dimension(600, 600));
    }

    // Listener for button pressing events
    private class ButtonListener implements ActionListener
    {
    public void actionPerformed(ActionEvent evt)
    {
    for (int i = 0; i < 4; i++)
    {
    for (int j = 0; j < 4; j++)
    {
    if (evt.getSource() == buttons[i][j])
    {
    handleButton(i,j);
    }
    }
    }
    if (evt.getSource() == quitButton)
    System.exit(0);
    }
    }

    public void handleButton(int row, int col)
    {
    int imageIdx = gameBoard[row][col];

    (buttons[row][col]).setIcon(images[imageIdx]);
    if (isFirstButton)
    {
    firstPictureIndex = imageIdx;
    firstButton = buttons[row][col];
    isFirstButton = false;
    } else // second button
    {
    secondPictureIndex = imageIdx;
    secondButton = buttons[row][col];
    if (firstPictureIndex == secondPictureIndex) // pictures match!
    {
    JOptionPane.showMessageDialog(this, "They match!");
    }
    else
    {
    JOptionPane.showMessageDialog(this, "They do not match!");
    try {
    Thread.sleep(2000);
    } catch (InterruptedException exc) { System.out.println("Interrupted exception"); }
    firstButton.setIcon(cardBack);
    secondButton.setIcon(cardBack);
    }
    isFirstButton = true;
    }
    }
    }

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    how about instead of randomly selecting 4 * 4 images, (e.g. 16 random images where we know we need 8 pairs,
    why not do a first pass where you randomly select (4 * 4) / 2 images. that is the set of images that will need to be rendered in pairs. oh, but in this case you just have 8 images anyway.

    ok then, so how about build a 'set' of image placement positions, where for each image, computes two locations,

    [psudocode]
    set<coordinates>

    for each image {
    for ( 1..2 ) {
    while ( (coordinate = random() ) is not in set of coordinates) { }
    add coodinate to set;
    }
    }
    [/psudocode]

  3. #3
    phb5004 is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default memorygame

    I think I understand what your saying, but I have no idea how I would actually code it. I'm very new to java and I just don't understand it well... I appreciate you helping me

Similar Threads

  1. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  2. randomizing an array HELP! please!
    By hugolord in forum New To Java
    Replies: 12
    Last Post: 08-10-2008, 03:36 AM
  3. Difficulties logging onto Sun Developer's Network
    By jocassid in forum New To Java
    Replies: 0
    Last Post: 07-01-2008, 02:02 AM
  4. JOptionPane Display Difficulties
    By kewlgeye in forum New To Java
    Replies: 7
    Last Post: 05-09-2008, 08:09 PM
  5. How do I create an Array of Images
    By wco5002 in forum New To Java
    Replies: 3
    Last Post: 03-21-2008, 03:45 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •