-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
88 lines (69 loc) · 2.67 KB
/
player.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 tabulate import tabulate
class Player(object):
def __init__(self, index):
self.index = index # player number
self.hand = []
self.hand_colour_info = {}
self.hand_number_info = {}
return None
def __str__(self):
s = f'Player {self.index}:\n'
tab = []
for c in self.hand:
tab.append([c.colour + ' ' + str(c.number),
str(self.hand_colour_info[c]).replace('None', '-')
+ ' ' +
str(self.hand_number_info[c]).replace('None', '-')])
s = s + tabulate(tab, headers=('hand', 'info'))
return s
def info_string(self):
s = f'Player {self.index}:\n'
tab = []
for c in self.hand:
tab.append([str(self.hand_colour_info[c]).replace('None', '-')
+ ' ' +
str(self.hand_number_info[c]).replace('None', '-')])
s = s + tabulate(tab, headers=('info known'))
return s
def decide_action(self, action, **kwargs):
pass
def move_card_in_hand(self, from_num, to_num):
# This function will only sort if it is valid, and will not thrown an
# error if invalid
n = len(self.hand)
if 0 <= from_num < n and 0 <= to_num < n:
card = self.hand.pop(from_num)
self.hand.insert(to_num, card)
return None
def play(self, card):
# print("\nPlaying a {} {}".format(card.colour, card.number))
self.hand.remove(card)
del self.hand_colour_info[card]
del self.hand_number_info[card]
return card
def discard(self, card):
print("\nDiscarding a {} {}".format(card.colour, card.number))
self.hand.remove(card)
del self.hand_colour_info[card]
del self.hand_number_info[card]
return card
def hint(self, to_player, info): # Not very elegant but it does the trick
return to_player, info
def receive_hint(self, info): # independent of card
if info in ['red', 'blue', 'green', 'yellow', 'white']:
for card in self.hand:
if card.colour == info:
self.hand_colour_info[card] = info
else:
for card in self.hand:
if card.number == info:
self.hand_number_info[card] = info
self.reorder_hand()
return None
def reorder_hand(self):
current_pos = 0
for i, card in enumerate(self.hand):
if self.hand_colour_info[card] or self.hand_number_info[card]:
self.move_card_in_hand(i, current_pos)
current_pos += 1
return None