cowiz20/wiz_game.py

118 lines
3.0 KiB
Python
Raw Normal View History

2020-04-05 16:00:48 +02:00
import random
2020-04-06 21:59:31 +02:00
from operator import attrgetter
2020-04-05 16:00:48 +02:00
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)
2020-04-06 22:42:31 +02:00
class Table:
def __init__(self):
self.played_cards = {0: None, 1: None, 2: None, 3: None, 4: None, 5: None}
self.num_players = 0
self.trump_card = None
def clear_table(self):
self.played_cards = {0: None, 1: None, 2: None, 3: None, 4: None, 5: None}
def play_card(self, player: int, card: Card):
self.played_cards[player] = card
2020-04-05 16:00:48 +02:00
class Player:
def __init__(self, name, player_id):
self.name = name
self.id = player_id
self.cards = []
2020-04-06 22:42:31 +02:00
#self.played_card = None
2020-04-05 16:00:48 +02:00
def set_cards(self, cards):
2020-04-06 21:59:31 +02:00
self.cards = sorted(cards, key=attrgetter('color', 'value'))
2020-04-05 16:00:48 +02:00
def show_cards(self):
print(self.cards)
2020-04-06 22:42:31 +02:00
def play_card(self, card_idx):
c = self.cards[card_idx]
self.cards.remove(c)
return c
#def play_card(self, card: Card):
# self.cards.remove(card)
# self.played_card = card
#def reset_played_card(self):
# self.played_card = None
2020-04-05 16:00:48 +02:00
class WizGame:
def __init__(self):
self.players = []
self.card_deck = []
2020-04-05 23:27:33 +02:00
self.trump_card = None
2020-04-06 22:42:31 +02:00
self.table = Table()
2020-04-05 16:00:48 +02:00
def create_deck(self):
for color in ["b", "r", "g", "y"]:
2020-04-05 23:27:33 +02:00
for val in range(1, 14):
2020-04-05 16:00:48 +02:00
self.card_deck.append(Card(color, val))
2020-04-05 23:27:33 +02:00
for _ in range(1, 5):
2020-04-06 21:59:31 +02:00
self.card_deck.append(Card('-', 'Z'))
2020-04-05 23:27:33 +02:00
for _ in range(1, 5):
2020-04-06 21:59:31 +02:00
self.card_deck.append(Card('-', 'N'))
2020-04-05 16:00:48 +02:00
print("carddeck:")
print(self.card_deck)
2020-04-05 23:27:33 +02:00
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
2020-04-05 16:00:48 +02:00
else:
2020-04-05 23:27:33 +02:00
return None
2020-04-05 16:00:48 +02:00
def deal_cards(self, cards_per_player):
random.shuffle(self.card_deck)
cs = list(chunks(self.card_deck, cards_per_player))
2020-04-05 23:27:33 +02:00
for idx, p in enumerate(self.players):
2020-04-05 16:00:48 +02:00
p.set_cards(cs[idx])
p.show_cards()
if len(cs) > len(self.players):
cc = cs[len(self.players)]
c = cc[0]
2020-04-05 23:27:33 +02:00
#self.set_trump_color(c)
self.trump_card = c
2020-04-05 16:00:48 +02:00
else:
2020-04-05 23:27:33 +02:00
#self.trump_color = None
self.trump_card = None
2020-04-05 16:00:48 +02:00
def add_player(self, name: str, player_id):
if len(self.players) < 6:
self.players.append((Player(name, player_id)))
2020-04-06 22:42:31 +02:00
def start_game(self):
self.table.num_players = len(self.players)
2020-04-05 16:00:48 +02:00
the_game = WizGame()