mirror of
https://github.com/cjdenio/crossword.git
synced 2026-10-03 11:06:19 +00:00
yeah
This commit is contained in:
1 file changed
+36
-9
@@ -1,16 +1,29 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/cjdenio/crossword/puz"
|
"github.com/cjdenio/crossword/puz"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RenderPuzzle(puzzle string, width, height int) string {
|
const (
|
||||||
|
AnsiInvert string = "\x1b[7m"
|
||||||
|
AnsiReset string = "\x1b[m"
|
||||||
|
AnsiWhiteBackground string = "\x1b[100m"
|
||||||
|
)
|
||||||
|
|
||||||
|
func AnsiInverted(s string) string {
|
||||||
|
return AnsiInvert + s + AnsiReset
|
||||||
|
}
|
||||||
|
func AnsiWhiteBackgrounded(s string) string {
|
||||||
|
return AnsiWhiteBackground + s + AnsiReset
|
||||||
|
}
|
||||||
|
|
||||||
|
func RenderPuzzle(puzzle string, width, height int, selectedClueCells []int, selectedCell int) string {
|
||||||
b := strings.Builder{}
|
b := strings.Builder{}
|
||||||
|
|
||||||
b.WriteRune('┌')
|
b.WriteRune('┌')
|
||||||
@@ -24,21 +37,35 @@ func RenderPuzzle(puzzle string, width, height int) string {
|
|||||||
b.WriteString("│ ")
|
b.WriteString("│ ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cell := ""
|
||||||
switch char {
|
switch char {
|
||||||
case '.':
|
case '.':
|
||||||
b.WriteRune(0x2588)
|
cell = string(rune(0x2588))
|
||||||
case '-':
|
case '-':
|
||||||
b.WriteRune('_')
|
cell = "_"
|
||||||
default:
|
default:
|
||||||
b.WriteRune(char)
|
cell = string(char)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if selectedCell == index {
|
||||||
|
cell = AnsiInverted(cell)
|
||||||
|
} else if slices.Contains(selectedClueCells, index) {
|
||||||
|
cell = AnsiWhiteBackgrounded(cell)
|
||||||
|
}
|
||||||
|
|
||||||
|
b.WriteString(cell)
|
||||||
|
|
||||||
if (index+1)%width == 0 {
|
if (index+1)%width == 0 {
|
||||||
b.WriteString(" │\n")
|
b.WriteString(" │\n")
|
||||||
} else if char == '.' && puzzle[index+1] == '.' {
|
} else if char == '.' && puzzle[index+1] == '.' {
|
||||||
b.WriteRune(0x2588)
|
b.WriteRune(0x2588)
|
||||||
} else {
|
} else {
|
||||||
b.WriteRune(' ')
|
// inefficient
|
||||||
|
if slices.Contains(selectedClueCells, index) && slices.Contains(selectedClueCells, index+1) {
|
||||||
|
b.WriteString(AnsiWhiteBackgrounded(" "))
|
||||||
|
} else {
|
||||||
|
b.WriteRune(' ')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,10 +101,10 @@ func main() {
|
|||||||
fmt.Printf("TITLE: %s\n", puzzle.Title)
|
fmt.Printf("TITLE: %s\n", puzzle.Title)
|
||||||
fmt.Printf("AUTHOR: %s\n", puzzle.Author)
|
fmt.Printf("AUTHOR: %s\n", puzzle.Author)
|
||||||
|
|
||||||
fmt.Println(RenderPuzzle(puzzle.State, puzzle.Width, puzzle.Height))
|
fmt.Println(RenderPuzzle(puzzle.State, puzzle.Width, puzzle.Height, puzzle.Clues[0].Cells, 1))
|
||||||
|
|
||||||
j, _ := json.Marshal(puzzle)
|
// j, _ := json.Marshal(puzzle)
|
||||||
fmt.Println(string(j))
|
// fmt.Println(string(j))
|
||||||
// for _, clue := range puzzle.Clues {
|
// for _, clue := range puzzle.Clues {
|
||||||
// if clue.Direction == puz.DirectionAcross {
|
// if clue.Direction == puz.DirectionAcross {
|
||||||
// fmt.Printf(". %d-across: %s: %s\n", clue.Number, clue.Clue, clue.Solution)
|
// fmt.Printf(". %d-across: %s: %s\n", clue.Number, clue.Clue, clue.Solution)
|
||||||
|
|||||||
Reference in new issue
Block a user