Example #1
0
    def play_round(self) -> GameAction:
        """Play all five tricks of the game once trump is called."""
        while len(self.cur_round.tricks) < 5:
            if len(self.cur_round.tricks) == 0:
                start_index = next((x.index for x in self.cur_round.players_cards if x.order == 0), None)
                self.cur_round.tricks.append(Trick(start_index, []))

            trick = self.cur_round.tricks[-1]
            if len(trick.cards) == 4:
                start_index = vm.trick_winner(self.cur_round.tricks[-1], self.cur_round.trump)
                trick = Trick(start_index, [])
                self.cur_round.tricks.append(trick)

            for i in range(self.web_player_start, 4):
                player = self.players[(i + trick.start_index) % 4]

                if not player.ready_to_play():
                    player.play_card(PartialRound.from_round(self.cur_round, (i + trick.start_index) % 4))
                    self.web_player_start = i
                    return GameAction.PLAY_CARDS
                else:
                    self.web_player_start = 0
                trick.cards.append(player.play_card(PartialRound.from_round(self.cur_round, (i + trick.start_index) % 4)))

        return GameAction.SCORE_ROUND
 def create_partial_round(self, tricks: [Trick],
                          player_index: int) -> PartialRound:
     """Creates a partial round data object to be given to a player logic to calculate the next move"""
     player = next(
         (p for p in self.players_cards if p.index == player_index), None)
     return PartialRound(player, tricks, self.deck[0], self.trump_caller,
                         self.trump)
Example #3
0
 def test_valid_moves_new_game(self):
     cards = CardDeck().deal_cards()
     players = [PlayerCards(i, i, cards[i]) for i in range(4)]
     game = Round(players, cards[4], [], 0, Suite(0))
     p_round = PartialRound.from_round(game, 0)
     valid_cards = vm.valid_trick_moves(p_round)
     self.assertEqual(len(valid_cards), 5)
Example #4
0
    def discard_card(self):
        """Ask the dealer to discard a card so game play can begin"""
        dealer_hand = next((x for x in self.cur_round.players_cards if x.order == 3), None)
        dealer = self.players[dealer_hand.index]

        if not dealer.ready_to_play():
            dealer.dealer_discard_card(PartialRound.from_round(self.cur_round, dealer_hand.index))
            return GameAction.DISCARD_CARD

        discarded_card = dealer.dealer_discard_card(PartialRound.from_round(self.cur_round, dealer_hand.index))
        dealer_hand.dropped_card = discarded_card
        if discarded_card in dealer_hand.hand:
            dealer_hand.hand.remove(discarded_card)
            dealer_hand.hand.append(self.cur_round.flipped_card)
            self.cur_round.kitty[0] = discarded_card # To keep integrity of the data structure
        return GameAction.PLAY_CARDS
    def __execute_player_calls(self, player: PlayerInterface):
        # test ability to pick up trump
        json_str = '{"players": [{"index": 0, "order": 0, "hand": [1, 11, 5, 10, 25], "dropped_card": null}, ' \
                   '{"index": 1, "order": 1, "hand": [26, 16, 20, 13, 29], "dropped_card": null}, ' \
                   '{"index": 2, "order": 2, "hand": [9, 27, 24, 12, 3], "dropped_card": null}, ' \
                   '{"index": 3, "order": 3, "hand": [0, 2, 18, 19, 17], "dropped_card": null}], ' \
                   '"deck": [28, 4, 8, 21], "tricks": [], "trump_caller": null, "trump": null}'
        f_round = PartialRound.from_round(json_to_game(json_str), 3)
        move = player.dealer_pick_up_trump(f_round)
        self.assertIn(move, [False, True],
                      "Must return a boolean of whether to pick up card")

        # test ability for dealer to discard card
        json_str = '{"players": [{"index": 0, "order": 0, "hand": [1, 11, 5, 10, 25], "dropped_card": null}, ' \
                   '{"index": 1, "order": 1, "hand": [26, 16, 20, 13, 29], "dropped_card": null}, ' \
                   '{"index": 2, "order": 2, "hand": [9, 27, 24, 12, 3], "dropped_card": null}, ' \
                   '{"index": 3, "order": 3, "hand": [0, 2, 18, 19, 17], "dropped_card": null}], ' \
                   '"deck": [28, 4, 8, 21], "tricks": [], "trump_caller": 0, "trump": 0}'
        f_round = PartialRound.from_round(json_to_game(json_str), 0)
        move = player.dealer_discard_card(f_round)
        self.assertIn(move.value, [1, 11, 5, 10, 25, 28],
                      "Invalid dealer picking up card ")

        # Test ability to call trump
        json_str = '{"players": [{"index": 0, "order": 0, "hand": [1, 11, 5, 10, 25], "dropped_card": null}, ' \
                   '{"index": 1, "order": 1, "hand": [26, 16, 20, 13, 29], "dropped_card": null}, ' \
                   '{"index": 2, "order": 2, "hand": [9, 27, 24, 12, 3], "dropped_card": null}, ' \
                   '{"index": 3, "order": 3, "hand": [0, 2, 18, 19, 17], "dropped_card": null}], ' \
                   '"deck": [28, 4, 8, 21], "tricks": [], "trump_caller": null, "trump": null}'
        f_round = PartialRound.from_round(json_to_game(json_str), 0)
        move = player.call_trump(f_round)
        self.assertIn(
            move, [None, 0, 1, 2],
            "Invalid calling trump move")  # Can't call trump of card dropped

        # test ability to make a move
        json_str = '{"players": [{"index": 0, "order": 0, "hand": [1, 11, 5, 10, 25], "dropped_card": null}, ' \
                   '{"index": 1, "order": 1, "hand": [26, 16, 20, 13, 29], "dropped_card": null}, ' \
                   '{"index": 2, "order": 2, "hand": [9, 27, 24, 12, 3], "dropped_card": null}, ' \
                   '{"index": 3, "order": 3, "hand": [0, 2, 18, 19, 17], "dropped_card": null}], ' \
                   '"deck": [28, 4, 8, 21], "tricks": [{"start_index": 0, "cards":[1]}], ' \
                   '"trump_caller": 0, "trump": 0}'
        f_round = PartialRound.from_round(json_to_game(json_str), 1)
        move = player.play_card(f_round)
        self.assertIn(move.value, [26, 16, 20, 13, 29], "Incorrect move made")
Example #6
0
    def dealer_pick_up_card(self) -> GameAction:
        """
        Ask each player if they want the dealer to pick up the card. If so, record player index and notify dealer.
        If not, move to ask each player to call a trump suite.
        """
        for pi in range(self.web_player_start, 4):
            p = self.cur_round.players_cards[pi]
            player = self.players[p.index]

            if not player.ready_to_play():
                player.dealer_pick_up_trump(PartialRound(self.cur_round, p.index))
                self.web_player_start = pi
                return GameAction.DOES_DEALER_PICK_UP
            else:
                self.web_player_start = 0

            if player.dealer_pick_up_trump(PartialRound.from_round(self.cur_round, p.index)):
                self.cur_round.trump_caller = p.index
                self.cur_round.trump = self.cur_round.flipped_card.suite
                return GameAction.DISCARD_CARD
        return GameAction.CALL_TRUMP
Example #7
0
    def call_trump(self) -> GameAction:
        """
        If face card was not picked, ask each player if they want to call trump.  If so, return player index and
        called trump.  If not, the round is finished with no winner, the dealer moves, and a new set of cards is dealt.
        """
        for pi in range(self.web_player_start, 4):
            p = self.cur_round.players_cards[pi]
            player = self.players[p.index]

            if not player.ready_to_play():
                player.call_trump(PartialRound.from_round(self.cur_round, p.index))
                self.web_player_start = pi
                return GameAction.CALL_TRUMP
            else:
                self.web_player_start = 0

            trump = player.call_trump(PartialRound.from_round(self.cur_round, p.index))
            if trump is not None:
                self.cur_round.trump_caller = p.index
                self.cur_round.trump = trump
                return GameAction.PLAY_CARDS
        return GameAction.SCORE_ROUND