Sign in to follow this  

Java Logical Problems - Memory Game

This topic is 3299 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I just found these forums surfing the web! My name is Zepang , nice to meet you all! 2 weeks ago I started a course in java programming. It's my first programming language so please bear with me, the structure of the code ain't pretty. This is also my first applet. I'm trying to make a memory game. I have no problems working with the look of the GUI and I'm able to put the cards in random order.. When you click two JToggleButtons with the same SelectedIcon I want them to be disabled. If the two JToggleButtons doesn't have the same selected icon I want them to be setSelected(false), in other words flip back. When you click two cards now nothing happens. I would be very grateful if anyone could help me track this logical error! If you have any other java / programming advice I would be very grateful! Side notes : the images for the selectedIcon are stored in the same directory and have the filenames mem0.jpg to mem14.jpg and they are properly loaded into the applet. Same goes for the back.jpg and disabled.jpg. EDIT : All images are 90x90pixels. EDIT : It should be mentioned that this compiles without errors. Thanks in advance! Cheers! EDIT: The listener is implemented at the bottom of the code, scroll down. EDIT: Problem solved = Code Removed. [Edited by - Zepang on December 5, 2008 7:44:28 AM]

Share this post


Link to post
Share on other sites
You are creating a Lyss instance in Memorii's init function, but you're also creating a Memorii instance in Lyss. I don't think that is what you want to do. The Memorii instance in Lyss is a different instance as the Memorii instance that holds the Lyss instance.

You're also creating 30 buttons in both Memorii and Lyss... that is, every instance of these types will have 30 buttons of it's own. Is that what you want?


Now, I'm not very familiar with Java, but it looks like you've got the wrong idea about how classes work. Can you explain what you want your listener to do, e.g. how you intended it to work?

Share this post


Link to post
Share on other sites
Thanks for answering Captain P!

I took what you wrote into consideration and removed the second instance of the memorii and the second array of 30 buttons. I've also added more comments to the code which hopefully will clearify my reasoning.

When the a button is clicked I want a reference to that buttons selected image to be stored. When a second button is clicked I want it's selected Image to be compared to the first's.

This should work since there's pairs of selectedImages generated at the begining of the application.

At the momment nothing happens when I click to cards. It's as if the listener didn't exist.

Thanks for your time!

// Zepang


EDIT: Problem solved = Code Removed.

[Edited by - Zepang on December 5, 2008 7:44:07 AM]

Share this post


Link to post
Share on other sites
Have you tried checking whether or not the actionPerformed function is reached? Perhaps you're setting up your listener incorrectly, but it may also be that your checks inside the actionPerformed function are flawed.

I do see one thing you're doing wrong though: you're not checking the last button. Remove the -1 from the for loop in actionPerformed: the less-than operator makes sure that you're not going out of boundaries there already.


On a side-note, the code you've posted contains a lot of extra empty lines. Makes it pretty hard to read. I assume something went wrong while copying the source around?

Share this post


Link to post
Share on other sites
Problem solved!

Thanks alot for pointing me in the right direction Captain P!

I started to check if the event was triggered. I added some output to the standard stream in the begining of the Actionperfomed clause.
I noticed that a nullException was thrown everytime I clicked a button.
Googled it and solved the problem.

I'm close to completing my first game!

Thanks a lot Captain P! You rock! Rate up!



Share this post


Link to post
Share on other sites

This topic is 3299 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this