File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help with array problem in matching game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with array problem in matching game" Watch "Help with array problem in matching game" New topic
Author

Help with array problem in matching game

Ryan Callen
Greenhorn

Joined: Nov 29, 2012
Posts: 22
I have designed a matching game using a 2 dimensional int array (4x4). The coordinates on the array are populated with n numbers 1-8, each appearing twice. The numbers are then randomized. My problem is that I do not want the array to be populated with numbers, but rather letters a-h. However, I cannot seem to find a way to get this to happen without screwing up the int array i have made. Any suggestions? Source code...


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Welcome to the Ranch

I have added code tags, and you can see how much better the code looks . I also removed some unnecessary blank lines, because they don’t make the code easier to read.
Why have you only got one class? Why are all its members static?
You can probably copy all those numbers to another array. [By the way, there ain’t no such thing as a 2D array in Java. What you appear to have is an array of arrays, which does exist. It is different. Better than a 2D array.] As I said, copy all those numbers to another array, but in the meantime do some arithmetic with them. Did you know you can do arithmetic with chars? They are not letters, but numbers. So you can add 'a' to 1. Since 1 is an int, the result comes out as an int, so you have to cast it back. Because cast has a higher precedence than +, you need parentheses.
(char)('a' + i)
Then you can put those chars into a new arrayOfArrays (char[][]). Then you can find out how I have misled you slightly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with array problem in matching game
 
Similar Threads
Operator precedence and associativity confusion........
Main class not recognized
How to display Text String at the bottom of JLabel??
How to group these JLabels?
performance issue in iterating through large list of result set