84 lines
2.1 KiB
Python
84 lines
2.1 KiB
Python
|
|
||
|
import random
|
||
|
|
||
|
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 = cards
|
||
|
|
||
|
def show_cards(self):
|
||
|
print(self.cards)
|
||
|
|
||
|
|
||
|
class WizGame:
|
||
|
def __init__(self):
|
||
|
self.players = []
|
||
|
self.card_deck = []
|
||
|
self.trump_color = 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(None, 'Z'))
|
||
|
for _ in range(1,5):
|
||
|
self.card_deck.append(Card(None, 'N'))
|
||
|
print("carddeck:")
|
||
|
print(self.card_deck)
|
||
|
|
||
|
def set_trump_color(self, card: Card):
|
||
|
if card.color:
|
||
|
self.trump_color = card.color
|
||
|
elif card.value == 'Z':
|
||
|
self.trump_color = 'choose'
|
||
|
else:
|
||
|
self.trump_color = 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]
|
||
|
print("trump card: {}".format(c))
|
||
|
self.set_trump_color(c)
|
||
|
else:
|
||
|
self.trump_color = None
|
||
|
print("trump color: {}".format(self.trump_color))
|
||
|
|
||
|
def add_player(self, name: str, player_id):
|
||
|
if len(self.players) < 6:
|
||
|
self.players.append((Player(name, player_id)))
|
||
|
|
||
|
|
||
|
the_game = WizGame()
|
||
|
|