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

3
4
5
6
7
8
9
Zzimon Zzimon

2014/5/2

#
well it feels good to fell like it's close heheh
Zzimon Zzimon

2014/5/2

#
Ok, my initial though as to tackling this is to put in something close to the code in the canPlace we have from the Bejeweled_World. Like this:
        if (y > 1 && getObjectsAt(x, y-2, Actor.class).get(0).getClass() == actor.getClass() && getObjectsAt(x, y-1, Actor.class).get(0).getClass() == actor.getClass())  
        { return false;} 
but instead of Actor.class then have something like Sword.class and then adjust the coordinates so the selected one checks either 2 squares both up, down, left and right or if possible have all of the tiles constantly run a check for one square up, down left and right. All theoretical, so far unapplicable ^^'
danpost danpost

2014/5/2

#
You know ... it would be nice if the 'makeSwap' method returned the other actor back to the calling method, 'moveSelected'. So, change line 3 in my last code posting to this:
Tile tile2 = makeSwap(tile, dx, dy);
change line 10 to this:
Tile tile2 = (Tile)getObjectsAt(x+dx, y+dy, Tile.class).get(0);
and insert the following at line 13:
return tile2;
Zzimon Zzimon

2014/5/2

#
I swapped what you said but I get an error saying 'incompatible types' at the third line
 Tile tile2 = makeSwap(tile, dx, dy);  
danpost danpost

2014/5/3

#
Let's see what you got, now. Show from the 'moveSelected' method on.
Zzimon Zzimon

2014/5/3

#
I have this:
    public void moveSelected(Tile tile, int dx, int dy)  
    {  
        Tile tile2 = makeSwap(tile, dx, dy);  
    }  
    private void makeSwap(Tile tile, int dx, int dy)  
    {  
        int x = tile.getX();  
        int y = tile.getY();  
        Tile tile2 = (Tile)getObjectsAt(x+dx, y+dy, Tile.class).get(0); 
        tile.setLocation(x+dx,y+dy);  
        tile2.setLocation(x, y);  
        return tile2;
    } 
danpost danpost

2014/5/3

#
Oh, I forgot, change line 5 in your last posting to:
private Tile makeSwap(Tile tile, int dx, int dy)
Zzimon Zzimon

2014/5/3

#
Awesome, that fixed it :) quick question when might you be available tomorrow? I'm asking since I'mgetting pretty tired but if the choice is between this game getting done by my time limit and sleep then I choose the game and I have to go to work tomorrow from 18:00 to 02:00 at night UTC+1. I am perfectly fine with continuing on now though
danpost danpost

2014/5/3

#
Well, I think testing for the next step is ready.
Zzimon Zzimon

2014/5/3

#
awesome! let's get some code in there ;)
danpost danpost

2014/5/3

#
Zzimon wrote...
Ok, my initial though as to tackling this is to put in something close to the code in the canPlace we have from the Bejeweled_World. <cont'd>
The problem with that is now the tile could be anywhere within the matching set, if it is in one. That means we have to look both ways along both the horizontal and vertical and count the matching tiles going in opposite directions. If either of the two counts (the sum of the horizontals, left and right, or the sum of the verticals, up and down, reaches two (this will not include itself), then we can return a true value from the 'matchingSetFound' method that this checking will be done in. Try this:
// 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);


// then add these methods
private boolean matchingSetFound(Tile tile)
{
    int x = tile.getX();
    int y = tile.getY();
    Class cls = tile.getClass();
    return getHorizontalMatchCount(x, y, cls) > 1 || getVerticalMatchCount(x, y, cls) > 1;
}

private int getHorizontalMatchCount(int x, int y, Class cls)
{
    int count = 0;
    for (int n=1; n<3 && x-n >= 0; n++)
    {
        if (getObjectsAt(x-n, y, Tile.class).get(0).getClass() == cls) count++;
    }
    for (int n=1; n<3 && x+n < getWidth(); n++)
    {
        if (getObjectsAt(x+n, y, Tile.class).get(0).getClass() == cls) count++;
    }
    return count;
}

private int getVerticalMatchCount(int x, int y, Class cls)
{
    int count = 0;
    for (int n=1; n<3 && y-n >= 0; n++)
    {
        if (getObjectsAt(x, y-n, Tile.class).get(0).getClass() == cls) count++;
    }
    for (int n=1; n<3 && y+n < getHeight(); n++)
    {
        if (getObjectsAt(x, y+n, Tile.class).get(0).getClass() == cls) count++;
    }
    return count;
}
It should now refute illegal moves.
Zzimon Zzimon

2014/5/3

#
This should be stopping illegal moves right? If so then appaerently it isn't working properly.
Zzimon Zzimon

2014/5/3

#
And all of the code should just go into my Bejeweled_world actor right?
danpost danpost

2014/5/3

#
Zzimon wrote...
And all of the code should just go into my Bejeweled_world actor right?
Correct. (but your Bejeweled_World is a World class, not an Actor class)
This should be stopping illegal moves right? If so then appaerently it isn't working properly.
See the last line of my last post. and What is it doing?
Zzimon Zzimon

2014/5/3

#
ok now I actually got a bit confused. you mean this
danpost wrote...
It should now refute illegal moves.
to my understanding that means that the code should refuse moves that would not make a line of 3 of the same tiles in a row occur.
There are more replies on the next page.
3
4
5
6
7
8
9