Forbedrer Direction

Forenkler getReverseDirection ved hjelp av matematikk
Legger til metoder for venstrerotert og høyrerotert retning
Lager tester for alle de 3 nevnte metodene
This commit is contained in:
Kristian Knarvik 2020-02-23 19:43:19 +01:00
parent b6aee9111a
commit 0b2f6c78c0
2 changed files with 145 additions and 21 deletions

View File

@ -51,25 +51,30 @@ public enum Direction {
* @return The reverse direction
*/
public static Direction getReverseDirection(Direction direction) {
switch (direction) {
case NORTH:
return SOUTH;
case SOUTH:
return NORTH;
case EAST:
return WEST;
case WEST:
return EAST;
case NORTH_EAST:
return SOUTH_WEST;
case NORTH_WEST:
return SOUTH_EAST;
case SOUTH_WEST:
return NORTH_EAST;
case SOUTH_EAST:
return NORTH_WEST;
default:
throw new IllegalArgumentException("Invalid input direction.");
}
return getDirectionFromID((((direction.getDirectionID() + 3) % 8) + 1));
}
/**
* Gets the direction if something rotated to the left
*
* A rotation is assumed to be a ninety degrees rotation, so NORTH would become WEST and so on.
*
* @param direction A direction
* @return The left rotated direction
*/
public static Direction getLeftRotatedDirection(Direction direction) {
return getDirectionFromID(((((direction.getDirectionID() - 3) + 8) % 8) + 1));
}
/**
* Gets the direction if something rotated to the right
*
* A rotation is assumed to be a ninety degrees rotation, so NORTH would become EAST and so on.
*
* @param direction A direction
* @return The left rotated direction
*/
public static Direction getRightRotatedDirection(Direction direction) {
return getDirectionFromID((((direction.getDirectionID() + 1) % 8) + 1));
}
}

View File

@ -1,6 +1,5 @@
package inf112.fiasko.roborally.element_properties;
import inf112.fiasko.roborally.element_properties.Direction;
import org.junit.Test;
import java.util.ArrayList;
@ -57,4 +56,124 @@ public class DirectionTest {
}
assertEquals(list.size(), set.size());
}
@Test
public void getNorthOppositeDirection() {
assertEquals(Direction.SOUTH, Direction.getReverseDirection(Direction.NORTH));
}
@Test
public void getSouthOppositeDirection() {
assertEquals(Direction.NORTH, Direction.getReverseDirection(Direction.SOUTH));
}
@Test
public void getEastOppositeDirection() {
assertEquals(Direction.WEST, Direction.getReverseDirection(Direction.EAST));
}
@Test
public void getWestOppositeDirection() {
assertEquals(Direction.EAST, Direction.getReverseDirection(Direction.WEST));
}
@Test
public void getNorthWestOppositeDirection() {
assertEquals(Direction.SOUTH_EAST, Direction.getReverseDirection(Direction.NORTH_WEST));
}
@Test
public void getNorthEastOppositeDirection() {
assertEquals(Direction.SOUTH_WEST, Direction.getReverseDirection(Direction.NORTH_EAST));
}
@Test
public void getSouthEastOppositeDirection() {
assertEquals(Direction.NORTH_WEST, Direction.getReverseDirection(Direction.SOUTH_EAST));
}
@Test
public void getSouthWestOppositeDirection() {
assertEquals(Direction.NORTH_EAST, Direction.getReverseDirection(Direction.SOUTH_WEST));
}
@Test
public void getLeftRotatedDirectionFromNorth() {
assertEquals(Direction.WEST, Direction.getLeftRotatedDirection(Direction.NORTH));
}
@Test
public void getRightRotatedDirectionFromNorth() {
assertEquals(Direction.EAST, Direction.getRightRotatedDirection(Direction.NORTH));
}
@Test
public void getRightRotatedDirectionFromSouth() {
assertEquals(Direction.WEST, Direction.getRightRotatedDirection(Direction.SOUTH));
}
@Test
public void getLeftRotatedDirectionFromSouth() {
assertEquals(Direction.EAST, Direction.getLeftRotatedDirection(Direction.SOUTH));
}
@Test
public void getRightRotatedDirectionFromEast() {
assertEquals(Direction.SOUTH, Direction.getRightRotatedDirection(Direction.EAST));
}
@Test
public void getLeftRotatedDirectionFromEast() {
assertEquals(Direction.NORTH, Direction.getLeftRotatedDirection(Direction.EAST));
}
@Test
public void getRightRotatedDirectionFromWest() {
assertEquals(Direction.NORTH, Direction.getRightRotatedDirection(Direction.WEST));
}
@Test
public void getLeftRotatedDirectionFromWest() {
assertEquals(Direction.SOUTH, Direction.getLeftRotatedDirection(Direction.WEST));
}
@Test
public void getRightRotatedDirectionFromNorthEast() {
assertEquals(Direction.SOUTH_EAST, Direction.getRightRotatedDirection(Direction.NORTH_EAST));
}
@Test
public void getLeftRotatedDirectionFromNorthEast() {
assertEquals(Direction.NORTH_WEST, Direction.getLeftRotatedDirection(Direction.NORTH_EAST));
}
@Test
public void getRightRotatedDirectionFromNorthWest() {
assertEquals(Direction.NORTH_EAST, Direction.getRightRotatedDirection(Direction.NORTH_WEST));
}
@Test
public void getLeftRotatedDirectionFromNorthWest() {
assertEquals(Direction.SOUTH_WEST, Direction.getLeftRotatedDirection(Direction.NORTH_WEST));
}
@Test
public void getRightRotatedDirectionFromSouthEast() {
assertEquals(Direction.SOUTH_WEST, Direction.getRightRotatedDirection(Direction.SOUTH_EAST));
}
@Test
public void getLeftRotatedDirectionFromSouthEast() {
assertEquals(Direction.NORTH_EAST, Direction.getLeftRotatedDirection(Direction.SOUTH_EAST));
}
@Test
public void getRightRotatedDirectionFromSouthWest() {
assertEquals(Direction.NORTH_WEST, Direction.getRightRotatedDirection(Direction.SOUTH_WEST));
}
@Test
public void getLeftRotatedDirectionFromSouthWest() {
assertEquals(Direction.SOUTH_EAST, Direction.getLeftRotatedDirection(Direction.SOUTH_WEST));
}
}