diff --git a/src/main/java/inf112/fiasko/roborally/abstractions/WallType.java b/src/main/java/inf112/fiasko/roborally/abstractions/WallType.java new file mode 100644 index 0000000..8eea294 --- /dev/null +++ b/src/main/java/inf112/fiasko/roborally/abstractions/WallType.java @@ -0,0 +1,43 @@ +package inf112.fiasko.roborally.abstractions; + +/** + * This enum represents all possible wall types + */ +public enum WallType { + WALL_NORMAL (1), + WALL_CORNER (2), + WALL_LASER_SINGLE (3), + WALL_LASER_DOUBLE (4); + + private final int wallTypeID; + + /** + * Constructor to let a wall type be represented by a numerical identifier + * @param wallTypeID

The numerical identifier assigned to the wall type

+ */ + WallType(int wallTypeID) { + this.wallTypeID = wallTypeID; + } + + /** + * Gets the numerical id used for alternate identification of a wall type + * @return

The numerical id of the wall type

+ */ + public int getWallTypeID() { + return this.wallTypeID; + } + + /** + * Gets a wall type value from its numerical representation + * @param wallTypeID

The numerical representation of a wall type

+ * @return

The enum value representing the wall type, or null if the id is invalid

+ */ + public static WallType getWallTypeFromID(int wallTypeID) { + for (WallType type : WallType.values()) { + if (type.wallTypeID == wallTypeID) { + return type; + } + } + return null; + } +} diff --git a/src/test/java/inf112/fiasko/roborally/WallTypeTest.java b/src/test/java/inf112/fiasko/roborally/WallTypeTest.java new file mode 100644 index 0000000..70f085f --- /dev/null +++ b/src/test/java/inf112/fiasko/roborally/WallTypeTest.java @@ -0,0 +1,61 @@ +package inf112.fiasko.roborally; + +import inf112.fiasko.roborally.abstractions.TileType; +import inf112.fiasko.roborally.abstractions.WallType; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class WallTypeTest { + + @Test + public void getWallTypeIDForWallNormal() { + assertEquals(1, WallType.WALL_NORMAL.getWallTypeID()); + } + + @Test + public void getWallTypeFromWallNormalID() { + assertEquals(WallType.WALL_NORMAL, WallType.getWallTypeFromID(1)); + } + + @Test + public void getWallTypeIDForWallLaserSingle() { + assertEquals(3, WallType.WALL_LASER_SINGLE.getWallTypeID()); + } + + @Test + public void getWallTypeFromWallLaserSingleID() { + assertEquals(WallType.WALL_LASER_SINGLE, WallType.getWallTypeFromID(3)); + } + + @Test + public void allWallTypesIDConversionToIDAndBack() { + for (WallType type : WallType.values()) { + assertEquals(type, WallType.getWallTypeFromID(type.getWallTypeID())); + } + } + + @Test + public void invalidWallTypeIDReturnsNull() { + assertNull(TileType.getTileTypeFromID(-1)); + } + + @Test + public void allWallsHaveUniqueId() { + /* This test is also done implicitly by the allTileTypesIDConversionToIDAndBack test, but that test may fail + even if this test passes, so this test is needed for clarity. */ + Set set = new HashSet<>(); + List list = new ArrayList<>(); + for (WallType type : WallType.values()) { + set.add(type.getWallTypeID()); + list.add(type.getWallTypeID()); + } + assertEquals(list.size(), set.size()); + } +}