Results 1 to 9 of 9
Thread: 2D Array Random Shuffle
- 11-23-2008, 12:57 AM #1
Member
- Join Date
- Feb 2008
- Posts
- 12
- Rep Power
- 0
2D Array Random Shuffle
Hey,
I would like to ask for some help concerning a game I am currently developing. Im pritty much there with all of the functionality however one thing is bothering me... I need to code a function that will randomly shuffle a 2 dimentional array of pre existing objects(classes). I can do a 1D array fine .. but I dont know where to start for a 2D, could anyone offer me any help on this?
The objects are drawn to an applet therefore they do indeed contain x, y possition information.
Thank you in advance for any assistance you can offer.
- 11-23-2008, 01:01 AM #2
Member
- Join Date
- Feb 2008
- Posts
- 12
- Rep Power
- 0
oh lets say for agruments sake that the array will only ever be:
int arraySize = >whatever integer<
Array[][] myArray = new Array[arraySize][arraySize]
square array....
and the fuction contained in:
public void Shuffle()
{
first for loop over rows
second (nested) for loop over columns
int r = (int)(Math.random() * arraySize) ??
<< array sorting code >>
}
sorry for not including that info.Last edited by Nuluvius; 11-23-2008 at 01:07 AM.
-
why not change it to a 1-D array or list, shuffle it, then lay it out in 2-D after the shuffle?
- 11-23-2008, 01:17 AM #4
Member
- Join Date
- Feb 2008
- Posts
- 12
- Rep Power
- 0
Thank you for your quick answer, I can see that changing it to a 1D array then back would solve it nicely. However I am not sure how to go about doing that.. unfortunatly... would I be able to get a hint?
Thanks again for your assistance.
-
Myself, I'd keep the data in an ArrayList. Then only when I needed to display it or manipulate it in an array, I'd use two nested for loops and place the data into each array slot. There's really not much to it.
For instance, say I want a 2-D array of objects of a type called "MyType":
Java Code:class MyType { private int value; public MyType(int value) { this.value = value; } public int getValue() { return value; } @Override public String toString() { return String.valueOf(value); } }
Java Code:import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Fubar { private static final int ARRAY_SIZE = 5; private MyType[][] myArray = new MyType[ARRAY_SIZE][ARRAY_SIZE]; private List<MyType> myList = new ArrayList<MyType>(); public Fubar() { init(); } public void init() { // add ARRAY_SIZE squared elements into the list: for (int i = 0; i < ARRAY_SIZE*ARRAY_SIZE; i++) { myList.add(new MyType(i)); } // initialize the array with unsorted data for (int i = 0; i < ARRAY_SIZE; i++) { for (int j = 0; j < ARRAY_SIZE; j++) { myArray[i][j] = myList.get(j + i * ARRAY_SIZE); } } } // Call whenever I want to shuffle the list and reinsert into array public void shuffle() { Collections.shuffle(myList); for (int i = 0; i < ARRAY_SIZE; i++) { for (int j = 0; j < ARRAY_SIZE; j++) { myArray[i][j] = myList.get(j + i * ARRAY_SIZE); } } } public String toString() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < myArray.length; i++) { for (int j = 0; j < myArray[i].length; j++) { sb.append(String.format("%4s", myArray[i][j])); } sb.append("\n"); } return sb.toString(); } public static void main(String[] args) { Fubar foo = new Fubar(); System.out.println(foo); foo.shuffle(); System.out.println(foo); foo.shuffle(); System.out.println(foo); } }
- 11-23-2008, 01:22 PM #6
Member
- Join Date
- Feb 2008
- Posts
- 12
- Rep Power
- 0
Ah yes, that is indeed quite easy to implement. Thank you for your help, I really apprechiate it. I should have absolutly no problems solving it now!!
- 11-23-2008, 04:36 PM #7
boolean shuffle(List l){}
See java.lang.UnsupportedOperationException
Java Code:try { Collections.shuffle(myList); } catch(UnsupportedOperationException) { return false; }
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 11-23-2008, 06:06 PM #8
Member
- Join Date
- Feb 2008
- Posts
- 12
- Rep Power
- 0
Nice, thank you also - very usefull.
- 11-23-2008, 06:15 PM #9
Okay, next thing is to use SecureRandom as the random source. In general, we may need to do shuffle as a preventive measure when using quicksort on degenerate cases - the array is already sorted usually - and going to SecureRandom early on prevents coding blunders later....the default implementation of shuffle provided by Collections is sufficient to prevent quicksort from hanging - that's what it's for.
newbies, oh the fun!Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Similar Threads
-
Array of instances using Math.random()
By xgi1008 in forum New To JavaReplies: 16Last Post: 01-25-2011, 11:10 PM -
How to get a random value in an array
By Franneldort in forum New To JavaReplies: 21Last Post: 11-01-2008, 02:42 PM -
How do I shuffle an arraylist?
By frasifrasi in forum New To JavaReplies: 2Last Post: 07-16-2008, 11:29 PM -
How to Shuffle a particular String
By Java Tip in forum java.langReplies: 0Last Post: 04-06-2008, 07:38 PM -
generating random numbers in a 5x5 array.
By acidacid in forum New To JavaReplies: 3Last Post: 08-14-2007, 03:44 AM
Bookmarks