/
board.go
154 lines (141 loc) · 4.08 KB
/
board.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package server
import (
"html"
"strconv"
"strings"
"time"
)
type BoardLayout struct {
Admin bool `json:"admin"`
Answers *struct {
Across []string `json:"across"`
Down []string `json:"down"`
} `json:"answers"`
Author string `json:"author"`
Autowrap interface{} `json:"autowrap"`
Bbars interface{} `json:"bbars"`
Circles interface{} `json:"circles"`
Clues struct {
Across []string `json:"across"`
Down []string `json:"down"`
} `json:"clues"`
Code interface{} `json:"code"`
Copyright string `json:"copyright"`
Date string `json:"date"`
Dow string `json:"dow"`
Downmap interface{} `json:"downmap"`
Editor string `json:"editor"`
Grid []string `json:"grid"`
Gridnums []int `json:"gridnums"`
Hastitle bool `json:"hastitle"`
Hold interface{} `json:"hold"`
ID string `json:"id" dynamo:"ID,hash"`
ID2 interface{} `json:"id2"`
Interpretcolors interface{} `json:"interpretcolors"`
Jnotes interface{} `json:"jnotes"`
Key interface{} `json:"key"`
Mini interface{} `json:"mini"`
Notepad interface{} `json:"notepad"`
Publisher string `json:"publisher"`
Rbars interface{} `json:"rbars"`
Shadecircles interface{} `json:"shadecircles"`
Size struct {
Cols int `json:"cols"`
Rows int `json:"rows"`
} `json:"size"`
Title string `json:"title"`
Track interface{} `json:"track"`
Type interface{} `json:"type"`
Uniclue bool `json:"uniclue"`
Valid bool `json:"valid"`
}
func (b BoardLayout) ClueLabel(number int, direction string) int {
clues := b.Clues.Down
if direction == "across" {
clues = b.Clues.Across
}
label := strings.Split(clues[number], ".")[0]
labelInt, _ := strconv.Atoi(label)
return labelInt
}
func (b BoardLayout) ClueLabelPosition(clueLabel int) (int, int) {
for idx, gridnum := range b.Gridnums {
if gridnum == clueLabel {
row := idx / b.Size.Rows
col := idx % b.Size.Cols
return row, col
}
}
return -1, -1
}
func (b BoardLayout) GetLastClue(number int, direction string) Clue {
prevNum, prevDirection := b.Prev(number, direction)
return b.GetClue(prevNum, prevDirection, nil)
}
func (b BoardLayout) GetClue(number int, direction string, expiresAt *time.Time) Clue {
clues := b.Clues.Down
answers := b.Answers.Down
if direction == "across" {
clues = b.Clues.Across
answers = b.Answers.Across
}
return Clue{
Number: number,
Direction: direction,
Description: html.UnescapeString(clues[number]),
Answer: answers[number],
ExpiresAt: expiresAt,
}
}
func (b BoardLayout) Next(number int, direction string) (int, string) {
if direction == "across" {
if len(b.Clues.Across) == number+1 {
return 0, "down"
}
} else {
if len(b.Clues.Down) == number+1 {
return 0, "across"
}
}
return number + 1, direction
}
func (b BoardLayout) Prev(number int, direction string) (int, string) {
if number == 0 {
if direction == "across" {
return len(b.Clues.Down) - 1, "down"
} else {
return len(b.Clues.Across) - 1, "across"
}
}
return number - 1, direction
}
func (b *BoardLayout) CorrectAnswer(clueNumber int, clueDirection string) string {
if clueDirection == "across" {
return b.Answers.Across[clueNumber]
}
return b.Answers.Down[clueNumber]
}
func (b *BoardLayout) CalculateScore(guess string, number int, direction string) (int, bool) {
if strings.ToUpper(guess) != b.CorrectAnswer(number, direction) {
return 0, false
}
return 100, true
}
func (b *BoardLayout) MaskAnswer(answer string, currentClueNumber int, currentClueDirection string, grid []string) string {
clueLabel := b.ClueLabel(currentClueNumber, currentClueDirection)
row, col := b.ClueLabelPosition(clueLabel)
numRows, numCols := b.Size.Rows, b.Size.Cols
a := ""
for i := range answer {
idx := row*numRows + (col+i)%numCols
if currentClueDirection == "down" {
idx = (row+i)*numRows + col%numCols
}
if grid[idx] == " " {
a += "?"
} else {
a += grid[idx]
}
}
return a
}