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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
package game
import (
"fmt"
"math"
"sort"
"strconv"
"strings"
"thehouseoficarus/internal/casino"
"thehouseoficarus/internal/color"
"thehouseoficarus/internal/net"
)
// Roulette art is rendered per recipient, just like cards. Never embed ANSI
// in engine snapshots: spectators may have different color/Unicode settings.
func renderRouletteSnapshot(s *casino.RouletteSnapshot, mode string, unicode bool, size int) string {
switch s.Phase {
case "playing":
return renderRouletteWheel(s.Wheel, mode, unicode)
case "result":
return renderRouletteResult(s.Pockets, mode, unicode, size)
default:
return renderRouletteLayout(s.DoubleZero, s.Racetrack, mode, unicode)
}
}
func rouletteInk(mode, text string) string {
return color.Render(mode, color.ColorSpec{Fg: 15, Bg: -1}, text)
}
// Use the basic palette so both ANSI-16 and xterm256 give white-on-red,
// white-on-green and white-on-black, rather than approximating a gradient.
func roulettePaint(mode, text string, bg int) string {
return color.Render(mode, color.ColorSpec{Fg: 15, Bg: bg, Bold: true}, text)
}
func roulettePocketColor(pocket string) (int, string) {
if pocket == "0" || pocket == "00" {
return 2, "GREEN"
}
n, _ := strconv.Atoi(pocket)
switch n {
case 1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36:
return 1, "RED"
default:
return 0, "BLACK"
}
}
func rouletteCenter(text string, width int) string {
pad := max(0, width-color.VisibleLen(text))
return strings.Repeat(" ", pad/2) + text + strings.Repeat(" ", pad-pad/2)
}
type rouletteLines struct {
h, v, tl, tr, bl, br, teeTop, teeBottom, teeLeft, teeRight, cross string
// zeroTL/zeroBL are the zero lane's outer corners. They are rounded while
// the rest of the board keeps square joints, so the tall green zero panel
// reads as a soft-edged block without disturbing the grid's connections.
zeroTL, zeroBL string
}
func rouletteBorders(unicode bool) rouletteLines {
if unicode {
return rouletteLines{"─", "│", "┌", "┐", "└", "┘", "┬", "┴", "├", "┤", "┼", "╭", "╰"}
}
return rouletteLines{"-", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+"}
}
func repeatRoulette(s string, n int) []string {
parts := make([]string, n)
for i := range parts {
parts[i] = s
}
return parts
}
// renderRouletteRacetrack draws the French call-bet track above the board. The
// numbers are shown in wheel order for reference; the named bets are what the
// player places. The Neighbors line explains the one bet that takes a number.
func renderRouletteRacetrack(mode string, unicode bool) string {
b := rouletteBorders(unicode)
tl, tr, bl, br := b.tl, b.tr, b.bl, b.br
if unicode {
tl, tr, bl, br = "╭", "╮", "╰", "╯"
}
// The panel matches the betting board's 74-column footprint.
const width = 72
bets := casino.RacetrackBets()
rows := make([]string, 0, len(bets)+1)
for _, bet := range bets {
label := fmt.Sprintf("%s (%d)", bet.Name, bet.Chips)
rows = append(rows, fmt.Sprintf("%-22s %s", label, strings.Join(bet.Series, " ")))
}
rows = append(rows, fmt.Sprintf("%-22s %s", "Neighbors (9)", "neighbors <n>: n plus four each side"))
lines := []string{rouletteInk(mode, tl+strings.Repeat(b.h, width)+tr)}
for _, row := range rows {
row = " " + row
if pad := width - color.VisibleLen(row); pad > 0 {
row += strings.Repeat(" ", pad)
}
lines = append(lines, rouletteInk(mode, b.v+row+b.v))
}
lines = append(lines, rouletteInk(mode, bl+strings.Repeat(b.h, width)+br))
return strings.Join(lines, "\n")
}
// renderRouletteLayout draws the betting board. Green is reserved for the
// 0/00 lane: every other cell is red, black, or plain. The odds reminder is
// not part of the board (it is shown by the "table" command), so the board
// stays compact enough to sit in a betting menu.
func renderRouletteLayout(double, racetrack bool, mode string, unicode bool) string {
// The call bets are a single-zero layout; never draw the track on an
// American board even if a malformed snapshot asks for it.
racetrack = racetrack && !double
b := rouletteBorders(unicode)
var lines []string
if racetrack {
lines = append(lines, renderRouletteRacetrack(mode, unicode))
}
// Five columns for the zero lane, twelve four-column pockets, and a
// six-column 2:1 lane on the right holding the row bets. Four adjacent
// pockets line up exactly over each 19-column dozen.
const rowCell = 6
gridRule := func(left, joint, right string) string {
return left + strings.Repeat(b.h, 5) + joint +
strings.Join(repeatRoulette(strings.Repeat(b.h, 4), 12), joint) +
joint + strings.Repeat(b.h, rowCell) + right
}
// The numbered grid's bottom edge closes the row-bet cells and carries the
// dozen-row dividers as downward stubs: crosses at the inner dozen
// boundaries and where the grid meets the row lane, an upward tee
// everywhere else.
gridBottom := func() string {
var sb strings.Builder
sb.WriteString(b.zeroBL)
for col := 6; col <= 66; col += 5 {
if col == 6 {
sb.WriteString(strings.Repeat(b.h, 5))
} else {
sb.WriteString(strings.Repeat(b.h, 4))
}
switch col {
case 6, 26, 46, 66:
sb.WriteString(b.cross)
default:
sb.WriteString(b.teeBottom)
}
}
sb.WriteString(strings.Repeat(b.h, rowCell))
sb.WriteString(b.br)
return sb.String()
}
lines = append(lines, rouletteInk(mode, gridRule(b.zeroTL, b.teeTop, b.tr)))
for row := 3; row >= 1; row-- {
zero := ""
switch {
case double && row == 3:
zero = "0"
case double && row == 1:
zero = "00"
case !double && row == 2:
zero = "0"
}
if mode == "none" || mode == "" {
zero += "G"
}
// The 0/00 lane is one green rectangle, so its blank cells above and
// below the zeros are painted to match the pockets beside them.
line := rouletteInk(mode, b.v) + roulettePaint(mode, rouletteCenter(zero, 5), 2) + rouletteInk(mode, b.v)
for n := row; n <= 36; n += 3 {
pocket := strconv.Itoa(n)
bg, name := roulettePocketColor(pocket)
if mode == "none" || mode == "" {
pocket += name[:1]
}
line += roulettePaint(mode, rouletteCenter(pocket, 4), bg) + rouletteInk(mode, b.v)
}
// row1 is the top numbered line (3,6,...36) and row3 the bottom.
line += rouletteInk(mode, rouletteCenter(fmt.Sprintf("row%d", 4-row), rowCell)+b.v)
lines = append(lines, line)
if row > 1 {
// The zero lane is one green rectangle, so the two separator rows
// are painted to match the cells above and below them.
line = rouletteInk(mode, b.v) + roulettePaint(mode, " ", 2) + rouletteInk(mode, b.teeLeft+
strings.Join(repeatRoulette(strings.Repeat(b.h, 4), 12), b.cross)+
b.cross+strings.Repeat(b.h, rowCell)+b.teeRight)
lines = append(lines, line)
}
}
lines = append(lines, rouletteInk(mode, gridBottom()))
// Outside bets share the exact 60-column numbered-grid footprint. The
// dozen row gets its own bottom rule so the 1to12/13to24/25to36 boxes are
// closed and do not run into the even-money spaces below them.
indent := " "
dozen := indent + rouletteInk(mode, b.v)
for _, label := range []string{"1to12", "13to24", "25to36"} {
dozen += rouletteInk(mode, rouletteCenter(label, 19)) + rouletteInk(mode, b.v)
}
lines = append(lines, dozen)
dozenBottom := indent + b.teeLeft
dozenJoints := []string{b.teeTop, b.cross, b.teeTop, b.cross, b.teeTop, b.teeRight}
for i := 0; i < 6; i++ {
dozenBottom += strings.Repeat(b.h, 9) + dozenJoints[i]
}
lines = append(lines, rouletteInk(mode, dozenBottom))
line := indent + rouletteInk(mode, b.v)
for _, bet := range []string{"low", "even", "red", "black", "odd", "high"} {
bg := -1
if bet == "red" {
bg = 1
}
if bet == "black" {
bg = 0
}
line += roulettePaint(mode, rouletteCenter(bet, 9), bg) + rouletteInk(mode, b.v)
}
// The bottom rule tees upward under each outside-bet divider so the
// vertical lines below the dozens meet the table's bottom edge.
bottom := indent + b.bl
for i := 0; i < 6; i++ {
bottom += strings.Repeat(b.h, 9)
if i < 5 {
bottom += b.teeBottom
}
}
bottom += b.br
lines = append(lines, line, rouletteInk(mode, bottom))
if mode == "none" || mode == "" {
lines = append(lines, "R = red, B = black, G = green")
}
return strings.Join(lines, "\n")
}
type rouletteCell struct {
r rune
bg int
}
// renderRouletteWheel draws a representative spinning wheel. There are no
// numbers: only the rim carries solid red and black pockets, alternating like
// a real wheel, with one green zero (plus its diametrically opposite pocket on
// American wheels). A simple plus marks the croupier's spindle. Without color
// the pockets fall back to R/B/G so the wheel still reads.
func renderRouletteWheel(wheel []string, mode string, unicode bool) string {
double := false
for _, p := range wheel {
if p == "00" {
double = true
}
}
const width, height = 17, 9
// Half-widths are hand-rounded so the 17x9 grid reads as a circle once the
// terminal's roughly 2:1 character cells are taken into account. The top
// and bottom rows stay wide instead of collapsing to a single cell.
halfWidths := [height]int{2, 5, 7, 8, 8, 8, 7, 5, 2}
cx, cy := float64(width-1)/2, float64(height-1)/2
rx, ry := cx, cy
grid := make([][]rouletteCell, height)
for y := range grid {
grid[y] = make([]rouletteCell, width)
for x := range grid[y] {
grid[y][x] = rouletteCell{' ', -1}
}
}
plain := mode == "none" || mode == ""
pocketRune := func(bg int) rune {
if !plain {
return ' '
}
switch bg {
case 1:
return 'R'
case 2:
return 'G'
default:
return 'B'
}
}
// Collect the rim cells in angular order so the pockets can alternate
// around the circumference instead of filling the disc with wedges.
type rimCell struct {
x, y int
ang float64
}
var rim []rimCell
for y := 0; y < height; y++ {
hw := halfWidths[y]
for x := int(cx) - hw; x <= int(cx)+hw; x++ {
nx := (float64(x) - cx) / rx
ny := (float64(y) - cy) / ry
if nx*nx+ny*ny >= 0.75 {
rim = append(rim, rimCell{x, y, math.Atan2(ny, nx)})
}
}
}
sort.Slice(rim, func(i, j int) bool {
if rim[i].ang != rim[j].ang {
return rim[i].ang < rim[j].ang
}
return rim[i].x < rim[j].x
})
// The top and bottom rows hold an odd number of cells, so anchor the walk
// there and leave those centre pockets one wide while every other pocket
// takes two adjacent rim cells.
topIdx, botIdx := -1, -1
for i, c := range rim {
switch {
case c.x == int(cx) && c.y == 0:
topIdx = i
case c.x == int(cx) && c.y == height-1:
botIdx = i
}
}
pocketIndex := make([]int, len(rim))
cur := 0
pocketIndex[topIdx] = cur
for i := (topIdx + 1) % len(rim); i != topIdx; {
cur++
pocketIndex[i] = cur
if i == botIdx {
i = (i + 1) % len(rim)
continue
}
i = (i + 1) % len(rim)
pocketIndex[i] = cur
i = (i + 1) % len(rim)
}
pockets := cur + 1
greenPocket := 0
for i, pocket := range wheel {
if pocket == "0" && len(wheel) > 0 {
greenPocket = i * pockets / len(wheel)
break
}
}
opposite := (greenPocket + pockets/2) % pockets
for i, c := range rim {
p := pocketIndex[i]
bg := 1
if p%2 == 1 {
bg = 0
}
if p == greenPocket || (double && p == opposite) {
bg = 2
}
grid[c.y][c.x] = rouletteCell{pocketRune(bg), bg}
}
// The croupier spins the wheel by its central spindle: a simple plus.
plus := []string{
" | ",
" | ",
"---+---",
" | ",
" | ",
}
if unicode {
plus = []string{
" │ ",
" │ ",
"───┼───",
" │ ",
" │ ",
}
}
baseX, baseY := int(cx)-len([]rune(plus[0]))/2, int(cy)-len(plus)/2
for dy, row := range plus {
for dx, r := range []rune(row) {
if r == ' ' {
continue
}
grid[baseY+dy][baseX+dx] = rouletteCell{r, -1}
}
}
lines := make([]string, 0, height)
for _, row := range grid {
lines = append(lines, renderRouletteRow(row, mode))
}
return strings.Join(lines, "\n")
}
func renderRouletteRow(row []rouletteCell, mode string) string {
last := len(row)
for last > 0 && row[last-1].r == ' ' && row[last-1].bg < 0 {
last--
}
if last == 0 {
return ""
}
var sb strings.Builder
for i := 0; i < last; {
bg := row[i].bg
j := i
var seg []rune
for j < last && row[j].bg == bg {
seg = append(seg, row[j].r)
j++
}
text := string(seg)
if bg >= 0 && mode != "none" && mode != "" {
sb.WriteString(roulettePaint(mode, text, bg))
} else {
sb.WriteString(rouletteInk(mode, text))
}
i = j
}
return sb.String()
}
// renderRouletteResult draws the winning pocket and its two physical
// neighbours at playing-card size. The pocket number sits on the top row, a
// rule separates it from the ball (one row above centre), and the color is
// spelled out on the bottom row only when color is off. There is no
// surrounding prose.
func renderRouletteResult(pockets [3]string, mode string, unicode bool, size int) string {
size = cardRenderSize(unicode, size)
if size < 5 {
size = 5
}
width := size + 2
b := rouletteBorders(unicode)
ball := "o"
if unicode {
ball = "●"
}
// The rule needs a spare row, so the smallest cards put the ball directly
// under the number instead.
const numberRow, ruleRow = 1, 2
ballRow := size / 2
hasRule := ballRow > ruleRow
colorRow := size - 2
plain := mode == "none" || mode == ""
var lines []string
for row := 0; row < size; row++ {
var cells []string
for i, pocket := range pockets {
bg, name := roulettePocketColor(pocket)
var text string
switch {
case row == 0:
text = b.tl + strings.Repeat(b.h, width-2) + b.tr
case row == size-1:
text = b.bl + strings.Repeat(b.h, width-2) + b.br
case hasRule && row == ruleRow:
text = b.teeLeft + strings.Repeat(b.h, width-2) + b.teeRight
default:
content := ""
if row == numberRow {
content = pocket
}
if i == 1 && row == ballRow {
content = ball
}
if plain && row == colorRow {
content = name
}
text = b.v + rouletteCenter(content, width-2) + b.v
}
cells = append(cells, roulettePaint(mode, text, bg))
}
lines = append(lines, strings.Join(cells, " "))
}
return strings.Join(lines, "\n")
}
// rouletteReminder is the odds/cheat-sheet block, shown only by "table" (and
// "table rules"), never with the ambient betting board. Racetrack tables also
// list their call-bet syntax.
func (g *Game) rouletteReminder(sess *net.Session, cfg casino.TableConfig) string {
text := "Numbers 35:1 | Split 17:1 | Street/trio 11:1 | Corner 8:1 | Line 5:1 | Dozens & rows 2:1 | Outside 1:1\n" +
"Combos: split 1 2 | street 1 2 3 | corner 1 2 4 5 | line 1 2 3 4 5 6 | trio 0 1 2 | basket (double zero)\n"
if cfg.Roulette.Racetrack {
text += "Racetrack: bet 10 on voisins | tiers | orphelins | zero spiel | neighbors 17\n"
}
text += "Low: 1-18 | High: 19-36 | Zero loses all outside bets\n" +
"bet 10 on 17 / bet 10 on red / bet 10 on split 1 2"
return g.colorize(sess, "casino_action", text)
}
func (g *Game) renderCasinoRules(sess *net.Session, cfg casino.TableConfig) string {
rules := g.colorize(sess, "casino_action", cfg.RulesText())
if cfg.Game == casino.GameRoulette {
snap := &casino.RouletteSnapshot{DoubleZero: cfg.Roulette.DoubleZero, Racetrack: cfg.Roulette.Racetrack, Phase: "betting", Wheel: casino.WheelOrder(cfg.Roulette.DoubleZero)}
rules += "\n" + g.renderCasinoSnapshot(sess, snap) + "\n" + g.rouletteReminder(sess, cfg)
}
return rules
}
|