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.
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
b) imageIcon array
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 = icon, icon = icon 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
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
Joined: Oct 14, 2012
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.