Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Animals move random if moveTowards did nothing
Browse files Browse the repository at this point in the history
  • Loading branch information
Spurberino committed Dec 20, 2023
1 parent 118f75b commit cb8d521
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
5 changes: 4 additions & 1 deletion src/main/Animal.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ protected void move(Location location, World world) {
*
* @param location - The location to move towards
*/
protected void moveTowards(Location location, World world) {
protected boolean moveTowards(Location location, World world) {
int targetX = location.getX();
int targetY = location.getY();
int thisX = world.getLocation(this).getX();
Expand All @@ -197,6 +197,9 @@ protected void moveTowards(Location location, World world) {
} else if (targetX < thisX && targetY == thisY) {
move(new Location(thisX - 1, thisY), world);
}
if(world.getTile(location) != world.getLocation(this)){
return true;
} else return false;
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/main/Predator.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ public boolean attackForFood(World world) {
for (Location location : world.getSurroundingTiles(vision)) {
if (world.getTile(location) instanceof Animal &&
!world.getTile(location).getClass().equals(this.getClass())) {
moveTowards(location, world);
System.out.println(this.getClass().getSimpleName() + " Move to attack");
return true;
if(moveTowards(location, world)){
moveTowards(location, world);
System.out.println(this.getClass().getSimpleName() + " Move to attack");
return true;
} else return false;
}
}
return false;
Expand Down
13 changes: 11 additions & 2 deletions src/main/Rabbit.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ public void act(World world) {
home.addAnimal(this, world);
return;
}
moveTowards(world.getLocation(home), world);
return;
if(moveTowards(world.getLocation(home), world)){
return;
} else {
move(getRandomEmptySurroundingTile(world), world);
return;
}
}

// move away from predator
Expand Down Expand Up @@ -112,6 +116,11 @@ public boolean moveAwayFromPredator(World world) {
for (Location location : world.getSurroundingTiles(vision)) {
if (world.getTile(location) instanceof Predator) {
if(moveAway(location, world)){
System.out.println("Rabbit move away from predator");
return true;
} else {
move(getRandomEmptySurroundingTile(world), world);
System.out.println("Rabbit move random");
return true;
}
}
Expand Down
22 changes: 16 additions & 6 deletions src/main/Wolf.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ public void act(World world) {
System.out.println("Wolf enters home");
return;
}
moveTowards(world.getLocation(myPack.getHome(world)), world);
System.out.println("Wolf move to home");
return;
if(moveTowards(world.getLocation(myPack.getHome(world)), world)){
System.out.println("Wolf moved towards home");
return;
} else {
move(getRandomEmptySurroundingTile(world), world);
System.out.println("Wolf move random because could not get to home");
return;
}
}

// if starving
Expand All @@ -77,9 +82,14 @@ public void act(World world) {
}
}
if (!closeToLeader) {
moveTowards(world.getLocation(myPack.getLeader()), world);
System.out.println("Wolf moved closer to leader");
return;
if(moveTowards(world.getLocation(myPack.getLeader()), world)){
System.out.println("Wolf moved closer to leader");
return;
} else {
move(getRandomEmptySurroundingTile(world), world);
System.out.println("Wolf move random because could not get to leader");
return;
}
}
}
}
Expand Down

0 comments on commit cb8d521

Please sign in to comment.