This commit is contained in:
RochesterX
2026-04-01 11:11:43 -04:00
parent 79b6a51d49
commit db77330754

View File

@@ -30,9 +30,10 @@ the grid.
Controls: Controls:
- Cursor movement: WASD, HJKL, or Arrow Keys - Cursor movement: WASD, HJKL, or Arrow Keys
- Wall placement: Space - Place wall: Space
- Origin movement: [ - Place start: [
- Target movement: ] - Place goal: ]
- Restrict valid movement options: number keys (directions based on number pad)
"""#) """#)
print("Enter the grid width: ", terminator: "") print("Enter the grid width: ", terminator: "")
@@ -71,6 +72,8 @@ Controls:
cursorPosition += Vector2(x: 0, y: 1) cursorPosition += Vector2(x: 0, y: 1)
default: default:
switch key { switch key {
case .Quit:
quit()
case .Start: case .Start:
start = cursorPosition start = cursorPosition
case .Finish: case .Finish:
@@ -231,21 +234,21 @@ Controls:
var output: String = "" var output: String = ""
output += draw["nw"] ?? "?" output += draw["nw"] ?? "?"
output += String(repeating: draw["n"] ?? "?", count: on[0].count) output += String(repeating: draw["n"] ?? "?", count: on[0].count + 2)
output += draw["ne"] ?? "?" output += draw["ne"] ?? "?"
output += "\n" output += "\n"
for y: Int in 0..<on.count { for y: Int in 0..<on.count {
output += draw["w"] ?? "?" output += "\(draw["w"] ?? "?") "
for x: Int in 0..<on[0].count { for x: Int in 0..<on[0].count {
output += cursorPosition == Vector2(x: x, y: y) ? "\u{1B}[7m" : "\u{1B}[27m" output += cursorPosition == Vector2(x: x, y: y) ? "\u{1B}[7m" : "\u{1B}[27m"
let position: Vector2 = Vector2(x: x, y: y) let position: Vector2 = Vector2(x: x, y: y)
if position == path.first { if position == path.first {
output += draw["start"] ?? "?" output += "\(Color.Yellow)\(draw["start"] ?? "?")\(Color.Default)"
continue continue
} }
if position == path.last { if position == path.last {
output += draw["goal"] ?? "?" output += "\(Color.Cyan)\(draw["goal"] ?? "?")\(Color.Default)"
continue continue
} }
if path.contains(position) { if path.contains(position) {
@@ -275,33 +278,41 @@ Controls:
default: default:
arrow = "?" arrow = "?"
} }
output += arrow output += "\(Color.Green)\(arrow)\(Color.Default)"
continue continue
} }
output += on[y][x] == 1 ? draw["wall"] ?? "?" : draw["empty"] ?? "?" output += on[y][x] == 1 ? draw["wall"] ?? "?" : draw["empty"] ?? "?"
} }
output += "\u{1B}[27m" output += "\u{1B}[27m"
output += draw["e"] ?? "?" output += " \(draw["e"] ?? "?")"
output += "\n" output += "\n"
} }
output += draw["sw"] ?? "?" output += draw["sw"] ?? "?"
output += String(repeating: draw["s"] ?? "?", count: on[0].count) output += String(repeating: draw["s"] ?? "?", count: on[0].count + 2)
output += draw["se"] ?? "?" output += draw["se"] ?? "?"
output += "\n\n" output += "\n\n"
output += draw["nw"] ?? "?"
output += String(repeating: draw["n"] ?? "?", count: 7)
output += draw["ne"] ?? "?"
output += "\n\(draw["w"] ?? "?")"
output += directions[0] ? "" : " " output += directions[0] ? "" : " "
output += directions[1] ? "" : " " output += directions[1] ? "" : " "
output += directions[2] ? "" : " " output += directions[2] ? "" : " "
output += "\n" output += " \(draw["e"] ?? "?") Path length: \(path.count <= 2 ? "\(Color.Red)Not found\(Color.Default)" : "\(Color.Green)\(String(path.count - 1))\(Color.Default)")\n\(draw["w"] ?? "?")"
output += directions[3] ? "" : " " output += directions[3] ? "" : " "
output += " " output += " "
output += directions[4] ? "" : " " output += directions[4] ? "" : " "
output += "\n" output += " \(draw["e"] ?? "?")\n\(draw["w"] ?? "?")"
output += directions[5] ? "" : " " output += directions[5] ? "" : " "
output += directions[6] ? "" : " " output += directions[6] ? "" : " "
output += directions[7] ? "" : " " output += directions[7] ? "" : " "
output += " \(draw["e"] ?? "?") Press [Q] to quit\n"
output += draw["sw"] ?? "?"
output += String(repeating: draw["s"] ?? "?", count: 7)
output += draw["se"] ?? "?"
print(draw["clear"] ?? "?", terminator: "") print(draw["clear"] ?? "?", terminator: "")
print(draw["home"] ?? "?", terminator: "") print(draw["home"] ?? "?", terminator: "")
@@ -337,6 +348,7 @@ enum Key {
case SW case SW
case W case W
case Quit
case Unknown case Unknown
} }
@@ -421,6 +433,8 @@ struct Terminal {
return .S return .S
case "3": case "3":
return .SE return .SE
case "q":
return .Quit
default: default:
return .Unknown return .Unknown
} }
@@ -473,3 +487,15 @@ struct Vector2: Hashable {
} }
} }
struct Color {
static let Black = "\u{1B}[90m"
static let Red = "\u{1B}[91m"
static let Green = "\u{1B}[92m"
static let Yellow = "\u{1B}[93m"
static let Blue = "\u{1B}[94m"
static let Magenta = "\u{1B}[95m"
static let Cyan = "\u{1B}[96m"
static let White = "\u{1B}[97m"
static let Default = "\u{1B}[39m"
}