aboutsummaryrefslogtreecommitdiff
path: root/internal/game/hacking/liars_dice.go
blob: 1dd27b6dd40e7c9e8aaa3bd356c8c5283bbbe206 (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
package hacking

import (
	"fmt"
	"math/rand"
	"strconv"
	"strings"
)

type LiarsDiceGame struct {
	playerDice []int
	aiDice     []int
	currentBid LiarsBid
	hasBid     bool
	playerTurn bool
	round      int
	gameOver   bool
	won        bool
	playerLost int
	completed  bool
}

type LiarsBid struct {
	Quantity int
	Face     int
}

func NewLiarsDiceGame() *LiarsDiceGame {
	return &LiarsDiceGame{
		playerDice: make([]int, 5),
		aiDice:     make([]int, 5),
	}
}

func (l *LiarsDiceGame) Name() string {
	return "Signal Bluff"
}

func (l *LiarsDiceGame) Init(level int) string {
	l.playerDice = make([]int, 5)
	l.aiDice = make([]int, 5)
	l.round = 0
	l.gameOver = false
	l.playerTurn = true
	l.hasBid = false
	l.playerLost = 0
	l.completed = false

	var sb strings.Builder
	sb.WriteString("=== SIGNAL BLUFF ===\n")
	sb.WriteString("\n")
	sb.WriteString("You've connected to a secure channel running a bluffing protocol.\n")
	sb.WriteString("You and the system each have 5 signal fragments (dice). Each round,\n")
	sb.WriteString("fragments are scrambled. You see only yours. Take turns making claims\n")
	sb.WriteString("about the total count of a specific signal across ALL fragments.\n")
	sb.WriteString("\n")
	sb.WriteString("1s are wild -- they count as any signal.\n")
	sb.WriteString("\n")
	sb.WriteString("Commands:\n")
	sb.WriteString("  bid <qty> <face>   - Claim at least N fragments show face F\n")
	sb.WriteString("                        (e.g., 'bid 3 4' = \"at least three 4s\")\n")
	sb.WriteString("  call               - Challenge the last bid (reveal all)\n")
	sb.WriteString("  exact              - Claim the bid is exactly right\n")
	sb.WriteString("  status             - Show your fragments and current bid\n")
	sb.WriteString("  jack out           - Disconnect (forfeit)\n")
	sb.WriteString("\n")
	sb.WriteString(l.startRound())
	return sb.String()
}

func (l *LiarsDiceGame) HandleInput(input string) (string, bool, bool) {
	input = strings.TrimSpace(strings.ToLower(input))
	parts := strings.Fields(input)

	if len(parts) == 0 {
		return "Commands: bid <qty> <face>, call, exact, status, jack out", false, false
	}

	cmd := parts[0]

	switch cmd {
	case "bid", "b":
		if len(parts) < 3 {
			return "Usage: bid <quantity> <face> (e.g., bid 3 4)", false, false
		}
		qty, err1 := strconv.Atoi(parts[1])
		face, err2 := strconv.Atoi(parts[2])
		if err1 != nil || err2 != nil {
			return "Usage: bid <quantity> <face> (e.g., bid 3 4)", false, false
		}
		return l.doBid(qty, face)
	case "call", "c", "intercept", "liar":
		return l.doCall(false)
	case "exact", "e":
		return l.doExact()
	case "status":
		return l.doStatus(), false, false
	default:
		if len(parts) == 2 {
			qty, err1 := strconv.Atoi(parts[0])
			face, err2 := strconv.Atoi(parts[1])
			if err1 == nil && err2 == nil {
				return l.doBid(qty, face)
			}
		}
		return "Commands: bid <qty> <face>, call, exact, status, jack out", false, false
	}
}

func (l *LiarsDiceGame) doBid(qty, face int) (string, bool, bool) {
	if face < 1 || face > 6 {
		return "Face must be 1-6.", false, false
	}
	if qty < 1 {
		return "Quantity must be at least 1.", false, false
	}
	if !l.playerTurn {
		return "It's not your turn to bid. Use 'call' or 'exact'.", false, false
	}

	if l.hasBid {
		if qty < l.currentBid.Quantity || (qty == l.currentBid.Quantity && face <= l.currentBid.Face) {
			return "You must bid higher (higher quantity or same quantity with higher face).", false, false
		}
	}

	l.currentBid = LiarsBid{Quantity: qty, Face: face}
	l.hasBid = true
	l.playerTurn = false

	return l.aiTurn()
}

func (l *LiarsDiceGame) doCall(player bool) (string, bool, bool) {
	if !l.hasBid {
		return "No bid to call yet. Make a bid first.", false, false
	}

	var sb strings.Builder
	if player {
		sb.WriteString("System intercepts!\n")
	} else {
		sb.WriteString("You intercept!\n")
	}

	sb.WriteString(l.showAllDice())
	sb.WriteString(fmt.Sprintf("\nBid was: %d %s\n", l.currentBid.Quantity, faceNamePlural(l.currentBid.Face)))

	total := l.countFace(l.currentBid.Face)
	sb.WriteString(fmt.Sprintf("Actual count: %d (%d %ss + %d wild 1s)\n\n",
		total,
		l.countFaceNonWild(l.currentBid.Face),
		faceName(l.currentBid.Face),
		l.countFace(1)-l.countFaceNonWild(1)))

	if total >= l.currentBid.Quantity {
		sb.WriteString("The bid holds! ")
		if player {
			sb.WriteString("System loses a fragment.")
			l.aiDice = l.aiDice[:len(l.aiDice)-1]
		} else {
			sb.WriteString("You lose a fragment.")
			l.playerDice = l.playerDice[:len(l.playerDice)-1]
			l.playerLost++
		}
	} else {
		sb.WriteString("Bluff called! ")
		if player {
			sb.WriteString("You lose a fragment.")
			l.playerDice = l.playerDice[:len(l.playerDice)-1]
			l.playerLost++
		} else {
			sb.WriteString("System loses a fragment.")
			l.aiDice = l.aiDice[:len(l.aiDice)-1]
		}
	}

	return l.checkGameOver(sb)
}

func (l *LiarsDiceGame) doExact() (string, bool, bool) {
	if !l.hasBid {
		return "No bid to claim exact on. Make a bid first.", false, false
	}

	var sb strings.Builder
	sb.WriteString("You claim exact match!\n")

	sb.WriteString(l.showAllDice())
	total := l.countFace(l.currentBid.Face)
	sb.WriteString(fmt.Sprintf("\nBid was: %d %s\n", l.currentBid.Quantity, faceNamePlural(l.currentBid.Face)))
	sb.WriteString(fmt.Sprintf("Actual count: %d\n\n", total))

	if total == l.currentBid.Quantity {
		sb.WriteString("Exact match! You recover a fragment.\n")
		if len(l.playerDice) < 5 {
			l.playerDice = append(l.playerDice, rand.Intn(6)+1)
		}
	} else {
		sb.WriteString("Not an exact match. You lose a fragment.\n")
		l.playerDice = l.playerDice[:len(l.playerDice)-1]
		l.playerLost++
	}

	return l.checkGameOver(sb)
}

func (l *LiarsDiceGame) checkGameOver(sb strings.Builder) (string, bool, bool) {
	if len(l.playerDice) == 0 {
		sb.WriteString("\nYou've lost all your fragments. Connection terminated.")
		l.gameOver = true
		l.won = false
		l.completed = true
		return sb.String(), true, false
	}
	if len(l.aiDice) == 0 {
		sb.WriteString("\nSystem has no fragments left. You win!")
		l.gameOver = true
		l.won = true
		l.completed = true
		return sb.String(), true, true
	}

	sb.WriteString(l.startRound())
	return sb.String(), false, false
}

func (l *LiarsDiceGame) showAllDice() string {
	var sb strings.Builder
	sb.WriteString(fmt.Sprintf("Your fragments: %s\n", l.diceStr(l.playerDice)))
	sb.WriteString(fmt.Sprintf("System fragments: %s\n", l.diceStr(l.aiDice)))
	return sb.String()
}

func (l *LiarsDiceGame) diceStr(dice []int) string {
	var parts []string
	for _, d := range dice {
		parts = append(parts, fmt.Sprintf("[%d]", d))
	}
	return strings.Join(parts, " ")
}

func (l *LiarsDiceGame) doStatus() string {
	var sb strings.Builder
	sb.WriteString(fmt.Sprintf("--- Cycle %d ---\n", l.round))
	sb.WriteString(fmt.Sprintf("Your fragments: %s    (You: %d, System: %d)\n",
		l.diceStr(l.playerDice), len(l.playerDice), len(l.aiDice)))
	if l.hasBid {
		who := "System"
		if !l.playerTurn {
			who = "System"
		} else {
			who = "you"
		}
		sb.WriteString(fmt.Sprintf("Current bid: %d %s (by %s)\n", l.currentBid.Quantity, faceNamePlural(l.currentBid.Face), who))
	}
	if l.playerTurn {
		sb.WriteString("Your turn: make a bid.")
	} else {
		sb.WriteString("Your turn: bid higher, call, or exact.")
	}
	return sb.String()
}

func (l *LiarsDiceGame) startRound() string {
	l.round++
	for i := range l.playerDice {
		l.playerDice[i] = rand.Intn(6) + 1
	}
	for i := range l.aiDice {
		l.aiDice[i] = rand.Intn(6) + 1
	}
	l.hasBid = false
	l.playerTurn = true

	var sb strings.Builder
	sb.WriteString(fmt.Sprintf("--- Cycle %d ---\n", l.round))
	sb.WriteString(fmt.Sprintf("Your fragments: %s    (You: %d, System: %d)\n",
		l.diceStr(l.playerDice), len(l.playerDice), len(l.aiDice)))
	sb.WriteString("You go first. Make a bid.\n")
	return sb.String()
}

func (l *LiarsDiceGame) aiTurn() (string, bool, bool) {
	totalDice := len(l.playerDice) + len(l.aiDice)

	aiCount := l.countFace(l.currentBid.Face)
	unknownDice := len(l.playerDice)
	estimated := float64(aiCount) + float64(unknownDice)/3.0

	if float64(l.currentBid.Quantity) > estimated*1.5 {
		return l.doCall(true)
	}

	bestFace, bestCount := l.aiBestFace()
	newQty := l.currentBid.Quantity
	newFace := l.currentBid.Face

	if bestFace > newFace && bestCount >= newQty {
		newFace = bestFace
	} else {
		newQty++
	}

	if newQty > totalDice {
		return l.doCall(true)
	}

	l.currentBid = LiarsBid{Quantity: newQty, Face: newFace}
	l.playerTurn = true

	var sb strings.Builder
	sb.WriteString(fmt.Sprintf("System broadcasts: %d %s.\n", newQty, faceNamePlural(newFace)))
	sb.WriteString("Your turn: bid higher, call, or exact.\n")
	return sb.String(), false, false
}

func (l *LiarsDiceGame) aiBestFace() (face int, count int) {
	counts := make(map[int]int)
	for _, d := range l.aiDice {
		if d == 1 {
			continue
		}
		counts[d]++
	}
	wilds := 0
	for _, d := range l.aiDice {
		if d == 1 {
			wilds++
		}
	}
	best := 2
	bestN := counts[2] + wilds
	for f := 3; f <= 6; f++ {
		n := counts[f] + wilds
		if n > bestN || (n == bestN && f > best) {
			best = f
			bestN = n
		}
	}
	return best, bestN
}

func (l *LiarsDiceGame) countFace(face int) int {
	count := 0
	for _, d := range l.playerDice {
		if d == face || d == 1 {
			count++
		}
	}
	for _, d := range l.aiDice {
		if d == face || d == 1 {
			count++
		}
	}
	return count
}

func (l *LiarsDiceGame) countFaceNonWild(face int) int {
	count := 0
	for _, d := range l.playerDice {
		if d == face {
			count++
		}
	}
	for _, d := range l.aiDice {
		if d == face {
			count++
		}
	}
	return count
}

func faceName(f int) string {
	names := []string{"", "one", "two", "three", "four", "five", "six"}
	if f >= 1 && f <= 6 {
		return names[f]
	}
	return fmt.Sprint(f)
}

func faceNamePlural(f int) string {
	names := []string{"", "ones", "twos", "threes", "fours", "fives", "sixes"}
	if f >= 1 && f <= 6 {
		return names[f]
	}
	return fmt.Sprint(f)
}

func (l *LiarsDiceGame) BonusXP() int {
	if l.completed && l.won && l.playerLost == 0 {
		return 150
	}
	return 0
}