• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Memory game giving me gray hairs

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic