Example #1
0
    def __init__(self):
        self.players = list()
        self.last_card = None
        self.round = 1
        self.temp_messages = []

        while not self.last_card or self.last_card.special:
            self.deck = Deck()
            self.last_card = self.deck.get_card()
Example #2
0
def test_jump_actions():
    player1 = Player("player1")
    player2 = Player("player2")
    players = [player1, player2]

    card1 = NumberedCard(color="BLUE", number="1")
    card2 = NumberedCard(color="RED", number="1")
    card3 = JumpCard(color="BLUE")

    assert card3.actions(card1, 0, players, Deck()) == 1
    assert card3.actions(card1, 1, players, Deck()) == 0

    with pytest.raises(UnoRuleException):
        card3.actions(card2, 1, players, Deck())
Example #3
0
def test_plustwo_actions():
    player1 = Player("player1")
    player2 = Player("player2")
    players = [player1, player2]

    card1 = PlusTwoCard(color="BLUE")
    card2 = PlusTwoCard(color="BLUE")
    card3 = PlusTwoCard(color="RED")

    assert card2.actions(card1, 0, players, Deck()) == 0
    assert len(player2.cards) == 2

    with pytest.raises(UnoRuleException):
        card3.actions(card1, 1, players, Deck())
Example #4
0
def test_joker_actions():
    player1 = Player("player1")
    player2 = Player("player2")
    players = [player1, player2]

    card1 = JokerCard()
    card2 = JokerCard()
    card3 = NumberedCard(color="BLUE", number="1")

    card2.setColor("BLUE")
    assert card2.actions(card1, 0, players, Deck()) == 0
    assert card3.actions(card2, 0, players, Deck()) == 0

    with pytest.raises(UnoRuleException):
        card3.actions(card1, 1, players, Deck())
Example #5
0
def test_deck_generation():
    deck = Deck()

    # deck should have 108 cards
    assert (len(deck.cards) == 108)

    instancesNCounters = {
        NumberedCard: 76,
        PlusTwoCard: 8,
        InvertedCard: 8,
        JumpCard: 8,
        JokerCard: 4,
        JokerPlusFourCard: 4
    }

    for card in deck.cards:

        for instance in instancesNCounters.keys():
            if isinstance(card, instance) is True:
                instancesNCounters[instance] -= 1
                break

    # verify if deck is completed checked
    for instance in instancesNCounters.keys():
        assert instancesNCounters[
            instance] == 0, f"{instance} has incorrect number"
Example #6
0
def test_numbered_actions():
    player1 = Player("player1")
    player2 = Player("player2")
    players = [player1, player2]

    card1 = NumberedCard(color="BLUE", number="1")
    card2 = NumberedCard(color="BLUE", number="2")
    card3 = NumberedCard(color="RED", number="2")

    # positive cases
    assert card2.actions(card1, 0, players, Deck()) == 0
    assert card3.actions(card2, 1, players, Deck()) == 1

    # negative case
    with pytest.raises(UnoRuleException):
        card3.actions(card1, 1, players, Deck())
Example #7
0
def test_inverted_actions():
    player1 = Player("player1")
    player2 = Player("player2")
    players = [player1, player2]

    card1 = InvertedCard(color="BLUE")
    card2 = InvertedCard(color="BLUE")
    card3 = InvertedCard(color="RED")
    card4 = NumberedCard(color="BLUE", number="2")

    assert card2.actions(card1, 0, players, Deck()) == 1
    assert players == [player2, player1]
    assert card3.actions(card1, 1, players, Deck()) == 0
    assert players == [player1, player2]

    with pytest.raises(UnoRuleException):
        card3.actions(card4, 1, players, Deck())
class TestDeck(unittest.TestCase):
    def setUp(self):
        self.deck = Deck()

    def tearDown(self):
        self.deck = Deck()

    def test_empty(self):
        with self.assertRaises(EmptyDeckError):
            self.deck.cards = list()
            self.deck.get_card()

    def test_shuffle(self):
        self.prev = self.deck.cards
        self.deck.shuffle_cards()
        self.assertListEqual(self.prev, self.deck.cards)

    def test_beaten(self):
        self.deck.beaten(self.deck.cards[0])
        self.assertNotEqual(self.deck.discard_pile, list())

    def test_beaten_empty(self):
        self.assertListEqual(self.deck.discard_pile, [])

    def test_get_card(self):
        self.testCard = self.deck.get_card()
        self.assertNotIn(self.testCard, self.deck.cards)
Example #9
0
def test_jokerplusfour_actions():
    player1 = Player("player1")
    player2 = Player("player2")
    players = [player1, player2]

    card1 = JokerPlusFourCard()
    card2 = JokerPlusFourCard()
    card3 = NumberedCard(color="RED", number="1")
    card4 = NumberedCard(color="BLUE", number="1")

    card1.setColor(color="BLUE")
    assert card2.actions(card1, 0, players, Deck()) == 0
    assert len(player2.cards) == 4

    with pytest.raises(UnoRuleException):
        card3.actions(card1, 1, players, Deck())

    assert card4.actions(card1, 0, players, Deck()) == 0
 def setUp(self):
     self.deck = Deck()
 def tearDown(self):
     self.deck = Deck()
Example #12
0
class Game:
    current_player = None
    choosing_color = False
    started = False
    draw_counter = 0

    def __init__(self):
        self.players = list()
        self.last_card = None
        self.round = 1
        self.temp_messages = []

        while not self.last_card or self.last_card.special:
            self.deck = Deck()
            self.last_card = self.deck.get_card()

    def add_player(self, player: Player):
        if self.players.__len__() == 0:
            self.players.append(player)
            self.current_player = player
        else:
            self.players[0].prev = player
            self.players[self.players.__len__() - 1].next = player
            player.prev = self.players[self.players.__len__() - 1]
            player.next = self.players[0]
            self.players.append(player)

    def next_turn(self):
        if self.draw_counter != 0:
            self.current_player.next.draw()
        self.current_player = self.current_player.next
        if not self.current_player.is_human:
            self.current_player.play()
        else:
            from telegram_commands import uno_play_msg
            uno_play_msg(chat_id=self.current_player.chat_id,
                         game=self.current_player.game)
        self.round += 1

    def skip_next(self):
        if self.draw_counter != 0:
            self.current_player.next.draw()
        self.current_player = self.current_player.next.next
        if not self.current_player.is_human:
            self.current_player.play()
        else:
            from telegram_commands import uno_play_msg
            uno_play_msg(chat_id=self.current_player.chat_id,
                         game=self.current_player.game)
        self.round += 1

    def play_card(self, card):
        self.deck.beaten(self.last_card)
        self.last_card = card
        self.get_board(
            f"{self.current_player.name} played {card.color} {card.value} {card.special}!"
        )

        if card.value == DRAW_TWO:
            self.draw_counter += 2
            self.skip_next()
            return None
        elif card.value == SKIP:
            self.skip_next()
            return None
        elif card.special == CHOOSE_COLOR:  # ADD INLINE COLOR KEYBOARD
            print("CHOOSING COLOR!")
            self.temp_messages.append(
                bot.send_message(chat_id=self.current_player.chat_id,
                                 text="CHOOSING COLOR!"))
            if not self.current_player.is_human:
                self.choose_color(random.choice(COLORS))
            else:
                from inline_handle import InlineKeyboardFactory
                self.temp_messages.append(
                    bot.send_message(
                        chat_id=self.current_player.chat_id,
                        text="Choose color:",
                        reply_markup=InlineKeyboardFactory.
                        get_inline_uno_choose_color()))  # noqa E501
            return None
        elif card.special == DRAW_FOUR:
            self.draw_counter += 4
            if not self.current_player.is_human:
                self.choose_color(random.choice(COLORS))
            else:
                from inline_handle import InlineKeyboardFactory
                bot.send_message(chat_id=self.current_player.chat_id,
                                 text="Choose color:",
                                 reply_markup=InlineKeyboardFactory.
                                 get_inline_uno_choose_color())
            print(f"Chosen color {self.last_card.color}")
            return None
        elif card.special not in SPECIAL_CARDS:
            self.next_turn()
            return None
        self.next_turn()
        return None

    def choose_color(self, color: COLORS):
        self.last_card.color = color
        print(f"Chosen color {self.last_card.color}")
        self.temp_messages.append(
            bot.send_message(chat_id=self.current_player.chat_id,
                             text=f"Chosen color {self.last_card.color}"))
        self.next_turn()

    def get_board(self, message: str = None):
        if self.round == 1:
            try:
                os.remove("uno/images/playing_bg.png")
                os.remove("uno/images/playing_bg_tmp.jpg")
            except FileNotFoundError:
                pass
        try:
            raw_bg = Image.open("uno/images/playing_bg.png")
        except FileNotFoundError:
            raw_bg = Image.open("uno/images/background.png")
        bg = Image.new('RGBA', (raw_bg.width, raw_bg.height))
        bg.paste(raw_bg, (0, 0))
        angle = random.randint(-15, 15)
        bg.paste(self.last_card.get_img().rotate(angle, expand=1.0),
                 (raw_bg.width // 2 - 200 - random.randint(-50, 50),
                  raw_bg.height // 2 - 200 - random.randint(-30, 30)),
                 self.last_card.get_img().rotate(angle, expand=1.0))
        bg.save("uno/images/playing_bg.png")
        temp = Image.open("uno/images/playing_bg.png")
        tmp_c = temp.convert('RGB')
        tmp_c.save("uno/images/playing_bg_tmp.jpg")
        self.temp_messages.append(
            bot.send_photo(chat_id=self.current_player.chat_id,
                           photo=open("uno/images/playing_bg_tmp.jpg",
                                      mode='rb'),
                           caption=message))

    @staticmethod
    def choose_color_static(current_game, color: COLORS) -> None:
        current_game.last_card.color = color
        current_game.temp_messages.append(
            bot.send_message(
                chat_id=current_game.current_player.chat_id,
                text=f"Chosen color {current_game.last_card.color}"))
        current_game.next_turn()