Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/main/java/nl/tudelft/jpacman/board/Direction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* An enumeration of possible directions on a two-dimensional square grid.
*
* @author Jeroen Roosen
* @author Jeroen Roosen
*/
public enum Direction {

Expand Down Expand Up @@ -69,4 +69,22 @@ public int getDeltaX() {
public int getDeltaY() {
return deltaY;
}

/**
* @return The direction that is opposite to this direction.
*/
public Direction opposite() {
switch (this) {
case NORTH:
return SOUTH;
case SOUTH:
return NORTH;
case WEST:
return EAST;
case EAST:
return WEST;
default:
throw new IllegalStateException("Received an unknown enum value.");
}
}
}
15 changes: 1 addition & 14 deletions src/main/java/nl/tudelft/jpacman/npc/ghost/Clyde.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package nl.tudelft.jpacman.npc.ghost;

import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -57,18 +56,6 @@ public class Clyde extends Ghost {
*/
private static final int MOVE_INTERVAL = 250;

/**
* A map of opposite directions.
*/
private static final Map<Direction, Direction> OPPOSITES = new EnumMap<>(Direction.class);

static {
OPPOSITES.put(Direction.NORTH, Direction.SOUTH);
OPPOSITES.put(Direction.SOUTH, Direction.NORTH);
OPPOSITES.put(Direction.WEST, Direction.EAST);
OPPOSITES.put(Direction.EAST, Direction.WEST);
}

/**
* Creates a new "Clyde", a.k.a. "Pokey".
*
Expand Down Expand Up @@ -105,7 +92,7 @@ public Optional<Direction> nextAiMove() {
if (path != null && !path.isEmpty()) {
Direction direction = path.get(0);
if (path.size() <= SHYNESS) {
return Optional.ofNullable(OPPOSITES.get(direction));
return Optional.of(direction.opposite());
}
return Optional.of(direction);
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/nl/tudelft/jpacman/board/DirectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,20 @@ void testNorth() {
Direction north = Direction.valueOf("NORTH");
assertThat(north.getDeltaY()).isEqualTo(-1);
}

/**
* The correct opposite is returned.
*/
@Test
void testOpposite() {
assertThat(Direction.NORTH.opposite()).isEqualTo(Direction.SOUTH);
}

/**
* The opposite of the opposite is the original.
*/
@Test
void testOppositeTwice() {
assertThat(Direction.WEST.opposite().opposite()).isEqualTo(Direction.WEST);
}
}