This commit is contained in:
cjdenio committed 2026-09-09 18:00:16 -05:00
1 parent 7bb0f00e71
commit 6109f70a45
6 files changed
+222 -113

No files matched your search

+7 -100
View File
@@ -6,9 +6,8 @@ import (
"fmt"
"log"
"os"
"slices"
"unicode"
"github.com/cjdenio/crossword/game"
"github.com/cjdenio/crossword/puz"
"golang.org/x/term"
)
@@ -51,7 +50,7 @@ func main() {
defer term.Restore(int(os.Stdin.Fd()), termState)
state := State{
state := game.State{
Puzzle: puzzle,
PuzzleState: []rune(puzzle.State),
SelectedClue: puzzle.Clues[0],
@@ -70,7 +69,7 @@ func main() {
}
}
uiHeight := state.RenderUI()
uiHeight := state.RenderUI(os.Stdout)
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
@@ -93,9 +92,8 @@ func main() {
scanner.Scan()
buffer := scanner.Bytes()
state.LastKeySequence = fmt.Sprintf("%v", buffer)
if buffer[0] == 3 {
exited := state.HandleInput(buffer)
if exited {
saveFile, err := state.CreateSaveFile()
if err == nil {
err = os.WriteFile(filename+".save", saveFile, fileInfo.Mode())
@@ -108,104 +106,13 @@ func main() {
state.Goodbye = true
fmt.Printf("\r\x1b[%dA", uiHeight)
fmt.Print("\x1b[J")
state.RenderUI()
state.RenderUI(os.Stdout)
return
}
if buffer[0] == ' ' {
switch state.SelectedClue.Direction {
case puz.DirectionAcross:
if state.Puzzle.Cells[state.SelectedCell][1] != nil {
state.SelectedClue = state.Puzzle.Cells[state.SelectedCell][1]
}
case puz.DirectionDown:
if state.Puzzle.Cells[state.SelectedCell][0] != nil {
state.SelectedClue = state.Puzzle.Cells[state.SelectedCell][0]
}
}
}
if buffer[0] >= 0x61 && buffer[0] <= 0x7a {
cellWasFilled := state.PuzzleState[state.SelectedCell] != '-'
state.PuzzleState[state.SelectedCell] = unicode.ToUpper(rune(buffer[0]))
i := slices.Index(state.SelectedClue.Cells, state.SelectedCell)
if !cellWasFilled {
// jump to next unfilled cell in clue
for x := i + 1; x < len(state.SelectedClue.Cells); x++ {
if state.PuzzleState[state.SelectedClue.Cells[x]] == '-' {
state.SelectedCell = state.SelectedClue.Cells[x]
break
}
}
} else {
// jump to next cell
if i < len(state.SelectedClue.Cells)-1 {
state.SelectedCell = state.SelectedClue.Cells[i+1]
}
}
if state.GridFilled() {
if state.CheckPuzzle() {
state.SolveState = 2
} else {
state.SolveState = 1
}
} else {
state.SolveState = 0
}
}
if buffer[0] == 0x7f {
// is there a filled cell underneath the cursor?
if state.PuzzleState[state.SelectedCell] != '-' {
state.PuzzleState[state.SelectedCell] = '-'
} else {
i := slices.Index(state.SelectedClue.Cells, state.SelectedCell)
if i > 0 {
state.PuzzleState[state.SelectedClue.Cells[i-1]] = '-' // clear the previous cell
state.SelectedCell = state.SelectedClue.Cells[i-1]
}
}
}
if buffer[0] == '\r' {
state.NextWord()
}
if buffer[0] == '~' {
state.PreviousWord()
}
if string(buffer[0:2]) == "\x1b[" {
if (buffer[2] == 68 || buffer[2] == 67) && state.SelectedClue != nil && state.SelectedClue.Direction == puz.DirectionDown {
state.SelectedClue = state.Puzzle.Cells[state.SelectedCell][0]
} else if (buffer[2] == 65 || buffer[2] == 66) && state.SelectedClue != nil && state.SelectedClue.Direction == puz.DirectionAcross {
state.SelectedClue = state.Puzzle.Cells[state.SelectedCell][1]
} else {
switch buffer[2] {
case 68: // left
state.MoveCursor(3)
case 67: // right
state.MoveCursor(1)
case 65: // up
state.MoveCursor(0)
case 66: // down
state.MoveCursor(2)
}
switch state.SelectedClue.Direction {
case puz.DirectionAcross:
state.SelectedClue = state.Puzzle.Cells[state.SelectedCell][0]
case puz.DirectionDown:
state.SelectedClue = state.Puzzle.Cells[state.SelectedCell][1]
}
}
}
fmt.Printf("\r\x1b[%dA", uiHeight)
fmt.Print("\x1b[J")
uiHeight = state.RenderUI()
uiHeight = state.RenderUI(os.Stdout)
}
}