mirror of
https://github.com/cjdenio/crossword.git
synced 2026-12-01 03:26:19 +00:00
Compare commits
2
Commits
1f208f80b7
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7da6e54c2a | ||
|
|
1c580b5f9f |
No files matched your search
+35
-15
@@ -172,6 +172,8 @@ func (state *State) NextWord() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
puzzleFilled := state.GridFilled()
|
||||||
|
|
||||||
foundSelectedClue := false
|
foundSelectedClue := false
|
||||||
|
|
||||||
for _, clue := range state.Puzzle.Clues {
|
for _, clue := range state.Puzzle.Clues {
|
||||||
@@ -180,7 +182,7 @@ func (state *State) NextWord() {
|
|||||||
continue
|
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.SelectedClue = clue
|
||||||
state.SelectedCell = state.FirstUnfilledCellForClue(clue)
|
state.SelectedCell = state.FirstUnfilledCellForClue(clue)
|
||||||
return
|
return
|
||||||
@@ -188,7 +190,7 @@ func (state *State) NextWord() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, clue := range state.Puzzle.Clues {
|
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.SelectedClue = clue
|
||||||
state.SelectedCell = state.FirstUnfilledCellForClue(clue)
|
state.SelectedCell = state.FirstUnfilledCellForClue(clue)
|
||||||
return
|
return
|
||||||
@@ -198,7 +200,7 @@ func (state *State) NextWord() {
|
|||||||
for _, clue := range state.Puzzle.Clues {
|
for _, clue := range state.Puzzle.Clues {
|
||||||
if clue == state.SelectedClue {
|
if clue == state.SelectedClue {
|
||||||
return
|
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.SelectedClue = clue
|
||||||
state.SelectedCell = state.FirstUnfilledCellForClue(clue)
|
state.SelectedCell = state.FirstUnfilledCellForClue(clue)
|
||||||
return
|
return
|
||||||
@@ -369,10 +371,18 @@ func (state *State) RenderUI(w io.Writer, width int) int {
|
|||||||
fmt.Fprint(w, AnsiDimmed("[c]: check puzzle/word | [r]: reveal puzzle/word | [x]: clear grid | [q]: exit menu\r\n"))
|
fmt.Fprint(w, AnsiDimmed("[c]: check puzzle/word | [r]: reveal puzzle/word | [x]: clear grid | [q]: exit menu\r\n"))
|
||||||
uiHeight += 1
|
uiHeight += 1
|
||||||
case StateCheckMenu:
|
case StateCheckMenu:
|
||||||
|
if state.SelectedClue != nil {
|
||||||
fmt.Fprint(w, AnsiDimmed("[w]: check word | [p]: check puzzle | [q]: exit menu\r\n"))
|
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
|
uiHeight += 1
|
||||||
case StateRevealMenu:
|
case StateRevealMenu:
|
||||||
|
if state.SelectedClue != nil {
|
||||||
fmt.Fprint(w, AnsiDimmed("[w]: reveal word | [p]: reveal puzzle | [q]: exit menu\r\n"))
|
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
|
uiHeight += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -435,12 +445,13 @@ func (state *State) RenderPuzzle() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cell := ""
|
cell := ""
|
||||||
switch char {
|
if char == ':' || state.Puzzle.Solution[index] == ':' {
|
||||||
case '.':
|
cell = " "
|
||||||
|
} else if char == '.' {
|
||||||
cell = string(rune(0x2588))
|
cell = string(rune(0x2588))
|
||||||
case '-':
|
} else if char == '-' {
|
||||||
cell = AnsiDimmed("_")
|
cell = AnsiDimmed("_")
|
||||||
default:
|
} else {
|
||||||
cell = string(char)
|
cell = string(char)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -463,7 +474,7 @@ func (state *State) RenderPuzzle() string {
|
|||||||
|
|
||||||
if (index+1)%state.Puzzle.Width == 0 {
|
if (index+1)%state.Puzzle.Width == 0 {
|
||||||
b.WriteString(" │\r\n")
|
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)
|
b.WriteRune(0x2588)
|
||||||
} else {
|
} else {
|
||||||
// inefficient
|
// inefficient
|
||||||
@@ -507,7 +518,7 @@ func (state *State) LoadSaveFile(f []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i, cell := range state.Puzzle.Solution {
|
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")
|
return errors.New("save is invalid")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -578,7 +589,9 @@ func (state *State) HandleInput(buffer []byte) (exited bool) {
|
|||||||
case StateCheckMenu:
|
case StateCheckMenu:
|
||||||
switch buffer[0] {
|
switch buffer[0] {
|
||||||
case 'w':
|
case 'w':
|
||||||
|
if state.SelectedClue != nil {
|
||||||
state.CheckWord(state.SelectedClue)
|
state.CheckWord(state.SelectedClue)
|
||||||
|
}
|
||||||
case 'p':
|
case 'p':
|
||||||
state.CheckPuzzle()
|
state.CheckPuzzle()
|
||||||
}
|
}
|
||||||
@@ -587,7 +600,9 @@ func (state *State) HandleInput(buffer []byte) (exited bool) {
|
|||||||
case StateRevealMenu:
|
case StateRevealMenu:
|
||||||
switch buffer[0] {
|
switch buffer[0] {
|
||||||
case 'w':
|
case 'w':
|
||||||
|
if state.SelectedClue != nil {
|
||||||
state.RevealWord(state.SelectedClue)
|
state.RevealWord(state.SelectedClue)
|
||||||
|
}
|
||||||
case 'p':
|
case 'p':
|
||||||
state.RevealPuzzle()
|
state.RevealPuzzle()
|
||||||
return true
|
return true
|
||||||
@@ -610,11 +625,12 @@ func (state *State) HandleInput(buffer []byte) (exited bool) {
|
|||||||
state.SwitchDirections()
|
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] != '-'
|
cellWasFilled := state.PuzzleState[state.SelectedCell] != '-'
|
||||||
state.PuzzleState[state.SelectedCell] = unicode.ToUpper(rune(buffer[0]))
|
state.PuzzleState[state.SelectedCell] = unicode.ToUpper(rune(buffer[0]))
|
||||||
state.CheckState[state.SelectedCell] = 0x00
|
state.CheckState[state.SelectedCell] = 0x00
|
||||||
|
|
||||||
|
if state.SelectedClue != nil {
|
||||||
i := slices.Index(state.SelectedClue.Cells, state.SelectedCell)
|
i := slices.Index(state.SelectedClue.Cells, state.SelectedCell)
|
||||||
|
|
||||||
if !cellWasFilled {
|
if !cellWasFilled {
|
||||||
@@ -631,6 +647,7 @@ func (state *State) HandleInput(buffer []byte) (exited bool) {
|
|||||||
state.SelectedCell = state.SelectedClue.Cells[i+1]
|
state.SelectedCell = state.SelectedClue.Cells[i+1]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if state.GridFilled() {
|
if state.GridFilled() {
|
||||||
if state.PuzzleSolved() {
|
if state.PuzzleSolved() {
|
||||||
@@ -683,18 +700,21 @@ func (state *State) HandleInput(buffer []byte) (exited bool) {
|
|||||||
state.MoveCursor(2)
|
state.MoveCursor(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch state.SelectedClue.Direction {
|
if (state.SelectedClue != nil && state.SelectedClue.Direction == puz.DirectionAcross) || state.SelectedClue == nil {
|
||||||
case puz.DirectionAcross:
|
|
||||||
if state.Puzzle.Cells[state.SelectedCell][0] != nil {
|
if state.Puzzle.Cells[state.SelectedCell][0] != nil {
|
||||||
state.SelectedClue = state.Puzzle.Cells[state.SelectedCell][0]
|
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]
|
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 {
|
if state.Puzzle.Cells[state.SelectedCell][1] != nil {
|
||||||
state.SelectedClue = state.Puzzle.Cells[state.SelectedCell][1]
|
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]
|
state.SelectedClue = state.Puzzle.Cells[state.SelectedCell][0]
|
||||||
|
} else {
|
||||||
|
state.SelectedClue = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-8
@@ -15,32 +15,32 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func startOfDownClue(puzzle string, width, height, index int) bool {
|
func startOfDownClue(puzzle string, width, height, index int) bool {
|
||||||
if puzzle[index] == '.' {
|
if CellIsBlank(puzzle[index]) {
|
||||||
return false
|
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
|
return false
|
||||||
}
|
}
|
||||||
if index < width {
|
if index < width {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if puzzle[index-width] == '.' {
|
if CellIsBlank(puzzle[index-width]) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func startOfAcrossClue(puzzle string, width, index int) bool {
|
func startOfAcrossClue(puzzle string, width, index int) bool {
|
||||||
if puzzle[index] == '.' {
|
if CellIsBlank(puzzle[index]) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if (index+1)%width == 0 || puzzle[index+1] == '.' {
|
if (index+1)%width == 0 || CellIsBlank(puzzle[index+1]) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if index%width == 0 {
|
if index%width == 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if puzzle[index-1] == '.' {
|
if CellIsBlank(puzzle[index-1]) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
@@ -316,13 +316,13 @@ func ParsePuz(file []byte) (*Puzzle, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if clue.Direction == DirectionAcross {
|
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
|
break
|
||||||
} else {
|
} else {
|
||||||
currentCell++
|
currentCell++
|
||||||
}
|
}
|
||||||
} else if clue.Direction == DirectionDown {
|
} 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
|
break
|
||||||
} else {
|
} else {
|
||||||
currentCell += puzzle.Width
|
currentCell += puzzle.Width
|
||||||
|
|||||||
@@ -43,6 +43,10 @@ type Puzzle struct {
|
|||||||
RebusCells map[int]string
|
RebusCells map[int]string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CellIsBlank(cell byte) bool {
|
||||||
|
return cell == ':' || cell == '.'
|
||||||
|
}
|
||||||
|
|
||||||
func LoadPuzzle(r io.Reader) (*Puzzle, error) {
|
func LoadPuzzle(r io.Reader) (*Puzzle, error) {
|
||||||
bufReader := bufio.NewReader(r)
|
bufReader := bufio.NewReader(r)
|
||||||
header, err := bufReader.Peek(16)
|
header, err := bufReader.Peek(16)
|
||||||
|
|||||||
Reference in new issue
Block a user