aspose file tools*
The moose likes Beginning Java and the fly likes Java Memory Game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Memory Game" Watch "Java Memory Game" New topic
Author

Java Memory Game

Ernie Velazquez
Greenhorn

Joined: Apr 25, 2010
Posts: 6
So i'm supposed to write a 'simple' memory game that lays 16 cards, upside down, and then prompts the user for a coordinate of two cards to see if they match. If they match, they stay face up, if not, they're face down and the user has to try again. I am also to write a function that shuffles the cards by selecting two cards at random and swapping them. This is the second problem, so I'm assuming it should be somewhat straight forward, but I've managed to turn it into a huge ordeal.

Here is the code that I have so far, but i'm hesitant to continue as I get the feeling i'm straying WAY beyond the beaten path. My function to shuffle works, but I'm guessing this shouldn't be as involved as I managed to make it. Any guidance would be much appreciated!

Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
With big pleasure but first could you please UseCodeTags ? It's really painful to read the lines of your code


Bye,
Nicola
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Since you are new, I have added code tags. Your code looks very complicated. I automatically get suspicious if I see anything that complicated.

Why are you declaring everything static?
Why have you got that switch block when you can access the indices of the array with the % and / operators?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'd have to agree--on cursory examination I am baffled by InitializeCards. I'm looking at it now and will follow up in a bit (yeah, I know, but it's been a stressful few months, and it's something to do :)

By the way, Java naming conventions suggest that method names begin with a lower-case letter. It's not a huge deal, but it makes things easier for people reading your code.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Upon closer examination of InitializeCards, if I were you, I'd start over--I'm not even sure what the method is *trying* to do :) (But I do note that it works, which is worth something ;)

One thing that would be helpful information for folks trying to help you is what a "card" looks like: is it just a number from 1-8? So the goal of InitializeCards would be to put a pair of each number 1-8 in the 4x4 grid? So you need: a list of number pairs, to shuffle that collection, and then to loop over the collection (or each grid location) and put a shuffled number into each grid location.

Just for comparison (and line count is *not* a useful metric... in general... here it kind of is, perhaps--a 90% code size reduction usually means something has gone wrong ;) I'm achieving the same results, somewhat naively, in maybe 20 lines. This usually means you should challenge your assumptions a bit--and my previous paragraph contains a few hints that might help you along your way.

My version, while still having everything static (one thing at a time :) has only two static vars (hidden and layout): the rest are superfluous. I added two constants for the number of rows and columns, and switched to characters to allow for cards > 9 to fit in a single character. I don't check for the even-ness of the grid or for needing more than 26 card pairs.)
Ernie Velazquez
Greenhorn

Joined: Apr 25, 2010
Posts: 6
Campbell Ritchie wrote:Since you are new, I have added code tags. Your code looks very complicated. I automatically get suspicious if I see anything that complicated.

Why are you declaring everything static?
Why have you got that switch block when you can access the indices of the array with the % and / operators?


Our professor hasn't covered access of indices by using % and / operators. How does that work?
Ernie Velazquez
Greenhorn

Joined: Apr 25, 2010
Posts: 6
David Newton wrote:Upon closer examination of InitializeCards, if I were you, I'd start over--I'm not even sure what the method is *trying* to do :) (But I do note that it works, which is worth something ;)

One thing that would be helpful information for folks trying to help you is what a "card" looks like: is it just a number from 1-8? So the goal of InitializeCards would be to put a pair of each number 1-8 in the 4x4 grid? So you need: a list of number pairs, to shuffle that collection, and then to loop over the collection (or each grid location) and put a shuffled number into each grid location.

Just for comparison (and line count is *not* a useful metric... in general... here it kind of is, perhaps--a 90% code size reduction usually means something has gone wrong ;) I'm achieving the same results, somewhat naively, in maybe 20 lines. This usually means you should challenge your assumptions a bit--and my previous paragraph contains a few hints that might help you along your way.

My version, while still having everything static (one thing at a time :) has only two static vars (hidden and layout): the rest are superfluous. I added two constants for the number of rows and columns, and switched to characters to allow for cards > 9 to fit in a single character. I don't check for the even-ness of the grid or for needing more than 26 card pairs.)


Yes, the cards were from 1-8. I over complicated the task of filling the initial array of random cards. I was trying to randomly initialize two random cell with the same random variable, but it turns out its much easier to initialize the array with the pairs from 1-8, and then create a function to shuffle them. Talk about making a mountain out of a mole hill! Thank you everyone for the guidance!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You might want to look at the Collections class, btw.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Memory Game