Results 1 to 7 of 7
Thread: help with memory game!
- 04-13-2009, 05:35 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 2
- Rep Power
- 0
help with memory game!
Hi! i am a beginner in Java and am using netbeans 6.5. my friend helped me a lot to write a memory game but am having some problems. sometimes, two cards that are not the same are being seen as a match. ave gone through the codes several times but cannot find where the problem is. am thinking the problem is from the checkstatus() i would be grateful if someone could help. thank u!
/* Memory game. Rachele Salmon (WLC) */
package java1;
//Import all java packages
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import javax.swing.border.*;
public class JavaMemory extends javax.swing.JFrame implements ActionListener
{
private JPanel contentPane;
private JMenuBar jMenuBar1 = new JMenuBar();
// Declare and initialise buttons
private JButton bt[] = new JButton[16];
private JButton bt1 = new JButton();
private JButton bt2 = new JButton();
private JButton bt3 = new JButton();
private JButton bt4 = new JButton();
private JButton bt5 = new JButton();
private JButton bt6 = new JButton();
private JButton bt7 = new JButton();
private JButton bt8 = new JButton();
private JButton bt9 = new JButton();
private JButton bt10 = new JButton();
private JButton bt11 = new JButton();
private JButton bt12 = new JButton();
private JButton bt13 = new JButton();
private JButton bt14 = new JButton();
private JButton bt15 = new JButton();
private JButton bt16 = new JButton();
private JButton btTimer = new JButton();
private JButton Exit = new JButton();
private JButton btreset = new JButton();
//Declare and initialise icon for the pictures
ImageIcon myimage = new ImageIcon("");
// To create an array of integer.
private int no[]= new int [16];
// To allow the player to enter his name
String name = new String();
// Create random generator to generate random numbers
private Random r= new Random();
// Counters
int count = bt.length;
// To record the number of tries
int numclick = 0;
// Keep track of which button is pressed
int ButtonPressed=-1;
// Keep track of the number
int numtrack;
// To check the state of a game when double clicking
boolean turn=false;
// Declare a boolean array
boolean Buttons[] = new boolean[16];
// To create timers. The time is in milliseconds
Timer timerGame = new Timer(1000,null);
// To store time
int timeStart;
// The time left for the player to solve the game
int timeLeft;
// Time Delay for turning cards face down
Timer timer = new Timer(600, null);
// Create Labels, border and components for the menu
TitledBorder BorderTitle;
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JLabel jLabel3 = new JLabel();
private JLabel jLabel4 = new JLabel();
private JLabel jLabel5 = new JLabel();
private JMenu jMenuFile = new JMenu();
private JMenuItem jMenuNewGame = new JMenuItem();
private JMenuItem jMenuFileExit = new JMenuItem();
private JMenu jMenuHelp = new JMenu();
private JMenuItem jMenuInstructions = new JMenuItem();
private JTextField jTextField1 = new JTextField();
private JTextField jTextName = new JTextField();
private JTextField jTextGameInfo = new JTextField();
// To construct the GUI
public JavaMemory()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
// Initialise Component
private void jbInit() throws Exception
{
BorderTitle = new TitledBorder("");
this.setBoard();
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(550, 560));
this.setTitle("MEMORY GAME");
contentPane = (JPanel) this.getContentPane();
// To handle all events when a button is clicked
bt1.setBounds(new Rectangle(75, 101, 64, 62));
bt1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bt1_actionPerformed(e);
}
});
bt2.setBounds(new Rectangle(139, 101, 64, 62));
bt2.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bt2_actionPerformed(e);
}
});
bt3.setBounds(new Rectangle(203, 101, 64, 62));
bt3.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bt3_actionPerformed(e);
}
});
bt4.setBounds(new Rectangle(267, 101, 64, 62));
bt4.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bt4_actionPerformed(e);
}
});
bt5.setBounds(new Rectangle(75, 163, 64, 62));
bt5.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bt5_actionPerformed(e);
}
});
bt6.setBounds(new Rectangle(139, 163, 64, 62));
bt6.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bt6_actionPerformed(e);
}
});
bt7.setBounds(new Rectangle(203, 163, 64, 62));
bt7.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bt7_actionPerformed(e);
}
});
bt8.setBounds(new Rectangle(267, 163, 64, 62));
bt8.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bt8_actionPerformed(e);
}
});
bt9.setBounds(new Rectangle(75, 225, 64, 62));
bt9.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bt9_actionPerformed(e);
}
});
bt10.setBounds(new Rectangle(139, 225, 64, 62));
bt10.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bt10_actionPerformed(e);
}
});
bt11.setBounds(new Rectangle(203, 225, 64, 62));
bt11.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bt11_actionPerformed(e);
}
});
bt12.setBounds(new Rectangle(267, 225, 64, 62));
bt12.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bt12_actionPerformed(e);
}
});
bt13.setBounds(new Rectangle(75, 287, 64, 62));
bt13.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bt13_actionPerformed(e);
}
});
bt14.setBounds(new Rectangle(139, 287, 64, 62));
bt14.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bt14_actionPerformed(e);
}
});
bt15.setBounds(new Rectangle(203, 287, 64, 62));
bt15.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bt15_actionPerformed(e);
}
});
bt16.setBounds(new Rectangle(267, 287, 64, 62));
bt16.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bt16_actionPerformed(e);
}
});
timer.setRepeats(false); // So that cards stay face up when turned
timer.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
timer_actionPerformed(e);
}
});
// Set the title of game
jLabel1.setBounds(new Rectangle(0, 18, 490, 28));
jLabel1.setFont(new java.awt.Font("Arial", 1, 24));
jLabel1.setText("MEMORY GAME");
jLabel1.setHorizontalAlignment(SwingConstants.CENT ER);
jLabel1.setForeground(Color.blue);
jLabel1.setRequestFocusEnabled(true);
jLabel2.setBounds(new Rectangle(0, 43, 570, 28));
// Set text to the menu bar options
jMenuFile.setText("File");
jMenuFileExit.setText("Exit");
jMenuHelp.setText("Help");
jMenuNewGame.setText("New Game");
// Implementation of the new game on the menu bar
jMenuNewGame.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jMenuNewGame_actionPerformed(e);
}
});
// Implementation of the exit functionality on the menu bar
jMenuFileExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jMenuFileExit_actionPerformed(e);
}
});
// Implementation of the timer limit
jTextField1.setText("200");// To set a time limit to complete the game
jTextField1.setBounds(new Rectangle(372, 243, 36, 25));
jTextField1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jTextField1_actionPerformed(e);
}
private void jTextField1_actionPerformed(ActionEvent e) {
//throw new UnsupportedOperationException("Not yet implemented");
}
});
// To implement the start button
btTimer.setBounds(new Rectangle(373, 120, 103, 30));
btTimer.setText("PLAY");
btTimer.setToolTipText("Press button to start game");
btTimer.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btTimer_actionPerformed(e);
}
});
timerGame.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
timerGame_actionPerformed(e);
}
});
// To implement the reset button
btreset.setBounds(new Rectangle(373, 155, 103, 30));
btreset.setToolTipText("Press button to reset game!");
btreset.setText("RESET");
btreset.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e) {
}
});
jLabel3.setText("Name:");
jLabel3.setBounds(new Rectangle(55, 75, 41, 17));
jTextName.setBounds(new Rectangle(100, 75, 158, 21));
// Implementation of the Exit button on the game
Exit.setBounds(new Rectangle(373, 300, 103, 30));
Exit.setToolTipText("Press button to quit!");
Exit.setText("EXIT");
Exit.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Quit_actionPerformed(e);
}
});
// Implementation of the Instructions functionality on the menu bar
jLabel5.setText("seconds");
jLabel5.setBounds(new Rectangle(416, 248, 68, 17));
jMenuInstructions.setText("Instructions");
jMenuInstructions.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jMenuInstructions_actionPerformed(e);
}
});
// To add components to the GUI
contentPane.setBackground(Color.lightGray);
contentPane.setBorder(BorderFactory.createRaisedBe velBorder());
contentPane.add(jLabel1, null);
contentPane.add(jLabel2, null);
contentPane.add(jLabel3, null);
contentPane.add(jLabel4, null);
contentPane.add(jLabel5, null);
contentPane.add(bt1, null);
contentPane.add(bt2, null);
contentPane.add(bt3, null);
contentPane.add(bt4, null);
contentPane.add(bt5, null);
contentPane.add(bt6, null);
contentPane.add(bt7, null);
contentPane.add(bt8, null);
contentPane.add(bt9, null);
contentPane.add(bt10, null);
contentPane.add(bt11, null);
contentPane.add(bt12, null);
contentPane.add(bt13, null);
contentPane.add(bt14, null);
contentPane.add(bt15, null);
contentPane.add(bt16, null);
contentPane.add(jTextField1, null);
contentPane.add(jTextName, null);
contentPane.add(jTextGameInfo, null);
contentPane.add(btTimer, null);
contentPane.add(btreset, null);
contentPane.add(Exit, null);
this.setJMenuBar(jMenuBar1);
jMenuBar1.add(jMenuFile);
jMenuFile.add(jMenuNewGame);
jMenuFile.add(jMenuFileExit);
jMenuBar1.add(jMenuHelp);
jMenuHelp.add(jMenuInstructions);
}
// To exit game
public void jMenuFileExit_actionPerformed(ActionEvent e)
{
System.exit(0);
}
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
jMenuFileExit_actionPerformed(null);
}
}
// To set a board to draw
void setBoard()
{
bt[0] = this.bt1;
bt[1] = this.bt2;
bt[2] = this.bt3;
bt[3] = this.bt4;
bt[4] = this.bt5;
bt[5] = this.bt6;
bt[6] = this.bt7;
bt[7] = this.bt8;
bt[8] = this.bt9;
bt[9] = this.bt10;
bt[10] = this.bt11;
bt[11] = this.bt12;
bt[12] = this.bt13;
bt[13] = this.bt14;
bt[14] = this.bt15;
bt[15] = this.bt16;
btTimer.setEnabled(true);
jTextName.setEditable(false);
jTextGameInfo.setEditable(false);
// To disable all buttons so that player has to click on start to begin
for (int i=0; i<16; i++)
bt[i].setEnabled(false);
// Create a set of picture
for (int i=0;i<8;i++)
{
no[i]=no[i+8]=i+1;
}
// Scramble the numbers
for (int i=0;i<300;i++)
{
// Select two positions randomly
int R1=r.nextInt(16);
int R2=r.nextInt(16);
// Change positions of card at every new game
int temp = no[R1];
no[R1] = no[R2];
no[R2] = temp;
}
}
// to check the status of the cards during play
void checkStatus(int intSecValue, int intSecButton)
{
if(turn == false)
{
timer.start();
this.bt1.setEnabled(false);
this.bt2.setEnabled(false);
this.bt3.setEnabled(false);
this.bt4.setEnabled(false);
this.bt5.setEnabled(false);
this.bt6.setEnabled(false);
this.bt7.setEnabled(false);
this.bt8.setEnabled(false);
this.bt9.setEnabled(false);
this.bt10.setEnabled(false);
this.bt11.setEnabled(false);
this.bt12.setEnabled(false);
this.bt13.setEnabled(false);
this.bt14.setEnabled(false);
this.bt15.setEnabled(false);
this.bt16.setEnabled(false);
if (no[numtrack] == no[intSecValue])
{
Buttons[ButtonPressed] = true;// If false the second matched card only remains on display
Buttons[intSecButton] = true; // If false first button matched clicked remains on display
// If both false, cards matched return face down
// Keep track of the number of cards left
count -=2;
if(count <=0)
{
endGame(true);
}
}
}
}
// To set images to the buttons and set the events to happen when the game is played
void bt1_actionPerformed(ActionEvent e)
{
ImageIcon img = new ImageIcon(no[0]+".gif");
this.bt1.setIcon(img);
this.bt1.setEnabled(false);
if (turn == false) // if true random two matched cards will not be displayed
{
ButtonPressed = 0;
numtrack = no[0];
numclick++;
}
turn = !turn;
this.checkStatus(no[0], 0);
this.bt1.setDisabledIcon(img); // Display the picture.
}
void bt2_actionPerformed(ActionEvent e)
{
ImageIcon img = new ImageIcon(no[1]+".gif");
this.bt2.setIcon(img);
this.bt2.setEnabled(false);
if (turn == false)
{
ButtonPressed = 1;
numtrack = no[1];
numclick++;
}
turn = !turn;
this.checkStatus(no[1], 1);
this.bt2.setDisabledIcon(img); // Display the picture.
}
void bt3_actionPerformed(ActionEvent e)
{
ImageIcon img = new ImageIcon(no[2]+".gif");
this.bt3.setIcon(img);
this.bt3.setEnabled(false);
if (turn == false)
{
ButtonPressed = 2;
numtrack = no[2];
numclick++;
}
turn = !turn;
this.checkStatus(no[2], 2);
this.bt3.setDisabledIcon(img); // Display the picture.
}
void bt4_actionPerformed(ActionEvent e)
{
ImageIcon img = new ImageIcon(no[3]+".gif");
this.bt4.setIcon(img);
this.bt4.setEnabled(false);
if (turn == false)
{
ButtonPressed = 3;
numtrack = no[3];
numclick++;
}
turn = !turn;
this.checkStatus(no[3], 3);
this.bt4.setDisabledIcon(img); // Display the picture.
}
void bt5_actionPerformed(ActionEvent e)
{
ImageIcon img = new ImageIcon(no[4]+".gif");
this.bt5.setIcon(img);
this.bt5.setEnabled(false);
if (turn == false)
{
ButtonPressed = 4;
numtrack = no[4];
numclick++;
}
turn = !turn;
this.checkStatus(no[4], 4);
this.bt5.setDisabledIcon(img); // Display the picture.
}
void bt6_actionPerformed(ActionEvent e)
{
ImageIcon img = new ImageIcon(no[5]+".gif");
this.bt6.setIcon(img);
this.bt6.setEnabled(false);
if (turn == false)
{
ButtonPressed = 5;
numtrack = no[5];
numclick++;
}
turn = !turn;
this.checkStatus(no[5], 5);
this.bt6.setDisabledIcon(img); // Display the picture.
}
void bt7_actionPerformed(ActionEvent e)
{
ImageIcon img = new ImageIcon(no[6]+".gif");
this.bt7.setIcon(img);
this.bt7.setEnabled(false);
if (turn == false)
{
ButtonPressed = 6;
numtrack = no[6];
numclick++;
}
turn = !turn;
this.checkStatus(no[6], 6);
this.bt7.setDisabledIcon(img); // Display the picture.
}
void bt8_actionPerformed(ActionEvent e)
{
ImageIcon img = new ImageIcon(no[7]+".gif");
this.bt8.setIcon(img);
this.bt8.setEnabled(false);
if (turn == false)
{
ButtonPressed = 7;
numtrack = no[7];
numclick++;
}
turn = !turn;
this.checkStatus(no[7], 7);
this.bt8.setDisabledIcon(img); // Display the picture.
}
void bt9_actionPerformed(ActionEvent e)
{
ImageIcon img = new ImageIcon(no[8]+".gif");
this.bt9.setIcon(img);
this.bt9.setEnabled(false);
if (turn == false)
{
ButtonPressed = 8;
numtrack = no[8];
numclick++;
}
turn = !turn;
this.checkStatus(no[8], 8);
this.bt9.setDisabledIcon(img); // Display the picture.
}
void bt10_actionPerformed(ActionEvent e)
{
ImageIcon img = new ImageIcon(no[9]+".gif");
this.bt10.setIcon(img);
this.bt10.setEnabled(false);
if (turn == false)
{
ButtonPressed = 9;
numtrack = no[9];
numclick++;
}
turn = !turn;
this.checkStatus(no[9], 9);
this.bt10.setDisabledIcon(img); // Display the picture.
}
void bt11_actionPerformed(ActionEvent e)
{
ImageIcon img = new ImageIcon(no[10]+".gif");
this.bt11.setIcon(img);
this.bt11.setEnabled(false);
if (turn == false)
{
ButtonPressed = 10;
numtrack = no[10];
numclick++;
}
turn = !turn;
this.checkStatus(no[10], 10);
this.bt11.setDisabledIcon(img); // Display the picture.
}
void bt12_actionPerformed(ActionEvent e)
{
ImageIcon img = new ImageIcon(no[11]+".gif");
this.bt12.setIcon(img);
this.bt12.setEnabled(false);
if (turn == false)
{
ButtonPressed = 11;
numtrack = no[11];
numclick++;
}
turn = !turn;
this.checkStatus(no[11], 11);
this.bt12.setDisabledIcon(img); // Display the picture.
}
void bt13_actionPerformed(ActionEvent e)
{
ImageIcon img = new ImageIcon(no[12]+".gif");
this.bt13.setIcon(img);
this.bt13.setEnabled(false);
if (turn == false)
{
ButtonPressed = 12;
numtrack = no[12];
numclick++;
}
turn = !turn;
this.checkStatus(no[12], 12);
this.bt13.setDisabledIcon(img); // Display the picture.
}
void bt14_actionPerformed(ActionEvent e)
{
ImageIcon img = new ImageIcon(no[13]+".gif");
this.bt14.setIcon(img);
this.bt14.setEnabled(false);
if (turn == false)
{
ButtonPressed = 13;
numtrack = no[13];
numclick++;
}
turn = !turn;
this.checkStatus(no[13], 13);
this.bt14.setDisabledIcon(img); // Display the picture.
}
void bt15_actionPerformed(ActionEvent e)
{
ImageIcon img = new ImageIcon(no[14]+".gif");
this.bt15.setIcon(img);
this.bt15.setEnabled(false);
if (turn == false)
{
ButtonPressed = 14;
numtrack = no[14];
numclick++;
}
turn = !turn;
this.checkStatus(no[14], 14);
this.bt15.setDisabledIcon(img); // Display the picture.
}
void bt16_actionPerformed(ActionEvent e)
{
ImageIcon img = new ImageIcon(no[15]+".gif");
this.bt16.setIcon(img);
this.bt16.setEnabled(false);
if (turn == false)
{
ButtonPressed = 15;
numtrack = no[15];
numclick++;
}
turn = !turn;
this.checkStatus(no[15], 15);
this.bt16.setDisabledIcon(img); // Display the picture.
}
void timer_actionPerformed(ActionEvent e)
{
// Hide all numbers
if (this.Buttons[0] == false)
{
bt1.setIcon(null);
bt1.setEnabled(true);
}
if (this.Buttons[1] == false)
{
bt2.setIcon(null);
bt2.setEnabled(true);
}
if (this.Buttons[2] == false)
{
bt3.setIcon(null);
bt3.setEnabled(true);
}
if (this.Buttons[3] == false)
{
bt4.setIcon(null);
bt4.setEnabled(true);
}
if (this.Buttons[4] == false)
{
bt5.setIcon(null);
bt5.setEnabled(true);
}
if (this.Buttons[5] == false)
{
bt6.setIcon(null);
bt6.setEnabled(true);
}
if (this.Buttons[6] == false)
{
bt7.setIcon(null);
bt7.setEnabled(true);
}
if (this.Buttons[7] == false)
{
bt8.setIcon(null);
bt8.setEnabled(true);
}
if (this.Buttons[8] == false)
{
bt9.setIcon(null);
bt9.setEnabled(true);
}
if (this.Buttons[9] == false)
{
bt10.setIcon(null);
bt10.setEnabled(true);
}
if (this.Buttons[10] == false)
{
bt11.setIcon(null);
bt11.setEnabled(true);
}
if (this.Buttons[11] == false)
{
bt12.setIcon(null);
bt12.setEnabled(true);
}
if (this.Buttons[12] == false)
{
bt13.setIcon(null);
bt13.setEnabled(true);
}
if (this.Buttons[13] == false)
{
bt14.setIcon(null);
bt14.setEnabled(true);
}
if (this.Buttons[14] == false)
{
bt15.setIcon(null);
bt15.setEnabled(true);
}
if (this.Buttons[15] == false)
{
bt16.setIcon(null);
bt16.setEnabled(true);
}
ButtonPressed= -1;
numtrack = 0;
}
// Ending the game
public void endGame(boolean win)
{
if(win)
{
timerGame.stop();
jTextGameInfo.setText("Well Done \n"+jTextName.getText()+" have completed the game in "+(timeStart-timeLeft)+"seconds");
JOptionPane.showMessageDialog(this,"Well Done " + jTextName.getText()+"!\nYou win in "+numclick+" number of Clicks \nYou completed the game in "+(timeStart-timeLeft)+"seconds","Game Over", JOptionPane.NO_OPTION);
btTimer.setEnabled(false);
}
else
{
jTextGameInfo.setText("Sorry! You lost in "+numclick+" number of Clicks");
JOptionPane.showMessageDialog(this,"Time is up "+jTextName.getText()+"! \nSorry! You lost in "+numclick+" number of Clicks", "Game Over",JOptionPane.CANCEL_OPTION);
btTimer.setEnabled(false);
}
}
void timerGame_actionPerformed(ActionEvent e)
{
timeLeft--;
if (timeLeft==-1)
timerGame.stop();
else
this.jTextField1.setText(timeLeft+"");
if(timeLeft==0)
{
endGame(false);
for (int i=0; i<16; i++)
bt[i].setEnabled(false);
}
}
// Start game
void btTimer_actionPerformed(ActionEvent e)
{
timeStart = Integer.parseInt(this.jTextField1.getText());
for (int i=0; i<16; i++)
bt[i].setEnabled(true);
jTextField1.setEnabled(false);
name= JOptionPane.showInputDialog(null,"Please Enter Your Name:", "Welcome to the Memory Game!", JOptionPane.CANCEL_OPTION);
jTextName.setText(name);
timeLeft = Integer.parseInt(this.jTextField1.getText());
timerGame.start();
btTimer.setEnabled(false);
}
// Instruction button
void jMenuInstructions_actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(this,"Instructions \n- Each card in the memory game hides a picture. \n- Click on the cards to reveal a picture and remember their positions to match all pairs within the time given.","How to Play",JOptionPane.PLAIN_MESSAGE);
}
// New game functionality
public void jMenuNewGame_actionPerformed(ActionEvent e)
{
/*
timerGame.start();
timeStart = 120;
numclick = 0;
count=bt.length;
for (int i = 0; i < 100; i++)
{
//randomly pick two position
int p1 = r.nextInt(16);
int p2 = r.nextInt(16);
//swap the two position
int temp = no[p1];
no[p1] = no[p2];
no[p2] = temp;
}
new JavaMemory();
for (int i=0; i<=16;i++)
{
bt[i].setIcon(null);
bt[i].setEnabled(true);
}
timeLeft = 120;
timerGame.start();*/
}
// To exit game
void Quit_actionPerformed(ActionEvent e)
{
jTextName.setText("");
jTextGameInfo.setText("");
JOptionPane.showMessageDialog(this," Press OK to leave this game!","Quit Game",JOptionPane.CANCEL_OPTION);
System.exit(0);
}
// This part of code is the main method to execute the program
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JavaMemory().setVisible(true);
}
});
}
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
- 04-13-2009, 09:08 PM #2
when showing large codes, please use code tags to make it readable.
[code ]
//code here
[/ code]USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 04-13-2009, 09:15 PM #3
Post any error messages that you get when you compile it or tell us what output you're expecting and aren't getting.
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 04-13-2009, 09:22 PM #4
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
i'm sure netbeans has a built in debugger. you should learn how to use it so see how your program gets stepped through. it really saves you a lot of time from having to guess where something is going wrong.
- 04-13-2009, 09:23 PM #5
i believe its a logic bug from the OP, but not too sure what you mean as well.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 04-13-2009, 09:39 PM #6
Member
- Join Date
- Apr 2009
- Posts
- 2
- Rep Power
- 0
Hi again!
thank u for replying!:)
I am not getting any error messages. the game is starting fine, timer is working but during play, at random two cards which do not have the same pictures are being shown as a match when they shouldnt. everytime a new game is started, another two odd pictures are matched.
-
You're using arrays, and that's good, but not to best advantage as you've got a lot of redundancy. By using arrays better, and using the layout managers to your advantage, you could eliminate more than 70% of your code. For instance
Java Code:import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; public class FuSwing3 { // constants private static final int ROWS = 4; private static final int COLS = 6; private static final Dimension BUTTON_SIZE = new Dimension(120, 60); // JPanel that holds all of the buttons private JPanel mainPanel = new JPanel(); // JButton 2-dimensional array private JButton[][] buttons = new JButton[ROWS][COLS]; public FuSwing3() { int gap = 4; // put all of the buttons in a JPanel that uses a GridLayout // that will automatically place the buttons where I want them // to be. mainPanel.setLayout(new GridLayout(ROWS, COLS, gap, gap)); ActionListener buttonListener = new ButtonListener(); for (int row = 0; row < buttons.length; row++) { for (int col = 0; col < buttons[row].length; col++) { // create each button in a nested for loop buttons[row][col] = new JButton(); // since we're using layout managers, we want to set the // preferredSize, not the size. buttons[row][col].setPreferredSize(BUTTON_SIZE); // use one actionlistener for [b]all[/b] of the buttons buttons[row][col].addActionListener(buttonListener); mainPanel.add(buttons[row][col]); } } } public JComponent getPanel() { return mainPanel; } // the actionlister shared by all buttons private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { //TODO: fix this JButton selectedBtn = (JButton) e.getSource(); int selectedRow = -1; int selectedCol = -1; outOfLoop: for (int row = 0; row < buttons.length; row++) { for (int col = 0; col < buttons[row].length; col++) { System.out.println("" + row + ", " + col); if (selectedBtn == buttons[row][col]) { selectedRow = row; selectedCol = col; break outOfLoop; } } } System.out.println("row = " + selectedRow + ", and col = " + selectedCol); } } private static void createAndShowGUI() { JFrame frame = new JFrame("FuSwing3 Application"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new FuSwing3().getPanel()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }Last edited by Fubarable; 04-13-2009 at 11:58 PM.
Similar Threads
-
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
2D strategy game or 2D war game
By led1433 in forum Java 2DReplies: 5Last Post: 02-10-2009, 06:00 AM -
how do I increase memory allocated to code cache (Non Heap Memory)
By manibhat in forum Advanced JavaReplies: 2Last Post: 08-21-2008, 07:33 PM -
Out of memory
By mew in forum New To JavaReplies: 1Last Post: 01-20-2008, 08:55 AM -
Memory
By mew in forum CLDC and MIDPReplies: 0Last Post: 12-28-2007, 11:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks