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)
|
|
|
|
|
|
|
|
|
|
|
|
class Player:
|
|
|
|
def __init__(self, name, player_id):
|
|
|
|
self.name = name
|
|
|
|
self.id = player_id
|
|
|
|
self.cards = []
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-04-12 15:27:05 +02:00
|
|
|
#def show_cards(self):
|
|
|
|
# print(self.cards)
|
2020-04-05 16:00:48 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-04-12 15:27:05 +02:00
|
|
|
|
|
|
|
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
|
2020-04-06 22:42:31 +02:00
|
|
|
|
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-12 15:27:05 +02:00
|
|
|
self.start_player = None
|
|
|
|
self.active_player = None
|
|
|
|
self.current_round = 1
|
2020-04-05 16:00:48 +02:00
|
|
|
|
|
|
|
def create_deck(self):
|
2020-04-12 15:27:05 +02:00
|
|
|
self.card_deck = []
|
2020-04-05 16:00:48 +02:00
|
|
|
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-12 15:27:05 +02:00
|
|
|
#print(self.card_deck)
|
2020-04-05 16:00:48 +02:00
|
|
|
|
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])
|
2020-04-12 15:27:05 +02:00
|
|
|
#p.show_cards()
|
2020-04-05 16:00:48 +02:00
|
|
|
if len(cs) > len(self.players):
|
|
|
|
cc = cs[len(self.players)]
|
|
|
|
c = cc[0]
|
2020-04-05 23:27:33 +02:00
|
|
|
self.trump_card = c
|
2020-04-05 16:00:48 +02:00
|
|
|
else:
|
2020-04-05 23:27:33 +02:00
|
|
|
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:
|
2020-04-12 15:27:05 +02:00
|
|
|
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]
|
2020-04-05 16:00:48 +02:00
|
|
|
|
2020-04-12 15:27:05 +02:00
|
|
|
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)
|
2020-04-06 22:42:31 +02:00
|
|
|
self.table.num_players = len(self.players)
|
|
|
|
|
2020-04-12 15:27:05 +02:00
|
|
|
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
|
2020-04-05 16:00:48 +02:00
|
|
|
|
|
|
|
the_game = WizGame()
|
|
|
|
|