koehandel / tests /test_engine.py
lukehola's picture
Upload folder using huggingface_hub (part 6)
08d876f verified
Raw History Blame Contribute Delete
9.24 kB
import random
from collections import Counter
import pytest
from koehandel.engine import KoehandelGame, final_scores
from koehandel.engine.actions import ActionKind
from koehandel.engine.cards import (
MONEY_COUNTS,
NUM_ANIMAL_CARDS,
STARTING_MONEY,
Animal,
)
from koehandel.engine import money as M
from koehandel.engine.state import CowTradeCtx, Phase
def _conserved_money_cards(state):
total = Counter()
for hand in state.money:
total += hand
total += state.bank
return total
def test_setup_invariants():
for n in (3, 4, 5):
s = KoehandelGame.new_game(num_players=n, seed=1)
assert len(s.deck) == NUM_ANIMAL_CARDS
assert _conserved_money_cards(s) == Counter(MONEY_COUNTS)
for hand in s.money:
assert hand == Counter(STARTING_MONEY)
assert s.phase == Phase.CHOOSE
assert KoehandelGame.legal_actions(s) # something to do
def test_rejects_bad_player_count():
with pytest.raises(ValueError):
KoehandelGame.new_game(num_players=2)
with pytest.raises(ValueError):
KoehandelGame.new_game(num_players=6)
def _play_random_game(seed, num_players=4):
rng = random.Random(seed)
s = KoehandelGame.new_game(num_players=num_players, seed=seed)
plies = 0
while not KoehandelGame.is_terminal(s):
actions = KoehandelGame.legal_actions(s)
assert actions, f"no legal actions in phase {s.phase}"
a = rng.choice(actions)
KoehandelGame.step(s, a)
plies += 1
assert plies < 50000
return s
def test_money_cards_conserved_every_step():
# Strong economy invariant: the full multiset of money cards (all players + bank)
# must equal the box supply at EVERY step. Catches any rule that creates, destroys,
# or "commits nothing" with money (the class of bug the cow-trade 0-offer was).
rng = random.Random(0)
box = Counter(MONEY_COUNTS)
for seed in range(30):
s = KoehandelGame.new_game(num_players=rng.choice([3, 4, 5]), seed=seed)
steps = 0
while not KoehandelGame.is_terminal(s) and steps < 20000:
total = Counter()
for hand in s.money:
total += hand
total += s.bank
assert total == box, f"money not conserved at step {steps} (seed {seed})"
KoehandelGame.step(s, rng.choice(KoehandelGame.legal_actions(s)))
steps += 1
def test_legal_bids_are_always_payable():
# Audit: every BID / BUY the engine offers must be payable (total >= amount), and
# every cow-trade offer/counter must commit >= 1 card. Verified across random play.
rng = random.Random(1)
for seed in range(25):
s = KoehandelGame.new_game(num_players=rng.choice([3, 4, 5]), seed=seed)
steps = 0
while not KoehandelGame.is_terminal(s) and steps < 20000:
for a in KoehandelGame.legal_actions(s):
if a.kind in (ActionKind.BID, ActionKind.CT_OFFER, ActionKind.CT_COUNTER):
if a.kind == ActionKind.BID:
payer = s.auction.to_act
else:
payer = s.current_player
assert M.total(s.money[payer]) >= a.amount
if a.kind in (ActionKind.CT_OFFER, ActionKind.CT_COUNTER):
cards = M.cards_for_exact(s.money[payer], a.amount)
assert cards is not None and M.num_cards(cards) >= 1
if a.kind == ActionKind.BUY:
assert M.total(s.money[s.auction.auctioneer]) >= s.auction.current_bid
KoehandelGame.step(s, rng.choice(KoehandelGame.legal_actions(s)))
steps += 1
def test_random_games_complete_and_conserve_animals():
for seed in range(40):
s = _play_random_game(seed)
# All 40 animal cards distributed.
dealt = Counter()
for hand in s.animals:
dealt += hand
assert sum(dealt.values()) == NUM_ANIMAL_CARDS
# No negative money, scores well-defined.
for hand in s.money:
assert all(c >= 0 for c in hand.values())
scores = final_scores(s)
assert all(sc >= 0 for sc in scores)
def test_random_games_varied_player_counts():
for n in (3, 5):
for seed in range(15):
s = _play_random_game(seed * 7 + 1, num_players=n)
assert KoehandelGame.is_terminal(s)
def test_donkey_payout_gives_everyone_money():
s = KoehandelGame.new_game(num_players=4, seed=0)
# Force a donkey on top of the deck.
s.deck[-1] = Animal.DONKEY
before = [M.total(h) for h in s.money]
auctioneer = s.current_player
KoehandelGame.step(s, next(a for a in KoehandelGame.legal_actions(s)
if a.kind == ActionKind.AUCTION))
after = [M.total(h) for h in s.money]
# Everyone got +50 (first donkey).
for b, a in zip(before, after):
assert a == b + 50
assert s.donkeys_revealed == 1
def test_no_bids_auctioneer_takes_free():
s = KoehandelGame.new_game(num_players=3, seed=2)
s.deck[-1] = Animal.ROOSTER
auctioneer = s.current_player
KoehandelGame.step(s, next(a for a in KoehandelGame.legal_actions(s)
if a.kind == ActionKind.AUCTION))
# Everyone passes.
while s.phase == Phase.AUCTION_BID:
KoehandelGame.step(s, next(a for a in KoehandelGame.legal_actions(s)
if a.kind == ActionKind.PASS))
assert s.animals[auctioneer].get(Animal.ROOSTER, 0) == 1
def test_auction_winner_pays_auctioneer():
s = KoehandelGame.new_game(num_players=3, seed=5)
s.deck[-1] = Animal.COW
auctioneer = s.current_player
money_before = M.total(s.money[auctioneer])
KoehandelGame.step(s, next(a for a in KoehandelGame.legal_actions(s)
if a.kind == ActionKind.AUCTION))
# First bidder bids 10, others pass.
bidder = s.auction.to_act
KoehandelGame.step(s, next(a for a in KoehandelGame.legal_actions(s)
if a.kind == ActionKind.BID and a.amount == 10))
while s.phase == Phase.AUCTION_BID:
KoehandelGame.step(s, next(a for a in KoehandelGame.legal_actions(s)
if a.kind == ActionKind.PASS))
# Auctioneer declines buy-right.
assert s.phase == Phase.AUCTION_BUYRIGHT
KoehandelGame.step(s, next(a for a in KoehandelGame.legal_actions(s)
if a.kind == ActionKind.DECLINE))
assert s.animals[bidder].get(Animal.COW, 0) == 1
# Auctioneer received >= 10 (overpay possible).
assert M.total(s.money[auctioneer]) >= money_before + 10
def _cowtrade_offer_state(money_counter, seed=0):
s = KoehandelGame.new_game(num_players=4, seed=seed)
s.money[0] = Counter(money_counter)
s.cowtrade = CowTradeCtx(initiator=0, target=1, animal=int(Animal.COW), on_pair=False)
s.phase = Phase.COWTRADE_OFFER
s.current_player = 0
return s
def test_cowtrade_offer_requires_a_card_no_free_zero():
# Regression: offering 0 commits zero cards, which is illegal. A player holding
# only 10-value cards (no 0 bluff card) must NOT be able to offer 0.
s = _cowtrade_offer_state({10: 5})
offers = [a.amount for a in KoehandelGame.legal_actions(s)
if a.kind == ActionKind.CT_OFFER]
assert offers, "should still have legal offers"
assert 0 not in offers, "cannot offer 0 without a 0-value card"
assert all(a >= 10 for a in offers)
def test_cowtrade_offer_zero_legal_only_with_bluff_card():
s = _cowtrade_offer_state({0: 1, 10: 2})
offers = [a.amount for a in KoehandelGame.legal_actions(s)
if a.kind == ActionKind.CT_OFFER]
assert 0 in offers, "a real 0-value bluff card makes an offer of 0 legal"
# And applying it transfers exactly the single 0-card (one card committed).
before = M.num_cards(s.money[0])
KoehandelGame.step(s, next(a for a in KoehandelGame.legal_actions(s)
if a.kind == ActionKind.CT_OFFER and a.amount == 0))
assert M.num_cards(s.cowtrade.offer_cards) == 1
def test_cowtrade_counter_requires_a_card():
# Target with only 10-cards cannot counter with 0 either.
s = _cowtrade_offer_state({10: 3})
s.money[1] = Counter({10: 4})
# Put an offer on the table so the target must respond.
KoehandelGame.step(s, next(a for a in KoehandelGame.legal_actions(s)
if a.kind == ActionKind.CT_OFFER))
assert s.phase == Phase.COWTRADE_RESPOND
counters = [a.amount for a in KoehandelGame.legal_actions(s)
if a.kind == ActionKind.CT_COUNTER]
assert 0 not in counters
def test_clone_independence():
s = KoehandelGame.new_game(num_players=4, seed=3)
c = s.clone()
KoehandelGame.step(s, KoehandelGame.legal_actions(s)[0])
assert c.step_count == 0
def test_observation_hides_opponent_money():
s = KoehandelGame.new_game(num_players=4, seed=4)
obs = s.observation(0)
# We know our own money but only card counts for others.
assert obs.my_money == s.money[0]
assert obs.opponent_card_counts[1] == sum(s.money[1].values())