-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
231 lines (165 loc) · 6.61 KB
/
game.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
import random
from typing import List
CARDS_PER_HAND = 3
DEFAULT_BOARD = 'ygobprbprygborpygobprgyobprygborpygborpN'
NEST_INDEX = len(DEFAULT_BOARD)
SUN = 'S'
YELLOW = 'y'
GREEN = 'g'
ORANGE = 'o'
BLUE = 'b'
PURPLE = 'p'
RED = 'r'
ALL_COLORS = 'ygobpr'
class Board:
def __init__(self):
'''Creates a game board.'''
self.spaces = DEFAULT_BOARD
self.owl_positions = [0, 1, 2, 3, 4, 5]
self.sun_position = 0
def __repr__(self) -> str:
'''Encodes the board state into a csv string.
Item 1: Space colors
Items 2-7: Owl positions, starting from zero
Item 8: position of the sun marker, 0-13'''
# Space colors
ret = self.spaces + ','
# Owl positions
for owl in self.owl_positions:
ret += str(owl) + ','
# Sun position
ret += str(self.sun_position)
return ret
def next_empty_space(self, starting_index: int, card_color: str) -> int:
'''From the specified starting index, return the next empty space of the specified color.
If none exist, the nest is returned, index NEST_INDEX.'''
for index, space_color in enumerate(self.spaces[starting_index + 1:], starting_index + 1):
if space_color == card_color and index not in self.owl_positions:
return index
else:
return NEST_INDEX
def sort_owls(self) -> None:
'''Sort the owls in order from closest to furthest from the nest.
index 0 is the owl furthest from the nest.
index 5 is the owl closest to the nest.'''
self.owl_positions.sort()
class Deck:
def __init__(self) -> None:
'''Create a deck of cards.'''
self.cards = 'yyyyyyggggggoooooobbbbbbpppppprrrrrrSSSSSSSSSSSSSS'
def shuffle(self) -> None:
'''Randomly shuffles the deck.'''
cards_list = list(self.cards)
random.shuffle(cards_list)
self.cards = ''.join(cards_list)
def draw(self, number_of_cards: int = 1) -> str:
'''Draw cards from the top of the deck, starting at position zero.
Removes the cards from the deck and returns them.'''
drawn_cards = self.cards[0: number_of_cards]
self.cards = self.cards[number_of_cards:]
return drawn_cards
def remaining(self) -> dict:
'''Returns a dictionary of the remaining card cound for each color.
i.e { 'y' : 6 }'''
remaining_cards = {}
for color in ALL_COLORS:
remaining_cards[color] = self.cards.count(color)
remaining_cards[SUN] = self.cards.count(SUN)
remaining_cards['total'] = len(self.cards)
return remaining_cards
def position_of_nth_card(self, card_color: str, n: int):
"""Returns the position within the deck of the nth card of the specified
color, starting from the zeroth index."""
count = 0
for k, color in enumerate(self.cards):
if color == card_color:
count += 1
if count == n:
return k
class Move:
def __init__(
self,
card_color: str,
owl_index: int = None
):
self.card_color = card_color
self.owl_index = owl_index
class Game:
WIN = 'Win'
LOSS = 'Loss'
IN_PROGRESS = 'In Progress'
def __init__(self, number_of_hands: int = 2) -> None:
'''Creates a game.'''
self.board = Board()
self.deck = Deck()
self.deck.shuffle()
self.number_of_hands = number_of_hands
self.hands = []
self.turn = 0
for _ in range(number_of_hands):
hand = self.deck.draw(number_of_cards=CARDS_PER_HAND)
self.hands.append(hand)
def __repr__(self):
'''Returns a csv string representation of the game.'''
ret_val = self.board.__repr__() + ','
ret_val += self.deck.cards + ','
ret_val += ','.join(self.hands) + ','
ret_val += str(self.turn)
return ret_val
def __str__(self):
'''Returns a string representation of the game state.'''
ret_val = ''
for position in range(len(self.board.spaces)):
if position in self.board.owl_positions:
ret_val += '*'
else:
ret_val += ' '
ret_val += '|'
for hand in self.hands:
ret_val += ' ' + hand
ret_val += f' | Sun: {self.board.sun_position}'
number_of_cards_in_hands = 0
for h in self.hands:
number_of_cards_in_hands += len(h)
ret_val += f' | Cards played: {50-len(self.deck.cards)-number_of_cards_in_hands}'
return ret_val
def move_owl(self, color: str, owl_index: int) -> None:
'''Moves the specified owl to the next empty space of the specified color.'''
owl_position = self.board.owl_positions[owl_index]
move_to_position = self.board.next_empty_space(owl_position, color)
self.board.owl_positions[owl_index] = move_to_position
self.board.sort_owls()
def move_sun(self):
'''Moves the sun forward one position.'''
self.board.sun_position += 1
def state(self) -> str:
'''Returns "WON", "LOST", or "IN PROGRESS".'''
if self.board.sun_position == 13:
return Game.LOSS
elif self.board.owl_positions.count(NEST_INDEX) == 6:
return Game.WIN
else:
return Game.IN_PROGRESS
def possible_moves(self) -> List[Move]:
'''Returns a list of possible moves.'''
moves = []
# If the hand contains a sun card, it must be played.
if SUN in self.hands[self.turn]:
moves.append(Move(SUN))
return moves
for card in self.hands[self.turn]:
for owl_index, owl_position in enumerate(self.board.owl_positions):
if owl_position != NEST_INDEX:
moves.append(Move(card, owl_index=owl_index))
return moves
def move(self, move: Move) -> None:
'''Makes a move.'''
if move.card_color == SUN:
self.move_sun()
else:
self.move_owl(move.card_color, move.owl_index)
self.hands[self.turn] = self.hands[self.turn].replace(
move.card_color, '', 1)
if self.state() == Game.IN_PROGRESS and self.deck.cards != '':
self.hands[self.turn] += self.deck.draw()
self.turn = (self.turn + 1) % self.number_of_hands