Skip to content

Instantly share code, notes, and snippets.

@SpyrexDE
Created November 28, 2023 12:54
Show Gist options
  • Save SpyrexDE/fe24d1b103f37294b9b7551bf8f6641b to your computer and use it in GitHub Desktop.
Save SpyrexDE/fe24d1b103f37294b9b7551bf8f6641b to your computer and use it in GitHub Desktop.
GDP Labyrinth
package de.uniwue.gdp.labyrinth;
import de.uniwue.gdp.labyrinth.examples.Examples;
import de.uniwue.gdp.labyrinth.model.Maze;
public class Explorer {
static Vector2D position = new Vector2D(1, 1);
// 0: left / west
// 1: up / north
// 2: right / east
// 3: down / south
static int lookDir = 3;
static int[][] field;
static Maze m;
public static void main(String[] args) {
m = Examples.example01();
System.out.println(exploreMaze());
}
public static String exploreMaze() {
field = new int[m.width()][m.height()];
// Initialize field with 0
for(int i = 0; i < m.width(); i++) {
for(int j = 0; j < m.height(); j++) {
field[i][j] = 0;
}
}
// Set edges to walls
for(int i = 0; i < m.width(); i++) {
field[i][0] = 1;
field[i][m.height() - 1] = 1;
}
for(int i = 0; i < m.height(); i++) {
field[0][i] = 1;
field[m.width() - 1][i] = 1;
}
// 0: empty
// 1: wall
// 2: visited
boolean finished = false;
// Spawn at 1,1, so it is already visited:
field[1][1] = 2;
while(!finished) {
finished = navigateMaze();
System.out.print(fieldToString(field));
System.out.println("Position: " + position.x + ", " + position.y + "\n");
}
return fieldToString(field);
}
static boolean navigateMaze() {
if (!checkWall(Maze.Direction.AHEAD)) {
move(Maze.Direction.AHEAD);
} else if (!checkWall(Maze.Direction.LEFT)) {
move(Maze.Direction.LEFT);
} else {
return true;
}
return false;
}
static void markWall(int direction) {
Vector2D v = coordinateAtDir(direction);
field[v.x][v.y] = 1;
System.out.println("Marked wall at " + v.x + ", " + v.y);
}
static boolean checkWall(int direction) {
if(m.isWall(direction)) {
markWall(direction);
return true;
}
return false;
}
static void move(int direction) {
Vector2D v2 = coordinateAtDir(direction);
field[v2.x][v2.y] = 2;
m.walk(direction);
position = v2;
lookDir = (lookDir + direction + 3) % 4;
}
static Vector2D coordinateAtDir(int direction) {
switch(Math.abs(direction - lookDir)) {
case 0:
System.out.println("relative:" + direction + " global:" + lookDir + position.x + ", " + position.y + "->" + position.x + ", " + (position.y - 1) + "\n");
return new Vector2D(position.x, position.y - 1);
case 1:
System.out.println("relative:" + direction + " global:" + lookDir + position.x + ", " + position.y + "->" + (position.x - 1) + ", " + position.y + "\n");
return new Vector2D(position.x - 1, position.y);
case 2:
System.out.println("relative:" + direction + " global:" + lookDir + position.x + ", " + position.y + "->" + position.x + ", " + (position.y + 1) + "\n");
return new Vector2D(position.x, position.y + 1);
case 3:
System.out.println("relative:" + direction + " global:" + lookDir + position.x + ", " + position.y + "->" + (position.x + 1) + ", " + position.y + "\n");
return new Vector2D(position.x + 1, position.y);
}
return null;
}
// Utility methods
static String fieldToString(int[][] f) {
String result = "";
for(int i = 0; i < f.length; i++) {
for(int j = 0; j < f[i].length; j++) {
if (j == position.x && i == position.y) {
switch(lookDir) {
case 0:
result += "<";
break;
case 1:
result += "^";
break;
case 2:
result += ">";
break;
case 3:
result += "v";
break;
}
continue;
}
switch(f[j][i])
{
case 0:
result += "=";
break;
case 1:
result += "#";
break;
case 2:
result += ".";
break;
}
}
result += "\n";
}
return result;
}
static class Vector2D{
int x;
int y;
public Vector2D(int x, int y) {
this.x = x;
this.y = y;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment