-
Notifications
You must be signed in to change notification settings - Fork 4
/
rules.py
65 lines (51 loc) · 1.67 KB
/
rules.py
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
import random
from domino import DominoManager
class BaseRule:
"""
Several matches of domino are played most of the time
on particular rules to determine the winner. This is a
wrapper to implement and play with different rules.
"""
def run(self, players):
"""
Return id of winner team (-1 for tie)
"""
raise NotImplementedError()
class OneGame:
"""
Play one game
"""
def start(self, player0, player1):
env = DominoManager()
players = [player0("0"), player1("1"), player0("2"), player1("3")]
return env.run(players)
class TwoOfThree:
"""
First to win two games. Last winner start next match
"""
def __init__(self, random_start=True):
self.random_start = random_start
def start(self, player0, player1):
env = DominoManager()
players = [player0("0"), player1("1"), player0("2"), player1("3")]
cur_start = 0
if self.random_start:
if random.choice([False, True]):
cur_start ^= 1
players[0], players[1] = players[1], players[0]
players[2], players[3] = players[3], players[2]
wins = [0, 0]
while max(wins) < 2:
result = env.run(players)
if result != -1:
wins[result ^ cur_start] += 1
if result == -1 or result != cur_start:
# Swap players
cur_start ^= 1
players[0], players[1] = players[1], players[0]
players[2], players[3] = players[3], players[2]
return 0 if wins[0] > wins[1] else 1
RULES = [
OneGame,
TwoOfThree,
]