-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.py
381 lines (317 loc) · 12.9 KB
/
play.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 2 15:34:56 2018
@author: levi
"""
import random
class msgr:
"""The messenger, which prints the messages."""
def __init__(self, InptLang='eng'):
if not(InptLang == 'eng' or InptLang == 'por'):
print("Unknown language.")
raise Exception
else:
self.lang = InptLang
def prnt(self, textDict):
"""Print a given text."""
if isinstance(textDict, dict):
print(textDict.get(self.lang, 'eng'))
else:
print(textDict)
class domino:
"""Class domino holds the basics for the game in general.
There are methods for the getting piece pair from integer number and vice
versa, compatibility test, etc.
"""
def isComp(self, handP, tablP):
"""Find out if piece is compatible with the table's current state.
handP: the piece in the player's hand (represented by its integer
(0 to 27) number; whose compatibility is being tested
tablP: the piece representing the state of the table (also represen-
ted by its integer (0 to 27).
It returns an integer with the sum of the possibilities for the given
piece:
0 means no possibility,
1 means the piece is appliable on the left (lesser) side,
with True orientation
2 means the piece is appliable on the right (greater) side,
with True orientation
4 means the piece is appliable on the left (lesser) side,
with False orientation
8 means the piece is appliable on the right (greater) side,
with False orientation
"""
# these are the piece pairs (e.g. [6,6] instead of 27)
handPP = self.getPiecPair(handP)
tablPP = self.getPiecPair(tablP)
NPsbl = 0
if handPP[0] == tablPP[0]:
NPsbl += 1
if handPP[0] == tablPP[1]:
NPsbl += 2
if handPP[1] == tablPP[0]:
NPsbl += 4
if handPP[1] == tablPP[1]:
NPsbl += 8
return NPsbl
@staticmethod
def getPiecPair(piecNum):
"""Get piece pair.
Gets the pair of numbers that represent the piece (e.g., [0,0], [2,3],
[4,4], [5,6]]), given its piece number (e.g., 0, 14, 22, 26).
"""
pair = [0, 0]
if piecNum < 7:
pair[1] = piecNum
elif piecNum < 13:
pair[0] = 1
pair[1] = piecNum - 6
elif piecNum < 18:
pair[0] = 2
pair[1] = piecNum - 11
elif piecNum < 22:
pair[0] = 3
pair[1] = piecNum - 15
elif piecNum < 25:
pair[0] = 4
pair[1] = piecNum - 18
elif piecNum < 27:
pair[0] = 5
pair[1] = piecNum - 20
else:
pair = [6, 6]
return pair
@staticmethod
def getPiecNum(piecPair):
"""Get piece number.
Gets the piece number (e.g., 0, 14, 22, 26) given the pair of
numbers that represent the piece (e.g., [0,0], [2,3], [4,4], [5,6]).
"""
piecPair.sort()
num = 0
for ind in range(6):
if piecPair[0] > ind:
num += 6-ind
num += piecPair[1]
return num
@staticmethod
def pcsWith(num):
"""Get all possible pieces with a given number.
For example:
- pieces #0 through #6 are the ones that have a 0 in it.
- pieces #6, #12, #17, #21, #24, #26 and #27 are the ones that
have a 6 in it.
returns: a list of the 7 pieces that have the given number 'num'.
"""
ret = [0]*7
for i in range(7):
ret[i] = domino.getPiecNum([num, i])
return ret
class player:
"""Represents the player."""
def __init__(self, plNumb, isAuto, hand, sttg='rand'):
self.plNumb = plNumb
self.isAuto = isAuto
self.sttg = sttg
self.hand = hand
def promUser(self, currPiec, psbl, msgr=None):
"""Prompt user for input, showing his/her possibilities.
Returns the index for the possibilites list psbl.
"""
dom = domino()
if currPiec is not None:
currPiecPair = dom.getPiecPair(currPiec)
else:
currPiecPair = [0, 0]
keepAsk = True
while keepAsk:
msgr.prnt({'eng': "\nThis is your hand:",
'por': "\nEsta é a sua mão:"})
hand = self.hand
handStr = ''
for ind in range(len(hand)):
handStr += str(dom.getPiecPair(hand[ind]))
handStr += ', '
msgr.prnt(handStr)
if len(psbl) == 0:
dic = {'eng': "\nI'm sorry, there is nothing you can play " +
"now.\nPress any key to continue...",
'por': "\nDesculpe, não há o que jogar agora." +
"\nPressione qualquer tecla para continuar..."}
msgr.prnt(dic)
input(" >> ")
keepAsk = False
choice = None
else:
if len(psbl) == 1:
msgr.prnt({'eng': "\nThere is only one possibility:",
'por': "\nSó há uma possibilidade:"})
else:
msgr.prnt({'eng': "\nChoose the piece to be played:",
'por': "\nEscolha a peça a ser jogada:"})
for ind in range(len(psbl)):
piec, posi, ornt = psbl[ind]
strPrnt = ' - ' + str(ind) + ' : '
strPrnt += str(dom.getPiecPair(piec))
if currPiec is not None:
str1, str2, str3 = '', '', ''
if msgr.lang == 'eng':
str1 = '(' + str(piec) + ') at the '
str2 = 'left position (by the '
str3 = 'right position (by the '
elif msgr.lang == 'por':
str1 = '(' + str(piec) + ') ao lado '
str2 = 'esquerdo (junto ao '
str3 = 'direito (junto ao '
if posi == 0:
str3 = ''
else:
str2 = ''
strPrnt += str1 + str2 + str3
strPrnt += str(currPiecPair[posi])
strPrnt += ')'
msgr.prnt(strPrnt)
#
if len(psbl) == 1:
msgr.prnt({'eng': "Press any key to play it.",
'por': "Pressione qualquer tecla para "
"jogá-la."})
input(" >> ")
choice = 0
keepAsk = False
else:
strChoice = input(" >> ")
try:
choice = int(strChoice)
isInt = True
except ValueError:
isInt = False
if isInt and choice > -1 and choice < len(psbl):
keepAsk = False
else:
msgr.prnt({'eng': "\nError while parsing input." +
"\nPlease try again.",
'por': "\nErro ao interpretar entrada." +
"\nPor favor, tente novamente."})
#
#
return choice
def getPsbl(self, currPiec):
"""Get the possibilities for playing.
Returns a list of possibilities.
Each possibility is a triple containing:
1. ind: the index (integer, 0 to 6) of the piece in the player's
hand;
2. posi: 0 or 1, correponding to the side of the table
(0 is for the lesser side, 1 for the greater one);
3. ornt: True or False, corresponding to the piece's orientation
(True is for setting the piece on its lesser side,
revealing the greater one; False for the opposite).
"""
dom = domino()
psbl = []
if currPiec is None:
# If there is no current piece, than all pieces are allowed.
# Orientation and position do not apply.
for ind in self.hand:
psbl.append([ind, 0, True])
else:
for ind in self.hand:
# all the possibilities for this particular piece are encoded
# in psblIndx
psblIndx = dom.isComp(ind, currPiec)
piecPair = dom.getPiecPair(ind)
if psblIndx > 0:
if psblIndx % 2 == 1:
psbl.append([ind, 0, True])
psblIndx -= 1
if psblIndx % 4 == 2:
psbl.append([ind, 1, True])
psblIndx -= 2
if psblIndx % 8 == 4:
if piecPair[0] != piecPair[1]:
psbl.append([ind, 0, False])
psblIndx -= 4
if psblIndx > 0:
if piecPair[0] != piecPair[1]:
psbl.append([ind, 1, False])
#
#
#
#
return psbl
def play(self, currPiec, piecHist, strtWith=None, msgr=None):
"""Play a piece.
This method chooses the piece to be played (if such piece exists),
its orientation and placement, and then removes the piece from the
player's hand (again, if it is the case).
Orientation is True for a lesser side fitting the table and exposing
a greater side (e.g., placing a [2,3] at an end with an exposed 2)
and False for the opposite. Of course, this makes no difference for
double pieces.
Position is either 0 or 1: 0 for the left (lesser) side, and 1 for the
right (greater) side. Again, this makes no difference if the current
state is equivalent to a double piece, e.g., [4,4].
"""
dom = domino()
# This first part is concerned with choosing a piece to be played
playPiec = strtWith
posi, ornt = None, None
# strtWith is only used when starting a match with the [6,6], [5,5],
# etc. No choice in this case.
if strtWith is None:
psbl = self.getPsbl(currPiec)
if self.isAuto:
if len(psbl) > 0:
# HERE IS WHERE THE PLAYER MAKES THE CHOICE
if self.sttg == 'rand':
# Random stategy: play anything
playPiec, posi, ornt = random.choice(psbl)
# print("Playing randomly here!")
if self.sttg == 'basic':
# Basic stategy: play the highest possible piece
playPiec, posi, ornt = psbl[-1]
# print("Playing basic here!")
# print("Not implemented yet!")
else:
# MANUAL MODE
choice = self.promUser(currPiec, psbl, msgr=msgr)
if choice is None:
return None, None, None
else:
playPiec, posi, ornt = psbl[choice]
#
if playPiec is not None:
str1, str2, str3 = '', '', ''
if msgr.lang == 'eng':
str1 = "\nPlayer #" + str(self.plNumb) + \
": I've played piece " + \
str(dom.getPiecPair(playPiec)) + " (" + \
str(playPiec) + ") at the "
str2 = "left side!"
str3 = "right side!"
elif msgr.lang == 'por':
str1 = "\nJogador #" + str(self.plNumb) + \
": Joguei a pedra " + \
str(dom.getPiecPair(playPiec)) + " (" + \
str(playPiec) + ") no lado "
str2 = "esquerdo!"
str3 = "direito!"
#
if posi == 0:
str3 = ''
else:
str2 = ''
msgr.prnt(str1+str2+str3)
self.hand.remove(playPiec)
else:
pair = dom.getPiecPair(currPiec)
msgr.prnt({'eng': "Player #" + str(self.plNumb) +
": Passed!\n" +
"> Has no " + str(pair[0]) + " or " +
str(pair[1]) + ".",
'por': "Jogador #" + str(self.plNumb) + ": Passou!\n" +
"> Não tem " + str(pair[0]) + " nem " +
str(pair[1]) + "."})
return playPiec, posi, ornt