This site requires JavaScript, please enable it in your browser!
Greenfoot back
Tomc84
Tomc84 wrote ...

2012/4/23

Second array?

Tomc84 Tomc84

2012/4/23

#
I need to make a second array I think. the task is this...Randomize the numbers that you are sorting between 1 and 6. Repeated numbers are not allowed though. Hint: Place 1 through 6 in an array of integers first. Then randomly pick two elements in the array and swap them. Do so six times. But after a long weekend of trying and burning out my brain on it I have nothing.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Sets up the SortGrid world. This includes creating all nodes
 * and numbers, connecting the nodes up and placing them in the world.
 * 
 * @author Joseph Lenton
 */
public class SortGrid extends World
{
    private static final int WIDTH = 800; // Lab 9, wider screen
    private static final int HEIGHT = 600;

    private static final int NUMBER_Y = 50;
    private static final int NUMBER_PADDING = 100;
    private static final int NUMBER_X = 150;

    private static final int NUM_POSITIONS = 6; // Lab9, more numbers

    /**
     * Creates and sets up the SortGrid world ready to be run.
     */
    public SortGrid()
    {
        super( WIDTH, HEIGHT, 1 );

        // end nodes
        End[] ends = new End[ NUM_POSITIONS ];
        for ( int i = 0; i < ends.length; i++ ) {
            End end = new End();
            ends[i] = end;
            addObject( end, NUMBER_X + NUMBER_PADDING*i, HEIGHT-NUMBER_Y );
        }

        // lab 9, more nodes
        final int leftX  = WIDTH/4;

        // create the network node
        Node fithRowNode  = new Node(); 
        fithRowNode.setDestinations(ends[2], ends[3]);
        addObject( fithRowNode, leftX + NUMBER_PADDING*2 , 450 );

        Node[] fourthRow  = new Node[2]; 
        for ( int i = 0; i < fourthRow.length; i++ ) {
            Node node = new Node();
            fourthRow[i] = node;
            addObject( fourthRow[i], 300 + NUMBER_PADDING*i*2, 375 );
        }
        fourthRow[0].setDestinations(ends[1], fithRowNode);
        fourthRow[1].setDestinations(fithRowNode, ends[4]);

        Node[] thirdRow  = new Node[3]; 
        for ( int i = 0; i < thirdRow.length; i++ ) {
            Node node = new Node();
            thirdRow[i] = node;
            thirdRow[i].setDestinations(fourthRow[0], fourthRow[1]);
            addObject( thirdRow[i], leftX + NUMBER_PADDING*i*2, 300 );
        }
        thirdRow[0].setDestinations(ends[0], fourthRow[0]);
        thirdRow[1].setDestinations(fourthRow[0], fourthRow[1]);
        thirdRow[2].setDestinations(fourthRow[1], ends[5]);
        

        Node[] secondRow  = new Node[3]; 
        for ( int i = 0; i < secondRow.length; i++ ) {
            Node node = new Node();
            secondRow[i] = node;
            //secondRow[i].setDestinations(thirdRow[0], thirdRow[1]);
            addObject( secondRow[i], leftX + NUMBER_PADDING*i*2, 225 );
        }
        secondRow[0].setDestinations(thirdRow[0], thirdRow[1] );
        secondRow[1].setDestinations(thirdRow[0], thirdRow[2] );
        secondRow[2].setDestinations(thirdRow[1], thirdRow[2] );

        Node[] firstRow  = new Node[3]; // closest to top of screen
        for ( int i = 0; i < firstRow.length; i++ ) {
            Node node = new Node();
            firstRow[i] = node;
            //firstRow[0].setDestinations(secondRow[0], secondRow[1] );
            addObject( firstRow[i], leftX + NUMBER_PADDING*i*2, 150 );
        }
        firstRow[0].setDestinations(secondRow[0], secondRow[1] );
        firstRow[1].setDestinations(secondRow[0], secondRow[2] );
        firstRow[2].setDestinations(secondRow[1], secondRow[2] );

        final int[] randomNums = {6, 5, 4, 3, 1, 2};
        final Number[] numbers = new Number[ NUM_POSITIONS ];
        
        // make the numbers visable
        for ( int i = 0; i < NUM_POSITIONS; i++ ) {
            final Number number = new Number( randomNums[i] );
            final int x = NUMBER_X + NUMBER_PADDING*i;
            final int y = NUMBER_Y;
            numbers[i] = number;
            addObject( number, x, y );
            addObject( new Start(), x, y );
        }

        // set the number destinations
        numbers[0].setDestination( firstRow[0] );
        numbers[1].setDestination( firstRow[0] );
        numbers[2].setDestination( firstRow[1] );
        numbers[3].setDestination( firstRow[1] );
        numbers[4].setDestination( firstRow[2] );
        numbers[5].setDestination( firstRow[2] );

        // paint ordering
        setPaintOrder(
            Number.class,
            Node.class,
            Position.class,
            Start.class
        );
    }
}
Any help would be awesome
Builderboy2005 Builderboy2005

2012/4/23

#
My method would be this:
int[] nums = {1,2,3,4,5,6};
int numsLeft = 6;
int[] randomizedNums = new int[6];\

for(int i=0;i<6;i++){
    int chooseIndex = Greenfoot.getRandomNumber(numsLeft);
    randomizedNums[i] = nums[chooseIndex];
    numsLeft--;
    nums[chooseIndex] = nums[numsLeft];
}
It first chooses a random element from a list of 1-6. Then it removes that element from the list and repeats. The resulting randomized list will be in randomizedNums.
Tomc84 Tomc84

2012/4/23

#
Thank you! that worked like a charm :)
Tomc84 Tomc84

2012/4/23

#
what does line 2 and 3 do? I'm trying by best to learn better and understand more
danpost danpost

2012/4/23

#
@Builderboy2005, your method will only randomly re-order the numbers once; and the task was to swap two of the numbers, six times
int[] nums = { 1, 2, 3, 4, 5, 6 };
// begin swapping, six times
for (int i = 0; i < 6; i++)
{
    // find two elements to swap
    int one = Greenfoot.getRandomNumber(6);
    int two = Greenfoot.getRandomNumber(6);
    // ensure the two elements are different elements
    while (two == one) two = Greenfoot.getRandomNumber(6);
    // swap the numbers in the array
    int hold = nums[one];
    nums[one] = nums[two];
    nums[two] = hold;
}
Builderboy2005 Builderboy2005

2012/4/23

#
I was under the impression that the task was simply to randomize a list of numbers, and he was suggesting a method? Also, Tomc, lines 2 and 3 declare two variables, numsLeft, and randomizedNums. numsLeft indicates the number of elements we have remaining in our nums array. The array randomizedNums is the place where we are putting our randomized numbers, and really is just a normal array.
Tomc84 Tomc84

2012/4/24

#
ok cool. @ danpost Thank you. I'm so new at this and I thought it worked just fine. Thank you both for helping with this. I wish there was more info online when I searched for array's, so I would not have to bug you guys.
Duta Duta

2012/4/24

#
What your teacher is suggesting sounds a bit like a bubble sort - if that is what he's getting at, I can provide code. It is, however, a very inefficient sorting method
You need to login to post a reply.