-
Notifications
You must be signed in to change notification settings - Fork 5
/
bblocks.py
executable file
·218 lines (179 loc) · 5.9 KB
/
bblocks.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import sys
from random import choice as rndchoice
from random import randrange
from itertools import cycle
from time import sleep
from utils import Loop, TextInput, range1, first, nl
from board import Board, BaseTile, Loc, Dir
from avkutil import Term
size = 6
pause_time = 0.2
players = {1: "➀➁➂➃", 2: "➊➋➌➍"}
ai_players = [1, ]
check_moves = 15
padding = 2, 1
blink_speed = 0.1
randomize_option = False
commands = {
'a' : "left",
'd' : "right",
'w' : "up",
's' : "down",
't' : "toggle",
'\n': "move",
' ' : "move",
'q' : "quit",
}
p=print
class Tile(BaseTile):
num = maxnum = player = blank = None
def __repr__(self):
if self.blank:
return ' '
if self.player:
n = players[self.player][self.num-1]
# need separate representations of 'maxnum' for both players
# if self.num == self.maxnum:
# return '='
return n
else:
return str(self.num)
def increment(self, player, initial=True):
""" Increment tile number; if number wraps, increment neighbour tiles.
`bblocks.counter` is used to avoid infinite recursion loops.
IF initial, get list of tiles, and blink all of them + original tile; otherwise, return list of tiles
"""
end = False
if not bblocks.counter.next():
end = bblocks.check_end(player)
tiles = []
do_wrap = self._increment(player)
if do_wrap and not end:
for tile in board.cross_neighbours(self):
tiles.append(tile)
tiles.extend(tile.increment(player, initial=False))
if initial or end:
self.blink_tiles(set(tiles + [self]))
if end:
bblocks.end(player)
else:
return tiles
def blink_tiles(self, tiles):
for _ in range(2):
for tile in tiles:
tile.blank = not tile.blank
sleep(blink_speed)
board.draw()
def _increment(self, player):
self.player = player
self.num.next()
return bool(self.num == 1) # return True if wrapping around
class BlocksBoard(Board):
def __init__(self, *args, **kwargs):
super(BlocksBoard, self).__init__(*args, **kwargs)
self.current = Loc(0,0)
self.hl_visible = False
neighbours = self.neighbour_cross_locs
for tile in self:
tile.maxnum = len( [self.valid(nbloc) for nbloc in neighbours(tile)] )
tile.num = Loop(range1(tile.maxnum))
if randomize_option:
for _ in range(randrange(0, tile.maxnum)):
tile.num.next()
def ai_move(self, player):
"""Randomly choose between returning the move closest to completing a tile or a random move."""
tiles = [t for t in self if self.valid_move(player, t)]
# tiles.sort(key=to_max)
to_max = lambda t: t.maxnum - t.num
loc = rndchoice( [min(tiles, key=to_max), rndchoice(tiles)] )
if loc == self.current:
self.hl_visible = False
return loc
def valid_move(self, player, tile):
return bool(tile.player==player or not tile.player)
class Commands:
player = None
commands = commands
def __getitem__(self, cmd):
return getattr(self, self.commands[cmd])
def move_dir(self, dir):
loc = board.nextloc(board.current, dir)
self.highlight(loc)
def down(self):
self.move_dir(Dir(0,1))
def up(self):
self.move_dir(Dir(0,-1))
def right(self):
self.move_dir(Dir(1,0))
def left(self):
self.move_dir(Dir(-1,0))
def toggle(self):
if board.hl_visible:
board.hl_visible = False
board.draw()
else:
i = board[board.current]
board[board.current] = '*'
board.draw()
board[board.current] = i
board.hl_visible = True
def move(self):
loc = board.current
if board.valid_move(self.player, board[loc]):
board.hl_visible = False
return board[loc]
else:
print("Invalid move")
def highlight(self, loc):
if not loc: return
i = board[loc]
board[loc] = '*'
board.draw()
board[loc] = i
board.current = loc
board.hl_visible = True
def quit(self):
sys.exit()
class BlockyBlocks(object):
winmsg = "player %s wins!"
counter = Loop(range(check_moves))
def __init__(self):
self.term = Term()
def check_end(self, player):
"""Check if game is finished."""
return all(tile.player==player for tile in board)
def end(self, player):
board.draw()
print(nl, self.winmsg % player)
sleep(2)
sys.exit()
def run(self):
for p in cycle(players.keys()):
board.draw()
tile = board.ai_move(p) if p in ai_players else self.get_move(p)
tile.increment(p)
if self.check_end(p):
self.end(p)
def get_move(self, player):
commands.player = player
while True:
cmd = self.term.getch()
try:
val = commands[cmd]()
if val:
return val
except KeyError:
print("unknown command:", cmd)
if __name__ == "__main__":
arg = sys.argv[1:]
if arg and arg[0] == '-r':
randomize_option = True
commands = Commands()
board = BlocksBoard(size, Tile, num_grid=False, padding=padding, pause_time=pause_time)
bblocks = BlockyBlocks()
try:
bblocks.run()
except KeyboardInterrupt:
sys.exit()