Cleans up project

This commit is contained in:
2020-02-14 01:01:38 +01:00
parent de6607442b
commit dfa7a142c3
126 changed files with 147 additions and 825 deletions

View File

@@ -0,0 +1,123 @@
import inf101.v18.grid.GridDirection;
import inf101.v18.grid.IArea;
import inf101.v18.grid.ILocation;
import inf101.v18.util.IGenerator;
import inf101.v18.util.generators.AreaGenerator;
import inf101.v18.util.generators.LocationGenerator;
import static org.junit.Assert.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.Test;
public class AreaRetting {
private static final int N = 10000;
private IGenerator<IArea> areaGen = new AreaGenerator();
public void allAreaProperties() {
}
public void canGoProperty(ILocation l, GridDirection dir) {
int x = l.getX() + dir.getDx();
int y = l.getY() + dir.getDy();
assertEquals(l.getArea().contains(x, y), l.canGo(dir));
try {
assertNotNull(l.go(dir));
assertTrue(String.format("Expected true: %s.canGo(%s) for %d,%d", l, dir, x, y), l.canGo(dir));
} catch (IndexOutOfBoundsException e) {
assertFalse(String.format("Expected false: %s.canGo(%s) for %d,%d", l, dir, x, y), l.canGo(dir));
}
}
public void distanceProperty(ILocation l1, ILocation l2) {
assertEquals(l1.gridDistanceTo(l2), l2.gridDistanceTo(l1));
assertEquals(l1.stepDistanceTo(l2), l2.stepDistanceTo(l1));
assertTrue(l1.gridDistanceTo(l2) <= l1.stepDistanceTo(l2));
assertTrue(l1.gridDistanceTo(l2) <= l1.geometricDistanceTo(l2));
}
public void gridLineProperty(ILocation l1, ILocation l2) {
// System.out.println(l1.toString() + " .. " + l2.toString());
List<ILocation> line = l1.gridLineTo(l2);
assertEquals(l1.gridDistanceTo(l2), line.size());
ILocation last = l1;
for (ILocation l : line) {
assertEquals(1, last.gridDistanceTo(l));
last = l;
}
assertEquals(l2, last);
}
@Test
public void gridLineTest() {
for (int i = 0; i < 10; i++) {
IArea area = areaGen.generate();
IGenerator<ILocation> lGen = new LocationGenerator(area);
for (int j = 0; j < N; j++) {
ILocation l1 = lGen.generate();
ILocation l2 = lGen.generate();
distanceProperty(l1, l2);
gridLineProperty(l1, l2);
}
}
}
@Test
public void locationsTest() {
for (int i = 0; i < 10; i++) {
IArea area = areaGen.generate();
for (ILocation l : area) {
neighboursDistProperty(l);
neighboursSymmetryProperty(l);
for (GridDirection d : GridDirection.EIGHT_DIRECTIONS)
canGoProperty(l, d);
}
}
}
public void neighboursDistProperty(ILocation loc) {
for (ILocation l : loc.allNeighbours()) {
assertEquals(1, loc.gridDistanceTo(l));
}
}
public void neighboursSymmetryProperty(ILocation loc) {
for (ILocation l : loc.allNeighbours()) {
assertTrue("My neighbour should have me as a neighbour", l.allNeighbours().contains(loc));
}
}
@Test
public void uniqueLocations() {
for (int i = 0; i < N / 10; i++) {
IArea area = areaGen.generate();
uniqueLocationsProperty(area);
}
}
public void uniqueLocationsProperty(IArea area) {
Set<ILocation> set = new HashSet<>();
for (ILocation l : area) {
assertTrue("Location should be unique: " + l, set.add(l));
}
}
@Test
public void validLocations() {
for (int i = 0; i < N / 10; i++) {
IArea area = areaGen.generate();
validLocationsProperty(area);
}
}
public void validLocationsProperty(IArea area) {
for (ILocation l : area) {
assertTrue("Location should be in area: " + l, area.contains(l));
assertTrue("Location should be in area: " + l, area.contains(l.getX(), l.getY()));
}
}
}

View File

@@ -0,0 +1,112 @@
import static org.junit.Assert.*;
import inf101.v18.rogue101.objects.IItem;
import org.junit.Test;
import inf101.v18.grid.ILocation;
import inf101.v18.rogue101.map.GameMap;
import java.util.List;
public class GameMapTest {
@Test
public void testSortedAdd() {
GameMap gameMap = new GameMap(20, 20);
ILocation location = gameMap.getLocation(10, 10);
for (int i = 0; i < 30; i++) {
gameMap.addRandomItems(location);
}
List<IItem> items = gameMap.getAll(location);
for (int i = 0; i < items.size() - 1; i++) {
assertTrue(items.get(i).compareTo(items.get(i + 1)) >= 0);
}
}
@Test
public void testGetNeighbours() {
GameMap gameMap = new GameMap(20, 20);
ILocation location = gameMap.getLocation(10, 10);
List<ILocation> neighbours = gameMap.getNeighbourhood(location, 1);
for (ILocation neighbour : neighbours) {
assertTrue(location.gridDistanceTo(neighbour) <= 1);
}
assertEquals(8, neighbours.size());
neighbours = gameMap.getNeighbourhood(location, 2);
for (ILocation neighbour : neighbours) {
assertTrue(location.gridDistanceTo(neighbour) <= 2);
}
assertEquals(24, neighbours.size());
neighbours = gameMap.getNeighbourhood(location, 3);
for (ILocation neighbour : neighbours) {
assertTrue(location.gridDistanceTo(neighbour) <= 3);
}
assertEquals(48, neighbours.size());
}
@Test
public void testNeighboursOutOfBounds() {
//This is a bit overkill, but it's meant to distinguish between kind of working and always working.
GameMap gameMap = new GameMap(20, 20);
ILocation location = gameMap.getLocation(0, 0);
List<ILocation> neighbours = gameMap.getNeighbourhood(location, 3);
for (ILocation neighbour : neighbours) {
assertTrue(location.gridDistanceTo(neighbour) <= 3);
}
assertEquals(15, neighbours.size());
location = gameMap.getLocation(19, 0);
neighbours = gameMap.getNeighbourhood(location, 3);
for (ILocation neighbour : neighbours) {
assertTrue(location.gridDistanceTo(neighbour) <= 3);
}
assertEquals(15, neighbours.size());
location = gameMap.getLocation(19, 19);
neighbours = gameMap.getNeighbourhood(location, 3);
for (ILocation neighbour : neighbours) {
assertTrue(location.gridDistanceTo(neighbour) <= 3);
}
assertEquals(15, neighbours.size());
location = gameMap.getLocation(0, 19);
neighbours = gameMap.getNeighbourhood(location, 3);
for (ILocation neighbour : neighbours) {
assertTrue(location.gridDistanceTo(neighbour) <= 3);
}
assertEquals(15, neighbours.size());
location = gameMap.getLocation(0, 10);
neighbours = gameMap.getNeighbourhood(location, 3);
for (ILocation neighbour : neighbours) {
assertTrue(location.gridDistanceTo(neighbour) <= 3);
}
assertEquals(27, neighbours.size());
location = gameMap.getLocation(19, 10);
neighbours = gameMap.getNeighbourhood(location, 3);
for (ILocation neighbour : neighbours) {
assertTrue(location.gridDistanceTo(neighbour) <= 3);
}
assertEquals(27, neighbours.size());
location = gameMap.getLocation(10, 0);
neighbours = gameMap.getNeighbourhood(location, 3);
for (ILocation neighbour : neighbours) {
assertTrue(location.gridDistanceTo(neighbour) <= 3);
}
assertEquals(27, neighbours.size());
location = gameMap.getLocation(10, 19);
neighbours = gameMap.getNeighbourhood(location, 3);
for (ILocation neighbour : neighbours) {
assertTrue(location.gridDistanceTo(neighbour) <= 3);
}
assertEquals(27, neighbours.size());
}
}

View File

@@ -0,0 +1,106 @@
import inf101.v18.grid.IGrid;
import inf101.v18.grid.ILocation;
import inf101.v18.util.IGenerator;
import inf101.v18.util.generators.GridGenerator;
import inf101.v18.util.generators.LocationGenerator;
import inf101.v18.util.generators.StringGenerator;
import static org.junit.Assert.assertEquals;
import java.util.function.Function;
import org.junit.Test;
public class GridRetting {
private static final int N = 10000;
private IGenerator<String> strGen = new StringGenerator();
private IGenerator<IGrid<String>> gridGen = new GridGenerator<String>(strGen);
public <T> void fillProperty1(IGrid<T> grid, T val) {
grid.fill(val);
for (ILocation l : grid.locations()) {
assertEquals(val, grid.get(l));
}
}
public <T> void fillProperty2(IGrid<T> grid, Function<ILocation, T> fun) {
grid.fill(fun);
for (ILocation l : grid.locations()) {
assertEquals(fun.apply(l), grid.get(l));
}
}
@Test
public void fillTest1() {
for (int i = 0; i < N / 10; i++) {
IGrid<String> grid = gridGen.generate();
String s = strGen.generate();
fillProperty1(grid, s);
}
}
@Test
public void fillTest2() {
for (int i = 0; i < N / 10; i++) {
IGrid<String> grid = gridGen.generate();
fillProperty2(grid, (l) -> l.toString());
}
}
/** A set on (x1,y1) doesn't affect a get on a different (x2,y2) */
public <T> void setGetIndependentProperty(IGrid<T> grid, ILocation l1, ILocation l2, T val) {
if (!l1.equals(l2)) {
T s = grid.get(l2);
grid.set(l1, val);
assertEquals(s, grid.get(l2));
}
}
@Test
public void setGetIndependentTest() {
for (int j = 0; j < 10; j++) {
IGrid<String> grid = gridGen.generate();
IGenerator<ILocation> lGen = new LocationGenerator(grid.getArea());
for (int i = 0; i < N; i++) {
ILocation l1 = lGen.generate();
ILocation l2 = lGen.generate();
String s = strGen.generate();
setGetIndependentProperty(grid, l1, l2, s);
}
}
}
/** get(x,y) is val after set(x, y, val) */
public <T> void setGetProperty(IGrid<T> grid, ILocation l, T val) {
grid.set(l, val);
assertEquals(val, grid.get(l));
}
/** Test that get gives back the same value after set. */
@Test
public void setGetTest() {
for (int j = 0; j < 10; j++) {
IGrid<String> grid = gridGen.generate();
IGenerator<ILocation> lGen = new LocationGenerator(grid.getArea());
for (int i = 0; i < N; i++) {
ILocation l = lGen.generate();
String s = strGen.generate();
setGetProperty(grid, l, s);
}
}
}
@Test
public void uniqueLocations() {
for (int i = 0; i < N / 10; i++) {
}
}
}

View File

@@ -0,0 +1,96 @@
import static org.junit.Assert.*;
import org.junit.Test;
import inf101.v18.grid.GridDirection;
import inf101.v18.grid.ILocation;
import inf101.v18.rogue101.game.Game;
import inf101.v18.rogue101.objects.IPlayer;
import javafx.scene.input.KeyCode;
public class PlayerTest {
@Test
public void testOutOfBounds() {
String NO_WALLS_MAP = "1 1\n"
+ "@\n";
Game game = new Game(NO_WALLS_MAP);
IPlayer player = (IPlayer) game.setCurrent(0, 0);
ILocation loc = game.getLocation();
player.keyPressed(game, KeyCode.LEFT);
assertEquals(loc, game.getLocation());
}
/*@Test
public void testActor() {
String RABBIT_MAP = "5 5\n"
+ "#####\n"
+ "#RRR#\n"
+ "#R@R#\n"
+ "#RRR#\n"
+ "#####\n";
Game game = new Game(RABBIT_MAP);
IPlayer player = (IPlayer) game.setCurrent(2, 2);
ILocation loc = game.getLocation();
player.keyPressed(game, KeyCode.LEFT);
assertEquals(loc, game.getLocation());
player.keyPressed(game, KeyCode.RIGHT);
assertEquals(loc, game.getLocation());
player.keyPressed(game, KeyCode.UP);
assertEquals(loc, game.getLocation());
player.keyPressed(game, KeyCode.DOWN);
assertEquals(loc, game.getLocation());
}*/ //TODO: Fix error "Toolkit not initialized"
@Test
public void testItem() {
String CARROT_MAP = "5 5\n"
+ "#####\n"
+ "#CCC#\n"
+ "#C@C#\n"
+ "#CCC#\n"
+ "#####\n";
Game game = new Game(CARROT_MAP);
IPlayer player = (IPlayer) game.setCurrent(2, 2);
ILocation loc = game.getLocation();
player.keyPressed(game, KeyCode.RIGHT);
assertEquals(loc.go(GridDirection.EAST), game.getLocation());
game.doTurn();
player.keyPressed(game, KeyCode.LEFT);
game.doTurn();
player.keyPressed(game, KeyCode.LEFT);
assertEquals(loc.go(GridDirection.WEST), game.getLocation());
}
@Test
public void testWallsAndKeys() { //This probably seems a little overkill, but we need to check all keys.
String EMPTY_MAP = "5 5\n"
+ "#####\n"
+ "# #\n"
+ "# @ #\n"
+ "# #\n"
+ "#####\n";
Game game = new Game(EMPTY_MAP);
IPlayer player = (IPlayer) game.setCurrent(2, 2);
ILocation loc = game.getLocation();
player.keyPressed(game, KeyCode.UP);
game.doTurn();
player.keyPressed(game, KeyCode.UP);
game.doTurn();
assertEquals(loc.go(GridDirection.NORTH), game.getLocation());
player.keyPressed(game, KeyCode.DOWN);
game.doTurn();
player.keyPressed(game, KeyCode.RIGHT);
game.doTurn();
assertEquals(loc.go(GridDirection.EAST), game.getLocation());
player.keyPressed(game, KeyCode.LEFT);
game.doTurn();
player.keyPressed(game, KeyCode.DOWN);
game.doTurn();
assertEquals(loc.go(GridDirection.SOUTH), game.getLocation());
player.keyPressed(game, KeyCode.UP);
game.doTurn();
player.keyPressed(game, KeyCode.LEFT);
assertEquals(loc.go(GridDirection.WEST), game.getLocation());
}
}