This site requires JavaScript, please enable it in your browser!
Greenfoot back
CavemanWrath
CavemanWrath wrote ...

2017/12/17

How To Expand Commands

CavemanWrath CavemanWrath

2017/12/17

#
I am trying to run a command through an object that would be in the list that I have created in a different object of the same class, but I have absolutely no understanding of how that might be done. Here is the code for the class in which I am trying to do this in:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import greenfoot.*;
import java.util.*;
public class Gifts extends Actor
{
    int amountTouching; //counts how many teachers it is touching
    int touchingZero; //looks for the adjacent gifts that are not next to a bomb
    List<Gifts> gifts = new ArrayList<Gifts> ();
    List<Gifts> gifts2 = new ArrayList<Gifts> ();
    public void act()
    {
        amountTouching = getNeighbours(51, true, Teacher.class).size();
        if (Greenfoot.mouseClicked(this))
        {
            checkProximity();
        }
    }   
    public void checkProximity()
    {
        if (amountTouching == 0)
        {
            setImage("empty.png");
            checkGifts();
        }
        else
        {
            setImage(amountTouching + "adjacent.png");
        }
    }
    public void checkGifts()
    {
        gifts = getNeighbours(51, true, Gifts.class);
        int i = gifts.size();
        int checkNumber = gifts.size() - 1;
        while (i > 0)
        {  
            if (gifts.get(checkNumber).amountTouching == 0)
            {
                gifts.get(checkNumber).setImage("empty.png");
            }
            else
            {
                gifts.get(checkNumber).setImage(gifts.get(checkNumber).amountTouching + "adjacent.png");
            }
            checkNumber--;
            i--;
        }
    }
}
Thanks!
Super_Hippo Super_Hippo

2017/12/17

#
(Btw, classes should have the singular as the name, so Gift and not Gifts.) Wouldn't it be the best to call the 'checkProximity' method on all neighbors? So they will again check their neighbors when their amountTouching is 0.
1
2
3
4
5
6
7
public void checkGifts()
{
    for (Gift g : getNeighbours(51, true, Gift.class))
    {
        g.checkProximity();
    }
}
But you should have a boolean if the gift was already "opened" or it will create an infinite loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private boolean opened = false;
 
 
//beginning of checkProximity method
opened = true;
 
 
public void checkGifts()
{
    for (Gift g : getNeighbours(51, true, Gift.class))
    {
        if (!g.opened) g.checkProximity();
    }
}
I guess the game will be like Minesweeper with Teachers as Mines. I doubt that you need the Teacher class. You could have a variable in the Gift class to check if there is a Teacher in it and then you can check all neighbor gifts to see how many have a teacher in it.
CavemanWrath CavemanWrath

2017/12/17

#
That worked like a charm. Thanks
You need to login to post a reply.