Skip to content

Instantly share code, notes, and snippets.

@echojc
Created December 23, 2021 21:59
Show Gist options
  • Save echojc/65186c6d4d43b9f2814f39c461cba358 to your computer and use it in GitHub Desktop.
Save echojc/65186c6d4d43b9f2814f39c461cba358 to your computer and use it in GitHub Desktop.
Interactive tool for manually solving the puzzle
package main
import (
"fmt"
"os"
"github.com/nsf/termbox-go"
)
func main() {
if err := termbox.Init(); err != nil {
panic(err)
}
defer termbox.Close()
cx, cy := 0, 0
moving := false
scoreY := 0
score := 0
printScore := func() {
str := fmt.Sprintf("score: %d", score)
for x, ch := range str {
termbox.SetChar(x, scoreY, ch)
}
}
for {
e := termbox.PollEvent()
switch e.Type {
case termbox.EventKey:
switch e.Ch {
case 'q':
os.Exit(0)
case '1', '2', '3', '4':
idx := e.Ch - '1'
termbox.Clear(0, 0)
for y, row := range initStates[idx] {
for x, ch := range row {
termbox.SetChar(x, y, ch)
}
scoreY = y + 2
}
score = 0
printScore()
termbox.SetCursor(cx, cy)
termbox.Flush()
case 0: // handle non character keys
updated := false
cx0, cy0 := cx, cy
switch e.Key {
case termbox.KeyArrowUp:
cy--
updated = true
case termbox.KeyArrowDown:
cy++
updated = true
case termbox.KeyArrowLeft:
cx--
updated = true
case termbox.KeyArrowRight:
cx++
updated = true
case termbox.KeyEnter:
moving = !moving
updated = true
}
if updated {
if moving && (cx0 != cx || cy0 != cy) {
c := termbox.GetCell(cx0, cy0)
termbox.SetChar(cx0, cy0, '.')
termbox.SetChar(cx, cy, c.Ch)
switch c.Ch {
case 'A':
score += 1
case 'B':
score += 10
case 'C':
score += 100
case 'D':
score += 1000
}
printScore()
}
termbox.SetCursor(cx, cy)
termbox.Flush()
}
}
}
}
}
var (
initStates = [][]string{
[]string{
"#############",
"#...........#",
"###B#A#A#D###",
" #D#C#B#C#",
" #########",
},
[]string{
"#############",
"#...........#",
"###A#B#C#D###",
" #A#B#C#D#",
" #########",
},
[]string{
"#############",
"#...........#",
"###B#A#A#D###",
" #D#C#B#A#",
" #D#B#A#C#",
" #D#C#B#C#",
" #########",
},
[]string{
"#############",
"#...........#",
"###A#B#C#D###",
" #A#B#C#D#",
" #A#B#C#D#",
" #A#B#C#D#",
" #########",
},
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment