Initial
This commit is contained in:
commit
2ca7a264b3
53 files changed
+7113
No files matched your search
@@ -0,0 +1,196 @@
|
||||
import copy
|
||||
import random
|
||||
import unittest
|
||||
from collections import Counter
|
||||
|
||||
from game import engine as e
|
||||
from game.strategy import decide
|
||||
|
||||
|
||||
class Rules(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.rng = random.Random(215)
|
||||
self.s = e.new_game(3, "purple", self.rng)
|
||||
self.s["active"] = 0
|
||||
self.log = []
|
||||
self.t = e.Turn(self.s, lambda s, k, d: self.log.append((k, d)), self.rng)
|
||||
self.own = [key for key, p in self.s["planes"].items() if p["owner"] == 0]
|
||||
|
||||
def test_setup_and_full_games(self):
|
||||
for bots in (3, 4, 5):
|
||||
s = e.new_game(bots, rng=self.rng)
|
||||
self.assertEqual(len(s["deck"]), 146 - (bots + 1) * 9)
|
||||
self.assertEqual(len(set(s["colors"])), bots + 1)
|
||||
|
||||
def audit(state, kind, data):
|
||||
cards = [*state["deck"], *state["discard"]]
|
||||
for h in state["hands"]:
|
||||
cards.extend(h)
|
||||
cards.extend(x for x in (state["weather"], state["transit"]) if x)
|
||||
self.assertEqual(Counter(cards), Counter(e.CAT["cards"]))
|
||||
positions = [
|
||||
p["pos"]
|
||||
for p in state["planes"].values()
|
||||
if p["pos"] not in ("H", "F")
|
||||
]
|
||||
self.assertEqual(len(positions), len(set(positions)))
|
||||
|
||||
while s["winner"] is None and s["turn"] < 1800:
|
||||
cmd = decide(
|
||||
dict(view=e.observation(s, e.actor(s)), actions=e.actions(s))
|
||||
)["command"]
|
||||
e.Turn(s, audit, self.rng).apply(cmd)
|
||||
self.assertIsNotNone(s["winner"])
|
||||
self.assertTrue(
|
||||
all(
|
||||
p["pos"] == "F"
|
||||
for p in s["planes"].values()
|
||||
if p["owner"] == s["winner"]
|
||||
)
|
||||
)
|
||||
|
||||
def test_snow_and_captain(self):
|
||||
key = self.own[0]
|
||||
self.s["planes"][key]["pos"] = "A:B1"
|
||||
self.s["hands"][0] = ["B", "B", "LC", "LC"]
|
||||
self.s["weather"] = "SN"
|
||||
cmd = dict(kind="move", plane=key, to="B:B1", cards=["B", "B", "LC"])
|
||||
prev = copy.deepcopy(self.s)
|
||||
with self.assertRaises(ValueError):
|
||||
self.t.apply(cmd)
|
||||
self.assertEqual(prev, self.s)
|
||||
cmd["cards"].append("LC")
|
||||
self.t.apply(cmd)
|
||||
self.assertEqual(self.s["planes"][key]["pos"], "B:B1")
|
||||
|
||||
def test_exchange_pauses_then_continues(self):
|
||||
s = self.s
|
||||
for i, key in enumerate(self.own[:2]):
|
||||
s["planes"][key]["pos"] = "A:B" + str(i + 1)
|
||||
s["deck"].extend(["B", "DLY", "A", "FG"])
|
||||
self.t.apply(dict(kind="exchange", cards=s["hands"][0][:2]))
|
||||
self.assertEqual(e.actor(s), 1)
|
||||
self.assertEqual(s["deck"][-1], "B")
|
||||
self.assertEqual(s["owed"], 1)
|
||||
self.assertEqual(s["weather"], "FG")
|
||||
self.assertEqual(len(e.actions(s)), 2)
|
||||
self.t.apply(e.actions(s)[0])
|
||||
self.assertEqual(s["known"][0], {"A": 1, "B": 1})
|
||||
self.assertEqual(s["planes"][self.own[0]]["pos"], "H")
|
||||
self.assertEqual(s["turn"], 2)
|
||||
|
||||
def test_private_draw(self):
|
||||
self.s["hands"][0] = ["SC"] * 7 + ["A", "A"]
|
||||
self.s["deck"].extend(["FG", "DLY", "ICE"])
|
||||
self.t.apply(dict(kind="weather", cards=["SC"]))
|
||||
self.assertEqual(self.s["weather"], "SC")
|
||||
self.assertIn("ICE", self.s["hands"][0])
|
||||
self.assertEqual(self.s["known"][0], {})
|
||||
|
||||
def test_vip_and_runway_choice(self):
|
||||
key = self.own[0]
|
||||
s = self.s
|
||||
s["hands"][0][0] = "VIP"
|
||||
self.t.apply(dict(kind="move", plane=key, to="V1:31L", cards=["VIP"]))
|
||||
self.assertEqual(s["planes"][key]["pos"], "V1:31L")
|
||||
s["active"] = 0
|
||||
self.t.finish()
|
||||
self.assertEqual(s["planes"][key]["pos"], "V2:31L")
|
||||
s["active"] = 0
|
||||
self.t.finish()
|
||||
self.assertTrue(s["pending"])
|
||||
self.t.apply(dict(kind="runway", plane=key, to="D:31R:0"))
|
||||
self.assertEqual(s["planes"][key]["pos"], "D:31R:0")
|
||||
s["active"] = 0
|
||||
self.t.finish()
|
||||
self.assertEqual(s["planes"][key]["pos"], "F")
|
||||
|
||||
def test_two_takeoffs_and_no_chain(self):
|
||||
for key, pos in zip(self.own, ["D:31L:0", "D:31R:0", "D:31R:1"]):
|
||||
self.s["planes"][key]["pos"] = pos
|
||||
self.t.finish()
|
||||
self.assertEqual(
|
||||
[self.s["planes"][k]["pos"] for k in self.own[:3]], ["F", "F", "D:31R:0"]
|
||||
)
|
||||
|
||||
def test_crosswind_snow_ice_and_closed(self):
|
||||
for weather in ["FG", "31L", "CW", "SN", "ICE", "SC"]:
|
||||
for size in (1, 2, 3):
|
||||
s = copy.deepcopy(self.s)
|
||||
head, follower = self.own[:2]
|
||||
s["planes"][head].update(pos="D:31L:0", size=size)
|
||||
s["planes"][follower]["pos"] = "D:31L:1"
|
||||
s["weather"] = weather
|
||||
e.Turn(s, lambda *args: None).finish()
|
||||
expected = (
|
||||
"D:31L:0"
|
||||
if weather in ("FG", "31L")
|
||||
else "D:31L:1"
|
||||
if weather == "ICE" or (weather, size) in (("CW", 1), ("SN", 3))
|
||||
else "F"
|
||||
)
|
||||
self.assertEqual(s["planes"][head]["pos"], expected)
|
||||
self.s["weather"] = "ICE"
|
||||
for n, lane in enumerate(e.LANES):
|
||||
self.s["planes"][self.own[n]]["pos"] = f"D:{lane}:0"
|
||||
self.s["hands"][0] = ["AI"] * 9
|
||||
self.t.apply(dict(kind="ice", plane=self.own[0], cards=["AI"] * 3))
|
||||
self.assertEqual(self.s["planes"][self.own[1]]["pos"], "D:31R:0")
|
||||
|
||||
def test_invisible_redistribution(self):
|
||||
s = self.s
|
||||
for pending in [None, dict(seat=1, card="DLY", targets=self.own[:2])]:
|
||||
s["pending"] = pending
|
||||
seat = e.actor(s)
|
||||
other = (seat + 1) % len(s["hands"])
|
||||
altered = copy.deepcopy(s)
|
||||
altered["deck"].reverse()
|
||||
h = altered["hands"][other]
|
||||
h[0], altered["deck"][-1] = altered["deck"][-1], h[0]
|
||||
|
||||
def req(game):
|
||||
return dict(view=e.observation(game, seat), actions=e.actions(game))
|
||||
|
||||
self.assertEqual(req(s), req(altered))
|
||||
self.assertEqual(decide(req(s)), decide(req(altered)))
|
||||
|
||||
def test_blocked_stand_and_frozen_queue(self):
|
||||
key = self.own[0]
|
||||
self.s["planes"][key]["pos"] = "C:C1"
|
||||
self.s["hands"][0] = ["A"] * 9
|
||||
self.assertFalse(any(c.get("to") == "A:C1" for c in e.actions(self.s)))
|
||||
self.s["weather"] = "FG"
|
||||
self.s["planes"][key]["pos"] = "D:31L:2"
|
||||
self.t.compact()
|
||||
self.assertEqual(e.tail(self.s, "31L"), "D:31L:3")
|
||||
self.t.weather("SC")
|
||||
self.assertEqual(self.s["planes"][key]["pos"], "D:31L:0")
|
||||
|
||||
def test_attack_targets_and_refill(self):
|
||||
enemy = next(k for k, p in self.s["planes"].items() if p["owner"] == 1)
|
||||
self.s["planes"][enemy]["pos"] = "D:31L:0"
|
||||
self.s["hands"][0] = ["DLY", "FLR"] + ["A"] * 7
|
||||
commands = e.actions(self.s)
|
||||
flr = dict(kind="attack", plane=enemy, cards=["FLR"])
|
||||
self.assertIn(flr, commands)
|
||||
self.assertNotIn(dict(kind="attack", plane=enemy, cards=["DLY"]), commands)
|
||||
self.t.apply(flr)
|
||||
self.assertEqual(self.s["planes"][enemy]["pos"], "H")
|
||||
self.s["active"] = 0
|
||||
self.s["deck"] = ["A"]
|
||||
self.s["discard"] = ["B", "B", "C"]
|
||||
self.s["weather"] = "ICE"
|
||||
self.t.apply(dict(kind="exchange", cards=["A", "A"]))
|
||||
self.assertEqual(len(self.s["hands"][0]), 9)
|
||||
self.assertEqual(self.s["weather"], "ICE")
|
||||
self.assertIn("shuffle", [kind for kind, _ in self.log])
|
||||
|
||||
def test_closed_hand_can_hold_trouble_and_weather(self):
|
||||
s = self.s
|
||||
key = self.own[0]
|
||||
s["hands"][0] = ["A"] * 9
|
||||
s["deck"].extend(["FG", "DLY", "SN"])
|
||||
self.t.apply(dict(kind="move", plane=key, to="A:B1", cards=["A"] * 3))
|
||||
self.assertIsNone(s["weather"])
|
||||
self.assertEqual(s["planes"][key]["pos"], "A:B1")
|
||||
self.assertTrue({"FG", "DLY", "SN"}.issubset(s["hands"][0]))
|
||||
Reference in new issue
Block a user