170 lines
4.5 KiB
Python
170 lines
4.5 KiB
Python
|
|
import random
|
|
from operator import attrgetter
|
|
|
|
def chunks(lst, n):
|
|
"""Yield successive n-sized chunks from lst."""
|
|
for i in range(0, len(lst), n):
|
|
yield lst[i:i + n]
|
|
|
|
|
|
class Card:
|
|
def __init__(self, color, value):
|
|
self.color = color
|
|
self.value = value
|
|
|
|
def __str__(self):
|
|
sc = self.color if self.color else ' '
|
|
sv = str(self.value)
|
|
return sc+' '+sv
|
|
|
|
def __repr__(self):
|
|
return str(self)
|
|
|
|
|
|
class Player:
|
|
def __init__(self, name, player_id):
|
|
self.name = name
|
|
self.id = player_id
|
|
self.cards = []
|
|
|
|
def set_cards(self, cards):
|
|
self.cards = sorted(cards, key=attrgetter('color', 'value'))
|
|
|
|
#def show_cards(self):
|
|
# print(self.cards)
|
|
|
|
def play_card(self, card_idx):
|
|
c = self.cards[card_idx]
|
|
self.cards.remove(c)
|
|
return c
|
|
|
|
|
|
|
|
class Table:
|
|
def __init__(self):
|
|
#self.played_cards = {0: None, 1: None, 2: None, 3: None, 4: None, 5: None}
|
|
self.played_cards = dict()
|
|
self.num_players = 0
|
|
self.num_cards_played = 0
|
|
self.trump_card = None
|
|
|
|
def init(self):
|
|
self.played_cards = dict()
|
|
self.num_players = 0
|
|
self.num_cards_played = 0
|
|
self.trump_card = None
|
|
|
|
def add_player(self, p: Player):
|
|
self.played_cards[p] = None
|
|
|
|
def clear_table(self):
|
|
for key,value in self.played_cards.items():
|
|
self.played_cards[key] = 0
|
|
self.num_cards_played = 0
|
|
|
|
def play_card(self, player: Player, card: Card):
|
|
self.played_cards[player] = card
|
|
self.num_cards_played += 1
|
|
|
|
|
|
class WizGame:
|
|
def __init__(self):
|
|
self.players = []
|
|
self.card_deck = []
|
|
self.trump_card = None
|
|
self.table = Table()
|
|
self.start_player = None
|
|
self.active_player = None
|
|
self.current_round = 1
|
|
|
|
def create_deck(self):
|
|
self.card_deck = []
|
|
for color in ["b", "r", "g", "y"]:
|
|
for val in range(1, 14):
|
|
self.card_deck.append(Card(color, val))
|
|
for _ in range(1, 5):
|
|
self.card_deck.append(Card('-', 'Z'))
|
|
for _ in range(1, 5):
|
|
self.card_deck.append(Card('-', 'N'))
|
|
#print(self.card_deck)
|
|
|
|
def get_trump_color(self):
|
|
card = self.trump_card
|
|
if card:
|
|
if card.color:
|
|
return card.color
|
|
elif card.value == 'Z':
|
|
return 'choose'
|
|
else:
|
|
return None
|
|
else:
|
|
return None
|
|
|
|
def deal_cards(self, cards_per_player):
|
|
random.shuffle(self.card_deck)
|
|
cs = list(chunks(self.card_deck, cards_per_player))
|
|
for idx, p in enumerate(self.players):
|
|
p.set_cards(cs[idx])
|
|
#p.show_cards()
|
|
if len(cs) > len(self.players):
|
|
cc = cs[len(self.players)]
|
|
c = cc[0]
|
|
self.trump_card = c
|
|
else:
|
|
self.trump_card = None
|
|
|
|
def add_player(self, name: str, player_id):
|
|
if len(self.players) < 6:
|
|
p = Player(name, player_id)
|
|
self.players.append(p)
|
|
self.table.add_player(p)
|
|
|
|
def get_active_player(self):
|
|
return self.players[self.active_player]
|
|
|
|
def is_round_finished(self):
|
|
if self.table.num_cards_played < self.table.num_players:
|
|
return False
|
|
else:
|
|
return True
|
|
|
|
def next_round(self):
|
|
max_rounds = len(self.card_deck)/len(self.players)
|
|
if self.current_round <= max_rounds:
|
|
self.current_round += 1
|
|
self.start_player = self.start_player+1 if self.start_player < len(self.players) else 0
|
|
self.active_player = self.start_player
|
|
self.table.clear_table()
|
|
self.deal_cards(self.current_round)
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
def init_game(self):
|
|
self.create_deck()
|
|
self.current_round = 1
|
|
self.table.clear_table()
|
|
self.active_player = 0
|
|
self.start_player = 0
|
|
self.deal_cards(1)
|
|
self.table.num_players = len(self.players)
|
|
|
|
def play_card(self, player, card):
|
|
p = self.players[player]
|
|
c = p.play_card(card)
|
|
self.table.play_card(p, c)
|
|
self.next_player()
|
|
|
|
def next_player(self):
|
|
self.active_player = (self.active_player + 1) if self.active_player < len(self.players) else 0
|
|
|
|
def is_players_turn(self, player):
|
|
if player == self.active_player:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
the_game = WizGame()
|
|
|