-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathiplayer.py
330 lines (263 loc) · 11.5 KB
/
iplayer.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# Written by Michelle Blom, 2019
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
from model import *
from utils import *
class InteractivePlayer(Player):
def __init__(self, _id):
super().__init__(_id)
def SelectMove(self, moves, game_state):
plr_state = game_state.players[self.id]
print("AZUL > Player {} is now in control\n".format(self.id))
print("------------")
print("Game State")
print("------------")
print(BoardToString(game_state))
print("------------")
print("Player State")
print("------------")
print(PlayerToString(self.id, plr_state))
move = None
while True:
move = None
print("> Select Option ('back' returns to this menu): ")
print("(1) See available moves")
print("(2) Take tiles from a factory display")
print("(3) Take tiles from the centre")
option = input()
if option == "back":
continue
if not option.isdigit():
print("> Option not recognised. Repeating request.")
continue
ioption = int(option)
cont = False
if ioption == 1:
# Print Moves
for mo in moves:
print(MoveToString(self.id, mo))
print("\n")
continue
elif ioption == 2:
# Ask for factory ID and tile type
cont = False
while True:
cont = False
print("> Factory ID?")
fid = input()
if fid == "back":
cont = True
break
if not fid.isdigit():
print("> Invalid input. Repeating request")
continue
ifid = int(fid)
if ifid < 1 or ifid > len(game_state.factories)+1:
print("> Invalid factory id. Repeating request.")
continue
td = game_state.factories[ifid-1]
if td.total == 0:
print("> No tiles available in this display")
cont = True
break
tiles_avail = ""
for tile in Tile:
if td.tiles[tile] > 0:
tiles_avail+="{}/".format(TileToShortString(tile))
tile_type = Tile.BLUE
while True:
print("> Tile type ({})?".format(tiles_avail))
tt = input()
if tt == "back":
cont = True
break
if tt == "R":
tile_type = Tile.RED
elif tt == "B":
tile_type = Tile.BLUE
elif tt == "W":
tile_type = Tile.WHITE
elif tt == "Y":
tile_type = Tile.YELLOW
elif tt == "K":
tile_type = Tile.BLACK
else:
print("> Invalid tile type. Repeating request")
continue
if td.tiles[tile_type] == 0:
print("> No tiles of that type available")
continue
break
if cont:
break
number = td.tiles[tile_type]
dest = 0
# Get destination (only display available destinations)
while True:
print("> Destination?")
dests = " (0) Floor line\n"
doptions = [0]
for i in range(plr_state.GRID_SIZE):
# Does the corresponding grid position already
# have a tile on it?
col = int(plr_state.grid_scheme[i][tile_type])
if plr_state.grid_state[i][col] == 1:
# This tile type cannot be placed in this
# pattern line
continue
if (plr_state.lines_tile[i] == -1 or \
plr_state.lines_tile[i] == tile_type) and \
plr_state.lines_number[i] < i+1:
dests += " ({}) Pattern line {}\n".format(
i+1, i+1)
doptions.append(i+1)
print(dests)
rdest = input()
if rdest == "back":
cont = True
break
if not rdest.isdigit():
print("> Invalid input. Repeating request")
continue
idest = int(rdest)
if not (idest in doptions):
print("> Invalid input. Repeating request")
continue
dest = idest
break
if cont:
break
# We have all the information we need to define the move
tgrab = TileGrab()
tgrab.tile_type = tile_type
tgrab.number = number
if dest == 0:
tgrab.num_to_floor_line = number
else:
# Compute number of tiles that could be placed
# in the selected pattern line, rest goes to floor
toline = min(dest - plr_state.lines_number[dest-1],
number)
tgrab.pattern_line_dest = dest - 1
tgrab.num_to_pattern_line = toline
tgrab.num_to_floor_line = number - toline
move = (Move.TAKE_FROM_FACTORY, ifid-1, tgrab)
break
if cont:
continue
elif ioption == 3:
cont = False
# We are taking tiles from the centre pool
td = game_state.centre_pool
if td.total == 0:
print("> No tiles available in the centre")
cont = True
break
tiles_avail = ""
for tile in Tile:
if td.tiles[tile] > 0:
tiles_avail+="{}/".format(TileToShortString(tile))
tile_type = Tile.BLUE
while True:
print("> Tile type ({})?".format(tiles_avail))
tt = input()
if tt == "back":
cont = True
break
if tt == "R":
tile_type = Tile.RED
elif tt == "B":
tile_type = Tile.BLUE
elif tt == "W":
tile_type = Tile.WHITE
elif tt == "Y":
tile_type = Tile.YELLOW
elif tt == "K":
tile_type = Tile.BLACK
else:
print("> Invalid tile type. Repeating request")
continue
if td.tiles[tile_type] == 0:
print("> No tiles of that type available")
continue
break
if cont:
continue
number = td.tiles[tile_type]
dest = 0
# Get destination (only display available destinations)
while True:
print("> Destination?")
dests = " (0) Floor line\n"
doptions = [0]
for i in range(plr_state.GRID_SIZE):
# Does the corresponding grid position already
# have a tile on it?
col = int(plr_state.grid_scheme[i][tile_type])
if plr_state.grid_state[i][col] == 1:
# This tile type cannot be placed in this
# pattern line
continue
if (plr_state.lines_tile[i] == -1 or
plr_state.lines_tile[i] == tile_type) and \
plr_state.lines_number[i] < i+1:
dests += " ({}) Pattern line {}\n".format(
i+1, i+1)
doptions.append(i+1)
print(dests)
rdest = input()
if rdest == "back":
cont = True
break
if not rdest.isdigit():
print("> Invalid input. Repeating request")
continue
idest = int(rdest)
if not (idest in doptions):
print("> Invalid input. Repeating request")
continue
dest = idest
break
if cont:
continue
# We have all the information we need to define the move
tgrab = TileGrab()
tgrab.tile_type = tile_type
tgrab.number = number
if dest == 0:
tgrab.num_to_floor_line = number
else:
# Compute number of tiles that could be placed
# in the selected pattern line, rest goes to floor
toline = min(dest - plr_state.lines_number[dest-1],
number)
tgrab.pattern_line_dest = dest - 1
tgrab.num_to_pattern_line = toline
tgrab.num_to_floor_line = number - toline
move = (Move.TAKE_FROM_CENTRE, -1, tgrab)
break
else:
cont = True
print("Option not recognised. Repeating request.")
if not cont:
break
# Does the move exist in the available set of moves?
found = False
for m in moves:
if m[0]==move[0] and m[1]==move[1] and SameTG(m[2],move[2]):
found = True
break
assert found
return move