Results 1 to 10 of 10
Thread: Memory game
- 04-09-2010, 05:59 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 3
- Rep Power
- 0
Memory game
i have problem when i am trying to run this code......can anyone solve this problem....its about memory matching game.....
here is the code i am trying to work with...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.applet.*;
import java.util.Random;
public class Memory extends JFrame
{
static JLabel guessLabel = new JLabel ();
static JLabel label0 = new JLabel ();
static JLabel label1 = new JLabel ();
static JLabel label2 = new JLabel ();
static JLabel label3 = new JLabel ();
static JLabel label4 = new JLabel ();
static JLabel label5 = new JLabel ();
static JLabel label6 = new JLabel ();
static JLabel label7 = new JLabel ();
static JLabel label8 = new JLabel ();
static JLabel label9 = new JLabel ();
static JLabel label10 = new JLabel ();
static JLabel label11 = new JLabel ();
static JLabel label12 = new JLabel ();
static JLabel label13 = new JLabel ();
static JLabel label14 = new JLabel ();
static JLabel label15 = new JLabel ();
static JLabel[] boxlabel = new JLabel [16];
static ImageIcon apple = new ImageIcon ("apple.gif");
static ImageIcon bannana = new ImageIcon ("bannana.gif");
static ImageIcon cherry = new ImageIcon ("cherry.gif");
static ImageIcon grape = new ImageIcon ("grape.gif");
static ImageIcon orange = new ImageIcon ("orange.gif");
static ImageIcon lemon = new ImageIcon ("lemon.gif");
static ImageIcon plum = new ImageIcon ("plum.gif");
static ImageIcon pear = new ImageIcon ("apple.pear");
static ImageIcon backing = new ImageIcon ("backing.gif");
static ImageIcon apple = new ImageIcon ("apple.gif");
static ImageIcon[] choiceIcon = new ImageIcon [8];
static JButton newButton =new JButton();
static JButton exitButton = new JButton();
static Random myRandom = new Random();
static int choice ;
static int index;
static int [] picked =new int [2] ;
static int [] behind = new int [16];
static int guesses ;
static int remaining ;
static AudioClip matchsound ;
static AudioClip noMatchSound;
static Timer delayTimer ;
public static void main (String args[])
{
new Memory().show(); //create frame
try //getsounds
{
matchsound =Applet.newAudioClip (new URL("file:" + "aaa.wav"));
noMatchSound =Applet.newAudioClip(new URL("file" +"abc.wav"));
}
catch (Exception ex)
{
}
newButton.doclick();
}
public Memory()
{
setTitle ("Memory game");
getContentPane().setBackground(Color BLUE);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt )
{
exitForm(evt);
}
});
getContentPane().setLayout(new GridLayout ());
GridBagConstraints gridConstraints = new GridBagConstraints();
guessLabel.setText("Guesses : 0");
guessLabel.setForeground(Color .WHITE);
guessLabel.setFont(new Font ("Arial",Font.BOLD,18));
gridConstraints.gridx =1;
gridConstraints.gridy =0;
gridConstraints.gridwidth =2;
gridConstraints.insets =new Insets(10,10,10,18);
getContentPane().add(guessLabel.gridConstraints);
boxLabel[0]=label0;
boxLabel[1]=label1;
boxLabel[2]=label2;
boxLabel[3]=label3;
boxLabel[4]=label4;
boxLabel[5]=label5;
boxLabel[6]=label6;
boxLabel[7]=label7;
boxLabel[8]=label8;
boxLabel[9]=label9;
boxLabel[10]=label10;
boxLabel[11]=label11;
boxLabel[12]=label12;
boxLabel[13]=label13;
boxLabel[14]=label14;
boxLabel[15]=label15;
int x=0;
int y=1;
for (i=0;i<16;i++)
{
gridConstraints= new GridBagConstraints();
boxLabel[i].setPreferredSize(new Dimension (70,70));
boxLabel[i].setIcon(backing);
gridConstraints.gridx = x;
gridConstraints.gridy = y;
gridConstraints.insets = new Insets(5,5,5,5);
getContentPane().add(boxLabel[i].gridConstraints);
boxLabel[i].addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
labelMouseClicked(e);
}
});
x++;
if (x==3)
{
x=0;
y+=1;
}
}
newButton.setText("New Game");
gridConstraints =new GridBagConstraints();
gridConstraints.gridx = 1;
gridConstraints.gridy = 6;
gridConstraints.gridwidth = 2;
gridConstraints.insets = new Insets(10,10,10,10);
getContentPane().add(newbutton.gridConstraints);
newButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
newButtonActionPerformed(e);
}
});
exitButton.setText("Exit");
gridConstraints= new GridBagConstraints();
gridConstraints.gridx=1;
gridConstraints.gridy=7;
gridConstraints.gridwidth=2;
gridConstraints.insets= new Insets (0,10,10,10);
getContentPane().add(exitButton.gridConstraints);
exitButton.addActionListener(new ActionListener()
{
public void actionperformed (ActionEvent e)
{
exitButtonActionPerformed(e);
}
});
delayTimer = new Timer (1.new ActionListener()
{
delayTimerActionPerfirmed(e) ;
});
pack();
Dimension screenSize = Toolkit. getDefaultToolkit().getScreenSize();
setBounds((int)(0.5*(screenSize.width-getWidth())).(int)(0.5*(screenSize.x-getX())).(int(0.5)*(screenSize.y-getY()))
choiceIcon[1]= apple;
choiceIcon[2]= bannana;
choiceIcon[3]= cherry;
choiceIcon[4]= grape;
choiceIcon[5]= orange;
choiceIcon[6]= pear;
choiceIcon[7]= plum;
}
private void labelMouseClicked(MouseEvent e)
{
Component clickedComponent =e.getComponent();
for (index=0;index<16;i++)
{
if (clickedComponent == boxLabel[index])
{
break;
}
}
if ((choice == 1 && index==picked[0])||behind[index==-1|| newButton])
{
return;
}
boxLabel[index].setIcon(choiceIcon[behind[index]]);
if (choice==0)
{
picked[0]=index;
choice =1;
return ;
}
delayTimer.start ();
}
private void newButtonActionPerformed(ActionEvent e)
{
guesses = 0;
remaining = 8;
guessLabel.setText("Guesses :0");
behind = sortIntegers(16);
for(int i=0;i<16;i++)
{
boxLabel[i].setIcon(backing);
if (behind[i]>7)
{
behind[i]=behind [i]-8;
}
}
choice =0 ;
newButton.setEnabled(false);
exitButton.setText("Stop" );
}
private void exitButtonActionPerformed(ActionEvent e)
{
if (exitButton. getText().equals ("Exit"))
{
exitform (null);
}
else
{
exitButton.setText("Exit");
newButton.setEnable(true);
}
}
private void exitForm(WindowEvent evt)
{
System.exit(0);
}
private void delayTimerActionPerformed(ActionEvent e)
{
delayTimer.stop();
guesses ++;
guessLabel.setText("Guesses"+String.value of (guesses));
Picked[i]=index;
if (behind[picked[0]]==behind[picked[i]])
{
matchSound.play();
behind[picked[0]]=-1;
behind[picked[1]]=-1;
remaining--;
}
else
{
noMatchSound.play();
long t=System .currentTimeMillis();
do {} while (System.currentTimeMillis()-t<1000);
boxLabel[picked[0]].setIcon(backing);
boxLabel[picked[1]].setIcon(backing);
}
choice =0 ;
if (remaining ==0)
{
exitButton.doClick();
newButton.requestFocus();
}
}
private static int[] sortIntegers(int n)
{
int nArray[]=new int [n];
int temp,s;
Random myRandom = new Random ();
for (int i=0;i<n;i++)
{
nArray[i]=i;
}
for (int i=0;i>=1;i--)
{
s = myRandom.nextInt(i);
temp = nArray[s];
nArray [s] = nArray[i-1];
nArray[i-1] = temp;
}
return (nArray);
}
}
- 04-09-2010, 06:06 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,421
- Blog Entries
- 7
- Rep Power
- 26
- 04-09-2010, 06:35 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 3
- Rep Power
- 0
it does not compile...showing some error....i do not know why....can u correct this....
i have problem in those two lines that given below.......
setBounds((int)(0.5*(screenSize.width-getWidth())).(int)(0.5*(screenSize.x-getX())).(int(0.5)*(screenSize.y-getY()))
if ((choice == 1 && index==picked[0])||behind[index==-1|| newButton])
- 04-09-2010, 06:48 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,421
- Blog Entries
- 7
- Rep Power
- 26
So you are having compilation problems and you weren't even able to run that code; please be as exact as possible and help us to help you: I bet the compiler didn't just whine "some error" and quit its job, it showed you the exact position where it didn't understand your code and spitted a message at you; what is that position and what did the compiler say?
kind regards,
Jos
- 04-09-2010, 06:51 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,421
- Blog Entries
- 7
- Rep Power
- 26
- 04-09-2010, 07:20 PM #6
Senior Member
- Join Date
- Feb 2009
- Posts
- 312
- Rep Power
- 10
Java Code:if ((choice == 1 && index==picked[0])||behind[index==-1|| newButton])
That is where you first problem is....
Also this,
Java Code:setBounds((int)(0.5*(screenSize.width-getWidth())).(int)(0.5*(screenSize.x-getX())).(int(0.5)*(screenSize.y-getY()))
Java Code:int val1 = 0.5 * (screenSize.width - getWidth()); int val2 = 0.5 * (screenSize.x - getX()); int val3 = 0.5 * (screenSize.y - getY()); setBounds(val1.val2.val3);
Break things down if you don't understand things and it usually always makes things clearer.
- 04-14-2010, 09:12 PM #7
Member
- Join Date
- Mar 2010
- Posts
- 3
- Rep Power
- 0
hey ..it is still not working.......i think i made mistake in those two lines......it's also showing some compiler error.....bt thats is not so handy bt...those two lines are handy i guess
- 04-14-2010, 10:58 PM #8
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 9
there are lots of mistakes, I'll try to fix some of them:
static ImageIcon apple = new ImageIcon ("apple.gif");
static ImageIcon bannana = new ImageIcon ("bannana.gif");
static ImageIcon cherry = new ImageIcon ("cherry.gif");
static ImageIcon grape = new ImageIcon ("grape.gif");
static ImageIcon orange = new ImageIcon ("orange.gif");
static ImageIcon lemon = new ImageIcon ("lemon.gif");
static ImageIcon plum = new ImageIcon ("plum.gif");
static ImageIcon pear = new ImageIcon ("apple.pear");
static ImageIcon backing = new ImageIcon ("backing.gif");
static ImageIcon apple = new ImageIcon ("apple.gif");
you have 2 times same line of code:
static ImageIcon apple = new ImageIcon ("apple.gif");
delete one of this lines.
2.
getContentPane().setBackground(Color BLUE);
change with
getContentPane().setBackground(Color.BLUE);
3.new Memory().show();
.show() is obsolete, I'll try to fix that later.
4.
for (i=0;i<16;i++)
change with
for (int i=0;i<16;i++)
5.
for (index=0;index<16;i++)
change with
for (int index=0;index<16;index++)
6.
guessLabel.setText("Guesses"+String.value of (guesses));
change with
guessLabel.setText("Guesses"+String.valueOf (guesses));
That's few little mistakes. There are still lots of mistakes. I'll look deep in code to resolve other problems.
- 04-14-2010, 11:31 PM #9
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 9
7.
newButton.doclick();
change with:
newButton.doClick();
8.
getContentPane().add(guessLabel,gridConstraints);
change with:
getContentPane().add(guessLabel,gridConstraints);
9.
getContentPane().add(newbutton.gridConstraints);
change with:
getContentPane().add(newButton,gridConstraints);
10.
getContentPane().add(exitButton.gridConstraints);
change with:
getContentPane().add(exitButton,gridConstraints);
11.
newButton.setEnable(true);
change with:
newButton.setEnabled(true);
12.
exitform (null);
change with
exitForm (null);
13.
public void actionperformed (ActionEvent e)
change with:
public void actionPerformed (ActionEvent e)
Now, I'll try to find problem with boxLabel
- 04-15-2010, 11:00 AM #10
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 9
I guess this is a complete code:
Java Code:/* * Memory.java */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.URL; import java.applet.*; import java.util.Random; public class Memory extends JFrame { static JLabel guessLabel = new JLabel(); static JLabel label0 = new JLabel(); static JLabel label1 = new JLabel(); static JLabel label2 = new JLabel(); static JLabel label3 = new JLabel(); static JLabel label4 = new JLabel(); static JLabel label5 = new JLabel(); static JLabel label6 = new JLabel(); static JLabel label7 = new JLabel(); static JLabel label8 = new JLabel(); static JLabel label9 = new JLabel(); static JLabel label10 = new JLabel(); static JLabel label11 = new JLabel(); static JLabel label12 = new JLabel(); static JLabel label13 = new JLabel(); static JLabel label14 = new JLabel(); static JLabel label15 = new JLabel(); static JLabel[] boxLabel = new JLabel[16]; static ImageIcon apple = new ImageIcon("apple.gif"); static ImageIcon banana = new ImageIcon("banana.gif"); static ImageIcon cherry = new ImageIcon("cherry.gif"); static ImageIcon grape = new ImageIcon("grape.gif"); static ImageIcon lemon = new ImageIcon("lemon.gif"); static ImageIcon orange = new ImageIcon("orange.gif"); static ImageIcon pear = new ImageIcon("pear.gif"); static ImageIcon plum = new ImageIcon("plum.gif"); static ImageIcon backing = new ImageIcon("backing.gif"); static ImageIcon[] choiceIcon = new ImageIcon[8]; static JButton newButton = new JButton(); static JButton exitButton = new JButton(); static Random myRandom = new Random(); static int choice; static int index; static int[] picked = new int[2]; static int[] behind = new int[16]; static int guesses; static int remaining; static AudioClip matchSound; static AudioClip noMatchSound; static Timer delayTimer; public static void main(String args[]) { // create frame new Memory().show(); // get sounds try { matchSound = Applet.newAudioClip(new URL("file:" + "tada.wav")); noMatchSound = Applet.newAudioClip(new URL("file:" + "uhoh.wav")); } catch (Exception ex) { } // start first game newButton.doClick(); } public Memory() { // frame constructor setTitle("Memory Game"); // setResizable(false); getContentPane().setBackground(Color.BLUE); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { exitForm(evt); } }); getContentPane().setLayout(new GridBagLayout()); // position controls GridBagConstraints gridConstraints = new GridBagConstraints(); guessLabel.setText("Guesses: 0"); guessLabel.setForeground(Color.WHITE); guessLabel.setFont(new Font("Arial", Font.BOLD, 18)); gridConstraints.gridx = 1; gridConstraints.gridy = 0; gridConstraints.gridwidth = 2; gridConstraints.insets = new Insets(10, 10, 10, 10); getContentPane().add(guessLabel, gridConstraints); boxLabel[0] = label0; boxLabel[1] = label1; boxLabel[2] = label2; boxLabel[3] = label3; boxLabel[4] = label4; boxLabel[5] = label5; boxLabel[6] = label6; boxLabel[7] = label7; boxLabel[8] = label8; boxLabel[9] = label9; boxLabel[10] = label10; boxLabel[11] = label11; boxLabel[12] = label12; boxLabel[13] = label13; boxLabel[14] = label14; boxLabel[15] = label15; int x = 0; int y = 1; for (int i = 0; i < 16; i++) { gridConstraints = new GridBagConstraints(); boxLabel[i].setPreferredSize(new Dimension(70, 70)); boxLabel[i].setIcon(backing); gridConstraints.gridx = x; gridConstraints.gridy = y; gridConstraints.insets = new Insets(5, 5, 5, 5); getContentPane().add(boxLabel[i], gridConstraints); boxLabel[i].addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { labelMouseClicked(e); } }); x++; if (x > 3) { x = 0; y += 1; } } newButton.setText("New Game"); gridConstraints = new GridBagConstraints(); gridConstraints.gridx = 1; gridConstraints.gridy = 6; gridConstraints.gridwidth = 2; gridConstraints.insets = new Insets(10, 10, 10, 10); getContentPane().add(newButton, gridConstraints); newButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { newButtonActionPerformed(e); } }); exitButton.setText("Exit"); gridConstraints = new GridBagConstraints(); gridConstraints.gridx = 1; gridConstraints.gridy = 7; gridConstraints.gridwidth = 2; gridConstraints.insets = new Insets(0, 10, 10, 10); getContentPane().add(exitButton, gridConstraints); exitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { exitButtonActionPerformed(e); } }); delayTimer = new Timer(1, new ActionListener() { public void actionPerformed(ActionEvent e) { delayTimerActionPerformed(e); } }); pack(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setBounds((int) (0.5 * (screenSize.width - getWidth())), (int) (0.5 * (screenSize.height - getHeight())), getWidth(), getHeight()); choiceIcon[0] = apple; choiceIcon[1] = banana; choiceIcon[2] = cherry; choiceIcon[3] = grape; choiceIcon[4] = lemon; choiceIcon[5] = orange; choiceIcon[6] = pear; choiceIcon[7] = plum; } private void labelMouseClicked(MouseEvent e) { Component clickedComponent = e.getComponent(); for (index = 0; index < 16; index++) { if (clickedComponent == boxLabel[index]) { break; } } // If trying to pick same box, picking already selected box // or trying pick when not playing, exit if ((choice == 1 && index == picked[0]) || behind[index] == -1 || newButton.isEnabled()) { return; } // Display selected picture boxLabel[index].setIcon(choiceIcon[behind[index]]); if (choice == 0) { picked[0] = index; choice = 1; return; } // use timer to process remaining code to allow // label control to refresh delayTimer.start(); } private void newButtonActionPerformed(ActionEvent e) { guesses = 0; remaining = 8; guessLabel.setText("Guesses: 0"); // Randomly sort 16 integers using Shuffle routine // Behind contains indexes (0 to 7) for hidden pictures behind = sortIntegers(16); for (int i = 0; i < 16; i++) { // reset image boxLabel[i].setIcon(backing); if (behind[i] > 7) { behind[i] = behind[i] - 8; } } choice = 0; newButton.setEnabled(false); exitButton.setText("Stop"); } private void exitButtonActionPerformed(ActionEvent e) { if (exitButton.getText().equals("Exit")) { exitForm(null); } else { exitButton.setText("Exit"); newButton.setEnabled(true); } } private void exitForm(WindowEvent evt) { System.exit(0); } private void delayTimerActionPerformed(ActionEvent e) { // finish processing of display delayTimer.stop(); guesses++; guessLabel.setText("Guesses: " + String.valueOf(guesses)); picked[1] = index; if (behind[picked[0]] == behind[picked[1]]) { // If match, play sound matchSound.play(); behind[picked[0]] = -1; behind[picked[1]] = -1; remaining--; } else { // If no match, blank picture, restore backs noMatchSound.play(); // delay 1 second long t = System.currentTimeMillis(); do {} while (System.currentTimeMillis() - t < 1000); boxLabel[picked[0]].setIcon(backing); boxLabel[picked[1]].setIcon(backing); } choice = 0; if (remaining == 0) { exitButton.doClick(); newButton.requestFocus(); } } private static int[] sortIntegers(int n) { /* * Returns n randomly sorted integers 0 -> n - 1 */ int nArray[] = new int[n]; int temp, s; Random myRandom = new Random(); // initialize array from 0 to n - 1 for (int i = 0; i < n; i++) { nArray[i] = i; } // i is number of items remaining in list for (int i = n; i >= 1; i--) { s = myRandom.nextInt(i); temp = nArray[s]; nArray[s] = nArray[i - 1]; nArray[i - 1] = temp; } return(nArray); } }
Similar Threads
-
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 -
memory game in JAVA
By lclclc in forum New To JavaReplies: 19Last Post: 10-18-2009, 04:41 PM -
Help to make memory game :=)
By arian88 in forum AWT / SwingReplies: 7Last Post: 10-15-2009, 06:23 AM -
Memory Game
By torres in forum AWT / SwingReplies: 6Last Post: 04-21-2009, 11:00 PM -
help with memory game!
By rac in forum New To JavaReplies: 6Last Post: 04-13-2009, 11:39 PM
Bookmarks