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

2011/11/19

Making Coordinates

kiarocks kiarocks

2011/11/19

#
I am trying to make coordinates for 25 objects, however i want them to be randomized each time. Here is my current code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
/**
 * Write a description of class CircutBuildAndDisplay here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CircutBuildAndDisplay extends Actor
{
    //public int one = rand(30) - rand(4) + rand(100);
    public World whichworld;
    public boolean dothisonce = false;
    int[] ints;
    int[] ints2;
    World cw = (CircutWorld) getWorld();
    public CircutBuildAndDisplay(World world)
    {
        whichworld = world;
    }

    public CircutBuildAndDisplay()
    {
    }

    /**
     * Act - do whatever the CircutBuildAndDisplay wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(getWorld() instanceof CircutWorld)
        {
            cw = (CircutWorld) getWorld();
            if(!dothisonce)
            {

                ints = new int[25];
                for(int l = 0; l < 24; l++)
                {

                    ints[l] = (rand(5) + 2);
                }

                ints2 = new int[25];
                for(int l = 0; l < 24; l++)
                {
                    ints2[l] = (rand(5) + 2);
                }
                for(int l = 0; l<24; l++)
                {
                    if(l < 2)
                    {
                        cw.addObject(new Light(),ints[l]*64,ints2[l]*64);
                    }
                    else if(l < 8)
                    {
                        cw.addObject(new Wire(),ints[l]*64,ints2[l]*64);
                    }
                    else if(l < 12)
                    {
                        cw.addObject(new Resistor(),ints[l]*64,ints2[l]*64);
                    }
                    else if(l < 15)
                    {
                        cw.addObject(new Diode(),ints[l]*64,ints2[l]*64);
                    } 
                    else if(l < (24 - (CircutWorld.level/2)))
                    {
                        cw.addObject(new EmptyCircuitArea(),ints[l]*64,ints2[l]*64);
                    }
                }
                dothisonce = true;
            }

        }
    }

    public int rand(int i)
    {
        int i2 = Greenfoot.getRandomNumber(i);
        return i2;
    }
}
davmac davmac

2011/11/19

#
In what way does the current code not do what you want it to?
kiarocks kiarocks

2011/11/19

#
Some of the coordinates end up on top of each other.
davmac davmac

2011/11/19

#
Perhaps combine all the loops into one - rather than having three separate loops - and check that there is no object there before settling on the coordinates. Inside "if (!doThisOnce)":
ints  = new int[25];
ints2 = new int[25];
for (int l = 0; l < 25; l++) {
    ints[l] = rand(5) + 2;
    ints2[l] = rand(5) + 2;
    while (! cw.getObjectsAt(ints[l], ints2[l]).isEmpty()) {
        ints[l] = rand(5) + 2;
        ints2[l] = rand(5) + 2;
    }
    
    if (l < 2) {
        cw.addObject(new Light(),ints[l]*64,ints2[l]*64);
    }
    else {
        // etc
    }
}
Note, your original loop count seems wrong - you only count to 23, because the condition is "l < 24". I've corrected that.
kiarocks kiarocks

2011/11/19

#
Your getObjectsAt(int,int) seems weird. Returns getObjectsAt(java.lang.Class,int,int) cannot be applied to getObjectsAt(int,int)
kiarocks kiarocks

2011/11/19

#
Also, this still looks like it could make objects end up on top of eachother
kiarocks kiarocks

2011/11/19

#
This loop partly works, but objects are
for(int l = 0; l<=24; l++)
                {
                    if(l < 4)
                    {
                        ints[l] = l + 3;
                        ints2[l] = l + 3;
                    }
                    else if(l < 9)
                    {
                        ints[l] = l-5 + 3;
                        ints2[l] = l-5 + 3;
                    }
                    else if(l <14)
                    {
                        ints[l] = l-10 + 3;
                        ints2[l] = l-10 + 3;
                    }
                    else if(l < 19)
                    {
                        ints[l] = l-15 + 3;
                        ints2[l] = l-15 + 3;
                    }
                    else if(l <= 24)
                    {
                        ints[l] = l-20 + 3;
                        ints2[l] = l-20 + 3;
                    }
                }
to end up on each other.
danpost danpost

2011/11/19

#
I ran a test scenario to simulate what you are trying to accomplish. I made an Actor super.class for Components with sub-classes for diode, resistor, light, and wire. In world constructor, randomly added components at (0, 0) and stored them in an Array of Component. In the Components super.class, used 'addedToWorld(World world)' with a 'while' statement to randomly set the x and y coordinates until (1) not at world's edge and (2) not intersecting other objects. The array of Components in the world class can be used to get the locations of each component e.g. components.getX() Actually, I used Animal as super and Bug, Fly, and Spider as subs. My code for creating them looks like this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Arena extends World
{
    public Animal[] animals ;
    
    public Arena()
    {    
        super(600, 400, 1);
        animals = new Animal[25];
        for (int myCt = 0; myCt < 25; myCt++)
        {
            Animal animal = new Animal();
            switch (Greenfoot.getRandomNumber(3))
            {
                case 0: animal = new Bug();
                        break;
                case 1: animal = new Fly();
                        break;
                case 2: animal = new Spider();
                        break;
           }
           animals[myCt] = animal;
           addObject(animal, 0, 0);
        }
    }
}
danpost danpost

2011/11/19

#
If you need a specific number of each type of component, use 'while (getObjects(Component.class).size() < 25)' and set up counters and maximum variables for each type to stop creating those types when maximum values are reached. Come to think of it, you will probably need a variable in place of 'getObjects(Component.class).size()' to count objects added to the world for use in components.
danpost danpost

2011/11/19

#
For your loop above, this should do the same thing:
for (int l = 0; l < 25; l++)
{
    ints[l] = (l % 5) + 3;
    ints2[l] = ints[l];
}
davmac davmac

2011/11/19

#
Revising the code I posted (correcting getObjectsAt call):

ints  = new int[25];
ints2 = new int[25];
for (int l = 0; l < 25; l++) {
    ints[l] = rand(5) + 2;
    ints2[l] = rand(5) + 2;
    while (! cw.getObjectsAt(ints[l], ints2[l], Actor.class).isEmpty()) {
        ints[l] = rand(5) + 2;
        ints2[l] = rand(5) + 2;
    }
    
    if (l < 2) {
        cw.addObject(new Light(),ints[l]*64,ints2[l]*64);
    }
    else {
        // etc
    }
}
The while loop ensures that no two objects appear at the same location. If there's already an object at the chosen location, it chooses a new location, until that's no longer true.
kiarocks kiarocks

2011/11/20

#
I think ill use davmac's idea. @danpost, you remember that scenario you downloaded to take a look at the code? It's that scenario.
You need to login to post a reply.