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

2014/5/1

Selecting characters and switching them with others

7
8
9
10
11
12
13
danpost danpost

2014/5/3

#
To build the complete list of tiles to be removed in the 'moveSelected' method, we can now use this:
1
2
3
4
5
List<Tile> tiles = new ArrayList();
tiles.addAll(getHorizontalMatchingTiles(tile.getX(), tile.getY()));
tiles.addAll(getVerticalMatchingTiles(tile.getX(), tile.getY()));
tiles.addAll(getHorizontalMatchingTiles(tile2.getX(), tile2.getY()));
tiles.addAll(getVerticalMatchingTiles(tile2.getX(), tile2.getY()));
and then later we can just use the following to remove the matching tiles:
1
removeObjects(tiles);
(possibly) we may want to remove them as we replace them.
danpost danpost

2014/5/3

#
You can go ahead and add the removal line and test it. Try to avoid swaps that are near each other, as the tiles are not being replaced yet and exceptions could be thrown. Do not forget to remove that line after testing.
Zzimon Zzimon

2014/5/3

#
So this goes into the moveSelected method just like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void moveSelected(Tile tile, int dx, int dy) 
    
        Tile tile2 = makeSwap(tile, dx, dy); 
        // add as the second line in the 'moveSelected' method (or insert at line 4 of your last code posting) 
        if (matchingSetFound(tile) || matchingSetFound(tile2))
        {
            Tile.setSelected(null);
        }else{
            makeSwap(tile2, dx, dy);
        }
        List<Tile> tiles = new ArrayList(); 
        tiles.addAll(getHorizontalMatchingTiles(tile.getX(), tile.getY())); 
        tiles.addAll(getVerticalMatchingTiles(tile.getX(), tile.getY())); 
        tiles.addAll(getHorizontalMatchingTiles(tile2.getX(), tile2.getY())); 
        tiles.addAll(getVerticalMatchingTiles(tile2.getX(), tile2.getY()));
    }
danpost danpost

2014/5/3

#
Zzimon wrote...
So this goes into the moveSelected method just like this? < Code Omitted >
No. The code to build the list needs to be inside the 'if' block where we determined that there was at least one matching set created by the move.
Zzimon Zzimon

2014/5/3

#
ok I'm getting this message if I add the removetiles command: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:635) at java.util.ArrayList.get(ArrayList.java:411) at Bejeweled_World.getVerticalMatchingTiles(Bejeweled_World.java:144) at Bejeweled_World.moveSelected(Bejeweled_World.java:59) at Tile.act(Tile.java:30) at PIP.act(PIP.java:46) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) though that seems to only pop up if the ones to be removed are adjacent to an already empty grid box. Now we just need to repopulate them! :D
Zzimon Zzimon

2014/5/3

#
Oh. just saw your reply with it not supposed to be like that, will fix and try again
Zzimon Zzimon

2014/5/3

#
Ok, still works (seemingly the same). Now, we just need to repopulate the empty spaces right?
danpost danpost

2014/5/3

#
danpost wrote...
You can go ahead and add the removal line and test it. Try to avoid swaps that are near each other, as the tiles are not being replaced yet and exceptions could be thrown. Do not forget to remove that line after testing.
Did you read this posting?
Zzimon Zzimon

2014/5/3

#
oh, I didn't read the 'try to avoid' thing ^^' was a bit too excited to try it out, though it seems to work as it should then :)
Zzimon Zzimon

2014/5/3

#
Ok, well i have to go in 10 min and I will be gone for about 9-10 hours, I hope that you'll be here when I come back so we can finish this tonight :) If you know that you won't be available either tonight or tomorrow could you please just post the last bit of code for replacing the tiles so I can implement it myself, since I need the game done by tomorrow night, Thanks!
danpost danpost

2014/5/3

#
NOW we can repopulate. We need to iterate through the list of tiles to be removed and add a new random tile to each of their locations. We can remove and replace the tiles in the grid AND the element in the list simultaneously so as to keep track of which tile is currently in the grid that we are working with. To iterate through the list, we can use the 'for-each' type of 'for' loop:
1
for (Tile tile : tiles)
To add a random tile that does not create a match we need to do a little work. This will be similar to what we did in the prepare method, but calling 'canPlace' will not work here. We need a 'canReplace' method. Also, we will be using some code that is exactly the same as part of the code in the 'prepare' method. So, let us refactor the code (break it up into smaller, more manageable or re-usable, bits -- in this case, re-usable). The current prepare method looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void prepare()
{
    for (int row=0; row<getHeight(); row++) for (int col=0; col<getWidth(); col++)
    {
        Actor actor = null;
        while (!canPlace(actor, col, row))
        {
            int which = Greenfoot.getRandomNumber(4);
            if(which == 0) actor = new Sword();
            if(which == 1) actor = new Coin();
            if(which == 2) actor = new Torch();
            if(which == 3) actor = new Key();
        }
        addObject(actor, col, row);
    }
}
We need the code that get a random actor as a separate method, so we can use it multiple times. So.
1
2
3
4
5
6
7
8
9
10
private Actor getRandomActor()
{
    Actor actor = null;
    int which = Greenfoot.getRandomNumber(4);
    if(which == 0) actor = new Sword();
    if(which == 1) actor = new Coin();
    if(which == 2) actor = new Torch();
    if(which == 3) actor = new Key();
    return actor;
}
With this method, we can now change the prepare method to this:
1
2
3
4
5
6
7
8
9
public void prepare()
{
    for (int row=0; row<getHeight(); row++) for (int col=0; col<getWidth(); col++)
    {
        Actor actor = null;
        while (!canPlace(actor, col, row)) actor = getRandomActor();
        addObject(actor, col, row);
    }
}
Zzimon Zzimon

2014/5/4

#
Ok, yeah so the 'getRandomActor' is just so we can use the 'getRandomNumber' part from the prepare method, understood. Now we just need the canReplace method.
danpost danpost

2014/5/4

#
Zzimon wrote...
Ok, yeah so the 'getRandomActor' is just so we can use the 'getRandomNumber' part from the prepare method, understood. Now we just need the canReplace method.
It is so we can use the code-set within the 'getRandomActor' method from more than one place in the code (the 'prepare' method being one of the places; the other place will be in the 'moveSelected' method, when replacing the removed tiles).
Zzimon Zzimon

2014/5/4

#
Ok, the moveSelected actor currently looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void moveSelected(Tile tile, int dx, int dy) 
    
        Tile tile2 = makeSwap(tile, dx, dy); 
        // add as the second line in the 'moveSelected' method (or insert at line 4 of your last code posting) 
        if (matchingSetFound(tile) || matchingSetFound(tile2))
        {
            Tile.setSelected(null); 
            List<Tile> tiles = new ArrayList(); 
            tiles.addAll(getHorizontalMatchingTiles(tile.getX(), tile.getY())); 
            tiles.addAll(getVerticalMatchingTiles(tile.getX(), tile.getY())); 
            tiles.addAll(getHorizontalMatchingTiles(tile2.getX(), tile2.getY())); 
            tiles.addAll(getVerticalMatchingTiles(tile2.getX(), tile2.getY()));
            removeObjects(tiles); 
        }else{
            makeSwap(tile2, dx, dy);
        }
    }
Now, the list function from before does it save the tiles placement after we use the 'removeObjects'? If it does can't we just input and use another couple of 'getHorizontalMatching' and 'getVerticalMatching' and then use the 'getRandomActor'?
danpost danpost

2014/5/4

#
danpost wrote...
You can go ahead and add the removal line and test it. Try to avoid swaps that are near each other, as the tiles are not being replaced yet and exceptions could be thrown. Do not forget to remove that line after testing.
Did you read this ^^? We will add that line after adding replacements for each one. If we remove them now, we would then have to 'hunt' for the empty cells, as opposed to just using 'getX' and 'getY" on the actors in the list while they are still in the world.
There are more replies on the next page.
7
8
9
10
11
12
13