2017-12-30 12:15:51 -06:00
|
|
|
from table.trash import Trash
|
|
|
|
|
from table.pile import Pile
|
2018-01-10 23:30:39 -05:00
|
|
|
from card.named.province import Province
|
2017-12-24 13:00:51 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Table:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.__player = list()
|
|
|
|
|
self.__pile = list()
|
|
|
|
|
self.__trash = Trash()
|
|
|
|
|
self.__winner = None
|
|
|
|
|
self.__winning_score = 0
|
|
|
|
|
|
|
|
|
|
def add_player(self, p):
|
|
|
|
|
self.__player.append(p)
|
|
|
|
|
|
|
|
|
|
def get_player(self, n):
|
|
|
|
|
return self.__player[n]
|
|
|
|
|
|
2017-12-26 15:33:05 -06:00
|
|
|
def get_players(self):
|
|
|
|
|
return self.__player
|
|
|
|
|
|
2017-12-28 18:24:07 -06:00
|
|
|
def get_trash(self):
|
|
|
|
|
return self.__trash
|
|
|
|
|
|
2018-01-04 01:50:34 -06:00
|
|
|
def create_pile(self, card):
|
|
|
|
|
p = Pile(card)
|
2017-12-31 17:02:27 -06:00
|
|
|
p.add_card(card)
|
|
|
|
|
card.setup()
|
2017-12-24 13:00:51 -06:00
|
|
|
self.__pile.append(p)
|
|
|
|
|
|
|
|
|
|
def get_piles(self):
|
|
|
|
|
return self.__pile
|
|
|
|
|
|
|
|
|
|
def get_pile(self, n):
|
|
|
|
|
return self.__pile[n]
|
|
|
|
|
|
2017-12-26 15:33:05 -06:00
|
|
|
def get_pile_count(self):
|
|
|
|
|
return len(self.__pile)
|
|
|
|
|
|
2017-12-24 13:00:51 -06:00
|
|
|
def get_pile_index_of_card(self, card_name):
|
|
|
|
|
result = 0
|
2017-12-30 11:56:31 -06:00
|
|
|
|
2017-12-24 13:00:51 -06:00
|
|
|
for p in self.__pile:
|
|
|
|
|
if p.get_card_group().get_name() == card_name:
|
|
|
|
|
result = self.__pile.index(p)
|
|
|
|
|
return result
|
|
|
|
|
|
2018-01-21 03:41:06 -05:00
|
|
|
def pile_is_empty(self, card_name):
|
|
|
|
|
return self.__pile[self.get_pile_index_of_card(card_name)].is_empty()
|
|
|
|
|
|
2018-01-10 23:30:39 -05:00
|
|
|
def are_there_three_empty_piles(self):
|
|
|
|
|
count = 0
|
2017-12-24 13:00:51 -06:00
|
|
|
for p in self.__pile:
|
2018-01-10 23:30:39 -05:00
|
|
|
if p.get_remaining() == 0:
|
|
|
|
|
count += 1
|
|
|
|
|
return count > 2
|
|
|
|
|
|
|
|
|
|
def has_provinces_run_out(self):
|
|
|
|
|
for p in self.__pile:
|
|
|
|
|
if isinstance(p.get_card_group(), Province):
|
|
|
|
|
return p.get_remaining() == 0
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def should_game_end(self):
|
|
|
|
|
return self.are_there_three_empty_piles() or self. has_provinces_run_out()
|
2017-12-24 13:00:51 -06:00
|
|
|
|
|
|
|
|
def play(self):
|
2018-01-10 23:30:39 -05:00
|
|
|
player_turn = 0
|
|
|
|
|
should_continue = True
|
|
|
|
|
while should_continue:
|
|
|
|
|
# game ends after
|
2018-01-21 03:41:06 -05:00
|
|
|
should_continue = not self.should_game_end() or player_turn % len(self.__player) != len(self.__player) - 1
|
2017-12-24 13:00:51 -06:00
|
|
|
self.print()
|
2018-01-10 23:30:39 -05:00
|
|
|
self.__player[player_turn % len(self.__player)].take_turn()
|
|
|
|
|
player_turn += 1
|
2017-12-24 13:00:51 -06:00
|
|
|
else:
|
|
|
|
|
self.print()
|
2018-01-10 23:30:39 -05:00
|
|
|
print("\n\nGame had " + str(player_turn) + " turns in " + str(player_turn/len(self.__player)) + " rounds.")
|
2017-12-24 13:00:51 -06:00
|
|
|
for p in self.__player:
|
2018-01-04 01:50:34 -06:00
|
|
|
print("" + str(p) + " scored " + str(p.get_score()) + " points.")
|
2017-12-24 13:00:51 -06:00
|
|
|
if p.get_score() > self.__winning_score:
|
2018-01-04 01:50:34 -06:00
|
|
|
self.__winning_score = p.get_score()
|
2017-12-24 13:00:51 -06:00
|
|
|
self.__winner = p
|
2018-01-05 04:23:27 -06:00
|
|
|
print("\n" + str(self.__winner) + " won with " + str(self.__winning_score) + " points.\n\n")
|
2017-12-24 13:00:51 -06:00
|
|
|
|
|
|
|
|
def print(self):
|
2017-12-26 15:33:05 -06:00
|
|
|
print("\nPiles: ")
|
|
|
|
|
index = 0
|
2017-12-24 13:00:51 -06:00
|
|
|
for s in self.__pile:
|
2017-12-26 15:33:05 -06:00
|
|
|
print(str(index) + ": " + s.get_card_group().identify() + ": " + str(s.get_remaining()))
|
|
|
|
|
index += 1
|
2017-12-30 11:56:31 -06:00
|
|
|
|
|
|
|
|
print("\nTrash: ")
|
|
|
|
|
index = 0
|
|
|
|
|
for s in self.__trash.get_supply():
|
|
|
|
|
print(str(index) + ": " + s.identify())
|
|
|
|
|
index += 1
|
2017-12-31 17:02:27 -06:00
|
|
|
|
2018-01-06 08:39:06 -06:00
|
|
|
print("")
|
|
|
|
|
|
2017-12-31 17:02:27 -06:00
|
|
|
def __str__(self):
|
|
|
|
|
return "A table with " + str(len(self.__pile)) + " card piles and " + str(len(self.__player)) + " players."
|