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

8
9
10
11
12
13
danpost danpost

2014/5/4

#
The 'canReplace' method should be fairly straight-forward. If the horizontal and vertical lists of matching actors are empty, then replace with that random actor:
private boolean canReplace(Tile actor, int x, int y)
{
    if (getHorizontalMatchCount(x, y, actor.getClass()) > 1) return false;
    if (getVerticalMatchCount(x, y, actor.getClass()) > 1) return false;
    return true;
}
Now, the last thing we did was create a list of actors that need to be removed and replaced. We need to iterate over those actors and determine a new random tile type for each of their locations. This should do the job:
for (Tile tile : tiles)
{
    int x = tile.getX(), y = tile.getY();
    Tile actor = null;
    while (!canReplace(actor, x, y)) actor = getRandomTile();
    addObject(actor, x, y);
}
Zzimon Zzimon

2014/5/4

#
Oh, yeah forgot to remove that line, now it makes more sense, wasn't depending on it saving the placement fo the emtpy cells after the things it kept a watch over were gone, ok so I'll remove that.
danpost danpost

2014/5/4

#
Guess what ... I think we can put the removal line back in now and test it out. EDIT: No. I do not think that will do it. There is a problem with the logic somewhere, and I need to work it out.
Zzimon Zzimon

2014/5/4

#
This is how the canReplace is supposed to look right?
    private boolean canReplace(Tile actor, int x, int y)
    {
        if (getHorizontalMatchCount(x, y, actor.getClass()) > 1) return false;
        if (getVerticalMatchCount(x, y, actor.getClass()) > 1) return false;
        return true;
        for (Tile tile : tiles)
        {
            int x = tile.getX(), y = tile.getY();
            Tile actor = null;
            while (!canReplace(actor, x, y)) actor = getRandomTile();
            addObject(actor, x, y);
        }
    }
though now when I try to compile it I'm getting an error saying 'cannot find symbol - variable tiles' at line 6
danpost danpost

2014/5/4

#
I want to say we need to create another type of Tile class, called 'Dummy' or something, to replace the actors in the list and replace them also in the world before we start refilling them with the random standard tiles. Otherwise, the check and get methods will not always return the proper information.
danpost danpost

2014/5/4

#
Zzimon wrote...
This is how the canReplace is supposed to look right? < Code Omitted > though now when I try to compile it I'm getting an error saying 'cannot find symbol - variable tiles' at line 6
You can never have statements in a method after a line beginning with the world 'return'. The following lines will never be executed. The 'for' loop code would to be placed where we removed the 'removeObjects(tiles);' line from. However, we need to something else before that (which I mentioned in my last post).
Zzimon Zzimon

2014/5/4

#
Ok, so how do we make the tiles in the list switch out with the 'Dummy' Tile class?
danpost danpost

2014/5/4

#
Another Tile subclass with extensive code:
public class Dummy extends Tile{}
Ok, replacing all actors in the list with Dummy actors and replacing them in the world:
for (int n=0; n<tiles.size(); n++)
{
    Tile tile = tiles.get(n);
    Tile dummy = new Dummy();
    addObject(dummy, tile.getX(), tile.getY());
    removeObject(tile);
    tiles.set(n, dummy);
}
I think that is what we need.
Zzimon Zzimon

2014/5/4

#
ok, so this:
    private boolean canReplace(Tile actor, int x, int y)
    {
        if (getHorizontalMatchCount(x, y, actor.getClass()) > 1) return false;
        if (getVerticalMatchCount(x, y, actor.getClass()) > 1) return false;
        return true;
        for (Tile tile : tiles)
        {
            int x = tile.getX(), y = tile.getY();
            Tile actor = null;
            while (!canReplace(actor, x, y)) actor = getRandomTile();
            addObject(actor, x, y);
        }
    }
should be changed to this:
 
    private boolean canReplace(Tile actor, int x, int y)
    {
        if (getHorizontalMatchCount(x, y, actor.getClass()) > 1) return false;
        if (getVerticalMatchCount(x, y, actor.getClass()) > 1) return false;
        return true;
        for (int n=0; n<tiles.size(); n++)
        {
            Tile tile = tiles.get(n);
            Tile dummy = new Dummy();
            addObject(dummy, tile.getX(), tile.getY());
            removeObject(tile);
            tiles.set(n, dummy);
        }
    }
right?
danpost danpost

2014/5/4

#
danpost wrote...
Zzimon wrote...
This is how the canReplace is supposed to look right? < Code Omitted > though now when I try to compile it I'm getting an error saying 'cannot find symbol - variable tiles' at line 6
You can never have statements in a method after a line beginning with the world 'return'. The following lines will never be executed. The 'for' loop code would to be placed where we removed the 'removeObjects(tiles);' line from. However, we need to something else before that (which I mentioned in my last post).
Please read. (me and my missing words!!)
Zzimon Zzimon

2014/5/4

#
heheh, ok thanks, so like this instead right?
private boolean canReplace(Tile actor, int x, int y)
    {
        if (getHorizontalMatchCount(x, y, actor.getClass()) > 1) return false;
        if (getVerticalMatchCount(x, y, actor.getClass()) > 1) return false;
        for (int n=0; n<tiles.size(); n++)
        {
            Tile tile = tiles.get(n);
            Tile dummy = new Dummy();
            addObject(dummy, tile.getX(), tile.getY());
            removeObject(tile);
            tiles.set(n, dummy);
        }
        return true;
    }
EDIT: again I didn't read properly, will fix
Zzimon Zzimon

2014/5/4

#
That means it should go in the moveSelected method like this right?
public void moveSelected(Tile tile, int dx, int dy)  
    {  
        Tile tile2 = makeSwap(tile, dx, dy); 
        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())); 
            for (int n=0; n<tiles.size(); n++)
            {
                Tile tile = tiles.get(n);
                Tile dummy = new Dummy();
                addObject(dummy, tile.getX(), tile.getY());
                removeObject(tile);
                tiles.set(n, dummy);
            }
        }else{
            makeSwap(tile2, dx, dy);
        }
    }  
Zzimon Zzimon

2014/5/4

#
if I do this I get an error message marking line 14 and telling me 'variable tile is already defined in method moveSelected(Tile,int,int)'
danpost danpost

2014/5/4

#
Very good. Now, add the next 'for' loop (the one that replaces the tiles with random standard tiles). Its at the top of the page after the 'canReplace' method.
danpost danpost

2014/5/4

#
Zzimon wrote...
if I do this I get an error message marking line 14 and telling me 'variable tile is already defined in method moveSelected(Tile,int,int)'
Remove the world 'Tile' (the first word) from line 14.
There are more replies on the next page.
8
9
10
11
12
13