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

10
11
12
13
danpost danpost

2014/5/4

#
Please post the entire Bejeweled_World class as you have it now.
Zzimon Zzimon

2014/5/4

#
Here it is:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;  
import java.util.ArrayList;  
/**
 * Write a description of class Bejeweled_World here.
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bejeweled_World extends World
{
    public Bejeweled_World()
    { 
        super(20, 10, 60); 
        GreenfootImage bg = new GreenfootImage("Bejeweled_bg.jpg");
        bg.scale(getCellSize(), getCellSize());
        setBackground(bg);
        prepare();
    }
    private boolean canPlace(Actor actor, int x, int y)
    {  
        if (actor == null) return false;  
        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;} 
        if (x > 1 && getObjectsAt(x-2, y, Actor.class).get(0).getClass() == actor.getClass() && getObjectsAt(x-1, y, Actor.class).get(0).getClass() == actor.getClass())
        {return false;}
        else
        {
         return true;
        }
    }
    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);
        }
    }
    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;
    }
    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 = tiles.get(n);
                Tile dummy = new Dummy();
                addObject(dummy, tile.getX(), tile.getY());
                removeObject(tile);
                tiles.set(n, dummy);
            }
            for (Tile tile3 : tiles)  
            {  
                int x = tile3.getX(), y = tile3.getY();  
                Tile actor = null;  
                while (!canReplace(actor, x, y)) actor = (Tile)getRandomActor();  
                addObject(actor, x, y);  
            }  
            removeObjects(tiles);
        }else{
            makeSwap(tile2, dx, dy);
        }
    }  
    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;
    }
    private Tile 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;
    } 
    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++;  
            else break;  
        }  
        for (int n=1; n<3 && x+n < getWidth(); n++)  
        {  
            if (getObjectsAt(x+n, y, Tile.class).get(0).getClass() == cls) count++;  
            else break;  
        }  
        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++;  
            else break;  
        }  
        for (int n=1; n<3 && y+n < getHeight(); n++)  
        {  
            if (getObjectsAt(x, y+n, Tile.class).get(0).getClass() == cls) count++;  
            else break;  
        }  
        return count;  
    }  
    private List<Tile> getHorizontalMatchingTiles(int x, int y)
    {
        java.util.List<Tile> list = new ArrayList();
        list.add((Tile)getObjectsAt(x, y, Tile.class).get(0));
        Class cls = list.get(0).getClass();
        for (int n=1; n<3 && x-n >= 0; n++)
        {
            Tile tile = (Tile)getObjectsAt(x-n,y, Tile.class).get(0);
            if (tile.getClass() == cls) list.add(tile);
            else break;
        }
        for (int n=1; n<3 && x+n <getWidth(); n++)
        {
            Tile tile = (Tile)getObjectsAt(x+n,y, Tile.class).get(0);
            if (tile.getClass() == cls) list.add(tile);
            else break;
        }
        if(list.size()>2) return list;
        list.clear();
        return list;
    }  
    private List<Tile> getVerticalMatchingTiles(int x, int y)
    {
        List<Tile> list = new ArrayList();
        list.add((Tile)getObjectsAt(x, y, Tile.class).get(0));
        Class cls = list.get(0).getClass();
        for (int n=1; n<3 && y-n >= 0; n++)
        {
            Tile tile = (Tile)getObjectsAt(x,y-n, Tile.class).get(0);
            if (tile.getClass() == cls) list.add(tile);
            else break;
        }
        for (int n=1; n<3 && y+n <getHeight(); n++)
        {
            Tile tile = (Tile)getObjectsAt(x,y+n, Tile.class).get(0);
            if (tile.getClass() == cls) list.add(tile);
            else break;
        }
        if(list.size() > 2) return list;
        list.clear();
        return list;
    }
}
danpost danpost

2014/5/4

#
Change line 72 to:
Tile actor = (Tile)getRandomActor();
Zzimon Zzimon

2014/5/4

#
IT WORKS! :D Thank you so much! ;) And now I'll see about getting some sleep :) (You're the best!) :)
Zzimon Zzimon

2014/5/4

#
Danpost need your help! my game can't export properly, really urgent!
danpost danpost

2014/5/4

#
You will have to be more specific as to what you are trying to do. Explain what it is doing when you try to export it. etc.
Zzimon Zzimon

2014/5/4

#
Fixed it, phew. Just had to make my world smaller like you said and it worked, I think it was because the heap space error thingy that it just went grey and it refused to show any info so I made my world into a 7x7 instead of 20x10 and then it worked. Thanks for saying that when you tried it out, else I wouldn't have known :)
You need to login to post a reply.
10
11
12
13