-
Notifications
You must be signed in to change notification settings - Fork 10
/
square.go
237 lines (208 loc) · 5.24 KB
/
square.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package board
import (
"fmt"
"os"
"github.com/rs/zerolog/log"
"github.com/domino14/macondo/alphabet"
)
var (
ColorSupport = os.Getenv("MACONDO_DISABLE_COLOR") != "on"
)
// A BonusSquare is a bonus square (duh)
type BonusSquare rune
const (
Bonus4WS BonusSquare = '~'
Bonus4LS BonusSquare = '^'
// Bonus3WS is a triple word score
Bonus3WS BonusSquare = '='
// Bonus3LS is a triple letter score
Bonus3LS BonusSquare = '"'
// Bonus2LS is a double letter score
Bonus2LS BonusSquare = '\''
// Bonus2WS is a double word score
Bonus2WS BonusSquare = '-'
)
// A Square is a single square in a game board. It contains the bonus markings,
// if any, a letter, if any (' ' if empty), and any cross-sets and cross-scores
type Square struct {
letter alphabet.MachineLetter
bonus BonusSquare
hcrossSet CrossSet
vcrossSet CrossSet
// the scores of the tiles on either side of this square.
hcrossScore int
vcrossScore int
hAnchor bool
vAnchor bool
}
func init() {
if ColorSupport {
log.Info().Msg("Terminal color support is on.")
}
}
func (s Square) String() string {
bonus := string(s.bonus)
return fmt.Sprintf("<(%v) (%s)>", s.letter, bonus)
}
func (s Square) Info() string {
return fmt.Sprintf("[let %v bonus %v hc %v vc %v hcs %v vcs %v ha %v va %v]",
s.letter, s.bonus, s.hcrossSet, s.vcrossSet, s.hcrossScore, s.vcrossScore,
s.hAnchor, s.vAnchor,
)
}
func (s *Square) copyFrom(s2 *Square) {
s.letter = s2.letter
s.bonus = s2.bonus
s.hcrossSet = s2.hcrossSet
s.vcrossSet = s2.vcrossSet
s.hcrossScore = s2.hcrossScore
s.vcrossScore = s2.vcrossScore
s.hAnchor = s2.hAnchor
s.vAnchor = s2.vAnchor
}
func (s *Square) equals(s2 *Square) bool {
if s.bonus != s2.bonus {
log.Debug().Msg("Bonuses not equal")
return false
}
if s.letter != s2.letter {
log.Debug().Msg("Letters not equal")
return false
}
if s.hcrossSet != s2.hcrossSet {
log.Debug().Msgf("horiz cross-sets not equal: %v %v", s.hcrossSet, s2.hcrossSet)
return false
}
if s.vcrossSet != s2.vcrossSet {
log.Debug().Msgf("vert cross-sets not equal: %v %v", s.vcrossSet, s2.vcrossSet)
return false
}
if s.hcrossScore != s2.hcrossScore {
log.Debug().Msgf("horiz cross-scores not equal: %v %v", s.hcrossScore, s2.hcrossScore)
return false
}
if s.vcrossScore != s2.vcrossScore {
log.Debug().Msgf("vert cross-scores not equal: %v %v", s.vcrossScore, s2.vcrossScore)
return false
}
if s.hAnchor != s2.hAnchor {
log.Debug().Msgf("horiz anchors not equal: %v %v", s.hAnchor, s2.hAnchor)
return false
}
if s.vAnchor != s2.vAnchor {
log.Debug().Msgf("vert anchors not equal: %v %v", s.vAnchor, s2.vAnchor)
return false
}
return true
}
func (s *Square) Letter() alphabet.MachineLetter {
return s.letter
}
func (s *Square) SetLetter(letter alphabet.MachineLetter) {
s.letter = letter
}
func (b BonusSquare) displayString() string {
if !ColorSupport {
return string(b)
}
switch b {
case Bonus4WS:
return fmt.Sprintf("\033[33m%s\033[0m", string(b))
case Bonus3WS:
return fmt.Sprintf("\033[31m%s\033[0m", string(b))
case Bonus2WS:
return fmt.Sprintf("\033[35m%s\033[0m", string(b))
case Bonus4LS:
return fmt.Sprintf("\033[95m%s\033[0m", string(b))
case Bonus3LS:
return fmt.Sprintf("\033[34m%s\033[0m", string(b))
case Bonus2LS:
return fmt.Sprintf("\033[36m%s\033[0m", string(b))
default:
return "?"
}
}
func (s Square) DisplayString(alph *alphabet.Alphabet) string {
var bonusdisp string
if s.bonus != ' ' {
bonusdisp = s.bonus.displayString()
} else {
bonusdisp = " "
}
if s.letter == alphabet.EmptySquareMarker {
return bonusdisp
}
return string(s.letter.UserVisible(alph))
}
func (s Square) BadDisplayString(alph *alphabet.Alphabet) string {
var hadisp, vadisp, bonusdisp string
if s.hAnchor {
hadisp = "→"
} else {
hadisp = " "
}
if s.vAnchor {
vadisp = "↓"
} else {
vadisp = " "
}
if s.bonus != 0 {
bonusdisp = string(s.bonus)
} else {
bonusdisp = " "
}
if s.letter == alphabet.EmptySquareMarker {
return fmt.Sprintf("[%v%v%v]", bonusdisp, hadisp, vadisp)
}
return fmt.Sprintf("[%v%v%v]", s.letter.UserVisible(alph), hadisp, vadisp)
}
func (s *Square) SetCrossSet(cs CrossSet, dir BoardDirection) {
if dir == HorizontalDirection {
s.hcrossSet = cs
} else if dir == VerticalDirection {
s.vcrossSet = cs
}
}
func (s *Square) SetCrossScore(score int, dir BoardDirection) {
if dir == HorizontalDirection {
s.hcrossScore = score
} else if dir == VerticalDirection {
s.vcrossScore = score
}
}
func (s *Square) GetCrossSet(dir BoardDirection) *CrossSet {
if dir == HorizontalDirection {
return &s.hcrossSet
} else if dir == VerticalDirection {
return &s.vcrossSet
}
return nil
}
func (s *Square) GetCrossScore(dir BoardDirection) int {
if dir == HorizontalDirection {
return s.hcrossScore
} else if dir == VerticalDirection {
return s.vcrossScore
}
return 0
}
func (s *Square) setAnchor(dir BoardDirection) {
if dir == HorizontalDirection {
s.hAnchor = true
} else if dir == VerticalDirection {
s.vAnchor = true
}
}
func (s *Square) resetAnchors() {
s.hAnchor = false
s.vAnchor = false
}
func (s *Square) IsEmpty() bool {
return s.letter == alphabet.EmptySquareMarker
}
func (s *Square) anchor(dir BoardDirection) bool {
if dir == HorizontalDirection {
return s.hAnchor
}
return s.vAnchor
}