package memories;
/**
*
* @author Raf
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Memories extends JFrame implements ActionListener {
private JButton exitbut, startbut, helpbut, pausebut, mybutton[];
private JPanel lowpanel,highpanel, mainpanel;
private JLabel timel, missedl, dtime;
private JTextField timef, missedf;
private JComboBox timeCOMBO;
private GridLayout grid;
private ImageIcon myicons[],intro;
private int TimeCount = 0,MissedCount = 0,first = 0,placer,pos,selected = 0;
private int ranpos,rannum,takenAR[],doubleAR[],picAR[],match = 0;
private String iconbasename="myIcon1", iconsuffix=".gif";
private String[] myCombo = { "1/4 second","1/2 second","1 second","2 seconds" };
private Timer increment, flipdelay;
private PicHandler picH = new PicHandler();
// set up GUI
public Memories()
{
super( "Program 2 - Memory Game" );
// get content pane and set its layout
Container container = getContentPane();
// panels:
lowpanel = new JPanel();
lowpanel.setLayout(new FlowLayout());
lowpanel.setBorder(BorderFactory.createRaisedBevelBorder());
highpanel = new JPanel();
highpanel.setLayout(new FlowLayout());
highpanel.setBorder(BorderFactory.createTitledBorder("Menu"));
ButtonHandler handler = new ButtonHandler();
// top panels buttons:
startbut = new JButton("Start Game");
startbut.addActionListener(handler);
helpbut = new JButton("Instructions");
helpbut.addActionListener(handler);
pausebut = new JButton("Pause");
pausebut.addActionListener(handler);
exitbut = new JButton("Quit");
exitbut.addActionListener(handler);
dtime = new JLabel("Delay Time:");
final JComboBox timeCOMBO;
timeCOMBO = new JComboBox(myCombo);
timeCOMBO.setSelectedIndex(2);
timeCOMBO.addActionListener(new ActionListener() { // changing time for cards to flip
public void actionPerformed(ActionEvent e) {
String newMode = (String)timeCOMBO.getSelectedItem();
switch (newMode) {
case "1/4 second":
flipdelay = new Timer(250,picH);
break;
case "1/2 second":
flipdelay = new Timer(500,picH);
break;
case "1 second":
flipdelay = new Timer(1000,picH);
break;
default:
flipdelay = new Timer(2000,picH);
break;
}
}
});
//bottom panel items:
timel = new JLabel("Time:");
timef = new JTextField(6);
timef.setEditable(false);
missedl = new JLabel("Missed:");
missedf = new JTextField(3);
missedf.setEditable(false);
// adding things to top panel:
highpanel.add(startbut);
highpanel.add(helpbut);
highpanel.add(pausebut);
highpanel.add(exitbut);
highpanel.add(dtime);
highpanel.add(timeCOMBO);
// adding things to bottom panel:
lowpanel.add(timel);
lowpanel.add(timef);
lowpanel.add(missedl);
lowpanel.add(missedf);
//setting up middle
myicons = new ImageIcon[12];
intro = new ImageIcon("myIcon1.gif");
for (int i=0; i < (myicons.length); i++) {
myicons = new ImageIcon ( iconbasename + Integer.toString(i+1) + iconsuffix );
}
takenAR = new int [myicons.length*2]; //array that will tell when picture is already in spot
mybutton = new JButton[myicons.length*2];//Allocate Labels for the pictures to go in
picAR = new int [myicons.length*2]; //array that will randomize the pictures
doubleAR = new int [myicons.length]; //array that tell if pictures has been used twice
grid = new GridLayout(4,6); //make a grid pattern in middle
mainpanel = new JPanel();
mainpanel.setLayout(grid);
for (int i=0; i < myicons.length*2; i++) { //Fill Labels with Pictures
mybutton = new JButton( intro );
mybutton.setToolTipText( "Pic: " + (i+1) );
mybutton.addActionListener(picH);
mainpanel.add( mybutton );
}
// adding the top and bottom panels:
container.add(lowpanel, BorderLayout.SOUTH);
container.add(highpanel, BorderLayout.NORTH);
container.add(mainpanel, BorderLayout.CENTER);
// Set Timer
increment = new Timer(1000, this);
//Set default
timef.setText(Integer.toString(TimeCount));
missedf.setText(Integer.toString(MissedCount));
setSize(700, 580);
setVisible( true );
}
// execute application
public static void main( String args[] )
{
Memories application = new Memories();
application.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
}
//************************************************************
// This class handles timer events
//
public void actionPerformed (ActionEvent e) {
if (e.getSource() == increment) {
TimeCount++;
timef.setText(Integer.toString(TimeCount)); //increment timer on display
}
}
//************************************************************
// This class will handle all events when a menu button is clicked
//
private class ButtonHandler implements ActionListener {
private int ampaused = 0;
@Override
public void actionPerformed( ActionEvent e ) {
// start game button:
if(e.getSource() == startbut) {
randompics(); //randomize array
if (first == 0) {
startbut.setText("Restart");
}
else {
for (int i=0; i < myicons.length*2; i++) {
mybutton.setIcon( intro );
}
increment.stop(); //reset variables
TimeCount = MissedCount = match = selected = 0;
missedf.setText(Integer.toString(MissedCount));
}
first++;
increment.start();
}
// instructions button:
else if(e.getSource() == helpbut) {
increment.stop(); //pause game
JOptionPane.showMessageDialog(null,
"Instructions: \n\n* Click the Start Game Button to Launch the Game." +
"\n* Any time after the initial start, press the Restart button to reset." +
"\n* Click any picture and try to find it's match." +
"\n* Any wrong match will increment the missed match counter." +
"\n* For each wrong match a second will be added to your total score." +
"\n* You can change the time the cards stay visible by clicking 'Change Delay Time'." +
"\n* The Lower your score, the better you are!" +
"\n* The game ends after all pictures have been matched." +
"\n* Thanks for Playing and Have Fun!","Welcome To The Memory Game",1);
if ((first != 0) && (!"Play".equals(pausebut.getText())) && (match != 12)) {
increment.start();
} //restart game if needed
}
//pause button:
else if(e.getSource() == pausebut) { //pause and resume game
if ((increment.isRunning()) && (ampaused == 0)) {
pausebut.setText("Play");
increment.stop();
ampaused = 1;
}
else if (ampaused != 0){
pausebut.setText("Pause");
increment.start();
ampaused =0;
}
}
// exit button:
else if(e.getSource() == exitbut) { //exit the game
increment.stop();
JOptionPane.showMessageDialog(null, "Thank You!\nCome Again!!");
System.exit(0);
}
}
} // end ButtonHandler
//************************************************************
// This class will handle all events when a picture is clicked
//
private class PicHandler implements ActionListener {
private int hold1, hold2, hold3, hold4, oldselection,j;
public PicHandler()
{
flipdelay = new Timer(1000,this);
}
@Override
public void actionPerformed( ActionEvent e ) {
if (increment.isRunning()) { //if game has started
if (e.getSource() == flipdelay) { //Allow user to see wrong answer be4 flipping
mybutton[hold4].setIcon(intro); // reset pics to starting pic
mybutton[hold3].setIcon(intro);
flipdelay.stop();
}
else if(!flipdelay.isRunning()) { //pics not flipped for user to select more
for (j = 0; j < (myicons.length*2);j++) { //find which icon had an action
if ((e.getSource() == mybutton[j]) && (takenAR[j] != 7) ) { //if button that was pressed
//and not matched
if (selected < 1) { //if first pic user selected
selected++;
hold1 = oldselection = picAR[j]; // holding old pics and flipping picture
hold3 = j;
mybutton[j].setIcon(myicons[hold1]);
}
else { //if second pic user selected
if (!(j == hold3)) { //if user doesn't select same pic twice
hold2 = picAR[j];
if (hold2 == oldselection) { //if pics matched
match++;
takenAR[hold3] = 7; //set pics as matched and flip
mybutton[j].setIcon(myicons[hold2]);
takenAR[j] = 7;
if (match == 12) { // if found all picture matches
increment.stop();
JOptionPane.showMessageDialog(null,"You Won!\nTime: " +
TimeCount + " seconds\nMisses: " + MissedCount +
"\nTotal Score: " + (TimeCount + MissedCount) +
"\n\nPress Restart to play again or Quit to leave!");
}
}
else {
mybutton[j].setIcon(myicons[hold2]);
hold4 = j;
flipdelay.start();
MissedCount++;
missedf.setText(Integer.toString(MissedCount));
}
selected = 0;
}
}
j = myicons.length*2; //exit loop
}
}
} //end else - delay hit
}
}
}
private void randompics() {
for (int i=0; i < myicons.length*2; i++) {
takenAR = 0;
}
for (int i=0; i < (myicons.length); i++) {
doubleAR = 0;
}
for (int i=0; i < myicons.length*2; i++) {
placer = getran();
pos = getpos();
picAR[pos] = placer;
takenAR[pos] = 1;
doubleAR[placer]++;
}
}
private int getran () {
rannum = (int)Math.round((Math.random())*(myicons.length-1));
if (doubleAR[rannum] == 2) {
getran();
}
return rannum;
}
private int getpos () {
ranpos = (int)Math.round((Math.random())*((myicons.length*2)-1));
if (takenAR[ranpos] == 1) {
getpos();
}
return ranpos;
}
}