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

2014/4/27

Creating a grid in a non-grid world

1
2
3
4
5
danpost danpost

2014/4/29

#
Zzimon wrote...
Ok, so now I've gotten to the part of populating the board, I'm intending to put one kind of actor in that will run a random number generator (1-4) or something like that which will in turn spawn some other actors that will then act as the tiles. Do you have any way to stop the first actor from keep spawning tiles when they are occupied?
You will have to be much more specific as to what you are trying to do. I have a feeling, however, that you may be trying to go about it in a much more complicated way than it has to be. Show some code, explain what you are trying to do in detail. First, you do not need to use an actor to run a spawning generator. By doing so, you are only utilizing its 'act' method to do your bidding. This can be accomplished in your world class 'act' method (or even maybe in the world constructor, depending on what you want visually (which should also be given in your posts). Second, you do not say what the tiles were for, how they were laid out, etc.
Zzimon Zzimon

2014/4/29

#
Well, I don't really have any code yet but what I'm planning is to have them laid out exactly as the game bejeweled, and my plan so far is to have something that generates each of the tiles separately so that i put one actor(the same actor, but multiple) in each empty box of the grid. Then I'd make the tiles that have then been spawned on-top of the spawner-actors check the surrounding tiles for the same tile in which case they should disappear and the spawners should make new tiles in their place. My grid currently looks like this, with one tile actor(not random). Picture of grid with each of the X's being a grid box. My question was more specific how to stop the spawners once the box is occupied by a tile. Hope this helps.
Zzimon Zzimon

2014/4/29

#
here is an example of what i'm trying to do, which i cannot get to work. This is the actor that I intended on using for the spawning. When i put this actor in my world shouldn't it just run the Randomnr void either endlessly or just once?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public void act()
    {
       Randomnr();
    }   
    public void Randomnr()
    {
        int z = Greenfoot.getRandomNumber(4+1);
        if (z == 1)
        {
            getWorld().addObject(new Sword(), 7, 0);
        }
        if (z == 2)
        {
            getWorld().addObject(new Torch(), 7, 0);
        }
        if (z == 2)
        {
            getWorld().addObject(new Coin(), 7, 0);
        }
    }
danpost danpost

2014/4/29

#
If you have that act method, calling that method, in an actor that you placed in the world, you will have either one sword or both a torch and a coin added to the world on each act cycle (repeatedly); and they will all spawn at the same location. I know that is not what you want; but, I am not sure of what, exactly, you do want. Edit: Actually, not EVERY act cycle as the 'getRandomNumber(4+1)' could return '0', '3', and '4', as well as '1' and '2'. But, you are still looking at about 30 new actors in the world every second.
Zzimon Zzimon

2014/4/29

#
for this I pretty much just want to fill up my grid board with 'Tile' actors such as my Sword, Torch and my Coin.
danpost danpost

2014/4/29

#
I am still in the dark as far as what the tiles are for, how they are to be arranged in the world, how many of them will fill the world, what your world size is, whether it is gridded (cellsize greater than 1) or not, etc.
Zzimon Zzimon

2014/4/29

#
every one of the tiles are supposed to fit in one cell, they are supposed to be arranged randomly in the world but at the first "fill-up" it would be best if there weren't more than 3 in a straight line. The tiles are supposed to match up 3 in a straight line so that scores one "point", I intend for these to work as sword-points, coin-points and torch-points. As for the amount of tiles the grid is made with the command "super(20, 10, 60); " so there should be 200 possible placements. Hope this helps ^^'. Sorry if I'm being difficult I'm really trying my best to help you help me ;)
danpost danpost

2014/4/29

#
Ok, so there are 200 possible placements. Do you want all 200 places to contain a tile to start? if not, how many tiles should be placed at the beginning?
Zzimon Zzimon

2014/4/29

#
all of the placements should preferrably be filled
danpost danpost

2014/4/29

#
Then you should be using a 'for' loop that iterates 200 times -- or better yet, a double 'for' loop (one inside of another) to iterate over the width and the height:
1
for (int row=0; row<getHeight(); row++) for (int col=0; col<getWidth(); col++)
Together, the two 'for' loops will contain the coordinates of each cell in your world, one time each (as '(col, row)' ). The order will be from left to right going from top to bottom (across each row, going down). Within the double 'for' loop, you should select a random tile and, before placing it, ensure that in both the 'up' an 'left' directions up to two cells away either the edge of the world is present or a tile of a different type is there. Use a 'while' loop (still inside the double 'for' loop) to retry when the chosen tile cannot be placed.
Zzimon Zzimon

2014/4/29

#
Nice! though, how should i incorporate the random spawn script then? Inside the double for loop or? I'm not so familiar with using for loops.
danpost danpost

2014/4/29

#
The following snippet can be used to get a random Actor object. The Actor object, 'actor', should then have its class compared to the classes of any two, or three, to the left of it, as well as to any two, or three, above it (barring the edge of the world).
1
2
3
4
5
6
7
8
Actor actor = null;
while (!canPlace(actor, col, row))
{
    int which = Greenfoot.getRandomNumber(3);
    if (which == 0) actor = new Sword();
    if (which == 1) actor = new Coin();
    if (which == 2) actor = new Torch();
}
This will be inside the double 'for' loop; you now only need to create a method called 'canPlace' that returns the proper boolean value for whether the actor can be placed there or not. Notice how close this is: For each row across and for each cell within that row; As long as the chosen actor cannot be placed at the current cell, choose another actor. Now we need to describe in detail when the actor cannot be placed in the cell given. First, we must preclude 'null' for the actor; so, return a false value if the actor given is 'null'. Then, we do one direction, say 'up', and then the other 'left'. We cannot place the actor if there are at least two tiles above it and both are of the same class. If the row is at least the third row and both locations above it hold the same class object, return a false value. Do similar to 'up' for 'left'. If at this point, we have not returned a false value, then the actor can be placed there; so, the last statement in the 'canPlace' method should be 'return true;'.
Zzimon Zzimon

2014/4/30

#
If I understand this correctly this double 'for' part, with the spawn part checks all the grid boxes and puts a random actor in each one and it's just supposed to go in my worlds act method, though it doesn't include the checking, the part that you said after the 'Notice how close this is:' part right?
danpost danpost

2014/4/30

#
Zzimon wrote...
If I understand this correctly this double 'for' part, with the spawn part checks all the grid boxes and puts a random actor in each one and it's just supposed to go in my worlds act method, though it doesn't include the checking, the part that you said after the 'Notice how close this is:' part right?
This is not something that is needed to be done continuously; only once. If you put the code in the act method, it will be executed multiple times -- which you DO NOT want. Put in the your world constructor or 'prepare' method.
Zzimon Zzimon

2014/4/30

#
danpost wrote...
This will be inside the double 'for' loop; you now only need to create a method called 'canPlace' that returns the proper boolean value for whether the actor can be placed there or not.
how do i make the canplace part work of the random generator work?
There are more replies on the next page.
1
2
3
4
5