wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Memory game giving me gray hairs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Memory game giving me gray hairs" Watch "Memory game giving me gray hairs" New topic
Author

Memory game giving me gray hairs

Marcel Goulart
Greenhorn

Joined: Oct 14, 2012
Posts: 6
This project was due yesterday but I cant get past how hard this is to do especially in the time frame Ive given myself.
The last part of this frustrating game I need to finish is comparing two of the cards and then if they are not matching flipping back over.
If they do match it turns the text to blue.

Thanks guys Sorry for the mess.

Think I just saw another gray hair fall out.


Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
too many errors in there to try and commpile to see what's going on,
so this is basically the steps I've used in the past (hopefully haven't missed any)

setup for a 4 x 4 grid of buttons
(JPanel(gridlayout(4,4)) and frame you should be familiar with)
a) button array[16]
b) imageIcon array[16]
c) class variable to hold index number of 'open' button (1st button of pair attempt)
int buttonShowingIndex = -1;//initial value should be -1
d) create an actionListener (it does all the work), can be an inner class/separate/whatever

for each button (in a loop):
a) create the button
b) set it's name property to the index of the loop
c) give it a preferredSize (optional)
d) add the listener
e) add it to the panel

for the imageIcons:
a) create the 1st 8 only (indexes 0 to 7)
b) set the next 8 linked to the 1st 8 (icon[8] = icon[0], icon[9] = icon[1] etc)
c) imageicon array gets shuffled in a reset() method which is called when game starts or for 'new game'

the actionListener's job:
a) get the source button (srcButton)
b) get it's index number by parsing srcButton.getName() (int srcBtnIndex)
c) set the corresponding imageIcon from the imageIcon array
d) now the nitty gritty:
check if buttonShowingIndex > -1
if false (this will be in the 'else' part) buttonShowingIndex = srcBtnIndex
as this will be the 1st button of a pair attempt
if true:
i) buttonShowingIndex will have the index number of the 1st 'open' button
ii) compare the icons of button[buttonShowingIndex] and button[srcBtnIndex]
and if equal, either do nothing, or remove the listener from each button (the icons will stay)
if not equal, start a swing timer (no repeats) to set both button's icons to null, and to reset
buttonShowingIndex to -1

the reset method:
java.util.Collections.shuffle(java.util.Arrays.asList(icons));//shuffles the array
for(int x = 0, y = buttons.length; x < y; x++) buttons[x].setIcon(null);//sets them all blank again
Marcel Goulart
Greenhorn

Joined: Oct 14, 2012
Posts: 6
The errors are coming from the button listener. It is where I was toying around with the if else statements in order to get the matching part to actually work.
Thank you for the step by step. We havent gone over collections yet and this is part of my first assignment dealing with guis.
 
wood burning stoves
 
subject: Memory game giving me gray hairs