games: UNO becomes a table you sit at, and the pot that pays whoever goes out first
Phase D backend: UNO is now a session like hold'em, not a single stake. You sit
with a buy-in stack, ante into a pot each hand, and leave with what's in front of
you. The engine lost its `You` constant and its measured multiples: ApplyMove
takes the acting seat, New takes a seat list, a Tier carries an ante instead of a
Base, and a hand settles by moving the pot to the winner (less rake, and never
when a bot takes it) rather than paying a multiple. A mercy kill puts a seat out
of the hand, not out of the game — the last one standing takes the pot.
The redaction moved to the web layer, where hold'em's already lives: the engine
now stamps every seat's hand onto its events, and viewUno/viewUnoEvents strip
everything that isn't the viewer's own. TestUnoViewNeverLeaksAnotherSeatsCards is
the wall. unoTable implements tableGame; /uno/{sit,move,leave,tables,stream,chat,
say} mirror hold'em, with stream/chat/say now shared game-agnostic handlers.
The frontend is not done: uno.js still calls the retired solo endpoint, so the
page renders but is not yet playable. All engine and web tests are green.
Claude-Session: https://claude.ai/code/session_013M5nD7PgUboJXoDcYHzpuJ
This commit is contained in:
@@ -8,19 +8,38 @@ import (
|
||||
|
||||
const rake = 0.05
|
||||
|
||||
// You is the human's seat in the solo tests, a test-local alias for what the
|
||||
// engine no longer hardcodes: a table is a list of seats, and seat zero being the
|
||||
// human is a convention only these fixtures keep.
|
||||
const You = 0
|
||||
|
||||
func duel() Tier { t, _ := TierBySlug("duel"); return t }
|
||||
func full() Tier { t, _ := TierBySlug("full"); return t }
|
||||
func table() Tier { t, _ := TierBySlug("table"); return t }
|
||||
|
||||
// deal starts a game, failing the test if it can't.
|
||||
func deal(t *testing.T, tier Tier, bet int64, seed uint64) State {
|
||||
// openSolo opens a solo table (one human, the tier's bots) without dealing.
|
||||
func openSolo(t *testing.T, tier Tier, seed uint64) State {
|
||||
t.Helper()
|
||||
s, evs, err := New(bet, tier, rake, seed, 0xC0FFEE)
|
||||
s, _, err := New(tier, SoloSeats(tier, tier.Bots, 1000), rake, seed, 0xC0FFEE)
|
||||
if err != nil {
|
||||
t.Fatalf("New: %v", err)
|
||||
}
|
||||
if len(evs) != 1 || evs[0].Kind != EvDeal {
|
||||
t.Fatalf("New should deal exactly one event, got %+v", evs)
|
||||
return s
|
||||
}
|
||||
|
||||
// deal opens a solo table and deals the first hand, so the human is to act.
|
||||
func deal(t *testing.T, tier Tier, seed uint64) State {
|
||||
t.Helper()
|
||||
s := openSolo(t, tier, seed)
|
||||
s, evs, err := ApplyMove(s, You, Move{Kind: MoveDeal})
|
||||
if err != nil {
|
||||
t.Fatalf("deal: %v", err)
|
||||
}
|
||||
if !hasKind(evs, EvDeal) {
|
||||
t.Fatalf("the deal emitted no deal event: %+v", evs)
|
||||
}
|
||||
if s.Turn != You {
|
||||
t.Fatalf("the human should act first at a solo table, turn is %d", s.Turn)
|
||||
}
|
||||
return s
|
||||
}
|
||||
@@ -38,11 +57,6 @@ func census(s State) map[Card]int {
|
||||
m[c]++
|
||||
}
|
||||
for _, c := range s.Discard {
|
||||
// A wild is stamped with the colour it was played as while it sits on the
|
||||
// pile, so it counts as the wild it really is. This asks the face, rather
|
||||
// than listing the wilds: No Mercy prints four more of them, and a census
|
||||
// that didn't know about them would count a played roulette as a red card
|
||||
// and report a deck that balances while the cards don't.
|
||||
if c.Value.Wild() {
|
||||
c.Color = Wild
|
||||
}
|
||||
@@ -76,7 +90,7 @@ func TestNewDeckIsADeck(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNewDeals(t *testing.T) {
|
||||
s := deal(t, full(), 100, 7)
|
||||
s := deal(t, full(), 7)
|
||||
if len(s.Hands) != 4 {
|
||||
t.Fatalf("full house is four seats, got %d", len(s.Hands))
|
||||
}
|
||||
@@ -85,21 +99,31 @@ func TestNewDeals(t *testing.T) {
|
||||
t.Errorf("seat %d holds %d cards, want %d", i, len(h), HandSize)
|
||||
}
|
||||
}
|
||||
if len(s.Bots) != 3 {
|
||||
t.Fatalf("want three bot names, got %v", s.Bots)
|
||||
bots := 0
|
||||
for _, seat := range s.Seats {
|
||||
if seat.Bot {
|
||||
bots++
|
||||
if seat.Name == "" {
|
||||
t.Error("a bot seat has no name")
|
||||
}
|
||||
}
|
||||
}
|
||||
if s.Turn != You {
|
||||
t.Errorf("you play first, turn is %d", s.Turn)
|
||||
if bots != 3 {
|
||||
t.Fatalf("want three bots, got %d", bots)
|
||||
}
|
||||
if got := total(census(s)); got != 108 {
|
||||
t.Fatalf("the deal lost cards: %d of 108", got)
|
||||
}
|
||||
// Every seat anted, so the pot is one ante per seat.
|
||||
if want := s.Tier.Ante * int64(len(s.Seats)); s.Pot != want {
|
||||
t.Errorf("pot is %d, want %d (one ante each)", s.Pot, want)
|
||||
}
|
||||
}
|
||||
|
||||
// The card turned over to start is never an action card — see New.
|
||||
// The card turned over to start is never an action card — see dealHand.
|
||||
func TestOpeningCardIsANumber(t *testing.T) {
|
||||
for seed := uint64(0); seed < 300; seed++ {
|
||||
s := deal(t, table(), 50, seed)
|
||||
s := deal(t, table(), seed)
|
||||
if s.Top().Value.Action() {
|
||||
t.Fatalf("seed %d opened on %v", seed, s.Top())
|
||||
}
|
||||
@@ -111,12 +135,9 @@ func TestOpeningCardIsANumber(t *testing.T) {
|
||||
|
||||
// ---- the rules ------------------------------------------------------------
|
||||
|
||||
// rig builds a state by hand, so a rule can be tested without hunting a seed
|
||||
// that happens to deal it.
|
||||
//
|
||||
// The deck is the rest of the deck: every card not in a hand and not the one in
|
||||
// play. So a rigged game still holds 108 cards, and the census invariant means
|
||||
// something in these tests too.
|
||||
// rig builds a live hand by hand, so a rule can be tested without hunting a seed
|
||||
// that happens to deal it. Every seat has anted, so the pot is set and a win
|
||||
// settles for real.
|
||||
func rig(hands [][]Card, top Card, color Color) State {
|
||||
left := map[Card]int{}
|
||||
for _, c := range NewDeck() {
|
||||
@@ -137,30 +158,38 @@ func rig(hands [][]Card, top Card, color Color) State {
|
||||
|
||||
var deck []Card
|
||||
for _, c := range NewDeck() {
|
||||
key := c
|
||||
if left[key] > 0 {
|
||||
left[key]--
|
||||
if left[c] > 0 {
|
||||
left[c]--
|
||||
deck = append(deck, c)
|
||||
}
|
||||
}
|
||||
ante := full().Ante
|
||||
seats := make([]Seat, len(hands))
|
||||
for i := range seats {
|
||||
// The stack is what is left after this seat anted: a real deal moves the ante
|
||||
// out of the stack and into the pot, so a refund or a win returns it here.
|
||||
seats[i] = Seat{Name: botPool[i], Bot: i != You, Stack: 1000 - ante, Ante: ante}
|
||||
}
|
||||
seats[You].Name = "You"
|
||||
return State{
|
||||
Tier: full(), Hands: hands, Discard: []Card{top}, Color: color,
|
||||
Deck: deck, Dir: 1, Turn: You, Phase: PhasePlay,
|
||||
Bet: 100, RakePct: rake, Seed1: 1, Seed2: 2,
|
||||
Tier: full(), Seats: seats, Hands: hands, Discard: []Card{top}, Color: color,
|
||||
Deck: deck, Dir: 1, Turn: You, Dealer: len(hands) - 1, Phase: PhasePlay,
|
||||
Pot: ante * int64(len(hands)), Winner: -1,
|
||||
Out: make([]bool, len(hands)), Called: make([]bool, len(hands)),
|
||||
RakePct: rake, Seed1: 1, Seed2: 2,
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlayMustMatch(t *testing.T) {
|
||||
s := rig([][]Card{{{Blue, Three}}, {{Red, Five}}}, Card{Red, Nine}, Red)
|
||||
if _, _, err := ApplyMove(s, Move{Kind: MovePlay, Index: 0}); err != ErrCantPlay {
|
||||
if _, _, err := ApplyMove(s, You, Move{Kind: MovePlay, Index: 0}); err != ErrCantPlay {
|
||||
t.Fatalf("a blue 3 on a red 9 should be refused, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlayMatchesFaceOrColor(t *testing.T) {
|
||||
// Same face, different colour: legal.
|
||||
s := rig([][]Card{{{Blue, Nine}, {Red, Two}}, {{Green, Five}}}, Card{Red, Nine}, Red)
|
||||
next, evs, err := ApplyMove(s, Move{Kind: MovePlay, Index: 0})
|
||||
next, evs, err := ApplyMove(s, You, Move{Kind: MovePlay, Index: 0})
|
||||
if err != nil {
|
||||
t.Fatalf("a blue 9 on a red 9 is legal: %v", err)
|
||||
}
|
||||
@@ -174,22 +203,20 @@ func TestPlayMatchesFaceOrColor(t *testing.T) {
|
||||
|
||||
func TestWildNeedsAColor(t *testing.T) {
|
||||
s := rig([][]Card{{{Wild, WildCard}}, {{Green, Five}}}, Card{Red, Nine}, Red)
|
||||
if _, _, err := ApplyMove(s, Move{Kind: MovePlay, Index: 0}); err != ErrNeedColor {
|
||||
if _, _, err := ApplyMove(s, You, Move{Kind: MovePlay, Index: 0}); err != ErrNeedColor {
|
||||
t.Fatalf("a wild with no colour should be refused, got %v", err)
|
||||
}
|
||||
if _, _, err := ApplyMove(s, Move{Kind: MovePlay, Index: 0, Color: Wild}); err != ErrNeedColor {
|
||||
if _, _, err := ApplyMove(s, You, Move{Kind: MovePlay, Index: 0, Color: Wild}); err != ErrNeedColor {
|
||||
t.Fatalf("naming 'wild' is not naming a colour, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWildNamesTheColor(t *testing.T) {
|
||||
s := rig([][]Card{{{Wild, WildCard}, {Green, One}}, {{Green, Five}}}, Card{Red, Nine}, Red)
|
||||
next, _, err := ApplyMove(s, Move{Kind: MovePlay, Index: 0, Color: Green})
|
||||
next, _, err := ApplyMove(s, You, Move{Kind: MovePlay, Index: 0, Color: Green})
|
||||
if err != nil {
|
||||
t.Fatalf("play wild: %v", err)
|
||||
}
|
||||
// The bot moved after us, so the colour in play is whatever it left behind —
|
||||
// what we can check is that the wild itself went down as green.
|
||||
top := next.Discard
|
||||
if len(top) < 2 {
|
||||
t.Fatalf("expected the wild and the bot's card on the pile: %v", top)
|
||||
@@ -200,10 +227,9 @@ func TestWildNamesTheColor(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDrawTwoHitsTheNextSeat(t *testing.T) {
|
||||
// Two seats, so the +2 lands on the bot and the turn comes straight back.
|
||||
s := rig([][]Card{{{Red, DrawTwo}, {Red, One}}, {{Blue, Five}, {Blue, Six}}}, Card{Red, Nine}, Red)
|
||||
s.Tier = duel()
|
||||
next, evs, err := ApplyMove(s, Move{Kind: MovePlay, Index: 0})
|
||||
next, evs, err := ApplyMove(s, You, Move{Kind: MovePlay, Index: 0})
|
||||
if err != nil {
|
||||
t.Fatalf("play +2: %v", err)
|
||||
}
|
||||
@@ -224,7 +250,7 @@ func TestDrawTwoHitsTheNextSeat(t *testing.T) {
|
||||
func TestReverseIsASkipHeadsUp(t *testing.T) {
|
||||
s := rig([][]Card{{{Red, Reverse}, {Red, One}}, {{Blue, Five}}}, Card{Red, Nine}, Red)
|
||||
s.Tier = duel()
|
||||
next, evs, err := ApplyMove(s, Move{Kind: MovePlay, Index: 0})
|
||||
next, evs, err := ApplyMove(s, You, Move{Kind: MovePlay, Index: 0})
|
||||
if err != nil {
|
||||
t.Fatalf("play reverse: %v", err)
|
||||
}
|
||||
@@ -243,15 +269,13 @@ func TestReverseIsASkipHeadsUp(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReverseTurnsTheTableAround(t *testing.T) {
|
||||
// Every bot holds a red card, so each of them can play the moment the turn
|
||||
// reaches it — which is what makes the *order* they play in observable.
|
||||
s := rig([][]Card{
|
||||
{{Red, Reverse}, {Red, One}},
|
||||
{{Red, Five}, {Blue, Six}},
|
||||
{{Red, Six}, {Green, Six}},
|
||||
{{Red, Seven}, {Yellow, Six}},
|
||||
}, Card{Red, Nine}, Red)
|
||||
next, evs, err := ApplyMove(s, Move{Kind: MovePlay, Index: 0})
|
||||
next, evs, err := ApplyMove(s, You, Move{Kind: MovePlay, Index: 0})
|
||||
if err != nil {
|
||||
t.Fatalf("play reverse: %v", err)
|
||||
}
|
||||
@@ -264,7 +288,6 @@ func TestReverseTurnsTheTableAround(t *testing.T) {
|
||||
if next.Turn != You {
|
||||
t.Errorf("the bots should have played round to you, turn is %d", next.Turn)
|
||||
}
|
||||
// The table now runs anticlockwise: seat 3 plays, then 2, then 1.
|
||||
var order []int
|
||||
for _, e := range evs {
|
||||
if e.Kind == EvPlay && e.Seat != You {
|
||||
@@ -277,15 +300,13 @@ func TestReverseTurnsTheTableAround(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSkipSkips(t *testing.T) {
|
||||
// Both bots hold a playable red, so the only reason either of them doesn't
|
||||
// play is that it wasn't asked to.
|
||||
s := rig([][]Card{
|
||||
{{Red, Skip}, {Red, One}},
|
||||
{{Red, Five}, {Blue, Six}},
|
||||
{{Red, Six}, {Green, Six}},
|
||||
}, Card{Red, Nine}, Red)
|
||||
s.Tier = table()
|
||||
next, evs, err := ApplyMove(s, Move{Kind: MovePlay, Index: 0})
|
||||
next, evs, err := ApplyMove(s, You, Move{Kind: MovePlay, Index: 0})
|
||||
if err != nil {
|
||||
t.Fatalf("play skip: %v", err)
|
||||
}
|
||||
@@ -310,7 +331,7 @@ func TestSkipSkips(t *testing.T) {
|
||||
func TestDrawnPlayableWaitsForYou(t *testing.T) {
|
||||
s := rig([][]Card{{{Blue, Three}}, {{Green, Five}}}, Card{Red, Nine}, Red)
|
||||
s.Deck = []Card{{Red, Four}} // exactly what you'll draw, and it plays
|
||||
next, evs, err := ApplyMove(s, Move{Kind: MoveDraw})
|
||||
next, evs, err := ApplyMove(s, You, Move{Kind: MoveDraw})
|
||||
if err != nil {
|
||||
t.Fatalf("draw: %v", err)
|
||||
}
|
||||
@@ -323,18 +344,16 @@ func TestDrawnPlayableWaitsForYou(t *testing.T) {
|
||||
if evs[0].Kind != EvDraw || evs[0].Card == nil || *evs[0].Card != (Card{Red, Four}) {
|
||||
t.Fatalf("your own drawn card comes face up: %+v", evs[0])
|
||||
}
|
||||
if got := next.Playable(); len(got) != 1 || got[0] != 1 {
|
||||
if got := next.Playable(You); len(got) != 1 || got[0] != 1 {
|
||||
t.Errorf("the drawn card, and only it, is playable: %v", got)
|
||||
}
|
||||
// You may not play the *other* card instead — drawing would otherwise be a
|
||||
// free look with no cost.
|
||||
if _, _, err := ApplyMove(next, Move{Kind: MovePlay, Index: 0}); err != ErrMustPlayNow {
|
||||
if _, _, err := ApplyMove(next, You, Move{Kind: MovePlay, Index: 0}); err != ErrMustPlayNow {
|
||||
t.Fatalf("only the drawn card may be played, got %v", err)
|
||||
}
|
||||
if _, _, err := ApplyMove(next, Move{Kind: MoveDraw}); err != ErrMustPlayNow {
|
||||
if _, _, err := ApplyMove(next, You, Move{Kind: MoveDraw}); err != ErrMustPlayNow {
|
||||
t.Fatalf("you can't draw twice, got %v", err)
|
||||
}
|
||||
after, _, err := ApplyMove(next, Move{Kind: MovePass})
|
||||
after, _, err := ApplyMove(next, You, Move{Kind: MovePass})
|
||||
if err != nil {
|
||||
t.Fatalf("pass: %v", err)
|
||||
}
|
||||
@@ -349,7 +368,7 @@ func TestDrawnPlayableWaitsForYou(t *testing.T) {
|
||||
func TestUnplayableDrawPassesTheTurn(t *testing.T) {
|
||||
s := rig([][]Card{{{Blue, Three}}, {{Green, Five}}}, Card{Red, Nine}, Red)
|
||||
s.Deck = []Card{{Blue, Four}, {Red, Two}} // draw a blue 4: it doesn't go on a red 9
|
||||
next, evs, err := ApplyMove(s, Move{Kind: MoveDraw})
|
||||
next, evs, err := ApplyMove(s, You, Move{Kind: MoveDraw})
|
||||
if err != nil {
|
||||
t.Fatalf("draw: %v", err)
|
||||
}
|
||||
@@ -363,69 +382,78 @@ func TestUnplayableDrawPassesTheTurn(t *testing.T) {
|
||||
|
||||
func TestPassOnlyAfterADraw(t *testing.T) {
|
||||
s := rig([][]Card{{{Red, Three}}, {{Green, Five}}}, Card{Red, Nine}, Red)
|
||||
if _, _, err := ApplyMove(s, Move{Kind: MovePass}); err != ErrCantPass {
|
||||
if _, _, err := ApplyMove(s, You, Move{Kind: MovePass}); err != ErrCantPass {
|
||||
t.Fatalf("you can't pass a turn you haven't drawn on, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// dead is a table nobody can move at: the deck is spent, the discard is one card
|
||||
// deep so there is nothing to reshuffle out of, and not a seat holds a card that
|
||||
// goes on the pile. Every seat can only pass, forever.
|
||||
// deep, and not a seat holds a card that goes on the pile.
|
||||
func dead(hands [][]Card) State {
|
||||
s := rig(hands, Card{Red, Nine}, Red)
|
||||
s.Deck = nil
|
||||
return s
|
||||
}
|
||||
|
||||
// The game has to end here. It used to not: the stuck guard counted how many
|
||||
// bots had passed in a row and asked for more of them than there are seats, so
|
||||
// it never fired once, and a dead table just handed the turn round and round.
|
||||
// That is a game the player cannot finish — and a game they cannot finish is
|
||||
// chips they cannot cash out, because the cage won't let them leave a live hand.
|
||||
// A dead table ends the hand rather than passing the turn round forever. It no
|
||||
// longer ends the *session* — a shared table plays another hand — so it lands on
|
||||
// PhaseHandOver, not PhaseDone.
|
||||
func TestDeadTableEnds(t *testing.T) {
|
||||
s := dead([][]Card{{{Blue, Three}}, {{Green, Five}}})
|
||||
s := dead([][]Card{{{Blue, Three}}, {{Green, Five}}}) // level: one card each
|
||||
|
||||
next, evs, err := ApplyMove(s, Move{Kind: MoveDraw})
|
||||
next, evs, err := ApplyMove(s, You, Move{Kind: MoveDraw})
|
||||
if err != nil {
|
||||
t.Fatalf("draw: %v", err)
|
||||
}
|
||||
if next.Phase != PhaseDone {
|
||||
t.Fatalf("nobody can move and there is nothing to draw: the game is over, not %q", next.Phase)
|
||||
if next.playing() {
|
||||
t.Fatalf("nobody can move and there is nothing to draw: the hand is over, not %q", next.Phase)
|
||||
}
|
||||
if next.Outcome != OutcomeStuck {
|
||||
t.Errorf("a tie on the shortest hand is nobody going out: %q", next.Outcome)
|
||||
if next.Phase != PhaseHandOver {
|
||||
t.Fatalf("a dead hand at a shared table returns to handover, not %q", next.Phase)
|
||||
}
|
||||
if next.Payout != 0 {
|
||||
t.Errorf("a stuck game pays nothing, not %d", next.Payout)
|
||||
if next.Outcome != OutcomeTie {
|
||||
t.Errorf("level on the shortest hand is a tie, got %q", next.Outcome)
|
||||
}
|
||||
// A tie hands the antes back: every seat is whole again.
|
||||
for i := range next.Seats {
|
||||
if next.Seats[i].Stack != 1000 {
|
||||
t.Errorf("seat %d wasn't refunded: stack %d, want 1000", i, next.Seats[i].Stack)
|
||||
}
|
||||
}
|
||||
if !hasKind(evs, EvSettle) {
|
||||
t.Errorf("the table has to be told it's over: %+v", evs)
|
||||
t.Errorf("the table has to be told the hand is over: %+v", evs)
|
||||
}
|
||||
}
|
||||
|
||||
// And the shortest hand takes it, which is the one way a stuck table still pays.
|
||||
// And the shortest hand takes the pot, which is the one way a stuck table pays.
|
||||
func TestDeadTablePaysTheShortestHand(t *testing.T) {
|
||||
s := dead([][]Card{{{Blue, Three}}, {{Green, Five}, {Green, Six}}})
|
||||
pot := s.Pot
|
||||
before := s.Seats[You].Stack
|
||||
|
||||
next, _, err := ApplyMove(s, Move{Kind: MoveDraw})
|
||||
next, _, err := ApplyMove(s, You, Move{Kind: MoveDraw})
|
||||
if err != nil {
|
||||
t.Fatalf("draw: %v", err)
|
||||
}
|
||||
if next.Outcome != OutcomeWon {
|
||||
t.Fatalf("one card against two is a win: %q", next.Outcome)
|
||||
if next.Outcome != OutcomeStuck || next.Winner != You {
|
||||
t.Fatalf("one card against two is a win for you: outcome %q winner %d", next.Outcome, next.Winner)
|
||||
}
|
||||
if next.Payout != s.Pays() {
|
||||
t.Errorf("payout %d, quoted %d — the felt has to honour what it advertised", next.Payout, s.Pays())
|
||||
profit := pot - s.Tier.Ante
|
||||
wantRake := int64(float64(profit) * rake)
|
||||
wantWon := pot - wantRake
|
||||
if next.Seats[You].Won != wantWon {
|
||||
t.Errorf("you took %d, want %d (pot %d less rake %d)", next.Seats[You].Won, wantWon, pot, wantRake)
|
||||
}
|
||||
if next.Seats[You].Stack != before+wantWon {
|
||||
t.Errorf("your stack is %d, want %d", next.Seats[You].Stack, before+wantWon)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReshuffleRebuildsTheDeck(t *testing.T) {
|
||||
s := rig([][]Card{{{Blue, Three}}, {{Green, Five}}}, Card{Red, Nine}, Red)
|
||||
// An empty deck, and a discard with something under the top card to become one.
|
||||
// The buried wild went down as green; it has to come back as a wild.
|
||||
s.Deck = nil
|
||||
s.Discard = []Card{{Green, WildCard}, {Red, Two}, {Red, Nine}}
|
||||
next, evs, err := ApplyMove(s, Move{Kind: MoveDraw})
|
||||
next, evs, err := ApplyMove(s, You, Move{Kind: MoveDraw})
|
||||
if err != nil {
|
||||
t.Fatalf("draw on an empty deck: %v", err)
|
||||
}
|
||||
@@ -449,89 +477,64 @@ func TestReshuffleRebuildsTheDeck(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// ---- the money ------------------------------------------------------------
|
||||
// ---- the pot --------------------------------------------------------------
|
||||
|
||||
// The rule every game in this casino has had to be taught: the number the felt
|
||||
// quotes and the number the settle lands on are one function, not two.
|
||||
func TestQuoteIsThePayout(t *testing.T) {
|
||||
for _, tier := range Tiers {
|
||||
s := rig([][]Card{{{Red, Three}}, {{Green, Five}}}, Card{Red, Nine}, Red)
|
||||
s.Tier = tier
|
||||
s.Hands = make([][]Card, tier.Bots+1)
|
||||
s.Hands[You] = []Card{{Red, Three}}
|
||||
for i := 1; i <= tier.Bots; i++ {
|
||||
s.Hands[i] = []Card{{Green, Five}, {Green, Six}}
|
||||
}
|
||||
quoted := s.Pays()
|
||||
// The winner takes the pot, and the house's rake comes out of the winnings, never
|
||||
// out of a seat's own ante.
|
||||
func TestWinnerTakesThePotLessRake(t *testing.T) {
|
||||
s := rig([][]Card{{{Red, Three}}, {{Green, Five}, {Green, Six}}, {{Blue, One}, {Blue, Two}}}, Card{Red, Nine}, Red)
|
||||
pot := s.Pot
|
||||
before := s.Seats[You].Stack
|
||||
|
||||
next, _, err := ApplyMove(s, Move{Kind: MovePlay, Index: 0}) // your last card
|
||||
if err != nil {
|
||||
t.Fatalf("%s: go out: %v", tier.Slug, err)
|
||||
}
|
||||
if next.Outcome != OutcomeWon {
|
||||
t.Fatalf("%s: playing your last card wins, got %q", tier.Slug, next.Outcome)
|
||||
}
|
||||
if next.Payout != quoted {
|
||||
t.Errorf("%s: the felt quoted %d, the house paid %d", tier.Slug, quoted, next.Payout)
|
||||
}
|
||||
if next.Net() != quoted-s.Bet {
|
||||
t.Errorf("%s: net is %d, want %d", tier.Slug, next.Net(), quoted-s.Bet)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The rake comes out of the winnings, never the stake.
|
||||
//
|
||||
// The arithmetic is derived from the tier rather than written down. It used to be
|
||||
// written down — payout 214, rake 6 — and those numbers were the 2.2× duel. When
|
||||
// the tiers were re-measured and repriced, this test failed on a rake that was
|
||||
// perfectly correct, which is a test asserting a *price* while claiming to assert
|
||||
// a *rule*. The rule is: the house takes its cut of the profit and never touches
|
||||
// the stake. That holds at any multiple.
|
||||
func TestRakeIsOnWinningsOnly(t *testing.T) {
|
||||
s := rig([][]Card{{{Red, Three}}, {{Green, Five}, {Green, Six}}}, Card{Red, Nine}, Red)
|
||||
s.Tier = duel()
|
||||
s.Bet = 100
|
||||
|
||||
gross := int64(float64(s.Bet) * s.Tier.Base) // what the tier pays back, before the house
|
||||
profit := gross - s.Bet
|
||||
wantRake := int64(float64(profit) * rake)
|
||||
wantPayout := s.Bet + profit - wantRake
|
||||
|
||||
next, _, err := ApplyMove(s, Move{Kind: MovePlay, Index: 0})
|
||||
next, _, err := ApplyMove(s, You, Move{Kind: MovePlay, Index: 0}) // your last card
|
||||
if err != nil {
|
||||
t.Fatalf("go out: %v", err)
|
||||
}
|
||||
if next.Payout != wantPayout {
|
||||
t.Errorf("payout %d, want %d (%d stake + %d winnings - %d rake)",
|
||||
next.Payout, wantPayout, s.Bet, profit, wantRake)
|
||||
if next.Outcome != OutcomeWon || next.Winner != You {
|
||||
t.Fatalf("playing your last card wins: outcome %q winner %d", next.Outcome, next.Winner)
|
||||
}
|
||||
profit := pot - s.Tier.Ante
|
||||
wantRake := int64(float64(profit) * rake)
|
||||
wantWon := pot - wantRake
|
||||
if next.Rake != wantRake {
|
||||
t.Errorf("rake %d, want %d — and never a penny of the %d stake",
|
||||
next.Rake, wantRake, s.Bet)
|
||||
t.Errorf("rake %d, want %d — and never a penny of an ante", next.Rake, wantRake)
|
||||
}
|
||||
if next.Net() != wantPayout-s.Bet {
|
||||
t.Errorf("net %d, want %d", next.Net(), wantPayout-s.Bet)
|
||||
if next.LastPot != pot {
|
||||
t.Errorf("last pot %d, want %d", next.LastPot, pot)
|
||||
}
|
||||
if next.Seats[You].Stack != before+wantWon {
|
||||
t.Errorf("your stack is %d, want %d (+%d)", next.Seats[You].Stack, before+wantWon, wantWon)
|
||||
}
|
||||
if next.Paid != wantRake {
|
||||
t.Errorf("the session rake tally is %d, want %d", next.Paid, wantRake)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLosingPaysNothingAndIsNotCharged(t *testing.T) {
|
||||
// The bot holds one card that plays on the pile, so it goes out the moment the
|
||||
// turn reaches it.
|
||||
// A bot winning rakes nothing: the house already keeps the whole pot when its own
|
||||
// seat takes it, so there is nothing to charge.
|
||||
func TestABotWinningRakesNothing(t *testing.T) {
|
||||
// The bot at seat 1 holds one card that plays; it goes out the moment the turn
|
||||
// reaches it.
|
||||
s := rig([][]Card{{{Red, Three}, {Red, Four}}, {{Red, Five}}}, Card{Red, Nine}, Red)
|
||||
s.Tier = duel()
|
||||
next, evs, err := ApplyMove(s, Move{Kind: MovePlay, Index: 0})
|
||||
pot := s.Pot
|
||||
|
||||
next, evs, err := ApplyMove(s, You, Move{Kind: MovePlay, Index: 0})
|
||||
if err != nil {
|
||||
t.Fatalf("play: %v", err)
|
||||
}
|
||||
if next.Outcome != OutcomeLost {
|
||||
t.Fatalf("the bot went out, so you lost: %q", next.Outcome)
|
||||
if next.Outcome != OutcomeWon || next.Winner != 1 {
|
||||
t.Fatalf("the bot went out: outcome %q winner %d", next.Outcome, next.Winner)
|
||||
}
|
||||
if next.Payout != 0 || next.Rake != 0 {
|
||||
t.Errorf("a loss pays nothing and is charged nothing: payout %d rake %d", next.Payout, next.Rake)
|
||||
if next.Rake != 0 {
|
||||
t.Errorf("a bot winning rakes nothing, got %d", next.Rake)
|
||||
}
|
||||
if next.Net() != -s.Bet {
|
||||
t.Errorf("a loss costs the stake and no more: %d", next.Net())
|
||||
if next.Seats[1].Won != pot {
|
||||
t.Errorf("the bot took %d, want the whole pot %d", next.Seats[1].Won, pot)
|
||||
}
|
||||
// You anted and lost it: your stack is down exactly one ante.
|
||||
if next.Seats[You].Stack != 1000-s.Tier.Ante {
|
||||
t.Errorf("your stack is %d, want %d (one ante gone)", next.Seats[You].Stack, 1000-s.Tier.Ante)
|
||||
}
|
||||
last := evs[len(evs)-1]
|
||||
if last.Kind != EvSettle || last.Seat != 1 {
|
||||
@@ -539,51 +542,81 @@ func TestLosingPaysNothingAndIsNotCharged(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoMoveAfterItIsOver(t *testing.T) {
|
||||
// A hand ending returns the table to handover, ready to deal again — it does not
|
||||
// take a hand move.
|
||||
func TestNoHandMoveBetweenHands(t *testing.T) {
|
||||
s := rig([][]Card{{{Red, Three}}, {{Green, Five}, {Green, Six}}}, Card{Red, Nine}, Red)
|
||||
s.Tier = duel()
|
||||
done, _, err := ApplyMove(s, Move{Kind: MovePlay, Index: 0})
|
||||
over, _, err := ApplyMove(s, You, Move{Kind: MovePlay, Index: 0})
|
||||
if err != nil {
|
||||
t.Fatalf("go out: %v", err)
|
||||
}
|
||||
if _, _, err := ApplyMove(done, Move{Kind: MoveDraw}); err != ErrGameOver {
|
||||
t.Fatalf("a finished game takes no more moves, got %v", err)
|
||||
if over.Phase != PhaseHandOver {
|
||||
t.Fatalf("a finished hand returns to handover, got %q", over.Phase)
|
||||
}
|
||||
if _, _, err := ApplyMove(over, You, Move{Kind: MoveDraw}); err != ErrNoHand {
|
||||
t.Fatalf("no hand is in progress between hands, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadBet(t *testing.T) {
|
||||
if _, _, err := New(0, duel(), rake, 1, 2); err != ErrBadBet {
|
||||
t.Fatalf("want ErrBadBet, got %v", err)
|
||||
// You can deal the next hand, ante again, and play on — the session shape.
|
||||
func TestDealTheNextHand(t *testing.T) {
|
||||
s := rig([][]Card{{{Red, Three}}, {{Green, Five}, {Green, Six}}}, Card{Red, Nine}, Red)
|
||||
s.Tier = duel()
|
||||
over, _, err := ApplyMove(s, You, Move{Kind: MovePlay, Index: 0})
|
||||
if err != nil {
|
||||
t.Fatalf("go out: %v", err)
|
||||
}
|
||||
again, evs, err := ApplyMove(over, You, Move{Kind: MoveDeal})
|
||||
if err != nil {
|
||||
t.Fatalf("deal the next hand: %v", err)
|
||||
}
|
||||
if again.HandNo != over.HandNo+1 {
|
||||
t.Errorf("hand number didn't advance: %d then %d", over.HandNo, again.HandNo)
|
||||
}
|
||||
if !again.playing() {
|
||||
t.Fatalf("the next hand should be live, phase %q", again.Phase)
|
||||
}
|
||||
if !hasKind(evs, EvAnte) || !hasKind(evs, EvDeal) {
|
||||
t.Errorf("the deal antes and turns a card: %+v", evs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadBuyIn(t *testing.T) {
|
||||
if _, _, err := New(duel(), SoloSeats(duel(), 1, 10), rake, 1, 2); err != ErrBadBuyIn {
|
||||
t.Fatalf("a buy-in under the minimum should be refused, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// ---- the whole game -------------------------------------------------------
|
||||
|
||||
// playOut plays a game to its end with a simple strategy: play the first legal
|
||||
// card, otherwise draw, otherwise pass. It asserts the invariants at every step.
|
||||
// playOut plays one hand to its end with a simple strategy: play the first legal
|
||||
// card, take a stack you can't answer, otherwise draw, otherwise pass.
|
||||
func playOut(t *testing.T, s State, maxTurns int) State {
|
||||
t.Helper()
|
||||
for turn := 0; s.Phase != PhaseDone; turn++ {
|
||||
for turn := 0; s.playing(); turn++ {
|
||||
if turn > maxTurns {
|
||||
t.Fatalf("the game never ended in %d turns", maxTurns)
|
||||
t.Fatalf("the hand never ended in %d turns", maxTurns)
|
||||
}
|
||||
if s.Turn != You {
|
||||
t.Fatalf("ApplyMove left the turn with seat %d — the bots should always run out", s.Turn)
|
||||
}
|
||||
|
||||
var m Move
|
||||
if p := s.Playable(); len(p) > 0 {
|
||||
if p := s.Playable(You); len(p) > 0 {
|
||||
m = Move{Kind: MovePlay, Index: p[0]}
|
||||
if s.Hands[You][p[0]].IsWild() {
|
||||
m.Color = Green
|
||||
}
|
||||
} else if s.Phase == PhaseStack {
|
||||
m = Move{Kind: MoveTake}
|
||||
} else if s.Phase == PhaseDrawn {
|
||||
m = Move{Kind: MovePass}
|
||||
} else {
|
||||
m = Move{Kind: MoveDraw}
|
||||
}
|
||||
|
||||
next, evs, err := ApplyMove(s, m)
|
||||
next, evs, err := ApplyMove(s, You, m)
|
||||
if err != nil {
|
||||
t.Fatalf("turn %d: %v (move %+v, phase %v)", turn, err, m, s.Phase)
|
||||
}
|
||||
@@ -598,13 +631,6 @@ func playOut(t *testing.T, s State, maxTurns int) State {
|
||||
t.Fatalf("turn %d: %d of %v, want %d — a card was duplicated", turn, n, c, want)
|
||||
}
|
||||
}
|
||||
// No event ever names a bot's card. That is the hole card of this game, and
|
||||
// it is most of the deck.
|
||||
for _, e := range evs {
|
||||
if (e.Kind == EvDraw || e.Kind == EvForced) && e.Seat != You && e.Card != nil {
|
||||
t.Fatalf("turn %d: a bot's drawn card crossed the wire: %+v", turn, e)
|
||||
}
|
||||
}
|
||||
s = next
|
||||
}
|
||||
return s
|
||||
@@ -622,72 +648,54 @@ func deckCount(c Card) int {
|
||||
}
|
||||
}
|
||||
|
||||
// A hundred games, played out, with the invariants checked at every step. This
|
||||
// is the test that would have caught a deck that leaks cards through the
|
||||
// reshuffle, a turn the bots don't hand back, or a game that can't end.
|
||||
// A hundred hands, played out, with the invariants checked at every step.
|
||||
func TestGamesPlayOut(t *testing.T) {
|
||||
wins, losses, stuck := 0, 0, 0
|
||||
yous, others, ties := 0, 0, 0
|
||||
for seed := uint64(0); seed < 100; seed++ {
|
||||
tier := Tiers[seed%3]
|
||||
end := playOut(t, deal(t, tier, 100, seed), 500)
|
||||
switch end.Outcome {
|
||||
case OutcomeWon:
|
||||
wins++
|
||||
if end.Payout != end.Pays() {
|
||||
t.Fatalf("seed %d: paid %d, quoted %d", seed, end.Payout, end.Pays())
|
||||
}
|
||||
case OutcomeLost:
|
||||
losses++
|
||||
case OutcomeStuck:
|
||||
stuck++
|
||||
default:
|
||||
t.Fatalf("seed %d ended as %q", seed, end.Outcome)
|
||||
end := playOut(t, deal(t, tier, seed), 500)
|
||||
if end.Phase != PhaseHandOver {
|
||||
t.Fatalf("seed %d ended in phase %q", seed, end.Phase)
|
||||
}
|
||||
if len(end.Hands[end.winnerSeat()]) != 0 && end.Outcome != OutcomeStuck {
|
||||
switch {
|
||||
case end.Winner == You:
|
||||
yous++
|
||||
case end.Winner >= 0:
|
||||
others++
|
||||
case end.Outcome == OutcomeTie:
|
||||
ties++
|
||||
default:
|
||||
t.Fatalf("seed %d ended with winner %d outcome %q", seed, end.Winner, end.Outcome)
|
||||
}
|
||||
if end.Winner >= 0 && end.Outcome == OutcomeWon && len(end.Hands[end.Winner]) != 0 {
|
||||
t.Fatalf("seed %d: the winner is still holding cards", seed)
|
||||
}
|
||||
}
|
||||
// Playing the first legal card is a poor strategy against bots that hold their
|
||||
// +4s back, so this is not a fairness assertion — it's a check that both
|
||||
// outcomes actually happen. A table that never pays is a bug in the bots.
|
||||
if wins == 0 || losses == 0 {
|
||||
t.Fatalf("100 games gave %d wins, %d losses, %d stuck — one side never happens", wins, losses, stuck)
|
||||
if yous == 0 || others == 0 {
|
||||
t.Fatalf("100 hands gave %d to you, %d to others, %d tied — one side never happens", yous, others, ties)
|
||||
}
|
||||
t.Logf("100 games: %d won, %d lost, %d stuck", wins, losses, stuck)
|
||||
t.Logf("100 hands: %d to you, %d to others, %d tied", yous, others, ties)
|
||||
}
|
||||
|
||||
// winnerSeat is the seat the settle event named — only used by the tests.
|
||||
func (s State) winnerSeat() int {
|
||||
best := 0
|
||||
for i := range s.Hands {
|
||||
if len(s.Hands[i]) < len(s.Hands[best]) {
|
||||
best = i
|
||||
}
|
||||
}
|
||||
return best
|
||||
}
|
||||
|
||||
// The same seed deals the same game and the bots make the same choices — which
|
||||
// is what lets a disputed game be replayed exactly as it fell.
|
||||
// The same seed deals the same hand and the bots make the same choices.
|
||||
func TestReplaysFromTheSeed(t *testing.T) {
|
||||
a := playOut(t, deal(t, full(), 250, 42), 500)
|
||||
b := playOut(t, deal(t, full(), 250, 42), 500)
|
||||
a := playOut(t, deal(t, full(), 42), 500)
|
||||
b := playOut(t, deal(t, full(), 42), 500)
|
||||
|
||||
ja, _ := json.Marshal(a)
|
||||
jb, _ := json.Marshal(b)
|
||||
if string(ja) != string(jb) {
|
||||
t.Fatal("the same seed played the same way gave two different games")
|
||||
}
|
||||
if a.Outcome == "" {
|
||||
if a.Winner < 0 && a.Outcome != OutcomeTie {
|
||||
t.Fatal("the replay didn't finish")
|
||||
}
|
||||
}
|
||||
|
||||
// A game in progress survives a redeploy: it is a plain value, so it round-trips
|
||||
// through the JSON it is stored as.
|
||||
// A game in progress survives a redeploy: it round-trips through its JSON.
|
||||
func TestStateSurvivesStorage(t *testing.T) {
|
||||
s := deal(t, table(), 100, 9)
|
||||
s, _, err := ApplyMove(s, Move{Kind: MoveDraw})
|
||||
s := deal(t, table(), 9)
|
||||
s, _, err := ApplyMove(s, You, Move{Kind: MoveDraw})
|
||||
if err != nil {
|
||||
t.Fatalf("draw: %v", err)
|
||||
}
|
||||
@@ -704,12 +712,10 @@ func TestStateSurvivesStorage(t *testing.T) {
|
||||
if string(again) != string(blob) {
|
||||
t.Fatal("a stored game came back different")
|
||||
}
|
||||
// And it plays on from there.
|
||||
playOut(t, back, 500)
|
||||
}
|
||||
|
||||
// A move the engine refuses leaves the caller's state exactly as it was — no
|
||||
// card half-played, no turn half-passed.
|
||||
// A move the engine refuses leaves the caller's state exactly as it was.
|
||||
func TestARefusedMoveChangesNothing(t *testing.T) {
|
||||
s := rig([][]Card{{{Blue, Three}, {Wild, WildCard}}, {{Green, Five}}}, Card{Red, Nine}, Red)
|
||||
before, _ := json.Marshal(s)
|
||||
@@ -721,7 +727,7 @@ func TestARefusedMoveChangesNothing(t *testing.T) {
|
||||
{Kind: MovePass}, // nothing drawn
|
||||
{Kind: "shuffle-the-deck-in-my-favour"}, // no
|
||||
} {
|
||||
if _, _, err := ApplyMove(s, m); err == nil {
|
||||
if _, _, err := ApplyMove(s, You, m); err == nil {
|
||||
t.Fatalf("%+v should have been refused", m)
|
||||
}
|
||||
}
|
||||
@@ -731,13 +737,12 @@ func TestARefusedMoveChangesNothing(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The bots choose. Two different seeds should not play the same bot game, or the
|
||||
// bot is a lookup table you can memorise.
|
||||
// The bots choose. Two different seeds should not play the same game.
|
||||
func TestBotsAreNotDeterministicAcrossSeeds(t *testing.T) {
|
||||
same := 0
|
||||
for seed := uint64(0); seed < 20; seed++ {
|
||||
a := playOut(t, deal(t, duel(), 100, seed), 500)
|
||||
b := playOut(t, deal(t, duel(), 100, seed+1000), 500)
|
||||
a := playOut(t, deal(t, duel(), seed), 500)
|
||||
b := playOut(t, deal(t, duel(), seed+1000), 500)
|
||||
if len(a.Discard) == len(b.Discard) {
|
||||
same++
|
||||
}
|
||||
@@ -747,8 +752,6 @@ func TestBotsAreNotDeterministicAcrossSeeds(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// botPick holds its +4 back while it's comfortable, and reaches for it when
|
||||
// somebody is about to go out.
|
||||
func TestBotSavesTheDrawFour(t *testing.T) {
|
||||
hand := []Card{{Wild, WildDrawFour}, {Red, Five}}
|
||||
top, color := Card{Red, Nine}, Red
|
||||
@@ -781,7 +784,6 @@ func TestBotPicksItsBestColor(t *testing.T) {
|
||||
if got := botColor(hand, rng); got != Blue {
|
||||
t.Errorf("the bot holds two blues: it should call blue, got %v", got)
|
||||
}
|
||||
// A hand of nothing but wilds still has to name something.
|
||||
for i := 0; i < 20; i++ {
|
||||
if got := botColor([]Card{{Wild, WildCard}}, rng); !got.Playable() {
|
||||
t.Fatalf("botColor named %v, which is not a colour", got)
|
||||
|
||||
Reference in New Issue
Block a user