class_name Play ## Plays whole games between bots (play.ts), for batch runs and tests. const MAX_ACTIONS := 200000 ## Bot makers by name. Each takes a seed and returns a bot (see RandomBot). The greedy bots ignore it. static func bots() -> Dictionary: return { "random": func(seed: int): return RandomBot.new(seed), "greedy": func(_seed: int): return GreedyBot.new(), "nocards": func(_seed: int): return GreedyBot.new({"useCards": false}), } ## The decks a game with this seed is dealt when none are given, from the cards `include` allows. static func deal(pool: Dictionary, seed: int, rules: Dictionary, include := Callable()) -> Array: var deck_rng := Rng.make(seed ^ 0x9e3779b9) return [Deck.random_deck(pool, deck_rng, rules, include), Deck.random_deck(pool, deck_rng, rules, include)] ## Plays one game to the end from {pool, bots: [maker, maker], seed, decks?, rules?, log?, check?}. ## The same options and seed always produce the same game. Returns the final state, or null if the ## engine failed (see Util.errors). static func play_game(o: Dictionary) -> Variant: var rules: Dictionary = o.get("rules", Rules.defaults()) var decks: Array = o.get("decks", deal(o.pool, o.seed, rules)) var s = RulesEngine.create_game({"pool": o.pool, "decks": decks, "seed": o.seed, "rules": rules, "log": o.get("log", false)}) if s == null: return null var players := [o.bots[0].call(o.seed * 2 + 1), o.bots[1].call(o.seed * 2 + 2)] var n := 0 while not s.over: n += 1 if n > MAX_ACTIONS: return Util.fail("Game with seed %d didn't finish in %d actions" % [o.seed, MAX_ACTIONS]) var p: int = RulesEngine.decider(s) var legal := RulesEngine.legal_actions(s) var action: Dictionary = players[p].choose(RulesEngine.view_for(s, p), legal, p) if not RulesEngine.apply_action(s, action, legal): return null if o.get("check", false): var errors := Invariants.check(s) if not errors.is_empty(): return Util.fail("Seed %d, turn %d:\n %s" % [o.seed, s.turn, "\n ".join(errors)]) return s