diff --git a/server.py b/server.py index 768259c..c69f17b 100755 --- a/server.py +++ b/server.py @@ -11,19 +11,7 @@ def main(): print("main started") random.seed() - - #wiz_game.the_game.create_deck() - #wiz_game.the_game.add_player("p1", 1) - #wiz_game.the_game.add_player("p2", 2) - #wiz_game.the_game.add_player("p3", 3) - #wiz_game.the_game.add_player("p4", 4) - #wiz_game.the_game.deal_cards(12) - wiz_game.the_game.add_player("p1") - wiz_game.the_game.add_player("p2") - wiz_game.the_game.add_player("p3") - wiz_game.the_game.add_player("p4") - wiz_game.the_game.init_game() - + wiz_game.the_game.start_game() api.run(port=19203, address="127.0.0.1") exit(0) diff --git a/templates/home/player.html b/templates/home/player.html index d2a03ef..18ecbf0 100644 --- a/templates/home/player.html +++ b/templates/home/player.html @@ -11,13 +11,7 @@ Table
{% for player,card in played_cards %} || - {% if player.id == active_player %} - - {% endif %} {{player.name}}: {{card}} - {% if player.id == active_player %} - - {% endif %} {% endfor %} ||
@@ -36,7 +30,7 @@ {% endif %} {% endfor %} - {% if choose_trump_color %} + {% if choose_trump_color and (player == choose_trump_player) %} Choose Trump Color: Red> Green> diff --git a/views/player.py b/views/player.py index ba9da7b..f223b49 100644 --- a/views/player.py +++ b/views/player.py @@ -9,17 +9,23 @@ def show(req, resp, player: str): name = the_game.players[p].name tcard = the_game.trump_card tcolor = the_game.get_trump_color() - table = the_game.table - isActive = the_game.is_players_turn(p) - active_player = the_game.active_player + #isActive = the_game.is_players_turn(p) + try: + isActive = the_game.players_ordered[the_game.active_player].id == p + print("show p: {} | {}".format(the_game.players_ordered[the_game.active_player].id, p)) + except IndexError: + isActive = False + print("show p: {} | {}".format('None', p)) + + #active_player = the_game.active_player prev_player = the_game.get_prev_player() - if tcard.value == 'Z' and p == prev_player and tcolor == '-': + if tcard.value == 'Z' and tcolor == '-': choose_trump_color = True else: choose_trump_color = False - resp.content = api.template('home/player.html', playerActive=isActive, active_player=active_player, player=p, + resp.content = api.template('home/player.html', player=p, playerActive=isActive, choose_trump_player=prev_player, cards=the_game.players[p].cards, choose_trump_color=choose_trump_color, - trump_card=tcard, trump_color=tcolor, played_cards=table.played_cards) + trump_card=tcard, trump_color=tcolor, played_cards=the_game.played_cards) @api.route("/player/{player}/play/{card}") diff --git a/wiz_game.py b/wiz_game.py index d5567ba..81ebae9 100644 --- a/wiz_game.py +++ b/wiz_game.py @@ -107,6 +107,146 @@ class Table: class WizGame: + def __init__(self, player_names: list): + self.players = [Player(pn, idx) for idx, pn in enumerate(player_names)] + if len(self.players) > 6: + raise Exception # TODO: figure out more specific type + self.card_deck = [] + self.trump_card = None + self.start_player = 0 + self.active_player = 0 + self.current_round = 1 + self.num_tricks_played = 0 + self.played_cards = list() + self.players_ordered = self.players + + def start_game(self): + self.create_deck() + self.current_round = 1 + self.active_player = 0 + self.start_player = 0 + self.deal_cards(1) + self.played_cards = list() + self.players_ordered = self.players + + 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')) + + 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]) + if len(cs) > len(self.players): + cc = cs[len(self.players)] + c = cc[0] + self.trump_card = c + else: + self.trump_card = None + + def get_trick_winner(self): + cards = self.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 len(self.played_cards) < 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 = 0 + self.players_ordered = self.players[self.start_player:] + self.players[:self.start_player] + self.played_cards.clear() + self.deal_cards(self.current_round) + self.num_tricks_played = 0 + return True + else: + return False + + def next_trick(self): + win = self.get_trick_winner() + winner = win[0] + #self.start_player = winner.id + starter = winner.id + self.players_ordered = self.players[starter:] + self.players[:starter] + self.active_player = 0 + self.played_cards.clear() + + def play_card(self, player, card): + p = self.players[player] + c = p.play_card(card) + t = (p, c) + self.played_cards.append(t) + #self.next_player() + self.active_player += 1 + if self.is_trick_finished(): + self.num_tricks_played += 1 + + 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 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 + + +class WizGame_legacy: def __init__(self): self.players = [] self.card_deck = [] @@ -254,5 +394,7 @@ class WizGame: else: return False -the_game = WizGame() + +p = ["p1", "p2", "p3", "p4"] +the_game = WizGame(p)