Results 1 to 9 of 9
  1. #1
    Nuluvius is offline Member
    Join Date
    Feb 2008
    Posts
    12
    Rep Power
    0

    Exclamation 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.

  2. #2
    Nuluvius is offline Member
    Join Date
    Feb 2008
    Posts
    12
    Rep Power
    0

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    why not change it to a 1-D array or list, shuffle it, then lay it out in 2-D after the shuffle?

  4. #4
    Nuluvius is offline Member
    Join Date
    Feb 2008
    Posts
    12
    Rep Power
    0

    Default

    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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);
      }
      
    }
    I'd do it like so:
    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);
      }
    }

  6. #6
    Nuluvius is offline Member
    Join Date
    Feb 2008
    Posts
    12
    Rep Power
    0

    Default

    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!!

  7. #7
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Exclamation boolean shuffle(List l){}

    See java.lang.UnsupportedOperationException
    Java Code:
    try
    {
       Collections.shuffle(myList);
    }
    catch(UnsupportedOperationException)
    {
       return false;
    }
    which would be defensive coding.....
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  8. #8
    Nuluvius is offline Member
    Join Date
    Feb 2008
    Posts
    12
    Rep Power
    0

    Default

    Nice, thank you also - very usefull.

  9. #9
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Talking

    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

  1. Array of instances using Math.random()
    By xgi1008 in forum New To Java
    Replies: 16
    Last Post: 01-25-2011, 11:10 PM
  2. How to get a random value in an array
    By Franneldort in forum New To Java
    Replies: 21
    Last Post: 11-01-2008, 02:42 PM
  3. How do I shuffle an arraylist?
    By frasifrasi in forum New To Java
    Replies: 2
    Last Post: 07-16-2008, 11:29 PM
  4. How to Shuffle a particular String
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-06-2008, 07:38 PM
  5. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 03:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •