259 lines
7.2 KiB
Python
259 lines
7.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)
|
|
|
|
|
|
def get_higher_card(card1: Card, card2: Card, trump_color: str):
|
|
"""
|
|
compares two cards, assuming card1 was played first and card2 played second.
|
|
returns: True if card2 beats card1, otherwise False
|
|
"""
|
|
if card2.value == 'N':
|
|
# no way to win in 'N' comes in second
|
|
return False
|
|
elif card1.value == 'Z':
|
|
# no way to win if 'Z' was already played
|
|
return False
|
|
elif card1.value == 'N':
|
|
# any other than 'N' will beat us here, and the special case
|
|
# where card2 also is 'N' was already handled above
|
|
return True
|
|
elif card2.value == 'Z':
|
|
# we only lose if card1 is also 'Z', and that was handled above,
|
|
# so this is an easy case also
|
|
return True
|
|
else:
|
|
if card1.color == card2.color:
|
|
if card1.value > card2.value:
|
|
return False
|
|
else:
|
|
return True
|
|
elif card2.color == trump_color:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
|
|
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.players = list()
|
|
self.played_cards = list()
|
|
self.num_cards_played = 0
|
|
self.trump_card = None
|
|
|
|
def init(self):
|
|
self.players.clear()
|
|
self.played_cards.clear()
|
|
self.num_cards_played = 0
|
|
self.trump_card = None
|
|
|
|
def add_player(self, p: Player):
|
|
self.players.append(p)
|
|
|
|
def clear_table(self, start_player):
|
|
self.played_cards.clear()
|
|
p1 = self.players[start_player:]
|
|
p2 = self.players[:start_player]
|
|
for p in p1:
|
|
t = (p, None)
|
|
self.played_cards.append(t)
|
|
for p in p2:
|
|
t = (p, None)
|
|
self.played_cards.append(t)
|
|
self.num_cards_played = 0
|
|
|
|
def play_card(self, player: Player, card: Card):
|
|
t = (player, card)
|
|
self.played_cards[player.id] = t
|
|
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 = 0
|
|
self.active_player = 0
|
|
self.current_round = 1
|
|
self.num_tricks_played = 0
|
|
|
|
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 set_trump_color(self, trump_color):
|
|
if self.trump_card.value != 'Z':
|
|
return False
|
|
else:
|
|
if trump_color not in ["b", "r", "g", "y"]:
|
|
return False
|
|
else:
|
|
self.trump_card.color = trump_color
|
|
return True
|
|
|
|
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):
|
|
if len(self.players) < 6:
|
|
player_id = len(self.players)
|
|
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 get_trick_winner(self):
|
|
cards = self.table.played_cards
|
|
high_player, high_card = cards[0]
|
|
|
|
for player, card in cards[1:]:
|
|
if get_higher_card(high_card, card, self.trump_card.color):
|
|
high_card = card
|
|
high_player = player
|
|
return high_player, high_card
|
|
|
|
def is_trick_finished(self):
|
|
if self.table.num_cards_played < len(self.players):
|
|
return False
|
|
else:
|
|
return True
|
|
|
|
def is_hand_finished(self):
|
|
h = self.is_trick_finished()
|
|
if h and self.num_tricks_played == self.current_round:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
def next_hand(self):
|
|
max_hands = len(self.card_deck)/len(self.players)
|
|
if self.current_round <= max_hands:
|
|
self.current_round += 1
|
|
self.start_player += 1
|
|
if self.start_player >= len(self.players):
|
|
self.start_player = 0
|
|
self.active_player = self.start_player
|
|
self.table.clear_table(self.start_player)
|
|
self.deal_cards(self.current_round)
|
|
self.num_tricks_played = 0
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
def next_trick(self):
|
|
self.table.clear_table(self.start_player)
|
|
self.start_player = self.get_trick_winner().id
|
|
|
|
def init_game(self):
|
|
self.create_deck()
|
|
self.current_round = 1
|
|
self.active_player = 0
|
|
self.start_player = 0
|
|
self.table.clear_table(self.start_player)
|
|
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()
|
|
if self.is_trick_finished():
|
|
self.num_tricks_played += 1
|
|
|
|
def next_player(self):
|
|
if not self.is_hand_finished():
|
|
self.active_player += 1
|
|
if self.active_player >= len(self.players):
|
|
self.active_player = 0
|
|
else:
|
|
self.active_player = None
|
|
|
|
def get_prev_player(self):
|
|
if self.start_player == 0:
|
|
return len(self.players)-1
|
|
else:
|
|
return self.start_player-1
|
|
|
|
def is_players_turn(self, player):
|
|
if self.is_trick_finished():
|
|
return False;
|
|
if player == self.active_player:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
the_game = WizGame()
|
|
|