Results 1 to 3 of 3
- 12-11-2009, 03:04 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 2
- Rep Power
- 0
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;
}
}
}
- 12-11-2009, 04:39 AM #2
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]
- 12-11-2009, 04:45 AM #3
Member
- Join Date
- Dec 2009
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM -
randomizing an array HELP! please!
By hugolord in forum New To JavaReplies: 12Last Post: 08-10-2008, 03:36 AM -
Difficulties logging onto Sun Developer's Network
By jocassid in forum New To JavaReplies: 0Last Post: 07-01-2008, 02:02 AM -
JOptionPane Display Difficulties
By kewlgeye in forum New To JavaReplies: 7Last Post: 05-09-2008, 08:09 PM -
How do I create an Array of Images
By wco5002 in forum New To JavaReplies: 3Last Post: 03-21-2008, 03:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks