-
Notifications
You must be signed in to change notification settings - Fork 4
/
queued_board.py
88 lines (78 loc) · 3.46 KB
/
queued_board.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 domino_puzzle import Board, Domino, DiceSet, ArrowSet
class QueuedBoard(Board):
def __init__(self,
width: int,
height: int,
max_pips: int = None,
dice_set: DiceSet = None,
arrows: ArrowSet = None):
super().__init__(width, height, max_pips, dice_set, arrows)
self.queue = []
@classmethod
def create(cls, state, border=0, max_pips=None) -> 'QueuedBoard':
board_state, *other_states = state.split('===\n')
board = super().create(board_state, border, max_pips)
if other_states:
queue_state, = other_states
heads, _, tails = queue_state.splitlines()
for head, tail in zip(heads, tails):
if head == ' ':
continue
domino = Domino(int(head), int(tail))
domino.rotate(-90)
board.add_to_queue(domino)
return board
def __eq__(self, other):
if not super().__eq__(other):
return False
return (len(self.queue) == len(other.queue) and
all(d1.head.pips == d2.head.pips and
d1.tail.pips == d2.tail.pips and
d1.direction == d2.direction
for d1, d2 in zip(self.queue, other.queue)))
def fill(self, random, matches_allowed=True, reset_cycles=True):
queue_size = self.width * self.height // 2
new_dominoes = random.sample(self.extra_dominoes, queue_size)
for domino in new_dominoes:
is_flipped = random.randint(0, 1)
if is_flipped:
domino.rotate(180)
self.add_to_queue(domino)
return True
# noinspection PyPep8Naming
def mutate(self, random, board_type=None, matches_allowed=True):
new_board = board_type.create(self.display(), max_pips=self.max_pips)
mutation_count = new_board.pick_mutation_count(len(new_board.queue),
random)
for _ in range(mutation_count):
i = random.randint(0, len(new_board.queue) - 1)
domino = new_board.queue.pop(i)
new_board.extra_dominoes.append(domino)
new_dominoes = random.sample(new_board.extra_dominoes, mutation_count)
for domino in new_dominoes:
is_flipped = random.randint(0, 1)
if is_flipped:
domino.rotate(180)
target_index = random.randint(0, len(new_board.queue))
new_board.add_to_queue(domino, target_index)
return new_board
def __repr__(self):
return 'QueuedBoard({}, {})'.format(self.width, self.height)
def add_to_queue(self, domino, target_index=None):
if self.extra_dominoes:
self.extra_dominoes.remove(domino)
if target_index is None:
self.queue.append(domino)
else:
self.queue[target_index:target_index] = [domino]
def get_from_queue(self):
return self.queue.pop(0)
def display(self, cropped=False, cropping_bounds=None):
display = super().display(cropped, cropping_bounds)
if self.queue:
display += '===\n'
queue_names = [domino.get_name() for domino in self.queue]
display += ' '.join(name[0] for name in queue_names) + '\n'
display += ' '.join('-' for _ in queue_names) + '\n'
display += ' '.join(name[1] for name in queue_names) + '\n'
return display