Created
April 10, 2023 18:25
-
-
Save maxandersen/253268a2b683921306e9e796f877d425 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///usr/bin/env jbang "$0" "$@" ; exit $? | |
//JAVA 17+ | |
import static java.lang.System.*; | |
// clears the screen using ANSI escape codes | |
static void clear_output() { | |
out.print("\033[H\033[2J"); | |
out.flush(); | |
} | |
var maze = new char[][] { | |
"######################".toCharArray(), | |
"#### ### # # ### #".toCharArray(), | |
"# # ## # # # # ".toCharArray(), | |
"# # # # ##### # #".toCharArray(), | |
"# # # ######## # # #".toCharArray(), | |
"# # # # # # # #".toCharArray(), | |
"# # ## # # # #".toCharArray(), | |
"######################".toCharArray() | |
}; | |
var x = 2; | |
var y = 1; | |
while(true) { | |
println("###" + (""+maze[x-1][y]).repeat(3) + "###"); | |
println("###" + (""+maze[x-1][y]).repeat(3) + "###"); | |
println((""+maze[x][y-1]).repeat(3) + " o " + (""+maze[x][y+1]).repeat(3)); | |
println("###" + (""+maze[x+1][y]).repeat(3) + "###"); | |
println("###" + (""+maze[x+1][y]).repeat(3) + "###"); | |
var move = console().readLine("w: up, s: down, a: left, d:right (h: map) - "); | |
clear_output(); | |
switch(move) { | |
case "w": // up | |
if (maze[x - 1][y] == '#') { | |
println("Illegal move"); | |
} else { | |
x -= 1; | |
} | |
break; | |
case "s": // down | |
if (maze[x + 1][y] == '#') { | |
println("Illegal move"); | |
} else { | |
x += 1; | |
} | |
break; | |
case "a": // left | |
if (maze[x][y - 1] == '#') { | |
println("Illegal move"); | |
} else { | |
y -= 1; | |
} | |
break; | |
case "d": // right | |
if (maze[x][y + 1] == '#') { | |
println("Illegal move"); | |
} else { | |
y += 1; | |
} | |
break; | |
case "h": // show map | |
maze[x][y] = 'o'; | |
for (var row : maze) { | |
println(new String(row)); | |
} | |
maze[x][y] = ' '; | |
console().readLine("Press enter to continue"); | |
clear_output(); | |
break; | |
case "q": // quit | |
break; | |
default: | |
println("Unknown move"); | |
} | |
if(y == maze[0].length - 1) { | |
println("You finished"); | |
break; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment