Created
December 23, 2021 21:59
-
-
Save echojc/65186c6d4d43b9f2814f39c461cba358 to your computer and use it in GitHub Desktop.
Interactive tool for manually solving the puzzle
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
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