mirror of
https://github.com/cjdenio/crossword.git
synced 2026-12-01 03:26:19 +00:00
Compare commits
5
Commits
d50b572fb5
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7da6e54c2a | ||
|
|
1c580b5f9f | ||
|
|
1f208f80b7 | ||
|
|
19e295493e | ||
|
|
6242535184 |
No files matched your search
@@ -2,7 +2,7 @@ Very silly terminal-based crossword solver. It can play .puz / .ipuz files from
|
||||
|
||||
- Requirements: [Go](https://go.dev)
|
||||
- Build: `make`
|
||||
- Usage: `./bin/crossword <file>.puz`
|
||||
- Usage: `./bin/crossword <file>.{ipuz,puz}`
|
||||
|
||||

|
||||
|
||||
|
||||
+58
-16
@@ -40,6 +40,7 @@ type State struct {
|
||||
SolveState SolveState
|
||||
CheckState []rune
|
||||
UIState UIState
|
||||
NoteVisible bool
|
||||
}
|
||||
|
||||
func RenderWithLineWrap(s string, width int) (out string, lines int) {
|
||||
@@ -171,6 +172,8 @@ func (state *State) NextWord() {
|
||||
return
|
||||
}
|
||||
|
||||
puzzleFilled := state.GridFilled()
|
||||
|
||||
foundSelectedClue := false
|
||||
|
||||
for _, clue := range state.Puzzle.Clues {
|
||||
@@ -179,7 +182,7 @@ func (state *State) NextWord() {
|
||||
continue
|
||||
}
|
||||
|
||||
if foundSelectedClue && clue.Direction == state.SelectedClue.Direction && !state.ClueFilled(clue) {
|
||||
if foundSelectedClue && clue.Direction == state.SelectedClue.Direction && (puzzleFilled || !state.ClueFilled(clue)) {
|
||||
state.SelectedClue = clue
|
||||
state.SelectedCell = state.FirstUnfilledCellForClue(clue)
|
||||
return
|
||||
@@ -187,7 +190,7 @@ func (state *State) NextWord() {
|
||||
}
|
||||
|
||||
for _, clue := range state.Puzzle.Clues {
|
||||
if clue.Direction != state.SelectedClue.Direction && !state.ClueFilled(clue) {
|
||||
if clue.Direction != state.SelectedClue.Direction && (puzzleFilled || !state.ClueFilled(clue)) {
|
||||
state.SelectedClue = clue
|
||||
state.SelectedCell = state.FirstUnfilledCellForClue(clue)
|
||||
return
|
||||
@@ -197,7 +200,7 @@ func (state *State) NextWord() {
|
||||
for _, clue := range state.Puzzle.Clues {
|
||||
if clue == state.SelectedClue {
|
||||
return
|
||||
} else if clue.Direction == state.SelectedClue.Direction && !state.ClueFilled(clue) {
|
||||
} else if clue.Direction == state.SelectedClue.Direction && (puzzleFilled || !state.ClueFilled(clue)) {
|
||||
state.SelectedClue = clue
|
||||
state.SelectedCell = state.FirstUnfilledCellForClue(clue)
|
||||
return
|
||||
@@ -327,8 +330,17 @@ func (state *State) RenderUI(w io.Writer, width int) int {
|
||||
uiHeight += 2
|
||||
fmt.Fprintf(w, "AUTHOR: %s\r\n", state.Puzzle.Author)
|
||||
uiHeight += 1
|
||||
|
||||
if state.Puzzle.Note != "" && state.NoteVisible {
|
||||
note, noteLines := RenderWithLineWrap(state.Puzzle.Note, min(width, 60))
|
||||
|
||||
fmt.Fprintf(w, "\r\n%s\r\n", AnsiDimmed(note))
|
||||
uiHeight += noteLines + 2
|
||||
}
|
||||
|
||||
fmt.Fprint(w, state.RenderPuzzle()+"\r\n")
|
||||
uiHeight += state.Puzzle.Height + 3
|
||||
|
||||
if state.SelectedClue != nil {
|
||||
directionStr := "down"
|
||||
if state.SelectedClue.Direction == puz.DirectionAcross {
|
||||
@@ -348,16 +360,29 @@ func (state *State) RenderUI(w io.Writer, width int) int {
|
||||
|
||||
switch state.UIState {
|
||||
case StateGame:
|
||||
fmt.Fprint(w, AnsiDimmed("[enter]/[tab]: next word | [ctrl+a]: menu | [ctrl+c]: exit\r\n"))
|
||||
line := "[enter]/[tab]: next word | [ctrl+a]: menu"
|
||||
if state.Puzzle.Note != "" {
|
||||
line += " | [ctrl+n]: toggle note"
|
||||
}
|
||||
line += " | [ctrl+c]: exit\r\n"
|
||||
fmt.Fprint(w, AnsiDimmed(line))
|
||||
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:
|
||||
if state.SelectedClue != nil {
|
||||
fmt.Fprint(w, AnsiDimmed("[w]: check word | [p]: check puzzle | [q]: exit menu\r\n"))
|
||||
} else {
|
||||
fmt.Fprint(w, AnsiDimmed("[p]: check puzzle | [q]: exit menu\r\n"))
|
||||
}
|
||||
uiHeight += 1
|
||||
case StateRevealMenu:
|
||||
if state.SelectedClue != nil {
|
||||
fmt.Fprint(w, AnsiDimmed("[w]: reveal word | [p]: reveal puzzle | [q]: exit menu\r\n"))
|
||||
} else {
|
||||
fmt.Fprint(w, AnsiDimmed("[p]: reveal puzzle | [q]: exit menu\r\n"))
|
||||
}
|
||||
uiHeight += 1
|
||||
}
|
||||
|
||||
@@ -420,12 +445,13 @@ func (state *State) RenderPuzzle() string {
|
||||
}
|
||||
|
||||
cell := ""
|
||||
switch char {
|
||||
case '.':
|
||||
if char == ':' || state.Puzzle.Solution[index] == ':' {
|
||||
cell = " "
|
||||
} else if char == '.' {
|
||||
cell = string(rune(0x2588))
|
||||
case '-':
|
||||
} else if char == '-' {
|
||||
cell = AnsiDimmed("_")
|
||||
default:
|
||||
} else {
|
||||
cell = string(char)
|
||||
}
|
||||
|
||||
@@ -448,7 +474,7 @@ func (state *State) RenderPuzzle() string {
|
||||
|
||||
if (index+1)%state.Puzzle.Width == 0 {
|
||||
b.WriteString(" │\r\n")
|
||||
} else if char == '.' && state.PuzzleState[index+1] == '.' {
|
||||
} else if (char == '.' && state.Puzzle.Solution[index] != ':') && state.PuzzleState[index+1] == '.' {
|
||||
b.WriteRune(0x2588)
|
||||
} else {
|
||||
// inefficient
|
||||
@@ -492,7 +518,7 @@ func (state *State) LoadSaveFile(f []byte) error {
|
||||
}
|
||||
|
||||
for i, cell := range state.Puzzle.Solution {
|
||||
if (save.State[i] == '.' && cell != '.') || (save.State[i] != '.' && cell == '.') {
|
||||
if (puz.CellIsBlank(save.State[i]) && !puz.CellIsBlank(byte(cell))) || (!puz.CellIsBlank(save.State[i]) && puz.CellIsBlank(byte(cell))) {
|
||||
return errors.New("save is invalid")
|
||||
}
|
||||
}
|
||||
@@ -563,7 +589,9 @@ func (state *State) HandleInput(buffer []byte) (exited bool) {
|
||||
case StateCheckMenu:
|
||||
switch buffer[0] {
|
||||
case 'w':
|
||||
if state.SelectedClue != nil {
|
||||
state.CheckWord(state.SelectedClue)
|
||||
}
|
||||
case 'p':
|
||||
state.CheckPuzzle()
|
||||
}
|
||||
@@ -572,7 +600,9 @@ func (state *State) HandleInput(buffer []byte) (exited bool) {
|
||||
case StateRevealMenu:
|
||||
switch buffer[0] {
|
||||
case 'w':
|
||||
if state.SelectedClue != nil {
|
||||
state.RevealWord(state.SelectedClue)
|
||||
}
|
||||
case 'p':
|
||||
state.RevealPuzzle()
|
||||
return true
|
||||
@@ -586,15 +616,21 @@ func (state *State) HandleInput(buffer []byte) (exited bool) {
|
||||
return false
|
||||
}
|
||||
|
||||
if buffer[0] == '\x0E' {
|
||||
state.NoteVisible = !state.NoteVisible
|
||||
return false
|
||||
}
|
||||
|
||||
if buffer[0] == ' ' {
|
||||
state.SwitchDirections()
|
||||
}
|
||||
|
||||
if buffer[0] >= 0x61 && buffer[0] <= 0x7a {
|
||||
if (buffer[0] >= 'a' && buffer[0] <= 'z') || (buffer[0] >= '0' && buffer[0] <= '9') {
|
||||
cellWasFilled := state.PuzzleState[state.SelectedCell] != '-'
|
||||
state.PuzzleState[state.SelectedCell] = unicode.ToUpper(rune(buffer[0]))
|
||||
state.CheckState[state.SelectedCell] = 0x00
|
||||
|
||||
if state.SelectedClue != nil {
|
||||
i := slices.Index(state.SelectedClue.Cells, state.SelectedCell)
|
||||
|
||||
if !cellWasFilled {
|
||||
@@ -611,6 +647,7 @@ func (state *State) HandleInput(buffer []byte) (exited bool) {
|
||||
state.SelectedCell = state.SelectedClue.Cells[i+1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if state.GridFilled() {
|
||||
if state.PuzzleSolved() {
|
||||
@@ -628,10 +665,12 @@ func (state *State) HandleInput(buffer []byte) (exited bool) {
|
||||
// is there a filled cell underneath the cursor?
|
||||
if state.PuzzleState[state.SelectedCell] != '-' {
|
||||
state.PuzzleState[state.SelectedCell] = '-'
|
||||
state.CheckState[state.SelectedCell] = 0x00
|
||||
} else {
|
||||
i := slices.Index(state.SelectedClue.Cells, state.SelectedCell)
|
||||
if i > 0 {
|
||||
state.PuzzleState[state.SelectedClue.Cells[i-1]] = '-' // clear the previous cell
|
||||
state.CheckState[state.SelectedClue.Cells[i-1]] = 0x00
|
||||
state.SelectedCell = state.SelectedClue.Cells[i-1]
|
||||
}
|
||||
}
|
||||
@@ -661,18 +700,21 @@ func (state *State) HandleInput(buffer []byte) (exited bool) {
|
||||
state.MoveCursor(2)
|
||||
}
|
||||
|
||||
switch state.SelectedClue.Direction {
|
||||
case puz.DirectionAcross:
|
||||
if (state.SelectedClue != nil && state.SelectedClue.Direction == puz.DirectionAcross) || state.SelectedClue == nil {
|
||||
if state.Puzzle.Cells[state.SelectedCell][0] != nil {
|
||||
state.SelectedClue = state.Puzzle.Cells[state.SelectedCell][0]
|
||||
} else {
|
||||
} else if state.Puzzle.Cells[state.SelectedCell][1] != nil {
|
||||
state.SelectedClue = state.Puzzle.Cells[state.SelectedCell][1]
|
||||
} else {
|
||||
state.SelectedClue = nil
|
||||
}
|
||||
case puz.DirectionDown:
|
||||
} else if state.SelectedClue != nil && state.SelectedClue.Direction == puz.DirectionDown {
|
||||
if state.Puzzle.Cells[state.SelectedCell][1] != nil {
|
||||
state.SelectedClue = state.Puzzle.Cells[state.SelectedCell][1]
|
||||
} else {
|
||||
} else if state.Puzzle.Cells[state.SelectedCell][0] != nil {
|
||||
state.SelectedClue = state.Puzzle.Cells[state.SelectedCell][0]
|
||||
} else {
|
||||
state.SelectedClue = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ type iPuzFile struct {
|
||||
Title string `json:"title"`
|
||||
Author string `json:"author"`
|
||||
Copyright string `json:"copyright"`
|
||||
Notes string `json:"notes"`
|
||||
Puzzle [][]any `json:"puzzle"`
|
||||
Solution [][]string `json:"solution"`
|
||||
Dimensions iPuzDimensions `json:"dimensions"`
|
||||
@@ -68,6 +69,7 @@ func ParseIPuz(file []byte) (*Puzzle, error) {
|
||||
puzzle.Title = parsed.Title
|
||||
puzzle.Author = parsed.Author
|
||||
puzzle.Copyright = parsed.Copyright
|
||||
puzzle.Note = parsed.Notes
|
||||
|
||||
puzzle.Width = parsed.Dimensions.Width
|
||||
puzzle.Height = parsed.Dimensions.Height
|
||||
|
||||
+10
-10
@@ -15,32 +15,32 @@ import (
|
||||
)
|
||||
|
||||
func startOfDownClue(puzzle string, width, height, index int) bool {
|
||||
if puzzle[index] == '.' {
|
||||
if CellIsBlank(puzzle[index]) {
|
||||
return false
|
||||
}
|
||||
if int(math.Floor(float64(index/width)))+1 == height || puzzle[index+width] == '.' {
|
||||
if int(math.Floor(float64(index/width)))+1 == height || CellIsBlank(puzzle[index+width]) {
|
||||
return false
|
||||
}
|
||||
if index < width {
|
||||
return true
|
||||
}
|
||||
if puzzle[index-width] == '.' {
|
||||
if CellIsBlank(puzzle[index-width]) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func startOfAcrossClue(puzzle string, width, index int) bool {
|
||||
if puzzle[index] == '.' {
|
||||
if CellIsBlank(puzzle[index]) {
|
||||
return false
|
||||
}
|
||||
if (index+1)%width == 0 || puzzle[index+1] == '.' {
|
||||
if (index+1)%width == 0 || CellIsBlank(puzzle[index+1]) {
|
||||
return false
|
||||
}
|
||||
if index%width == 0 {
|
||||
return true
|
||||
}
|
||||
if puzzle[index-1] == '.' {
|
||||
if CellIsBlank(puzzle[index-1]) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
@@ -215,11 +215,11 @@ func ParsePuz(file []byte) (*Puzzle, error) {
|
||||
clues = append(clues, clue)
|
||||
}
|
||||
|
||||
// read the "note" field, currently unused
|
||||
_, err = readText(buf, string(version[0]))
|
||||
note, err := readText(buf, string(version[0]))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
puzzle.Note = note
|
||||
|
||||
extraBlocks := make(map[string]*ExtraBlock)
|
||||
|
||||
@@ -316,13 +316,13 @@ func ParsePuz(file []byte) (*Puzzle, error) {
|
||||
}
|
||||
|
||||
if clue.Direction == DirectionAcross {
|
||||
if ((currentCell+1)%puzzle.Width == 0) || puzzle.Solution[currentCell+1] == '.' {
|
||||
if ((currentCell+1)%puzzle.Width == 0) || CellIsBlank(puzzle.Solution[currentCell+1]) {
|
||||
break
|
||||
} else {
|
||||
currentCell++
|
||||
}
|
||||
} else if clue.Direction == DirectionDown {
|
||||
if int(math.Floor(float64(currentCell/puzzle.Width)))+1 == puzzle.Height || puzzle.Solution[currentCell+puzzle.Width] == '.' {
|
||||
if int(math.Floor(float64(currentCell/puzzle.Width)))+1 == puzzle.Height || CellIsBlank(puzzle.Solution[currentCell+puzzle.Width]) {
|
||||
break
|
||||
} else {
|
||||
currentCell += puzzle.Width
|
||||
|
||||
@@ -29,6 +29,7 @@ type Puzzle struct {
|
||||
Title string
|
||||
Author string
|
||||
Copyright string
|
||||
Note string
|
||||
|
||||
Solution string
|
||||
State string
|
||||
@@ -42,6 +43,10 @@ type Puzzle struct {
|
||||
RebusCells map[int]string
|
||||
}
|
||||
|
||||
func CellIsBlank(cell byte) bool {
|
||||
return cell == ':' || cell == '.'
|
||||
}
|
||||
|
||||
func LoadPuzzle(r io.Reader) (*Puzzle, error) {
|
||||
bufReader := bufio.NewReader(r)
|
||||
header, err := bufReader.Peek(16)
|
||||
|
||||
Reference in new issue
Block a user