cowiz20/wiz_game.py

89 lines
2.2 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)
class WizGame:
def __init__(self):
self.players = []
self.card_deck = []
self.trump_card = None
def create_deck(self):
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("carddeck:")
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.set_trump_color(c)
self.trump_card = c
else:
#self.trump_color = None
self.trump_card = None
def add_player(self, name: str, player_id):
if len(self.players) < 6:
self.players.append((Player(name, player_id)))
the_game = WizGame()