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

6
7
8
9
10
11
12
danpost danpost

2014/5/3

#
Here is half of the first one. It does not contain the second 'for' loop or the return statement (which I gave above):
private List<Tile> getHorizontalMatchingTiles(int x, int y)
{
    List<Tile> list = new ArrayList();
    list.add(getObjectsAt(x, y, Tile.class));
    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;
    }
danpost danpost

2014/5/3

#
About ready to retire. I will be available in about 9 hours time.
Zzimon Zzimon

2014/5/3

#
ok, i got this now:
  
    private List<Tile> getHorizontalMatchingTiles(int x, int y)
    {
        List<Tile> list = new ArrayList();
        list.add(getObjectsAt(x, y, Tile.class));
        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(matchingTile);
            else break;
        }
        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;
        }
    }  
and i get the message 'cannot find symbol - class List'
danpost danpost

2014/5/3

#
Change 'matchingTile' on line 9 to 'tile'. And you probably did not add the import statements I said you needed.
Zzimon Zzimon

2014/5/3

#
I added the
import java.util.List.*;  
import java.util.ArrayList.*;  
at the top of my actor and changed the 'matchingTile' to 'tile' but that doesn't really seem to change much, I'm still getting the 'cannot find symbol - class List' for line 1
danpost danpost

2014/5/3

#
Zzimon wrote...
I added the < Code Omitted > at the top of my actor and changed the 'matchingTile' to 'tile' but that doesn't really seem to change much, I'm still getting the 'cannot find symbol - class List' for line 1
Please show the entire class code at this point.
Zzimon Zzimon

2014/5/3

#
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))
            {
                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);
        }
    }
    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);
        }
    }  
    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)
    {
        List<Tile> list = new ArrayList();
        list.add(getObjectsAt(x, y, Tile.class));
        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 >= 0; n++)
        {
            Tile tile = (Tile)getObjectsAt(x+n,y, Tile.class.get(0));
            if (tile.getClass() == cls) list.add(tile);
            else break;
        }
    }  
    private List<Tile> getVerticalMatchingTiles(int x, int y)
    {
        List<Tile> list = new ArrayList();
        list.add(getObjectsAt(x, y, Tile.class));
        Class cls = list.get(0).getClass();
        for (int n=1; n<3 && x-n >= 0; n++)
        {
            Tile tile = (Tile)getObjectsAt(x,y-n, Tile.class.get(0));
            if (tile.getClass() == cls) list.add(matchingTile);
            else break;
        }
        for (int n=1; n<3 && x-n >= 0; n++)
        {
            Tile tile = (Tile)getObjectsAt(x,y+n, Tile.class.get(0));
            if (tile.getClass() == cls) list.add(tile);
            else break;
        }
    }
}
danpost danpost

2014/5/3

#
OK. Change the import statements to this:
import java.util.List;
import java.util.ArrayList;
Then, change lines 107 and 125 to this:
list.add((Tile)getObjectsAt(x, y, Tile.class).get(0));
and add the return statement at the end of both the 'getHorizontalMatchingTiles' and 'getVerticalMatchingTiles' methods to return the lists created. I think we both we getting pretty tired before the break.
Zzimon Zzimon

2014/5/3

#
Changed the import statements and lines 107 and 125 but I'm still confused about the return statement. for some reason I'm now getting the message 'cannot find symbol - method get(int)' at line 111(previous post) about the 'Tile.class.get'. Also I'm pretty confused about the whole return statement thing, in this one all I'm thinking is really to end it like this:
  
    private List<Tile> getHorizontalMatchingTiles(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 && 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 >= 0; n++)
        {
            Tile tile = (Tile)getObjectsAt(x+n,y, Tile.class.get(0));
            if (tile.getClass() == cls) list.add(tile);
            else break;
        }
        return list;
    }  
danpost danpost

2014/5/3

#
Sorry, missed that one. Move the final closing parenthesis in lines 111, 117, 129, and 135 to immediately following the word 'class'. They should all end with 'class).get(0);'.
Zzimon Zzimon

2014/5/3

#
Ok, with that fixed and it compiling with no errors on to the next bit now we have the list, now we just need to remove the ones in the list and replace them right?
danpost danpost

2014/5/3

#
Let us make sure we are on the same page now. You should now have this:
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;
}
I did modify the ending to only return the list if it was a matching set (list size at least 3); otherwise, they will return an empty list.
Zzimon Zzimon

2014/5/3

#
why is it java.util.List<Tile> at the horizontal one and List<Tile> at the vertical one?
danpost danpost

2014/5/3

#
Zzimon wrote...
why is it java.util.List<Tile> at the horizontal one and List<Tile> at the vertical one?
I just did not remove the 'java.util.' from the one after solving the List class not found issue.
Zzimon Zzimon

2014/5/3

#
Ok. Now I've got the same code as you just posted, now it's to the removal and replacement of the tiles in the list right?
There are more replies on the next page.
6
7
8
9
10
11
12