summaryrefslogtreecommitdiff
path: root/swiftstory/board.py
blob: 01082e4086091f2822c18328e03a5311afa8027f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
""" This module defines the Board class. """

import random
from typing import List, Optional, Tuple

from swiftstory.player import Player


class Board:
    ''' This class automatically handles the reshuffling of different deck/heap
    of cards '''

    def __init__(self) -> None:
        """ Initializer for the board. """
        self.white_pick: List[Tuple[int, str]] = []
        self.black_pick: List[Tuple[int, str]] = []
        self.white_recycled: List[Tuple[int, str]] = []
        self.black_recycled: List[Tuple[int, str]] = []

        self.current_black_card: Optional[Tuple[int, str]] = None

        self.played_cards: List[Tuple[Tuple[int, str], Player]] = []

    def reveal_next_black_card(self) -> None:
        """ Fetch the next black card and reveal it. The current card (if any)
        moves to the recycling heap. """
        if self.current_black_card is not None:
            self.black_recycled.append(self.current_black_card)
            self.current_black_card = None

        if not self.black_pick:
            # If we have no more cards in the deck, we need to reform one using
            # the cards that have been recycled.
            random.shuffle(self.black_recycled)

            self.black_pick, self.black_recycled = self.black_recycled, list()

        self.current_black_card = self.black_pick.pop()

    def pick_white_card(self) -> Tuple[int, str]:
        """ Fetch and return the next white card. """
        if not self.white_pick:
            # If we have no more cards in the deck, we need to reform one using
            # the cards that have been recycled.
            random.shuffle(self.white_recycled)

            self.white_pick, self.white_recycled = self.white_recycled, list()

        return self.white_pick.pop()

    def play_card(self, player: Player, card: Tuple[int, str]) -> None:
        """ Add a card to the played cards for this round. """
        self.played_cards.append((card, player))

    def shuffle_played_cards(self) -> None:
        """ Shuffle the heap of played cards. """
        random.shuffle(self.played_cards)

    def recycle_played_cards(self) -> None:
        """ Move the played cards to the recycling heap of white cards. """
        self.white_recycled += [i[0] for i in self.played_cards]

        self.played_cards = []