aboutsummaryrefslogtreecommitdiff
path: root/internal/game/render_craps.go
blob: 12714dcc7a5034f6b8950a2d511f01daa96e09cf (plain)
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
package game

import (
	"fmt"
	"strings"

	"thehouseoficarus/internal/casino"
	"thehouseoficarus/internal/color"
)

// A half-table: numbered boxes and the come/field/line rails on the left;
// the shared centre-action bets on the right. Every region is a real box,
// and shared edges are joined rather than pasted together as broken borders.
const crapsFelt = 22 // #005f00: darker than ANSI green / the roulette felt.
const (
	crapsInk = iota
	crapsGold
	crapsRed
	crapsMuted
	crapsMarked
	crapsPuckOn
	crapsPuckOff
)

var crapsPaint = []color.ColorSpec{
	{Fg: 15, Bg: crapsFelt},
	{Fg: 229, Bg: crapsFelt, Bold: true},
	{Fg: 217, Bg: crapsFelt, Bold: true},
	{Fg: 108, Bg: crapsFelt},
	{Fg: 0, Bg: 220, Bold: true},
	{Fg: 0, Bg: 15, Bold: true},
	{Fg: 15, Bg: 0, Bold: true},
}

type crapsCell struct {
	glyph rune
	edges uint8
	ink   int
}
type crapsCanvas struct{ cells [][]crapsCell }

// crapsCanvasWidth is the fixed felt width every craps render uses.
const crapsCanvasWidth = 77

func newCrapsCanvas(h int) *crapsCanvas {
	c := &crapsCanvas{cells: make([][]crapsCell, h)}
	for y := range c.cells {
		c.cells[y] = make([]crapsCell, crapsCanvasWidth)
	}
	return c
}
func (c *crapsCanvas) box(x1, y1, x2, y2 int) {
	// N=1, E=2, S=4, W=8. Unioning edges makes all T/cross joints exact.
	for x := x1; x < x2; x++ {
		c.cells[y1][x].edges |= 2
		c.cells[y1][x+1].edges |= 8
		c.cells[y2][x].edges |= 2
		c.cells[y2][x+1].edges |= 8
	}
	for y := y1; y < y2; y++ {
		c.cells[y][x1].edges |= 4
		c.cells[y+1][x1].edges |= 1
		c.cells[y][x2].edges |= 4
		c.cells[y+1][x2].edges |= 1
	}
}
func (c *crapsCanvas) text(x, y int, s string, ink int) {
	for _, r := range s {
		if x >= 0 && x < len(c.cells[y]) {
			c.cells[y][x] = crapsCell{glyph: r, ink: ink}
		}
		x++
	}
}
func (c *crapsCanvas) center(x1, x2, y int, s string, ink int) {
	c.text(x1+(x2-x1+1-color.VisibleLen(s))/2, y, s, ink)
}
func (c *crapsCanvas) render(mode string, unicode bool) string {
	joints := map[uint8]rune{3: '└', 6: '┌', 9: '┘', 12: '┐', 7: '├', 11: '┴', 13: '┤', 14: '┬', 15: '┼'}
	var out strings.Builder
	for _, row := range c.cells {
		var run strings.Builder
		ink := row[0].ink
		flush := func() {
			if run.Len() > 0 {
				out.WriteString(color.Render(mode, crapsPaint[ink], run.String()))
				run.Reset()
			}
		}
		for _, cell := range row {
			if cell.ink != ink {
				flush()
				ink = cell.ink
			}
			r := cell.glyph
			if r == 0 {
				r = ' '
				if cell.edges != 0 {
					switch {
					case cell.edges&5 == 0:
						r = '─'
					case cell.edges&10 == 0:
						r = '│'
					default:
						r = joints[cell.edges]
					}
					if !unicode {
						switch r {
						case '─':
							r = '-'
						case '│':
							r = '|'
						default:
							r = '+'
						}
					}
				}
			}
			run.WriteRune(r)
		}
		flush()
		out.WriteByte('\n')
	}
	return out.String()
}
func crapsHas(ns []int, n int) bool {
	for _, v := range ns {
		if v == n {
			return true
		}
	}
	return false
}

// Die-face glyph, e.g. ⚀ for one, used only where a felt paints dice pictures:
// the hardway pairs and horn combinations. Blobs come first so a die face
// never lexes as 1–6 elsewhere.
func crapsDie(n int) string {
	return string([]rune{'⚀', '⚁', '⚂', '⚃', '⚄', '⚅'}[n-1])
}
func renderCrapsTrackers(s *casino.CrapsSnapshot, mode string, unicode bool) string {
	c := newCrapsCanvas(12)
	c.box(0, 0, 76, 2)
	c.box(0, 5, 38, 8)
	c.box(38, 5, 76, 8)
	c.text(3, 1, "FIRE", crapsGold)
	for i, n := range []int{4, 5, 6, 8, 9, 10} {
		ink, mark := crapsMuted, " "
		if crapsHas(s.Made, n) {
			ink, mark = crapsMarked, "*"
		}
		c.text(15+i*9, 1, fmt.Sprintf("[%2d%s]", n, mark), ink)
	}
	bounds := []int{0, 25, 50, 76}
	for i, payout := range []string{"24:1", "249:1", "999:1"} {
		lo, hi := bounds[i], bounds[i+1]
		c.box(lo, 2, hi, 5)
		c.center(lo+1, hi-1, 3, fmt.Sprintf("%d DISTINCT POINTS", i+4), crapsGold)
		c.center(lo+1, hi-1, 4, payout, crapsInk)
	}
	c.text(2, 6, "ALL SMALL", crapsGold)
	c.text(40, 6, "TALL", crapsGold)
	for i, n := range []int{2, 3, 4, 5, 6, 8, 9, 10, 11, 12} {
		x := 12 + i*5
		if i >= 5 {
			x = 48 + (i-5)*5
		}
		ink, mark := crapsMuted, " "
		if crapsHas(s.Seen, n) {
			ink, mark = crapsMarked, "*"
		}
		c.text(x, 6, fmt.Sprintf("%2d%s ", n, mark), ink)
	}
	c.center(1, 37, 7, "* = rolled", crapsMuted)
	c.center(39, 75, 7, "* = rolled", crapsMuted)
	for i, row := range [][2]string{{"SMALL", "34:1"}, {"ALL", "175:1"}, {"TALL", "34:1"}} {
		lo, hi := bounds[i], bounds[i+1]
		c.box(lo, 8, hi, 11)
		c.center(lo+1, hi-1, 9, row[0], crapsGold)
		c.center(lo+1, hi-1, 10, row[1], crapsInk)
	}
	return c.render(mode, unicode)
}
func renderCrapsFelt(s *casino.CrapsSnapshot, mode string, unicode bool) string {
	c := newCrapsCanvas(26)
	c.box(0, 0, 14, 6)
	c.center(1, 13, 4, "DON'T COME", crapsInk)
	c.center(1, 13, 5, "BAR 12", crapsMuted)
	nums := []int{4, 5, 6, 8, 9, 10}
	labels := []string{"4", "5", "SIX", "8", "NINE", "10"}
	pays := []string{"9:5", "7:5", "7:6", "7:6", "7:5", "9:5"}
	puck := func(x int, on bool) {
		ink, word := crapsPuckOff, "OFF"
		if on {
			ink, word = crapsPuckOn, "ON "
		}
		a, b, d := "+---+", "|"+word+"|", "+---+"
		if unicode {
			a, b, d = "╭───╮", "│"+word+"│", "╰───╯"
		}
		c.text(x, 1, a, ink)
		c.text(x, 2, b, ink)
		c.text(x, 3, d, ink)
	}
	if s.Point == 0 {
		puck(5, false)
	}
	for i, n := range nums {
		x := 14 + i*6
		c.box(x, 0, x+6, 6)
		c.center(x+1, x+5, 4, labels[i], crapsGold)
		c.center(x+1, x+5, 5, pays[i], crapsMuted)
		if s.Point == n {
			puck(x+1, true)
		}
	}
	c.box(0, 6, 50, 10)
	c.center(1, 49, 7, "PLACE / BUY / LAY ON THE NUMBERS ABOVE", crapsMuted)
	c.center(1, 49, 9, "C O M E", crapsRed)
	c.box(0, 10, 50, 16)
	c.center(1, 49, 11, "FIELD", crapsGold)
	c.center(1, 49, 13, "( 2 )    3    4    9    10    11    ( 12 )", crapsInk)
	twelve := s.Field12
	if twelve == 0 {
		twelve = 2
	}
	c.center(1, 49, 15, fmt.Sprintf("2 PAYS DOUBLE               12 PAYS %d:1", twelve), crapsGold)
	c.box(0, 16, 50, 19)
	c.center(1, 49, 18, "DON'T PASS   /   BAR 12", crapsInk)
	c.box(0, 19, 50, 22)
	c.center(1, 49, 21, "PASS LINE", crapsGold)
	c.box(0, 22, 50, 25)
	c.center(1, 49, 24, "FREE ODDS  /  BEHIND THE LINE", crapsInk)
	// Centre action: four bonus spots, four hardways, six propositions, horn/hop.
	bonus := []struct {
		label, pays string
	}{{"FIRE", "24x"}, {"SMALL", "34:1"}, {"ALL", "175:1"}, {"TALL", "34:1"}}
	xs := []int{50, 56, 63, 69, 76}
	for i, spot := range bonus {
		c.box(xs[i], 0, xs[i+1], 5)
		c.center(xs[i]+1, xs[i+1]-1, 2, spot.label, crapsGold)
		c.center(xs[i]+1, xs[i+1]-1, 4, spot.pays, crapsMuted)
	}
	for i, n := range []int{4, 6, 8, 10} {
		x, y := 50+(i%2)*13, 5+(i/2)*4
		c.box(x, y, x+13, y+4)
		c.center(x+1, x+12, y+1, fmt.Sprintf("HARD %d", n), crapsInk)
		pay := 7
		if n == 6 || n == 8 {
			pay = 9
		}
		pair := fmt.Sprintf("%d+%d", n/2, n/2)
		if unicode {
			pair = crapsDie(n/2) + "+" + crapsDie(n/2)
		}
		c.center(x+1, x+12, y+3, fmt.Sprintf("%s   %d:1", pair, pay), crapsGold)
	}
	props := []string{"SEVEN", "CRAPS", "ELEVEN", "ACES", "ACE 3", "BOXCARS"}
	odds := []string{"4:1", "7:1", "15:1", "30:1", "15:1", "30:1"}
	px := []int{50, 59, 67, 76}
	for i, label := range props {
		col, y := i%3, 13+(i/3)*4
		x, x2 := px[col], px[col+1]
		c.box(x, y, x2, y+4)
		c.center(x+1, x2-1, y+1, label, crapsRed)
		c.center(x+1, x2-1, y+3, odds[i], crapsInk)
	}
	c.box(50, 21, 63, 25)
	c.center(51, 62, 22, "HORN", crapsGold)
	horn := "2  3  11  12"
	if unicode {
		// Horn combinations as painted dice pairs: 2, 3, 11, 12.
		horn = strings.Join([]string{crapsDie(1) + crapsDie(1), crapsDie(1) + crapsDie(2), crapsDie(5) + crapsDie(6), crapsDie(6) + crapsDie(6)}, " ")
	}
	c.center(51, 62, 24, horn, crapsMuted)
	c.box(63, 21, 76, 25)
	c.center(64, 75, 22, "HOP", crapsGold)
	c.center(64, 75, 24, "15:1 / 30:1", crapsMuted)
	return c.render(mode, unicode)
}
func renderCrapsDice(dice [2]int, mode string, unicode bool, style string) string {
	if dice[0] < 1 || dice[0] > 6 || dice[1] < 0 || dice[1] > 6 {
		return ""
	}
	patterns := [][3]string{{}, {"     ", "  o  ", "     "}, {"o    ", "     ", "    o"}, {"o    ", "  o  ", "    o"}, {"o   o", "     ", "o   o"}, {"o   o", "  o  ", "o   o"}, {"o   o", "o   o", "o   o"}}
	top, bottom, v, pip := "+-------+", "+-------+", "|", "o"
	if unicode {
		top, bottom, v, pip = "╭───────╮", "╰───────╯", "│", "●"
	}
	spec := color.ColorSpec{Fg: 15, Bg: 0, Bold: true}
	if style == "light" {
		spec = color.ColorSpec{Fg: 0, Bg: 15, Bold: true}
	}
	var out strings.Builder
	for row := 0; row < 5; row++ {
		out.WriteString("  ")
		for i, n := range dice {
			if n == 0 {
				continue
			}
			line := top
			if row == 4 {
				line = bottom
			} else if row > 0 {
				line = v + " " + strings.ReplaceAll(patterns[n][row-1], "o", pip) + " " + v
			}
			out.WriteString(color.Render(mode, spec, line))
			if i == 0 {
				out.WriteString("   ")
			}
		}
		if row == 2 {
			out.WriteString(color.Render(mode, color.ColorSpec{Fg: 229, Bg: -1, Bold: true}, fmt.Sprintf("   Total: %d", dice[0]+dice[1])))
		}
		out.WriteByte('\n')
	}
	return out.String()
}
func renderCrapsSnapshot(s *casino.CrapsSnapshot, mode string, unicode bool, styles ...string) string {
	if s == nil {
		return ""
	}
	style := "dark"
	if len(styles) > 0 {
		style = styles[0]
	}
	var out strings.Builder
	title := func(text string) string {
		return color.Render(mode, color.ColorSpec{Fg: 229, Bg: -1, Bold: true}, text)
	}
	subtle := func(text string) string { return color.Render(mode, color.ColorSpec{Fg: 250, Bg: -1}, text) }
	point := "COME-OUT"
	if s.Point != 0 {
		point = fmt.Sprintf("POINT %d", s.Point)
	}
	if s.Variant == "simplified" {
		// The title alone says it: simplified craps has no point phase.
		point = ""
	}
	variant := s.Variant
	if variant == "" {
		variant = "bank"
	}
	out.WriteString("\n" + title("  "+strings.ToUpper(strings.ReplaceAll(variant, "_", " "))+" CRAPS"))
	if point != "" {
		out.WriteString(subtle("  /  " + point))
	}
	out.WriteString("\n")
	if variant == "bank" || variant == "diceless" {
		out.WriteString(renderCrapsTrackers(s, mode, unicode))
		out.WriteByte('\n')
		out.WriteString(renderCrapsFelt(s, mode, unicode))
	} else if variant == "simplified" {
		out.WriteString(renderSimplifiedCrapsFelt(mode, unicode))
	} else {
		height := 7
		if variant == "high_point" || variant == "die_rich" {
			height = 6
		}
		c := newCrapsCanvas(height)
		c.box(0, 0, 76, height-1)
		c.center(1, 75, 1, "HOUSE EDITION — table rules for payouts", crapsGold)
		switch variant {
		case "crapless":
			c.center(1, 75, 3, "POINTS: 2 3 4 5 6 8 9 10 11 12 | PASS / COME / ODDS", crapsInk)
			c.center(1, 75, 4, "PLACE / BUY / LAY / FIELD / HARD / HOP / PROPOSITIONS", crapsInk)
			c.center(1, 75, 5, "NO DON'T BETS OR BONUSES", crapsMuted)
		case "new_york":
			c.center(1, 75, 3, "PASS / DON'T PASS (BAR 12) / ODDS / BUY / LAY", crapsInk)
			c.center(1, 75, 4, "BUY / LAY: 4 5 6 8 9 10 | 5% WIN-ONLY COMMISSION", crapsMuted)
		default:
			c.center(1, 75, 3, "PASS ONLY | WINS PAY 1:1 | NO ODDS OR BONUSES", crapsInk)
		}
		out.WriteString(c.render(mode, unicode))
	}
	out.WriteByte('\n')
	if variant == "diceless" {
		out.WriteString(renderCrapsCards(s.Dice, mode, unicode, style))
	} else {
		out.WriteString(renderCrapsDice(s.Dice, mode, unicode, style))
	}
	if len(s.Queue) > 0 {
		out.WriteString(title("  Shooter  ") + subtle(s.Queue[0]) + "\n")
		if len(s.Queue) > 1 {
			out.WriteString(subtle("  On deck  "+strings.Join(s.Queue[1:], "  >  ")) + "\n")
		}
	}
	hint := ""
	if s.Phase != "waiting" && s.Phase != "" {
		hint = ""
		if s.Variant == "simplified" {
			if s.ShootIn > 0 {
				hint = fmt.Sprintf("The dice roll in %ds.", s.ShootIn)
			}
		} else if s.ShootIn > 0 {
			hint = fmt.Sprintf("Bets open. Shooter ready in %ds.", s.ShootIn)
		} else if s.DealerIn > 0 {
			hint = fmt.Sprintf("Shooter ready. Dealer rolls in %ds; bets still open.", s.DealerIn)
		}
	}
	if s.AutoRoll {
		hint = fmt.Sprintf("AUTOROLL  /  Shooter's next roll in %ds", s.AutoIn)
	}
	if hint != "" {
		out.WriteString(subtle("  "+hint) + "\n")
	}
	return out.String()
}