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

2015/5/25

Creating Basic AI?

1
2
MeinKraftBubba MeinKraftBubba

2015/5/25

#
I'm pretty new to greenfoot, and for my first big project I'm making a Pacman style game. I've hit a roadblock when coding the ghosts, however. I have two actors that interact, MovePad and Ghost. I need the MovePad to detect when a ghost passes over it, run a method that checks which paths are blocked, and return a variable that tells the ghost to move left, right, up, or down. I've already made the movepad detect when a ghost passes it, but I don't know the code to make MovePad change a variable in Ghost's code. What's the most basic way to do this?
danpost danpost

2015/5/26

#
It would probably be best to have the ghosts detect the movepad and call a method in the MovePad class to return the direction to proceed. Something like:
1
2
MovePad mp = (MovePad) getOneObjectAtOffset(0, 0, MovePad.class);
if (mp != null) setDirection(mp.getDirection(currentDirection);
The 'setDirection' method in the Ghost class does just that -- set the new direction of the ghost to the direction returned by the movepad. The 'currentDirection' field in the Ghost class holds what it suggests -- the current direction that the ghost is moving (the movepad would need that information so as not to have the ghosts reverse directions). The 'getDirection' method in the MovePad class gets the current direction of the ghost and returns a direction to proceed. You probably already have a field for the current direction of the ghosts (or fields that indicate the current movement directions along both the horizontal and the vertical); however, you will probably have to add the various methods that I incorporated into line 2 above.
MeinKraftBubba MeinKraftBubba

2015/5/26

#
This is exactly what I needed. I already have something equivalent to getDirection, I just didn't know how to send the updated variable over. Thanks a ton! By the way, if I encounter another problem while I'm trying to make the ghosts function properly, should I put another response in this thread, or make a new one? I'd rather ask than assume what the proper etiquette is.
MeinKraftBubba MeinKraftBubba

2015/5/26

#
Well, I've implemented the previous solution the best I could, but I've hit another bump. First, I'll post my code. My comments should explain what I've done easily enough. This is for the Ghost, who is Subclass "Blinky"
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import greenfoot.*;
 
public class Blinky extends Ghost
{
    //Create Image Variables for Animation
    GreenfootImage ghost01;
    GreenfootImage ghost02;
    GreenfootImage ghost03;
    GreenfootImage ghost04;
    GreenfootImage ghost05;
    GreenfootImage ghost06;
    GreenfootImage ghost07;
    GreenfootImage ghost08;
    GreenfootImage ghost09;
    GreenfootImage ghost10;
    //Create animation variables
    int frame;
    int imageCounter;
    int frameDelay;
     
    //Create movement variables
    int speed;
    public int direction;
 
     
    public Blinky()
    {
        //Construct Ghost upon spawning
        //Assign image files to image variables
        ghost01 = new GreenfootImage("baseGhost01.fw.png");
        ghost02 = new GreenfootImage("baseGhost02.fw.png");
        ghost03 = new GreenfootImage("baseGhost03.fw.png");
        ghost04 = new GreenfootImage("baseGhost04.fw.png");
        ghost05 = new GreenfootImage("baseGhost05.fw.png");
        ghost06 = new GreenfootImage("baseGhost06.fw.png");
        ghost07 = new GreenfootImage("baseGhost07.fw.png");
        ghost08 = new GreenfootImage("baseGhost08.fw.png");
        ghost09 = new GreenfootImage("baseGhost09.fw.png");
        ghost10 = new GreenfootImage("baseGhost10.fw.png");
         
         
        //Assign initial values to animation variables
        frame = 1;
        imageCounter = 0;
        frameDelay = 2;
         
        //Assign values to movement variables
        speed = 2;
        direction = 1;
         
        //Set initial image
        setImage(ghost01);
    }
    public void act()
    {
        // Add your action code here.
        animate();
        //checkKeyPress();
        checkMove();
    }
    public void checkKeyPress()
    {
        //For testing purposes
        if(Greenfoot.isKeyDown("up"))
        {
            direction = 4;
        }
        if(Greenfoot.isKeyDown("down"))
        {
            direction = 2;
        }
        if(Greenfoot.isKeyDown("left"))
        {
            direction = 3;
        }
        if(Greenfoot.isKeyDown("right"))
        {
            direction = 1;
        }
    }
    public void checkMove()
    {
        checkDirection();
        if(direction == 4)
        {
            //UP
            setLocation(getX(), getY() - speed);
        }
        if(direction == 2)
        {
            //DOWN
            setLocation(getX(), getY() + speed);
        }
        if(direction == 3)
        {
            //LEFT
            setLocation(getX() - speed, getY());
        }
        if(direction == 1)
        {
            //RIGHT
            setLocation(getX() + speed, getY());
        }
    }
     
    //Code as suggested previously
    public void setDirection()
    {
       direction = BlinkyPad.currentDirection;             
    }
    public void checkDirection()
    {
        BlinkyPad bp = (BlinkyPad) getOneObjectAtOffset(0, 0, BlinkyPad.class);
        if(bp != null)
        {
            setDirection(bp.getDirection(currentDirection));
        }
    }
     
     
    public void animate()
    {
        //Increase Image Counter to run animation       
        imageCounter++;
         
        //Run animation Code when the Image Counter is divisble by the frame delay
        if(imageCounter % frameDelay == 0)
        {
            if(frame == 1)
            {
                setImage(ghost01);
                frame = 2;
            }
            else if(frame == 2)
            {
                setImage(ghost02);
                frame = 3;
            }
            else if(frame == 3)
            {
                setImage(ghost03);
                frame = 4;
            }
            else if(frame == 4)
            {
                setImage(ghost04);
                frame = 5;
            }
            else if(frame == 5)
            {
                setImage(ghost05);
                frame = 6;
            }
            else if(frame == 6)
            {
                setImage(ghost06);
                frame = 7;
            }
            else if(frame == 7)
            {
                setImage(ghost07);
                frame = 8;
            }
            else if(frame == 8)
            {
                setImage(ghost08);
                frame = 9;
            }
            else if(frame == 9)
            {
                setImage(ghost09);
                frame = 10;
            }
            else if(frame == 10)
            {
                setImage(ghost10);
                frame = 1;
            }
        }
     
     
    }
}
This bit is the BlinkyPad, a subclass of MovePad. I have the subclasses because I eventually hope to add four different ghosts, who would interact with their own MovePads to follow their unique patrol paths.
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class BlinkyPad here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class BlinkyPad extends MovePad
{
    int currentDirection;
    boolean d1; //Right
    boolean d2; //Down
    boolean d3; //Left
    boolean d4; //UP
    public BlinkyPad()
    
 
    }
 
    public void getDirection()
    {
        checkWalls();
        createRandomDirection();
    }
 
    public void checkWalls()
    {
        //Check Walls to the left
        Wall lw = getOneObjectAtOffset(-15, 0, Wall.class);
        if(lw != null)
        {
            d3 = false;
        }
        else
        {
            d3 = true;
        }
        //Check Walls to the right
        Wall rw = getOneObjectAtOffset(15, 0, Wall.class);
        if(rw != null)
        {
            d1 = false;
        }
        else
        {
            d1 = true;
        }
        //Check Wall above
        Wall aw = getOneObjectAtOffset(0, -15, Wall.class);
        if(aw != null)
        {
            d4 = false;
        }
        else
        {
            d4 = true;
        }
        //Check Wall below
        Wall bw = getOneObjectAtOffset(0, 15, Wall.class);
        if(bw != null)
        {
            d2 = false;
        }
        else
        {
            d2 = true;
        }
 
    }
 
    public void createRandomDirection()
    {
        currentDirection = Greenfoot.getRandomNumber(4) +1;
        checkWallVariables();
    }
    //This is the best idea I had to re-run the code until an acceptable variable popped up
    public void checkWallVariables()
    {
 
        if(d1 == false && currentDirection == 1)
        {
            createRandomDirection();
        }
        else if(d2 == false && currentDirection == 2)
        {
            createRandomDirection();
        }
        else if(d3 == false && currentDirection == 3)
        {
            createRandomDirection();
        }
        else if(d4 == false && currentDirection == 4)
        {
            createRandomDirection();
        }
    }
}
The problem is that when I try to compile, I receive an error that says "non-static variable currentDirection cannot be referenced from a static context". I'm still new to greenfoot, so I don't exactly know what this error means, or how to fix it. What am I doing wrong?
Super_Hippo Super_Hippo

2015/5/26

#
Line 109 in the Blinky class:
1
direction = BlinkyPad.currentDirection;
'currentDirection' in the BlinkyPad class is a public, but not static int. That means, every BlinkyPad object which is created, has its own 'currentDirection' variable. This means, you can't do 'BlinkyPad.currentDirection' because this wants to get a variable from a class. You can only use static methods and variables directly from a class, other methods and variables have to be used on an object. If there is only one and always one object of BlinkyPad in the world, you could change the line to the following:
1
direction = ((BlinkyPad)(getWorld().getObjects(BlinkyPad.class).get(0)).currentDirection;
danpost danpost

2015/5/26

#
Your 'getDirection' method in the BlinkyPad class and the 'setDirection' method in the Blinky class are not coded correctly as I suggested. Both these methods were given with parameter values (both methods were to have an int values passed to them) and the 'getDirection' method was to return an int value as well. Therefore, they should start as follows:
1
2
3
public void setDirection(int dir)
// and
public int getDirection(int currentDirection)
The only thing the 'setDirection' method should do is set 'currentDirection' to the value of thee 'dir' parameter. The 'getDirection' method does several things. First, its parameter value will be the current direction of the ghost currently on the pad. The value should be used in the method to prevent the ghost from reversing directions (normally, pacman ghosts are not supposed to do an about face at intersections; they only continue forward along one of the available branches). The method then will choose one of those "forward" directions and return that direction to the calling statement.
MeinKraftBubba MeinKraftBubba

2015/5/27

#
I'm sorry, but I'm not entirely sure I follow. Both of those methods would go into BlinkyPad, correct? setDirection would look something like this:
1
2
3
4
public void setDirection(int dir)
{
     currentDirection = Blinky.direction;
}
Then, the getDirection would look something like:
1
2
3
4
5
6
7
8
9
10
public int getDirection(int currentDirection)
{
     if(currentDirection == 2)
     {
          d2 = false;
     }
     //Repeat this for all directions
 
     //Check Walls and generate new direction.
}
I still highly doubt that is what I'm supposed to do, and I would appreciate more help with this. I have virtually no experience with Actor interactions, this area was skimmed over by my teacher in favor of more basic things. Consider anything that I didn't put into my code previously as foreign knowledge to me.
danpost danpost

2015/5/27

#
danpost wrote...
The 'setDirection' method in the Ghost class... The 'getDirection' method in the MovePad class...
'Ghost' would be 'Blinky' and 'MovePad' would be 'BlinkyPad' in this case.
MeinKraftBubba wrote...
1
2
3
4
public void setDirection(int dir)
{
     currentDirection = Blinky.direction;
}
The current direction of the Blinky object is given in the 'dir' variable. Again 'Blinky' is a class name -- it does not refer to any actor object created from the class. Besides, it is not the class that has a direction -- it is the actors created from the class that can have a direction state. Replace 'Blinky.direction' with 'dir'. As far as your 'getDirection' method, I think you are on the right track; however, the last statement in the method needs to be in the following form:
1
return <int>;
The 'int' value is the new generated direction and can be any expression whose end result is an int value in the appropriate range for the setting of the 'currentDirection' field of the Blinky class. To help in reducing the amount of code you are using, the following:
1
2
3
4
5
6
7
8
9
Wall lw = getOneObjectAtOffset(-15, 0, Wall.class);
if(lw != null)
{
    d3 = false;
}
else
{
    d3 = true;
}
can be simplified to this:
1
d3 = getOneObjectAtOffset(-15, 0, Wall.class) == null;
and to incorporate the current direction, something like this:
1
d3 = getOneObjectAtOffset(-15, 0, Wall.class) == null && currentDirection != 3;
danpost danpost

2015/5/27

#
danpost wrote...
The 'setDirection' method in the Ghost class... The 'getDirection' method in the MovePad class...
'Ghost' would be 'Blinky' and 'MovePad' would be 'BlinkyPad' in this case.
MeinKraftBubba wrote...
1
2
3
4
public void setDirection(int dir)
{
     currentDirection = Blinky.direction;
}
The new generated direction for the Blinky object is given in the 'dir' variable. Again 'Blinky' is a class name -- it does not refer to any actor object created from the class. Besides, it is not the class that has a direction -- it is the actors created from the class that can have a direction state. Replace 'Blinky.direction' with 'dir'. As far as your 'getDirection' method, I think you are on the right track; however, the last statement in the method needs to be in the following form:
1
return <int>;
The 'int' value is the new generated direction and can be any expression whose end result is an int value in the appropriate range for the setting of the 'currentDirection' field of the Blinky class. To help in reducing the amount of code you are using, the following:
1
2
3
4
5
6
7
8
9
Wall lw = getOneObjectAtOffset(-15, 0, Wall.class);
if(lw != null)
{
    d3 = false;
}
else
{
    d3 = true;
}
can be simplified to this:
1
d3 = getOneObjectAtOffset(-15, 0, Wall.class) == null;
and to incorporate the current direction, something like this:
1
d3 = getOneObjectAtOffset(-15, 0, Wall.class) == null && currentDirection != 3;
MeinKraftBubba MeinKraftBubba

2015/5/27

#
Okay, so I've implemented the above, as far as I can tell. Here's the updated code: Blinky:
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
public int dir;
 
public Blinky()
    {
        //Construct Ghost upon spawning
                 
        //Assign values to movement variables
        dir = 1;
    }   
 public void act()
    {
        //checkKeyPress();
        checkMove();
    }
 
public void checkKeyPress()
    {
        //For testing purposes
        if(Greenfoot.isKeyDown("up"))
        {
            dir = 4;
        }
        if(Greenfoot.isKeyDown("down"))
        {
            dir = 2;
        }
        if(Greenfoot.isKeyDown("left"))
        {
            dir = 3;
        }
        if(Greenfoot.isKeyDown("right"))
        {
            dir = 1;
        }
    }
 
public void checkMove()
    {
        checkDirection();
        if(dir == 4)
        {
            //UP
            setLocation(getX(), getY() - speed);
        }
        if(dir == 2)
        {
            //DOWN
            setLocation(getX(), getY() + speed);
        }
        if(dir == 3)
        {
            //LEFT
            setLocation(getX() - speed, getY());
        }
        if(dir == 1)
        {
            //RIGHT
            setLocation(getX() + speed, getY());
        }
    }
     
    //Code as suggested previously
    public void setDirection(int dir)
    {
        currentDirection = dir;
    }
    public void checkDirection()
    {
        BlinkyPad bp = (BlinkyPad) getOneObjectAtOffset(0, 0, BlinkyPad.class);
        if(bp != null)
        {
            setDirection(bp.getDirection(currentDirection));
        }
    }
BlinkyPad:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int currentDirection;
    boolean d1; //Right
    boolean d2; //Down
    boolean d3; //Left
    boolean d4; //UP
    public BlinkyPad()
    
 
    }
 
    public void getDirection()
    {
        d1 = getOneObjectAtOffset(15, 0, Wall.class) == null && currentDirection != 1;
        d2 = getOneObjectAtOffset(0, -15, Wall.class) == null && currentDirection != 2;
        d3 = getOneObjectAtOffset(-15, 0, Wall.class) == null && currentDirection != 3;
        d4 = getOneObjectAtOffset(0, 15, Wall.class) == null && currentDirection != 4;
         
        return <int>;
    }
So, as of this point, what's next? I apologize if this is something that you feel should be obvious, but I really appreciate the help. I feel like in the last two days this moved forward more than almost a week with my teacher attempting to help me.
danpost danpost

2015/5/27

#
It is difficult to keep up with what you have when you omit the top part of the class. You are now showing one field for 'Blinky' -- the 'dir' field, which probably should not be there (though, it is hard to tell because of the omission). Also, I cannot tell if you deleted methods you previously had in the BlinkyPad class, such as the 'checkWallVariables' and 'createRandomDirection'. About these methods, however -- you seem to have each one calling the other. This could be very troublesome in that there may be no exiting of this infinite looping. The '<int>' at line 18 needs to be replaced with a method call or code that generates a new random direction. I said that the statement needs to be in that form -- it was not intended as something to be copy/pasted into your code; the statement needs to be modified:
danpost wrote...
The 'int' value is the new generated direction and can be any expression whose end result is an int value in the appropriate range for the setting of the 'currentDirection' field of the Blinky class.
danpost danpost

2015/5/27

#
You may want to count the number of directions (referring to d1, d2, d3 and d4) that are true and generate a random number using that count. Then count, again, the true values up to the generated number and return that value. As an alternative to the above, you could use a 'while' loop to generate random directions until the direction field corresponding to the generated value is true.
MeinKraftBubba MeinKraftBubba

2015/5/27

#
Sorry about the omission, I was trimming what I though was repetitive (like Blinky's animation code, which works fine). Here's the full code for both. Blinky:
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import greenfoot.*;
 
public class Blinky extends Ghost
{
    //Create Image Variables for Animation
    GreenfootImage ghost01;
    GreenfootImage ghost02;
    GreenfootImage ghost03;
    GreenfootImage ghost04;
    GreenfootImage ghost05;
    GreenfootImage ghost06;
    GreenfootImage ghost07;
    GreenfootImage ghost08;
    GreenfootImage ghost09;
    GreenfootImage ghost10;
    //Create animation variables
    int frame;
    int imageCounter;
    int frameDelay;
     
    //Create movement variables
    int speed;
    public int dir;
 
     
    public Blinky()
    {
        //Construct Ghost upon spawning
        //Assign image files to image variables
        ghost01 = new GreenfootImage("baseGhost01.fw.png");
        ghost02 = new GreenfootImage("baseGhost02.fw.png");
        ghost03 = new GreenfootImage("baseGhost03.fw.png");
        ghost04 = new GreenfootImage("baseGhost04.fw.png");
        ghost05 = new GreenfootImage("baseGhost05.fw.png");
        ghost06 = new GreenfootImage("baseGhost06.fw.png");
        ghost07 = new GreenfootImage("baseGhost07.fw.png");
        ghost08 = new GreenfootImage("baseGhost08.fw.png");
        ghost09 = new GreenfootImage("baseGhost09.fw.png");
        ghost10 = new GreenfootImage("baseGhost10.fw.png");
         
         
        //Assign initial values to animation variables
        frame = 1;
        imageCounter = 0;
        frameDelay = 2;
         
        //Assign values to movement variables
        speed = 2;
        dir = 1;
         
        //Set initial image
        setImage(ghost01);
    }
    public void act()
    {
        animate();
        //checkKeyPress();
        checkMove();
    }
    public void checkKeyPress()
    {
        //For testing purposes
        if(Greenfoot.isKeyDown("up"))
        {
            dir = 4;
        }
        if(Greenfoot.isKeyDown("down"))
        {
            dir = 2;
        }
        if(Greenfoot.isKeyDown("left"))
        {
            dir = 3;
        }
        if(Greenfoot.isKeyDown("right"))
        {
            dir = 1;
        }
    }
    public void checkMove()
    {
        checkDirection();
        if(dir == 4)
        {
            //UP
            setLocation(getX(), getY() - speed);
        }
        if(dir == 2)
        {
            //DOWN
            setLocation(getX(), getY() + speed);
        }
        if(dir == 3)
        {
            //LEFT
            setLocation(getX() - speed, getY());
        }
        if(dir == 1)
        {
            //RIGHT
            setLocation(getX() + speed, getY());
        }
    }
     
    //Code as suggested previously
    public void setDirection(int dir)
    {
        currentDirection = dir;
    }
    public void checkDirection()
    {
        BlinkyPad bp = (BlinkyPad) getOneObjectAtOffset(0, 0, BlinkyPad.class);
        if(bp != null)
        {
            setDirection(bp.getDirection(currentDirection));
        }
    }
     
     
    public void animate()
    {
        //Increase Image Counter to run animation       
        imageCounter++;
         
        //Run animation Code when the Image Counter is divisble by the frame delay
        if(imageCounter % frameDelay == 0)
        {
            if(frame == 1)
            {
                setImage(ghost01);
                frame = 2;
            }
            else if(frame == 2)
            {
                setImage(ghost02);
                frame = 3;
            }
            else if(frame == 3)
            {
                setImage(ghost03);
                frame = 4;
            }
            else if(frame == 4)
            {
                setImage(ghost04);
                frame = 5;
            }
            else if(frame == 5)
            {
                setImage(ghost05);
                frame = 6;
            }
            else if(frame == 6)
            {
                setImage(ghost06);
                frame = 7;
            }
            else if(frame == 7)
            {
                setImage(ghost07);
                frame = 8;
            }
            else if(frame == 8)
            {
                setImage(ghost08);
                frame = 9;
            }
            else if(frame == 9)
            {
                setImage(ghost09);
                frame = 10;
            }
            else if(frame == 10)
            {
                setImage(ghost10);
                frame = 1;
            }
        }
     
     
    }
}
In Blinky Pad, I did delete most of it, believing it conflicted with what had been added. I most likely misunderstood your suggestons.
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
import greenfoot.*; 
 
public class BlinkyPad extends MovePad
{
    int currentDirection;
    boolean d1; //Right
    boolean d2; //Down
    boolean d3; //Left
    boolean d4; //UP
    public BlinkyPad()
    
 
    }
 
    public void getDirection()
    {
        d1 = getOneObjectAtOffset(15, 0, Wall.class) == null && currentDirection != 1;
        d2 = getOneObjectAtOffset(0, -15, Wall.class) == null && currentDirection != 2;
        d3 = getOneObjectAtOffset(-15, 0, Wall.class) == null && currentDirection != 3;
        d4 = getOneObjectAtOffset(0, 15, Wall.class) == null && currentDirection != 4;
         
        return <int>;
    }
 
    
}
EDIT: I'm sorry, I didn't realize that I posted the code for Blinky twice. As to counting directions, would it appear similar to this? getRandomNumber() probably appear similar to how it did above.
1
2
3
4
5
6
7
8
9
//Repeat for all directions
if(d3 == false && randomNumber == 3)
{
     getRandomNumber();
}
else
{
     currentDirection == 3;
}
Super_Hippo Super_Hippo

2015/5/27

#
Question: These d1...d4 booleans should say if the movement in the direction is possible, right?
1
d1 = getOneObjectAtOffset(15, 0, Wall.class) == null && currentDirection != 1;
Shouldn't it be '!=3' at the end? Like don't turn 180 degrees. As it is now, it prevents to move forward. Or did I misunderstand that?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public int getDirections(int currentDirection)
{
    boolean[] d =
    {
        getOneObjectAtOffset(15, 0, Wall.class) == null && currentDirection != 3,
        getOneObjectAtOffset(0, -15, Wall.class) == null && currentDirection != 4,
        getOneObjectAtOffset(-15, 0, Wall.class) == null && currentDirection != 1,
        getOneObjectAtOffset(0, 15, Wall.class) == null && currentDirection != 2
    };
    while (true)
    {
        int r = Greenfoot.getRandomNumber(4);
        if (d[r]) return r;
    }
}
danpost danpost

2015/5/27

#
Super_Hippo wrote...
Question: These d1...d4 booleans should say if the movement in the direction is possible, right?
1
d1 = getOneObjectAtOffset(15, 0, Wall.class) == null && currentDirection != 1;
Shouldn't it be '!=3' at the end? Like don't turn 180 degrees. As it is now, it prevents to move forward. Or did I misunderstand that? < Code Omitted >
(a) from what was given, I would say you are right; (b) I do not believe that you misunderstood;
There are more replies on the next page.
1
2