feat: add 4-connected Bresenham pathfinding algorithm

This commit is contained in:
2021-06-01 18:30:32 +02:00
parent 8e6f61a665
commit 2f45e2b772
2 changed files with 65 additions and 1 deletions

View File

@ -196,6 +196,34 @@ class GameLogicTest {
), new HashSet<>(Arrays.asList(result3)), "Tilted lines work correctly");
}
@Test
void testPathfind() {
int size = 20;
IntVector2 a = new IntVector2(Math.abs(randomIntegers.next() % size), Math.abs(randomIntegers.next() % size));
IntVector2 b = new IntVector2(Math.abs(randomIntegers.next() % size), Math.abs(randomIntegers.next() % size));
ArrayList<IntVector2> result = GameLogic.Bresenham4Connected(a, b);
StringBuilder sb = new StringBuilder();
for(int y = 0; y < size; y++) {
for(int x = 0; x < size; x++) {
IntVector2 pos = new IntVector2(x, y);
if(pos.equals(a)) {
sb.append("A ");
}else if(pos.equals(b)) {
sb.append("B ");
}else if(result.contains(pos)) {
sb.append("o ");
}else {
sb.append(". ");
}
}
sb.append("\n");
}
System.out.println(sb.toString());
}
// @Provide("gamestate")
// Arbitrary<GameState> gamestate() {