mirror of
https://github.com/cjdenio/crossword.git
synced 2026-10-03 11:06:19 +00:00
Compare commits
13
Commits
644ce0d273
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d50b572fb5 | ||
|
|
7b35f86e32 | ||
|
|
419f0ae3cf | ||
|
|
b918c5be23 | ||
|
|
60d1bd644c | ||
|
|
50be9ab605 | ||
|
|
b7c64dabe3 | ||
|
|
c139490dc2 | ||
|
|
21f9caec34 | ||
|
|
5d62a4b611 | ||
|
|
176c3997cd | ||
|
|
4f92cdd941 | ||
|
|
143020a21d |
No files matched your search
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 Caleb Denio
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -1,6 +1,6 @@
|
||||
all: bin/crossword bin/crossword-ssh
|
||||
|
||||
bin/crossword: main.go puz/puz.go game/game.go
|
||||
go build -o $@ .
|
||||
bin/crossword-ssh: ssh/main.go puz/puz.go game/game.go
|
||||
bin/crossword: cmd/main.go puz/*.go game/game.go
|
||||
go build -o $@ ./cmd
|
||||
bin/crossword-ssh: ssh/main.go puz/*.go game/game.go
|
||||
go build -o $@ ./ssh
|
||||
@@ -1,8 +1,16 @@
|
||||
Very silly terminal-based crossword solver. It can play .puz / .ipuz files from a variety of sources, including [Crosshare](https://crosshare.org).
|
||||
|
||||
- Requirements: [Go](https://go.dev)
|
||||
- Build: `make`
|
||||
- Usage: `./bin/crossword <file>.puz`
|
||||
|
||||

|
||||
|
||||
---
|
||||
|
||||
todo
|
||||
|
||||
- correctly calculate uiHeight with multi-line clues
|
||||
- timer
|
||||
- reveal grid/word
|
||||
- .puz checksums
|
||||
- fix shift+enter / shift+tab in other terminals
|
||||
- clear grid
|
||||
- rebus
|
||||
+19
-11
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
@@ -12,6 +13,11 @@ import (
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
func getTermWidth() int {
|
||||
w, _, _ := term.GetSize(int(os.Stdin.Fd()))
|
||||
return w
|
||||
}
|
||||
|
||||
func main() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
@@ -20,10 +26,11 @@ func main() {
|
||||
}()
|
||||
|
||||
debugMode := flag.Bool("debug", false, "")
|
||||
dumpMode := flag.Bool("dump", false, "")
|
||||
flag.Parse()
|
||||
|
||||
filename := flag.Arg(0)
|
||||
file, err := os.ReadFile(filename)
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@@ -33,11 +40,17 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
puzzle, err := puz.ParsePuz(file)
|
||||
puzzle, err := puz.LoadPuzzle(file)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if dumpMode != nil && *dumpMode {
|
||||
json.NewEncoder(os.Stdout).Encode(puzzle)
|
||||
fmt.Print("\r\n")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Print("\x1b[?25l")
|
||||
|
||||
defer func() {
|
||||
@@ -50,12 +63,7 @@ func main() {
|
||||
|
||||
defer term.Restore(int(os.Stdin.Fd()), termState)
|
||||
|
||||
state := game.State{
|
||||
Puzzle: puzzle,
|
||||
PuzzleState: []rune(puzzle.State),
|
||||
SelectedClue: puzzle.Clues[0],
|
||||
SelectedCell: puzzle.Clues[0].Cells[0],
|
||||
}
|
||||
state := game.NewState(puzzle)
|
||||
if debugMode != nil {
|
||||
state.DebugMode = *debugMode
|
||||
}
|
||||
@@ -69,7 +77,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
uiHeight := state.RenderUI(os.Stdout)
|
||||
uiHeight := state.RenderUI(os.Stdout, getTermWidth())
|
||||
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
||||
@@ -105,13 +113,13 @@ func main() {
|
||||
state.SelectedCell = -1
|
||||
fmt.Printf("\r\x1b[%dA", uiHeight)
|
||||
fmt.Print("\x1b[J")
|
||||
state.RenderUI(os.Stdout)
|
||||
state.RenderUI(os.Stdout, getTermWidth())
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("\r\x1b[%dA", uiHeight)
|
||||
fmt.Print("\x1b[J")
|
||||
|
||||
uiHeight = state.RenderUI(os.Stdout)
|
||||
uiHeight = state.RenderUI(os.Stdout, getTermWidth())
|
||||
}
|
||||
}
|
||||
+242
-31
@@ -12,15 +12,90 @@ import (
|
||||
"github.com/cjdenio/crossword/puz"
|
||||
)
|
||||
|
||||
type SolveState int
|
||||
|
||||
const (
|
||||
Unsolved SolveState = iota
|
||||
FilledNotSolved
|
||||
Solved
|
||||
)
|
||||
|
||||
type UIState int
|
||||
|
||||
const (
|
||||
StateGame UIState = iota
|
||||
StateRebusConfirmation
|
||||
StateMenu
|
||||
StateRevealMenu
|
||||
StateCheckMenu
|
||||
)
|
||||
|
||||
type State struct {
|
||||
Puzzle *puz.Puzzle
|
||||
PuzzleState []rune
|
||||
SelectedCell int
|
||||
SelectedClue *puz.Clue
|
||||
Goodbye bool
|
||||
LastKeySequence string
|
||||
DebugMode bool
|
||||
SolveState int
|
||||
SolveState SolveState
|
||||
CheckState []rune
|
||||
UIState UIState
|
||||
}
|
||||
|
||||
func RenderWithLineWrap(s string, width int) (out string, lines int) {
|
||||
builder := strings.Builder{}
|
||||
runes := []rune(s)
|
||||
|
||||
counter := 0
|
||||
lines = 1
|
||||
|
||||
for i := 0; i < len(runes); i++ {
|
||||
char := runes[i]
|
||||
|
||||
if char == '\n' {
|
||||
builder.WriteString("\r\n")
|
||||
lines++
|
||||
counter = 0
|
||||
continue
|
||||
}
|
||||
|
||||
if char == '\r' && i < len(runes)-1 && runes[i+1] == '\n' {
|
||||
builder.WriteString("\r\n")
|
||||
lines++
|
||||
counter = 0
|
||||
i++
|
||||
continue
|
||||
}
|
||||
|
||||
builder.WriteRune(char)
|
||||
counter++
|
||||
|
||||
if counter >= width && i < len(runes)-1 {
|
||||
lines++
|
||||
builder.WriteString("\r\n")
|
||||
counter = 0
|
||||
}
|
||||
}
|
||||
|
||||
builder.WriteString("\r\n")
|
||||
|
||||
out = builder.String()
|
||||
return
|
||||
}
|
||||
|
||||
func NewState(puzzle *puz.Puzzle) *State {
|
||||
state := StateGame
|
||||
if puzzle.HasRebus {
|
||||
state = StateRebusConfirmation
|
||||
}
|
||||
return &State{
|
||||
Puzzle: puzzle,
|
||||
PuzzleState: []rune(puzzle.State),
|
||||
SelectedClue: puzzle.Clues[0],
|
||||
SelectedCell: puzzle.Clues[0].Cells[0],
|
||||
CheckState: make([]rune, len(puzzle.State)),
|
||||
UIState: state,
|
||||
}
|
||||
}
|
||||
|
||||
func (state *State) MoveCursor(direction int) {
|
||||
@@ -179,7 +254,7 @@ func (state *State) GridFilled() bool {
|
||||
return !slices.Contains(state.PuzzleState, '-')
|
||||
}
|
||||
|
||||
func (state *State) CheckPuzzle() bool {
|
||||
func (state *State) PuzzleSolved() bool {
|
||||
for i, cell := range state.PuzzleState {
|
||||
if cell == '.' {
|
||||
continue
|
||||
@@ -191,10 +266,59 @@ func (state *State) CheckPuzzle() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (state *State) RenderUI(w io.Writer) int {
|
||||
selectedClueCells := []int{}
|
||||
if state.SelectedClue != nil {
|
||||
selectedClueCells = state.SelectedClue.Cells
|
||||
func (state *State) CheckPuzzle() {
|
||||
result := make([]rune, len(state.PuzzleState))
|
||||
|
||||
for i, cell := range state.PuzzleState {
|
||||
if cell == '.' || cell == '-' {
|
||||
continue
|
||||
}
|
||||
if cell == rune(state.Puzzle.Solution[i]) {
|
||||
result[i] = 'y'
|
||||
} else {
|
||||
result[i] = 'n'
|
||||
}
|
||||
}
|
||||
|
||||
state.CheckState = result
|
||||
}
|
||||
|
||||
func (state *State) CheckWord(clue *puz.Clue) {
|
||||
for i, cellIdx := range clue.Cells {
|
||||
cellState := state.PuzzleState[cellIdx]
|
||||
if cellState == '-' || cellState == '.' {
|
||||
continue
|
||||
}
|
||||
|
||||
if cellState == rune(clue.Solution[i]) {
|
||||
state.CheckState[cellIdx] = 'y'
|
||||
} else {
|
||||
state.CheckState[cellIdx] = 'n'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (state *State) RevealWord(clue *puz.Clue) {
|
||||
for i, cellIdx := range clue.Cells {
|
||||
state.PuzzleState[cellIdx] = rune(clue.Solution[i])
|
||||
state.CheckState[cellIdx] = 'y'
|
||||
}
|
||||
}
|
||||
|
||||
func (state *State) RevealPuzzle() {
|
||||
for i, cell := range state.PuzzleState {
|
||||
if cell == '.' {
|
||||
continue
|
||||
}
|
||||
state.PuzzleState[i] = rune(state.Puzzle.Solution[i])
|
||||
state.CheckState[i] = 'y'
|
||||
}
|
||||
}
|
||||
|
||||
func (state *State) RenderUI(w io.Writer, width int) int {
|
||||
if state.UIState == StateRebusConfirmation {
|
||||
fmt.Fprint(w, "This puzzle contains a rebus. This tool does not support rebuses.\r\nPress 'y' to continue anyway, or press any other key to exit.\r\n")
|
||||
return 2
|
||||
}
|
||||
|
||||
uiHeight := 0
|
||||
@@ -203,24 +327,45 @@ func (state *State) RenderUI(w io.Writer) int {
|
||||
uiHeight += 2
|
||||
fmt.Fprintf(w, "AUTHOR: %s\r\n", state.Puzzle.Author)
|
||||
uiHeight += 1
|
||||
fmt.Fprint(w, RenderPuzzle(state.PuzzleState, state.Puzzle.Width, state.Puzzle.Height, selectedClueCells, state.SelectedCell)+"\r\n")
|
||||
fmt.Fprint(w, state.RenderPuzzle()+"\r\n")
|
||||
uiHeight += state.Puzzle.Height + 3
|
||||
if state.SelectedClue != nil {
|
||||
directionStr := "down"
|
||||
if state.SelectedClue.Direction == puz.DirectionAcross {
|
||||
fmt.Fprintf(w, "%d-across: %s\r\n\r\n", state.SelectedClue.Number, state.SelectedClue.Clue)
|
||||
} else {
|
||||
fmt.Fprintf(w, "%d-down: %s\r\n\r\n", state.SelectedClue.Number, state.SelectedClue.Clue)
|
||||
directionStr = "across"
|
||||
}
|
||||
|
||||
clueStr, clueLines := RenderWithLineWrap(fmt.Sprintf("%d-%s: %s", state.SelectedClue.Number, directionStr, state.SelectedClue.Clue), width)
|
||||
w.Write([]byte(clueStr))
|
||||
uiHeight += clueLines
|
||||
io.WriteString(w, "\r\n")
|
||||
uiHeight += 1
|
||||
}
|
||||
if state.Puzzle.Copyright != "" {
|
||||
fmt.Fprintf(w, "%s\r\n\r\n", state.Puzzle.Copyright)
|
||||
uiHeight += 2
|
||||
}
|
||||
fmt.Fprintf(w, "%s\r\n", state.Puzzle.Copyright)
|
||||
|
||||
switch state.UIState {
|
||||
case StateGame:
|
||||
fmt.Fprint(w, AnsiDimmed("[enter]/[tab]: next word | [ctrl+a]: menu | [ctrl+c]: exit\r\n"))
|
||||
uiHeight += 1
|
||||
case StateMenu:
|
||||
fmt.Fprint(w, AnsiDimmed("[c]: check puzzle/word | [r]: reveal puzzle/word | [x]: clear grid | [q]: exit menu\r\n"))
|
||||
uiHeight += 1
|
||||
case StateCheckMenu:
|
||||
fmt.Fprint(w, AnsiDimmed("[w]: check word | [p]: check puzzle | [q]: exit menu\r\n"))
|
||||
uiHeight += 1
|
||||
case StateRevealMenu:
|
||||
fmt.Fprint(w, AnsiDimmed("[w]: reveal word | [p]: reveal puzzle | [q]: exit menu\r\n"))
|
||||
uiHeight += 1
|
||||
}
|
||||
|
||||
switch state.SolveState {
|
||||
case 1:
|
||||
case FilledNotSolved:
|
||||
fmt.Fprintf(w, "\r\n%s\r\n", AnsiRed("The puzzle was filled, but at least 1 letter is incorrect..."))
|
||||
uiHeight += 2
|
||||
case 2:
|
||||
case Solved:
|
||||
fmt.Fprintf(w, "\r\n%s\r\n", AnsiGreen("The puzzle was solved!"))
|
||||
uiHeight += 2
|
||||
}
|
||||
@@ -230,11 +375,6 @@ func (state *State) RenderUI(w io.Writer) int {
|
||||
uiHeight += 2
|
||||
}
|
||||
|
||||
if state.Goodbye {
|
||||
fmt.Fprint(w, "\r\nsee ya\r\n")
|
||||
uiHeight += 2
|
||||
}
|
||||
|
||||
return uiHeight
|
||||
}
|
||||
|
||||
@@ -260,17 +400,22 @@ func AnsiRed(s string) string {
|
||||
return fmt.Sprintf("\x1b[31m%s\x1b[0m", s)
|
||||
}
|
||||
|
||||
func RenderPuzzle(puzzle []rune, width, height int, selectedClueCells []int, selectedCell int) string {
|
||||
func (state *State) RenderPuzzle() string {
|
||||
selectedClueCells := []int{}
|
||||
if state.SelectedClue != nil {
|
||||
selectedClueCells = state.SelectedClue.Cells
|
||||
}
|
||||
|
||||
b := strings.Builder{}
|
||||
|
||||
b.WriteRune('┌')
|
||||
for range (width * 2) + 1 {
|
||||
for range (state.Puzzle.Width * 2) + 1 {
|
||||
b.WriteRune('─')
|
||||
}
|
||||
b.WriteString("┐\r\n")
|
||||
|
||||
for index, char := range puzzle {
|
||||
if index%width == 0 {
|
||||
for index, char := range state.PuzzleState {
|
||||
if index%state.Puzzle.Width == 0 {
|
||||
b.WriteString("│ ")
|
||||
}
|
||||
|
||||
@@ -284,17 +429,26 @@ func RenderPuzzle(puzzle []rune, width, height int, selectedClueCells []int, sel
|
||||
cell = string(char)
|
||||
}
|
||||
|
||||
if selectedCell == index {
|
||||
if state.SelectedCell == index {
|
||||
cell = AnsiInverted(cell)
|
||||
} else if slices.Contains(selectedClueCells, index) {
|
||||
cell = AnsiWhiteBackgrounded(cell)
|
||||
}
|
||||
|
||||
if len(state.CheckState) == len(state.PuzzleState) {
|
||||
switch state.CheckState[index] {
|
||||
case 'y':
|
||||
cell = AnsiGreen(cell)
|
||||
case 'n':
|
||||
cell = AnsiRed(cell)
|
||||
}
|
||||
}
|
||||
|
||||
b.WriteString(cell)
|
||||
|
||||
if (index+1)%width == 0 {
|
||||
if (index+1)%state.Puzzle.Width == 0 {
|
||||
b.WriteString(" │\r\n")
|
||||
} else if char == '.' && puzzle[index+1] == '.' {
|
||||
} else if char == '.' && state.PuzzleState[index+1] == '.' {
|
||||
b.WriteRune(0x2588)
|
||||
} else {
|
||||
// inefficient
|
||||
@@ -307,7 +461,7 @@ func RenderPuzzle(puzzle []rune, width, height int, selectedClueCells []int, sel
|
||||
}
|
||||
|
||||
b.WriteRune('└')
|
||||
for range (width * 2) + 1 {
|
||||
for range (state.Puzzle.Width * 2) + 1 {
|
||||
b.WriteRune('─')
|
||||
}
|
||||
b.WriteString("┘\r\n")
|
||||
@@ -365,6 +519,15 @@ func (state *State) SwitchDirections() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (state *State) ClearGrid() {
|
||||
for i, cell := range state.PuzzleState {
|
||||
if cell != '.' {
|
||||
state.PuzzleState[i] = '-'
|
||||
state.CheckState[i] = '\x00'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (state *State) HandleInput(buffer []byte) (exited bool) {
|
||||
if len(buffer) == 0 {
|
||||
return false
|
||||
@@ -376,6 +539,53 @@ func (state *State) HandleInput(buffer []byte) (exited bool) {
|
||||
return true
|
||||
}
|
||||
|
||||
switch state.UIState {
|
||||
case StateRebusConfirmation:
|
||||
if buffer[0] == 'y' {
|
||||
state.UIState = StateGame
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
case StateMenu:
|
||||
switch buffer[0] {
|
||||
case 'c':
|
||||
state.UIState = StateCheckMenu
|
||||
case 'r':
|
||||
state.UIState = StateRevealMenu
|
||||
case 'x':
|
||||
state.ClearGrid()
|
||||
state.UIState = StateGame
|
||||
default:
|
||||
state.UIState = StateGame
|
||||
}
|
||||
return false
|
||||
case StateCheckMenu:
|
||||
switch buffer[0] {
|
||||
case 'w':
|
||||
state.CheckWord(state.SelectedClue)
|
||||
case 'p':
|
||||
state.CheckPuzzle()
|
||||
}
|
||||
state.UIState = StateGame
|
||||
return false
|
||||
case StateRevealMenu:
|
||||
switch buffer[0] {
|
||||
case 'w':
|
||||
state.RevealWord(state.SelectedClue)
|
||||
case 'p':
|
||||
state.RevealPuzzle()
|
||||
return true
|
||||
}
|
||||
state.UIState = StateGame
|
||||
return false
|
||||
}
|
||||
|
||||
if buffer[0] == '\x01' {
|
||||
state.UIState = StateMenu
|
||||
return false
|
||||
}
|
||||
|
||||
if buffer[0] == ' ' {
|
||||
state.SwitchDirections()
|
||||
}
|
||||
@@ -383,6 +593,7 @@ func (state *State) HandleInput(buffer []byte) (exited bool) {
|
||||
if buffer[0] >= 0x61 && buffer[0] <= 0x7a {
|
||||
cellWasFilled := state.PuzzleState[state.SelectedCell] != '-'
|
||||
state.PuzzleState[state.SelectedCell] = unicode.ToUpper(rune(buffer[0]))
|
||||
state.CheckState[state.SelectedCell] = 0x00
|
||||
|
||||
i := slices.Index(state.SelectedClue.Cells, state.SelectedCell)
|
||||
|
||||
@@ -402,14 +613,14 @@ func (state *State) HandleInput(buffer []byte) (exited bool) {
|
||||
}
|
||||
|
||||
if state.GridFilled() {
|
||||
if state.CheckPuzzle() {
|
||||
state.SolveState = 2
|
||||
if state.PuzzleSolved() {
|
||||
state.SolveState = Solved
|
||||
return true
|
||||
} else {
|
||||
state.SolveState = 1
|
||||
state.SolveState = FilledNotSolved
|
||||
}
|
||||
} else {
|
||||
state.SolveState = 0
|
||||
state.SolveState = Unsolved
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+183
@@ -0,0 +1,183 @@
|
||||
package puz
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type iPuzDimensions struct {
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
}
|
||||
type iPuzClue struct {
|
||||
Number int `json:"number"`
|
||||
Clue string `json:"clue"`
|
||||
}
|
||||
type iPuzFile struct {
|
||||
Kind []string `json:"kind"`
|
||||
Title string `json:"title"`
|
||||
Author string `json:"author"`
|
||||
Copyright string `json:"copyright"`
|
||||
Puzzle [][]any `json:"puzzle"`
|
||||
Solution [][]string `json:"solution"`
|
||||
Dimensions iPuzDimensions `json:"dimensions"`
|
||||
Clues map[string][]iPuzClue `json:"clues"`
|
||||
}
|
||||
|
||||
func unwrapCell(cell any) (any, error) {
|
||||
switch c := cell.(type) {
|
||||
case string:
|
||||
if c == "#" {
|
||||
return "#", nil
|
||||
} else if i, err := strconv.Atoi(c); err == nil {
|
||||
return i, nil
|
||||
} else {
|
||||
return nil, errors.New("invalid string")
|
||||
}
|
||||
case float64:
|
||||
return int(c), nil
|
||||
case map[string]any:
|
||||
if unwrappedCell, ok := c["cell"]; ok {
|
||||
return unwrapCell(unwrappedCell) // recursion babyyyyyy
|
||||
}
|
||||
}
|
||||
|
||||
return nil, errors.New("unknown type")
|
||||
}
|
||||
|
||||
func ParseIPuz(file []byte) (*Puzzle, error) {
|
||||
var parsed iPuzFile
|
||||
err := json.Unmarshal(file, &parsed)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(parsed.Kind) == 0 || !slices.ContainsFunc(parsed.Kind, func(k string) bool {
|
||||
return strings.HasPrefix(k, "http://ipuz.org/crossword")
|
||||
}) {
|
||||
return nil, errors.New("ipuz file is not a crossword")
|
||||
}
|
||||
|
||||
puzzle := new(Puzzle)
|
||||
|
||||
puzzle.RebusCells = make(map[int]string)
|
||||
|
||||
puzzle.Title = parsed.Title
|
||||
puzzle.Author = parsed.Author
|
||||
puzzle.Copyright = parsed.Copyright
|
||||
|
||||
puzzle.Width = parsed.Dimensions.Width
|
||||
puzzle.Height = parsed.Dimensions.Height
|
||||
|
||||
puzzle.ClueCount = len(parsed.Clues["Across"]) + len(parsed.Clues["Down"])
|
||||
|
||||
clueToCellMap := make(map[int][2]int)
|
||||
|
||||
state := strings.Builder{}
|
||||
for y, row := range parsed.Puzzle {
|
||||
for x, cell := range row {
|
||||
unwrappedCell, err := unwrapCell(cell)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch c := unwrappedCell.(type) {
|
||||
case string:
|
||||
state.WriteRune('.')
|
||||
case int:
|
||||
clueToCellMap[c] = [2]int{x, y}
|
||||
state.WriteRune('-')
|
||||
default:
|
||||
return nil, errors.New("unexpected type in .ipuz file")
|
||||
}
|
||||
}
|
||||
}
|
||||
puzzle.State = state.String()
|
||||
|
||||
solution := strings.Builder{}
|
||||
for y, row := range parsed.Solution {
|
||||
for x, cell := range row {
|
||||
if cell == "#" {
|
||||
solution.WriteRune('.')
|
||||
} else if len(cell) > 1 {
|
||||
solution.WriteByte(cell[0])
|
||||
puzzle.RebusCells[y*puzzle.Width+x] = cell
|
||||
puzzle.HasRebus = true
|
||||
} else if len(cell) == 1 {
|
||||
solution.WriteString(cell)
|
||||
} else {
|
||||
return nil, errors.New("invalid ipuz file")
|
||||
}
|
||||
}
|
||||
}
|
||||
puzzle.Solution = solution.String()
|
||||
|
||||
puzzle.Clues = make([]*Clue, 0, puzzle.ClueCount)
|
||||
puzzle.Cells = make([][2]*Clue, puzzle.Width*puzzle.Height)
|
||||
|
||||
for _, clue := range parsed.Clues["Across"] {
|
||||
cellCoords := clueToCellMap[clue.Number]
|
||||
|
||||
clueCells := make([]int, 0)
|
||||
clueSolution := strings.Builder{}
|
||||
|
||||
clueStruct := &Clue{
|
||||
Clue: clue.Clue,
|
||||
Direction: DirectionAcross,
|
||||
Number: clue.Number,
|
||||
}
|
||||
|
||||
for i := cellCoords[0]; i < puzzle.Width; i++ {
|
||||
cell := parsed.Solution[cellCoords[1]][i]
|
||||
if cell == "#" {
|
||||
break
|
||||
}
|
||||
|
||||
cellIdx := cellCoords[1]*puzzle.Width + i
|
||||
|
||||
clueCells = append(clueCells, cellIdx)
|
||||
puzzle.Cells[cellIdx][0] = clueStruct
|
||||
clueSolution.WriteByte(cell[0])
|
||||
}
|
||||
|
||||
clueStruct.Solution = clueSolution.String()
|
||||
clueStruct.Cells = clueCells
|
||||
|
||||
puzzle.Clues = append(puzzle.Clues, clueStruct)
|
||||
}
|
||||
|
||||
for _, clue := range parsed.Clues["Down"] {
|
||||
cellCoords := clueToCellMap[clue.Number]
|
||||
|
||||
clueCells := make([]int, 0)
|
||||
clueSolution := strings.Builder{}
|
||||
|
||||
clueStruct := &Clue{
|
||||
Clue: clue.Clue,
|
||||
Direction: DirectionDown,
|
||||
Number: clue.Number,
|
||||
}
|
||||
|
||||
for i := cellCoords[1]; i < puzzle.Height; i++ {
|
||||
cell := parsed.Solution[i][cellCoords[0]]
|
||||
if cell == "#" {
|
||||
break
|
||||
}
|
||||
|
||||
cellIdx := i*puzzle.Width + cellCoords[0]
|
||||
|
||||
clueCells = append(clueCells, cellIdx)
|
||||
puzzle.Cells[cellIdx][1] = clueStruct
|
||||
clueSolution.WriteByte(cell[0])
|
||||
}
|
||||
|
||||
clueStruct.Solution = clueSolution.String()
|
||||
clueStruct.Cells = clueCells
|
||||
|
||||
puzzle.Clues = append(puzzle.Clues, clueStruct)
|
||||
}
|
||||
|
||||
return puzzle, nil
|
||||
}
|
||||
+156
-48
@@ -5,44 +5,15 @@ import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"io"
|
||||
"math"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/text/encoding/charmap"
|
||||
)
|
||||
|
||||
type Direction int
|
||||
|
||||
const (
|
||||
DirectionAcross Direction = iota
|
||||
DirectionDown
|
||||
)
|
||||
|
||||
type Clue struct {
|
||||
Clue string
|
||||
Solution string
|
||||
Cells []int
|
||||
Direction Direction
|
||||
Number int
|
||||
}
|
||||
|
||||
type Puzzle struct {
|
||||
ClueCount int
|
||||
Width int
|
||||
Height int
|
||||
|
||||
Title string
|
||||
Author string
|
||||
Copyright string
|
||||
|
||||
Solution string
|
||||
State string
|
||||
|
||||
Clues []*Clue
|
||||
|
||||
Cells [][2]*Clue
|
||||
}
|
||||
|
||||
func startOfDownClue(puzzle string, width, height, index int) bool {
|
||||
if puzzle[index] == '.' {
|
||||
return false
|
||||
@@ -87,39 +58,134 @@ func readText(b *bufio.Reader, version string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return decoded, nil
|
||||
return strings.TrimSpace(decoded), nil
|
||||
} else {
|
||||
return string(text[:len(text)-1]), nil
|
||||
return strings.TrimSpace(string(text[:len(text)-1])), nil
|
||||
}
|
||||
}
|
||||
|
||||
type ExtraBlock struct {
|
||||
Name string
|
||||
Data []byte
|
||||
}
|
||||
|
||||
func readExtraBlock(r *bufio.Reader) (*ExtraBlock, error) {
|
||||
name := make([]byte, 4)
|
||||
_, err := r.Read(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
length := make([]byte, 2)
|
||||
_, err = r.Read(length)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = r.Discard(2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data := make([]byte, binary.LittleEndian.Uint16(length))
|
||||
_, err = r.Read(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// consume trailing null byte
|
||||
_, err = r.Discard(1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ExtraBlock{
|
||||
Name: string(name),
|
||||
Data: data,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ParsePuz(file []byte) (*Puzzle, error) {
|
||||
r := bytes.NewReader(file)
|
||||
|
||||
_, err := r.Seek(2, io.SeekStart) // consume the checksum
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// read magic bytes
|
||||
magic := string(file[2:14])
|
||||
if magic != "ACROSS&DOWN\x00" {
|
||||
magic := make([]byte, 12)
|
||||
_, err = r.Read(magic)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !bytes.Equal(magic, []byte("ACROSS&DOWN\x00")) {
|
||||
return nil, errors.New("not a valid .puz file")
|
||||
}
|
||||
|
||||
puzzle := new(Puzzle)
|
||||
|
||||
puzzle.RebusCells = make(map[int]string)
|
||||
|
||||
_, err = r.Seek(10, io.SeekCurrent)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// read header
|
||||
version := string(file[0x18:0x1B])
|
||||
puzzle.Width = int(file[0x2C])
|
||||
puzzle.Height = int(file[0x2D])
|
||||
puzzle.ClueCount = int(binary.LittleEndian.Uint16(file[0x2E:0x30]))
|
||||
version := make([]byte, 4)
|
||||
_, err = r.Read(version)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = r.Seek(16, io.SeekCurrent)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
width, err := r.ReadByte()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
height, err := r.ReadByte()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
puzzle.Width = int(width)
|
||||
puzzle.Height = int(height)
|
||||
|
||||
clueCount := make([]byte, 2)
|
||||
_, err = r.Read(clueCount)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
puzzle.ClueCount = int(binary.LittleEndian.Uint16(clueCount))
|
||||
|
||||
puzzleSize := puzzle.Width * puzzle.Height
|
||||
solutionStart := 0x34
|
||||
solutionEnd := 0x34 + puzzleSize
|
||||
|
||||
stateStart := solutionEnd
|
||||
stateEnd := stateStart + puzzleSize
|
||||
_, err = r.Seek(4, io.SeekCurrent)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
puzzle.Solution = string(file[solutionStart:solutionEnd])
|
||||
puzzle.State = string(file[stateStart:stateEnd])
|
||||
solution := make([]byte, puzzleSize)
|
||||
_, err = r.Read(solution)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
state := make([]byte, puzzleSize)
|
||||
_, err = r.Read(state)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// read text
|
||||
buf := bufio.NewReader(bytes.NewReader(file[stateEnd:]))
|
||||
puzzle.Solution = string(solution)
|
||||
puzzle.State = string(state)
|
||||
|
||||
// read text fields
|
||||
buf := bufio.NewReader(r)
|
||||
|
||||
title, err := readText(buf, string(version[0]))
|
||||
if err != nil {
|
||||
@@ -149,6 +215,48 @@ func ParsePuz(file []byte) (*Puzzle, error) {
|
||||
clues = append(clues, clue)
|
||||
}
|
||||
|
||||
// read the "note" field, currently unused
|
||||
_, err = readText(buf, string(version[0]))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
extraBlocks := make(map[string]*ExtraBlock)
|
||||
|
||||
for {
|
||||
extra, err := readExtraBlock(buf)
|
||||
if errors.Is(err, io.EOF) {
|
||||
break
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
extraBlocks[extra.Name] = extra
|
||||
}
|
||||
|
||||
grbs, grbsOk := extraBlocks["GRBS"]
|
||||
trbl, trblOk := extraBlocks["RTBL"]
|
||||
if grbsOk && trblOk {
|
||||
puzzle.HasRebus = true
|
||||
rebusCellMap := make(map[int][]int)
|
||||
for i, b := range grbs.Data {
|
||||
if b > 0 {
|
||||
rebusCellMap[int(b)-1] = append(rebusCellMap[int(b)-1], i)
|
||||
}
|
||||
}
|
||||
|
||||
rebusData := regexp.MustCompile(`\s*(\d+):(\w+);\s*`).FindAllStringSubmatch(string(trbl.Data), -1)
|
||||
for _, rebus := range rebusData {
|
||||
cell, err := strconv.Atoi(rebus[1])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, c := range rebusCellMap[cell] {
|
||||
puzzle.RebusCells[c] = rebus[2]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// assign clue numbers
|
||||
puzzle.Clues = make([]*Clue, 0, puzzle.ClueCount)
|
||||
puzzle.Cells = make([][2]*Clue, len(puzzle.Solution))
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package puz
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"io"
|
||||
)
|
||||
|
||||
type Direction int
|
||||
|
||||
const (
|
||||
DirectionAcross Direction = iota
|
||||
DirectionDown
|
||||
)
|
||||
|
||||
type Clue struct {
|
||||
Clue string
|
||||
Solution string
|
||||
Cells []int
|
||||
Direction Direction
|
||||
Number int
|
||||
}
|
||||
|
||||
type Puzzle struct {
|
||||
ClueCount int
|
||||
Width int
|
||||
Height int
|
||||
|
||||
Title string
|
||||
Author string
|
||||
Copyright string
|
||||
|
||||
Solution string
|
||||
State string
|
||||
|
||||
Clues []*Clue
|
||||
|
||||
Cells [][2]*Clue
|
||||
|
||||
HasRebus bool
|
||||
|
||||
RebusCells map[int]string
|
||||
}
|
||||
|
||||
func LoadPuzzle(r io.Reader) (*Puzzle, error) {
|
||||
bufReader := bufio.NewReader(r)
|
||||
header, err := bufReader.Peek(16)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if string(header[2:14]) == "ACROSS&DOWN\x00" {
|
||||
file, err := io.ReadAll(bufReader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ParsePuz(file)
|
||||
} else if header[0] == '{' {
|
||||
file, err := io.ReadAll(bufReader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ParseIPuz(file)
|
||||
} else {
|
||||
return nil, errors.New("could not detect file type")
|
||||
}
|
||||
}
|
||||
+17
-9
@@ -31,12 +31,7 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
state := game.State{
|
||||
Puzzle: puzzle,
|
||||
PuzzleState: []rune(puzzle.State),
|
||||
SelectedClue: puzzle.Clues[0],
|
||||
SelectedCell: puzzle.Clues[0].Cells[0],
|
||||
}
|
||||
state := game.NewState(puzzle)
|
||||
|
||||
io.WriteString(s, "\x1b[?25l")
|
||||
|
||||
@@ -44,7 +39,20 @@ func main() {
|
||||
io.WriteString(s, "\x1b[?25h")
|
||||
}()
|
||||
|
||||
uiHeight := state.RenderUI(s)
|
||||
pty, winCh, isPty := s.Pty()
|
||||
if !isPty {
|
||||
return
|
||||
}
|
||||
|
||||
windowWidth := pty.Window.Width
|
||||
|
||||
uiHeight := state.RenderUI(s, windowWidth)
|
||||
|
||||
go func() {
|
||||
for window := range winCh {
|
||||
windowWidth = window.Width
|
||||
}
|
||||
}()
|
||||
|
||||
scanner := bufio.NewScanner(s)
|
||||
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
||||
@@ -76,14 +84,14 @@ func main() {
|
||||
state.SelectedCell = -1
|
||||
fmt.Fprintf(s, "\r\x1b[%dA", uiHeight)
|
||||
fmt.Fprint(s, "\x1b[J")
|
||||
state.RenderUI(s)
|
||||
state.RenderUI(s, windowWidth)
|
||||
break
|
||||
}
|
||||
|
||||
fmt.Fprintf(s, "\r\x1b[%dA", uiHeight)
|
||||
fmt.Fprint(s, "\x1b[J")
|
||||
|
||||
uiHeight = state.RenderUI(s)
|
||||
uiHeight = state.RenderUI(s, windowWidth)
|
||||
}
|
||||
|
||||
log.Printf("%s disconnected\n", s.RemoteAddr().String())
|
||||
|
||||
Reference in new issue
Block a user