/
ladder.go
334 lines (291 loc) · 9.37 KB
/
ladder.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"math/rand"
"sync"
gg "github.com/nanitefactory/amidakuji/glossary"
"golang.org/x/image/colornames"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
)
// Ladder is an imdraw that does not animate at all,
// hence does not need to be modified every frame.
// Something kinda static and bone-like.
type Ladder struct {
imd *imdraw.IMDraw // shared variable
mutex sync.Mutex // synchronize
//
bound pixel.Rect
grid [][]pixel.Vec
bridges [][]bool
nParticipants int
nLevel int
paddingTop float64
paddingRight float64
paddingBottom float64
paddingLeft float64
colors []pixel.RGBA
}
// NewLadder is a constructor.
func NewLadder(_nParticipants, _nLevel int,
_width, _height,
_paddingTop, _paddingRight,
_paddingBottom, _paddingLeft float64) *Ladder {
// get random colors
colors := []pixel.RGBA{}
for i := 0; i < _nParticipants; i++ {
colors = append(colors, gg.RandomNiceColor())
}
// new grid
newGrid := func(nRow, nCol int) [][]pixel.Vec {
arr := make([][]pixel.Vec, nRow)
for i := range arr {
arr[i] = make([]pixel.Vec, nCol)
}
// log.Println(arr) //
return arr
}
// new bridges
newBridges := func(nParticipants, nLevel int) [][]bool {
nRow := nParticipants - 1
nCol := nLevel
arr := make([][]bool, nRow)
for i := range arr {
arr[i] = make([]bool, nCol)
}
return arr
}
// init ladder
l := Ladder{
imd: imdraw.New(nil),
bound: pixel.R(0, 0, _width, _height),
grid: newGrid(_nParticipants, _nLevel),
bridges: newBridges(_nParticipants, _nLevel),
nParticipants: _nParticipants,
nLevel: _nLevel,
paddingTop: _paddingTop,
paddingBottom: _paddingBottom,
paddingRight: _paddingRight,
paddingLeft: _paddingLeft,
colors: colors,
}
// init grid
updateGrid := func(l *Ladder) {
for participant := range l.grid { // row
for level := range l.grid[participant] { // col
y := l.Height() - (float64(participant) * l.DistParticipant()) // reverse
x := float64(level) * l.DistLevel()
y -= l.paddingTop
x += l.paddingLeft
l.grid[participant][level] = pixel.V(x, y)
}
}
} // Indices would not be aligned with the screen coordinates. (Reverse Y - Rows)
updateGrid(&l)
// log.Println(l.grid) //
return &l
}
// -------------------------------------------------------------------------
// Important methods
// Draw guarantees the thread safety, though it's not a necessary condition.
// It is quite dangerous to access this struct's member (imdraw) directly from outside these methods.
func (l *Ladder) Draw(t pixel.Target) {
l.mutex.Lock()
defer l.mutex.Unlock()
if l.imd == nil { // isInvisible set to true.
return // An empty image is drawn.
}
l.imd.Draw(t)
}
// Update draws a ladder on an imdraw.
func (l *Ladder) Update() {
ptsMovedAbout := func(sub pixel.Vec, pts ...pixel.Vec) (ptsMoved []pixel.Vec) {
ptsMoved = make([]pixel.Vec, len(pts))
copy(ptsMoved, pts)
for i, vec := range ptsMoved {
ptsMoved[i] = vec.Sub(sub)
}
// log.Println(ptsMoved) // debug
// log.Println(pts) // debug
return ptsMoved
}
ptsStart := l.PtsAtLevelOfPicks()
ptsEnd := l.PtsAtLevelOfPrizes()
circleRadius := 20.0
circlePts := ptsMovedAbout(pixel.V(circleRadius+10, 0), ptsStart...)
// lock shared imdraw access
l.mutex.Lock()
defer l.mutex.Unlock()
// imdraw (a state machine)
if l.imd == nil { // lazy creation
l.imd = imdraw.New(nil)
}
imd := l.imd
imd.Clear()
// draw lanes
imd.Color = colornames.White
imd.EndShape = imdraw.RoundEndShape
for i := range ptsStart {
imd.Push(ptsStart[i], ptsEnd[i])
imd.Line(13)
}
// draw bridges
imd.Color = colornames.White
imd.EndShape = imdraw.RoundEndShape
for nrow, row := range l.bridges {
for ncol, e := range row {
if e {
imd.Push(l.grid[nrow][ncol], l.grid[nrow+1][ncol])
imd.Line(13)
}
}
}
// draw start points
imd.EndShape = imdraw.RoundEndShape
for i := range ptsStart {
imd.Color = l.colors[i]
imd.Push(circlePts[i])
}
imd.Circle(circleRadius, 0)
}
// -------------------------------------------------------------------------
// Read only methods
// Height returns the height of a ladder.
// A non-ptr Ladder as a read only argument passes lock by value within itself but that seems totally fine.
func (l Ladder) Height() float64 {
return l.bound.H()
}
// Width returns the width of a ladder.
// A non-ptr Ladder as a read only argument passes lock by value within itself but that seems totally fine.
func (l Ladder) Width() float64 {
return l.bound.W()
}
// DistLevel returns the distance between each level.
// A non-ptr Ladder as a read only argument passes lock by value within itself but that seems totally fine.
func (l Ladder) DistLevel() float64 {
return (l.Width() - (l.paddingLeft + l.paddingRight)) / float64(l.nLevel-1)
}
// DistParticipant returns the distance between each lane.
// A non-ptr Ladder as a read only argument passes lock by value within itself but that seems totally fine.
func (l Ladder) DistParticipant() float64 {
return (l.Height() - (l.paddingTop + l.paddingBottom)) / float64(l.nParticipants-1)
}
// PtsAtLevelOfPicks returns all starting points of a ladder.
// A non-ptr Ladder as a read only argument passes lock by value within itself but that seems totally fine.
func (l Ladder) PtsAtLevelOfPicks() (ret []pixel.Vec) {
const levelOfDraw int = 0 // where it starts
ret = make([]pixel.Vec, l.nParticipants, l.nParticipants)
for participant := range l.grid { // row
ret[participant] = l.grid[participant][levelOfDraw]
}
// log.Println(len(ret), ret) //
return ret //[:l.nParticipants] //
}
// PtAtLevelOfPicks returns a starting point of a ladder.
// A non-ptr Ladder as a read only argument passes lock by value within itself but that seems totally fine.
func (l Ladder) PtAtLevelOfPicks(participant int) pixel.Vec {
const levelOfDraw int = 0 // where it starts
return l.grid[participant][levelOfDraw]
}
// PtsAtLevelOfPrizes returns all end points of a ladder.
// A non-ptr Ladder as a read only argument passes lock by value within itself but that seems totally fine.
func (l Ladder) PtsAtLevelOfPrizes() (ret []pixel.Vec) {
levelOfPrize := l.nLevel - 1 // where it ends
ret = make([]pixel.Vec, l.nParticipants, l.nParticipants)
for participant := range l.grid { // row
ret[participant] = l.grid[participant][levelOfPrize]
}
// log.Println(len(ret), ret) //
return ret //[:l.nParticipants] //
}
// PtAtLevelOfPrizes returns an end point of a ladder.
// A non-ptr Ladder as a read only argument passes lock by value within itself but that seems totally fine.
func (l Ladder) PtAtLevelOfPrizes(participant int) pixel.Vec {
levelOfPrize := l.nLevel - 1 // where it ends
return l.grid[participant][levelOfPrize]
}
// -------------------------------------------------------------------
// Methods that write to itself
// ClearBridges of a ladder.
// Only values are changed, not the pointers.
func (l *Ladder) ClearBridges() {
for _, row := range l.bridges {
for i := range row {
row[i] = false
}
}
}
// GenerateRandomBridges of an approximate amount.
// Only values are changed, not the pointers.
func (l *Ladder) GenerateRandomBridges(amountApprox int) {
pickOneBridgeInRandom := func(l *Ladder) {
nRow := len(l.bridges)
nCol := l.nLevel
row := rand.Intn(int(nRow)) // participant
col := rand.Intn(int(nCol)) // level
// check right
isOkRight := func(rowRight, col int) bool {
includeLowerBound := func(rowRight int) bool {
return rowRight >= 0
}
if !includeLowerBound(rowRight) { // out of bound
return true
}
if !l.bridges[rowRight][col] {
return true
}
return false
}
// check left
isOkLeft := func(rowLeft, col int) bool {
excludeUpperBound := func(rowLeft int) bool {
return rowLeft < len(l.bridges)
}
if !excludeUpperBound(rowLeft) { // out of bound
return true
}
if !l.bridges[rowLeft][col] {
return true
}
return false
}
rowRight := row - 1
rowLeft := row + 1
if isOkRight(rowRight, col) && isOkLeft(rowLeft, col) {
l.bridges[row][col] = true
}
} // func
// repeat
for i := 0; i < amountApprox; i++ {
pickOneBridgeInRandom(l)
}
} // method
// RegenerateRandomBridges clears out all bridges and then GenerateRandomBridges() an approximate amount.
// Only values are changed, not the pointers.
func (l *Ladder) RegenerateRandomBridges(amountApprox int) {
l.ClearBridges()
l.GenerateRandomBridges(amountApprox)
}
// RegenerateRandomColors sets all colors of a ladder random for each.
// Only values are changed, not the pointers.
func (l *Ladder) RegenerateRandomColors() {
for i := range l.colors {
l.colors[i] = gg.RandomNiceColor()
}
}
// Reset all bridges and colors.
// Only values are changed, not the pointers.
func (l *Ladder) Reset() {
aboutTwo := l.nParticipants * (l.nLevel - 1) * 2
aboutOne := l.nParticipants * (l.nLevel - 1)
aboutHalf := (l.nParticipants * (l.nLevel - 1)) / 2
//
var pick [4]int
pick[0] = aboutTwo
pick[1] = aboutOne
pick[2] = aboutHalf
i := rand.Intn(3)
// log.Println(i) //
//
l.RegenerateRandomBridges(pick[i])
l.RegenerateRandomColors()
}