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

2013/4/11

i need a help

freeij8949 freeij8949

2013/4/11

#
how can i randomize the number
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 = 400;
    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 = 2;
    
    /**
     * Creates and sets up the SortGrid world ready to be run.
     */
    public SortGrid()
    {
        super( WIDTH, HEIGHT, 1 );
        // create the network node
        Node first = new Node();final int leftX = WIDTH/2;
        addObject( first , leftX, 300 );
        // 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 );
        }
        // create the connections
        first.setDestinations( ends[0], ends[1] );
        final int[] randomNums = {1, 2};
        final Number[] numbers = new Number[ NUM_POSITIONS ];
        // make the numbers
        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( first );
        numbers[1].setDestination( first );
        // paint ordering
        setPaintOrder(
            Number.class,
            Node.class,
            Position.class,
            Start.class
        );
      
    }
}
freeij8949 freeij8949

2013/4/11

#
how can i change that first showed number (1,2) change randomly?
danpost danpost

2013/4/11

#
Are you trying to make it that lines 47 is sometimes (in a random manner) executed after line 48? OR, do you want the element in line 35 to sometimes be { 1, 2 } and sometimes { 2, 1 } (in a random manner)?
freeij8949 freeij8949

2013/4/11

#
i want to change line 35..
danpost danpost

2013/4/11

#
Put the following code in place of line 35:
int val = Greenfoot.getRandomNumber(2);
final int[] randomNums = { val%2+1, (val+1)%2+1 };
freeij8949 freeij8949

2013/4/11

#
.. uhm i mean i want to change number of 1,2,3,4,5,6 such as (1,5) , (2,4) .. like this
danpost danpost

2013/4/11

#
I am sure there are several ways to code it to do that. The following may not be the best, but will at least do the trick. Replace line 35 with the following:
int num1 = Greenfoot.getRandomNumber(6)+1;
int num2 = Greenfoot.getRandomNumber(5)+1;
if (num2 >= num1) num2++;
final int[] randomNums = { num1, num2 };
EDIT: after looking at it, it is hard to imagine anything more concise or straight-forward.
You need to login to post a reply.