The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
December 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Java help

Posted by Mike wankel on December 09, 2000 at 4:20 AM

Hi can you help me fix this game? I would like to be able to keep score, +10 for right guess and -5 for wrong guess and keep track of the top 5 scores. Also I would like to be able to restart the game without exiting first and recompiling it. Last I would like to add images to the buttons. I have to finish this before Sunday 12-10-200. Thank you for any help you can give.

//Title: Memory
//Version:
//Copyright: Copyright (c) 1999
//Author: Mike Wankel
//Company:
//Description: Game of Concentration
package Final;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Memory extends JFrame implements ActionListener {
//instance variables:
private Container c;
private JPanel gameGrid;
private JButton[] gameButton;
private int randomNumber1, randomNumber2, firstButton, secondButton;
private int tempValue;
private int arraySize = 51;
private int value[];
private boolean pickOne = true;

public Memory(){

super ("Memory Game"); // title

c = getContentPane(); //set up Content pane

gameGrid = new JPanel(); //set up Panel
gameGrid.setLayout(new GridLayout(5,5));
gameButton = new JButton[arraySize];

value = new int[arraySize];
value[1] = 1;
value[2] = 1;
value[3] = 2;
value[4] = 2;
value[5] = 3;
value[6] = 3;
value[7] = 4;
value[8] = 4;
value[9] = 5;
value[10] = 5;
value[11] = 6;
value[12] = 6;
value[13] = 7;
value[14] = 7;
value[15] = 8;
value[16] = 8;
value[17] = 9;
value[18] = 9;
value[19] = 10;
value[20] = 10;
value[21] = 11;
value[22] = 11;
value[23] = 12;
value[24] = 12;
value[25] = 13;
value[26] = 13;
value[27] = 14;
value[28] = 14;
value[29] = 15;
value[30] = 15;
value[31] = 16;
value[32] = 16;
value[33] = 17;
value[34] = 17;
value[35] = 18;
value[36] = 18;
value[37] = 19;
value[38] = 19;
value[39] = 20;
value[40] = 20;
value[41] = 21;
value[42] = 21;
value[43] = 22;
value[44] = 22;
value[45] = 23;
value[46] = 23;
value[47] = 24;
value[48] = 24;
value[49] = 25;
value[50] = 25;

//*************************************shuffle routine*************************************
for (int i = 1; i < 2000; i++) {
randomNumber1 = (1 + (int)(Math.random() * 50) ); //get random number
randomNumber2 = (1 + (int)(Math.random() * 50) ); //get second random number
if (randomNumber1 != randomNumber2) { //if not equal the first one
tempValue = value[randomNumber1]; // put #1 into temp
value[randomNumber1] = value[randomNumber2]; // put #2 into #1 space
value[randomNumber2] = tempValue; // put temp into #2 space
}
}
//*****************************************************************************************


for(int i = 1; i < gameButton.length; i++) { //run through x times
gameButton[i] = new JButton(); //make each new button
gameButton[i].addActionListener(this); //add listener to each
gameButton[i].setBackground(Color.cyan); // put color in background
gameGrid.add(gameButton[i]); //add button to panel
}

c.add(gameGrid, BorderLayout.CENTER); //add panel to container

setSize(700, 500); // size screen
show(); // display to the screen

}//end constructor
//*******************************actionPerformed**************************
public void actionPerformed(ActionEvent e){
if (pickOne == true){ // first pick
for(int i = 1; i < gameButton.length; i++) { // what button is it
if (gameButton[i] == e.getSource()) { //
gameButton[i].setText("" + value[i]); // display what on button
firstButton = i;
pickOne = false; // ready for second pick
}//end if
}//next

}
else if (pickOne == false) { // make second pick
for(int t = 1; t < gameButton.length; t++) {
if (gameButton[t] == e.getSource()) { // is it first button again
if (gameButton[firstButton] != e.getSource()) {
gameButton[t].setText("" + value[t]);
secondButton = t;
pickOne = true;
}

if (value[firstButton] == value[secondButton]) { // value match then vis fale
gameButton[firstButton].setVisible(false);
gameButton[secondButton].setVisible(false);
}
else {
gameButton[firstButton].setText(" "); // turn back over
gameButton[secondButton].setText(" "); // turn back over
}
}
}//next

}

}//end actionPerformed
//*******************************main***********************************
public static void main(String args[]) {
Memory app = new Memory();
app.addWindowListener(
new WindowAdapter() {
public void WindowClosing ( WindowEvent e )
{
System.exit(0);
}
}
);
} //end main
}//end class Memory




Replies:
  • button scooter June 12, 2001 at 9:52 PM (0)

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us