Please post the entire Bejeweled_World class as you have it now.
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;
}
}