I am currently working on a program that shows the movements of a critter in a field. The critters are marked by a char (example: 'M' for mouse) and each have their own way of movement. The method that I am having trouble with is a method that should make my Mouse critter move.
- They should be moving in diagonals (NORTH then EAST or WEST. EAST, then NORTH or SOUTH) a certain amount of times and then changes diagonal directions.
- The amount of times the mice move diagonally is controlled by
myCount
. ifmyCount
is less thanmyTarget
, then the mice should move diagonally. The mice change direction whenmyCount
is equal tomyTarget
. ThenmyCount
is set back to 0. I want my mice to move in a diagonal 8 times before changing diagonal directions.
- The amount of times the mice move diagonally is controlled by
- However, instead my mice sometimes go diagonally for a little bit, but they always end up moving up then down or left then right over and over about 5 times.
Here is what my program's output looks like. (The S are stones that don't move, they are considered another critter in this case).
The interface:
public interface Critter
{
public static final int NORTH = 0;
public static final int WEST = 1;
public static final int SOUTH = 2;
public static final int EAST = 3;
public static final int CENTER = 4;
public char getChar();
public int getMove(CritterInfo theInfo);
}
The abstract class:
public abstract class AbstractCritter implements Critter
{
private char myCritterName;
public AbstractCritter(final char theChar)
{
myCritterName = theChar;
}
public char getChar()
{
return myCritterName;
}
}
The method I am having issues with:
import java.util.*;
public class Mouse extends AbstractCritter
{
private int myDirection;
private int myCount;
private boolean myFirst;
private int myTarget;
private Random myRand;
private int myCheck;
public Mouse()
{
super('M');
myRand = new Random();
myDirection = myRand.nextInt(4);
myCount = 0;
myFirst = false;
myTarget = 16;
myCheck = 0;
}
public int getMove(CritterInfo theInfo)
{
if (myCount == myTarget)
{
myCount = 0;
int direction = myRand.nextInt(4);
myFirst = false;
}
int direction = myDirection;
int direction2 = myRand.nextInt(4);
myCount++;
if (!myFirst)
{
myCheck = myRand.nextInt(2);
if (myDirection == NORTH || myDirection == SOUTH)
{
if (myCheck == 0)
{
direction2 = EAST;
}
else
{
direction2 = WEST;
}
}
else if (myDirection == EAST || myDirection == WEST)
{
if (myCheck == 0)
{
direction2 = NORTH;
}
else
{
direction2 = SOUTH;
}
}
myFirst = true;
}
if (myCount % 2 == 0)
{
return direction;
}
else
{
return direction2;
}
}
}