-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtripled.go
390 lines (363 loc) · 7.16 KB
/
tripled.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Package tripled implements Triple Diamond slots logic.
package tripled
import (
"fmt"
"maps"
"math"
"math/rand"
"slices"
"strconv"
"strings"
)
// DefaultShuffles is the default shuffles.
var DefaultShuffles = 3
// DefaultPayout is the default payout.
var DefaultPayout = 0.97
// DefaultDist is the default dist.
var DefaultDist = NewDist(DefaultPayout)
// Rand is the shared random interface.
type Rand interface {
Intn(n int) int
}
// Dist is a dist.
type Dist struct {
strip [9][][]int
}
// NewDist creates a dist with the specified return-to-player (RTP) value.
func NewDist(rtp float64) *Dist {
if rtp <= 0.0 || 1.0 <= rtp {
panic("invalid f")
}
d := new(Dist)
for n := range 9 {
var payout int
var w, l [][]int
for i := range 22 {
for j := range 22 {
for k := range 22 {
res := NewResult(n+1, i, j, k)
if res.Payout != 0 {
w = append(w, res.Pos)
payout += res.Payout // delta
} else {
l = append(l, res.Pos)
}
}
}
}
// fill
d.strip[n] = make([][]int, int(math.Ceil(float64(payout)/float64(n+1)/rtp)))
copy(d.strip[n], w)
for i := len(w); i < len(d.strip[n]); i += len(l) {
copy(d.strip[n][i:], l)
}
// shuffle
r := rand.New(rand.NewSource(1788975))
for range DefaultShuffles {
r.Shuffle(len(d.strip[n]), func(i, j int) {
d.strip[n][i], d.strip[n][j] = d.strip[n][j], d.strip[n][i]
})
}
}
return d
}
// Spin spins the reels.
func (d *Dist) Spin(r Rand, lines int) (Result, error) {
// validate
if lines < 1 || len(Lines) < lines {
return Result{}, ErrInvalidLines
}
return NewResult(lines, d.strip[lines-1][r.Intn(len(d.strip[lines-1]))]...), nil
}
// Spin spins the reels, calculating the results for the spin.
func Spin(r Rand, lines int) (Result, error) {
// validate
if lines <= 0 || len(Lines) < lines {
return Result{}, ErrInvalidLines
}
// randomize reel positions
pos := make([]int, len(Reels))
for i := range Reels {
pos[i] = r.Intn(len(Reels[i]))
}
return NewResult(lines, pos...), nil
}
// Result is a spin result.
type Result struct {
Pos []int `json:"pos"`
Lines map[int]int `json:"lines"`
Payout int `json:"payout"`
}
// NewResult creates a result with the specified reel positions.
func NewResult(lines int, pos ...int) Result {
if len(pos) != len(Reels) {
panic(fmt.Sprintf("must have %d pos", len(Reels)))
}
res := Result{
Pos: pos,
Lines: make(map[int]int),
}
symbols := Symbols(pos...)
// line payout
for i := range lines {
if d := Payout(Lines[i], symbols...); d != 0 {
res.Lines[i] = d
res.Payout += d
}
}
return res
}
// Format satisfies the [fmt.Formatter] interface.
func (res Result) Format(f fmt.State, _ rune) {
fmt.Fprintln(f, "pos:", res.Pos[0], res.Pos[1], res.Pos[2])
fmt.Fprintln(f, res.Symbols())
if len(res.Lines) > 0 {
fmt.Fprintln(f, "lines:")
for _, n := range slices.Sorted(maps.Keys(res.Lines)) {
fmt.Fprintf(f, "% 2d pays %dx\n", n+1, res.Lines[n])
}
}
fmt.Fprintf(f, "payout: %dx", res.Payout)
}
// Symbols produces a string representing the final view of the result.
func (res Result) Symbols() string {
return SymbolsString(res.Pos...)
}
// Symbol is a slot symbol.
type Symbol int
// Symbols.
const (
Blank Symbol = iota
Bar1
Bar2
Bar3
Seven
Diamond
)
// Format satisfies the [fmt.Formatter] interface.
func (s Symbol) Format(f fmt.State, verb rune) {
var buf []byte
switch verb {
case 's', 'v':
buf = append(buf, s.Name()...)
case 'c':
buf = append(buf, string(s.Rune())...)
case 'd':
buf = append(buf, strconv.Itoa(int(s))...)
default:
buf = append(buf, fmt.Sprintf("%%!%c(ERROR=unknown verb, symbol: %d)", verb, int(s))...)
}
_, _ = f.Write(buf)
}
// Name returns the symbol name.
func (s Symbol) Name() string {
switch s {
case Bar1:
return "Bar1"
case Bar2:
return "Bar2"
case Bar3:
return "Bar3"
case Seven:
return "Seven"
case Diamond:
return "Diamond"
}
return "Blank"
}
// Rune returns the symbol rune.
func (s Symbol) Rune() rune {
switch s {
case Bar1:
return '-'
case Bar2:
return '='
case Bar3:
return '≡'
case Seven:
return '7'
case Diamond:
return '◆'
}
return '∙'
}
// Error is an error.
type Error string
// Error satisfies the [error] interface.
func (err Error) Error() string {
return string(err)
}
// Errors.
const (
// ErrInvalidLines is the invalid lines error.
ErrInvalidLines Error = "invalid lines"
)
// Symbols returns the symbols in the positions.
func Symbols(pos ...int) []Symbol {
symbols := make([]Symbol, 9)
for j := range 3 {
for i := range 3 {
n := len(Reels[i])
symbols[i+j*3] = Reels[i][(((pos[i]+j-1)%n)+n)%n]
}
}
return symbols
}
// SymbolsString returns the symbols as a string.
func SymbolsString(pos ...int) string {
var sb strings.Builder
for i, s := range Symbols(pos...) {
if i != 0 && i%3 == 0 {
_ = sb.WriteByte('\n')
}
_, _ = sb.WriteRune(s.Rune())
}
return sb.String()
}
// Payout determines the payout for matching line symbols based on the bit
// mask.
func Payout(mask int, line ...Symbol) int {
d, s, b3, b2, b1, n := 0, 0, 0, 0, 0, 0
for i := range line {
if line[i] == Blank || mask&(1<<i) == 0 {
continue
}
switch line[i] {
case Diamond:
d++
case Seven:
s++
case Bar3:
b3++
case Bar2:
b2++
case Bar1:
b1++
}
n++
}
switch {
case d == 3:
return 1199 // 3x diamond -- 1199x
case n == 3: // diamond multiplier
switch mlt := int(math.Pow(3, float64(d))); n - d {
case s:
return mlt * 100 // 3x Seven -- 100x
case b3:
return mlt * 40 // 3x Bar3 -- 40x
case b2:
return mlt * 20 // 3x Bar2 -- 20x
case b1:
return mlt * 10 // 3x Bar1 -- 10x
case b3 + b2 + b1:
return mlt * 5 // Any Bar -- 5x
}
}
// 2 or more diamonds
switch d {
case 2:
return 10
case 1:
return 2
}
return 0
}
// Reels are reels of slot symbols.
var Reels = [3][22]Symbol{
{
Blank, // 0
Bar1, // 1
Blank, // 2
Bar2, // 3
Blank, // 4
Bar1, // 5
Blank, // 6
Bar3, // 7
Blank, // 8
Bar1, // 9
Blank, // 10
Bar2, // 11
Blank, // 12
Bar1, // 13
Blank, // 14
Seven, // 15
Blank, // 16
Bar1, // 17
Blank, // 18
Bar1, // 19
Blank, // 20
Diamond, // 21
},
{
Blank, // 0
Blank, // 1
Bar2, // 2
Blank, // 3
Bar1, // 4
Blank, // 5
Bar2, // 6
Blank, // 7
Bar3, // 8
Blank, // 9
Seven, // 10
Blank, // 11
Bar2, // 12
Blank, // 13
Bar2, // 14
Blank, // 15
Bar3, // 16
Blank, // 17
Bar2, // 18
Blank, // 19
Blank, // 20
Diamond, // 21
},
{
Blank, // 0
Blank, // 1
Bar1, // 2
Blank, // 3
Bar2, // 4
Blank, // 5
Bar1, // 6
Blank, // 7
Bar3, // 8
Blank, // 9
Seven, // 10
Blank, // 11
Bar1, // 12
Blank, // 13
Bar1, // 14
Blank, // 15
Bar2, // 16
Blank, // 17
Bar1, // 18
Blank, // 19
Blank, // 20
Diamond, // 21
},
}
// Lines are pay line masks.
var Lines = [9]int{
0: cw | cc | ce,
1: nw | nc | ne,
2: sw | sc | se,
3: nw | cc | se,
4: sw | cc | ne,
5: cw | nc | ce,
6: cw | sc | ce,
7: sw | cc | se,
8: nw | cc | ne,
}
// coordinates.
const (
nw = 1 << iota
nc
ne
cw
cc
ce
sw
sc
se
)