-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimple.py
88 lines (65 loc) · 2.27 KB
/
simple.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from player import BasePlayer
import random
class BigDrop(BasePlayer):
""" Always drop piece with highest score
"""
def __init__(self, name):
super().__init__(f"BigDrop::{name}")
def choice(self):
heads = self.heads
max_weight = 0
fat = []
for piece in self.pieces:
for head in range(2):
if self.valid(piece, head):
weight = piece[0] + piece[1]
if weight > max_weight:
fat.clear()
max_weight = weight
if weight == max_weight:
fat.append((piece, head))
assert len(fat) > 0
move = random.choice(fat)
return move
class Random(BasePlayer):
""" Make a random move at each step
"""
def __init__(self, name):
super().__init__(f"Random::{name}")
def choice(self):
heads = self.heads
valids = []
for piece in self.pieces:
for head in range(2):
if self.valid(piece, head):
valids.append((piece, head))
return random.choice(valids)
class Frequent(BasePlayer):
""" Find piece most frequent in its hand. It tries to avoid passing.
"""
def __init__(self, name):
super().__init__(f"Frequent::{name}")
def choice(self):
# List all valid moves in the form (piece, head).
# This is put piece on head.
valids = []
for piece in self.pieces:
for head in range(2):
if self.valid(piece, head):
valids.append((piece, head))
# One piece A is neighbor of B if have at least one common number
# Find pieces with largest number of neighbors
pieces = []
best_freq = -1
for (cur_piece, head) in valids:
freq = 0
for piece in self.pieces:
if piece[0] in cur_piece or piece[1] in cur_piece:
freq += 1
if freq > best_freq:
best_freq = freq
pieces = []
if freq == best_freq:
pieces.append((cur_piece, head))
# Return one piece with largest number of neighbors randomly
return random.choice(pieces)