Created
July 20, 2020 17:54
-
-
Save westscz/b093db5264988267fea8f3fb6e253c83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import random | |
class Deck: | |
def __init__(self, cards): | |
self.cards = cards | |
def get_deck(self): | |
""" | |
Returns the deck 'updated', after shuffling it. | |
""" | |
self.shuffle_deck() | |
return self.cards | |
def draw_card(self): | |
""" | |
Returns the top card of the deck to the player's cards. | |
P.S.: This function will never be called if the deck is empty. | |
That will be more understandable when the class Game be implemented. | |
That class will check if it has cards on the deck, and if doesn't, it | |
will call the create_new_deck_with_played_cards method. | |
""" | |
return self.cards.pop(-1) | |
def shuffle_deck(self): | |
""" | |
Takes the self.cards list and shuffle using randint function. | |
""" | |
amount_of_cards_available = len(self.cards) | |
indexes_shuffled = [] | |
deck_shuffled = [] | |
for i in range(amount_of_cards_available): | |
num = random.randint(0, amount_of_cards_available) | |
if num not in indexes_shuffled: | |
indexes_shuffled.append(num) | |
for i in range(amount_of_cards_available): | |
deck_shuffled.append(self.cards[indexes_shuffled[i]]) | |
self.cards = deck_shuffled | |
def is_empty(self): | |
""" | |
Returns True if the deck is empty, otherwise returns False | |
""" | |
if self.cards == []: | |
return True | |
return False | |
def create_new_deck_with_played_cards(self, played_cards): | |
""" | |
It takes the played cards and create a new deck, then it returns | |
the self.get_deck() method that shuffles the deck and returns it | |
""" | |
print("Creating new deck with the cards played...\n") | |
self.cards = played_cards | |
return self.get_deck() | |
import unittest | |
class TestDeck(unittest.TestCase): | |
def test_empty_deck(self): | |
deck = Deck([]) | |
self.assertListEqual(deck.cards, []) | |
def test_deck_with_cards(self): | |
deck = Deck([1,2,3,4,5]) | |
self.assertListEqual(deck.cards, [1,2,3,4,5]) | |
def test_shuffled_deck_other_than_base_deck(self): | |
deck = Deck([1,2,3,4,5]) | |
cards_before = deck.cards.copy() | |
deck.shuffle_deck() | |
self.assertNotEqual(deck.cards, cards_before) | |
def test_draw_card(self): | |
deck = Deck([1,2,3,4,5]) | |
card = deck.draw_card() | |
self.assertEqual(card, 5) | |
self.assertEqual(len(deck.cards), 4) | |
def test_draw_card_when_no_cards_available(self): | |
deck = Deck([]) | |
card = deck.draw_card() | |
self.assertEqual | |
if __name__ == "__main__": | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment