mirror of
https://github.com/cjdenio/crossword.git
synced 2026-10-03 11:06:19 +00:00
parse rebus cells
This commit is contained in:
3 files changed
+36
-3
No files matched your search
+5
-2
@@ -63,6 +63,8 @@ func ParseIPuz(file []byte) (*Puzzle, error) {
|
|||||||
|
|
||||||
puzzle := new(Puzzle)
|
puzzle := new(Puzzle)
|
||||||
|
|
||||||
|
puzzle.RebusCells = make(map[int]string)
|
||||||
|
|
||||||
puzzle.Title = parsed.Title
|
puzzle.Title = parsed.Title
|
||||||
puzzle.Author = parsed.Author
|
puzzle.Author = parsed.Author
|
||||||
puzzle.Copyright = parsed.Copyright
|
puzzle.Copyright = parsed.Copyright
|
||||||
@@ -95,12 +97,13 @@ func ParseIPuz(file []byte) (*Puzzle, error) {
|
|||||||
puzzle.State = state.String()
|
puzzle.State = state.String()
|
||||||
|
|
||||||
solution := strings.Builder{}
|
solution := strings.Builder{}
|
||||||
for _, row := range parsed.Solution {
|
for y, row := range parsed.Solution {
|
||||||
for _, cell := range row {
|
for x, cell := range row {
|
||||||
if cell == "#" {
|
if cell == "#" {
|
||||||
solution.WriteRune('.')
|
solution.WriteRune('.')
|
||||||
} else if len(cell) > 1 {
|
} else if len(cell) > 1 {
|
||||||
solution.WriteByte(cell[0])
|
solution.WriteByte(cell[0])
|
||||||
|
puzzle.RebusCells[y*puzzle.Width+x] = cell
|
||||||
puzzle.HasRebus = true
|
puzzle.HasRebus = true
|
||||||
} else if len(cell) == 1 {
|
} else if len(cell) == 1 {
|
||||||
solution.WriteString(cell)
|
solution.WriteString(cell)
|
||||||
|
|||||||
+29
-1
@@ -7,6 +7,8 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/text/encoding/charmap"
|
"golang.org/x/text/encoding/charmap"
|
||||||
@@ -123,6 +125,8 @@ func ParsePuz(file []byte) (*Puzzle, error) {
|
|||||||
|
|
||||||
puzzle := new(Puzzle)
|
puzzle := new(Puzzle)
|
||||||
|
|
||||||
|
puzzle.RebusCells = make(map[int]string)
|
||||||
|
|
||||||
_, err = r.Seek(10, io.SeekCurrent)
|
_, err = r.Seek(10, io.SeekCurrent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -217,6 +221,8 @@ func ParsePuz(file []byte) (*Puzzle, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extraBlocks := make(map[string]*ExtraBlock)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
extra, err := readExtraBlock(buf)
|
extra, err := readExtraBlock(buf)
|
||||||
if errors.Is(err, io.EOF) {
|
if errors.Is(err, io.EOF) {
|
||||||
@@ -224,8 +230,30 @@ func ParsePuz(file []byte) (*Puzzle, error) {
|
|||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if extra.Name == "RTBL" {
|
extraBlocks[extra.Name] = extra
|
||||||
|
}
|
||||||
|
|
||||||
|
grbs, grbsOk := extraBlocks["GRBS"]
|
||||||
|
trbl, trblOk := extraBlocks["RTBL"]
|
||||||
|
if grbsOk && trblOk {
|
||||||
puzzle.HasRebus = true
|
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]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,6 +38,8 @@ type Puzzle struct {
|
|||||||
Cells [][2]*Clue
|
Cells [][2]*Clue
|
||||||
|
|
||||||
HasRebus bool
|
HasRebus bool
|
||||||
|
|
||||||
|
RebusCells map[int]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadPuzzle(r io.Reader) (*Puzzle, error) {
|
func LoadPuzzle(r io.Reader) (*Puzzle, error) {
|
||||||
|
|||||||
Reference in new issue
Block a user