class TestDeckOfCard(TestCase): def setUp(self): self.tester = DeckOfCards() def test_sum_cards(self): """בודקת האם בחפיסה חדשה שנוצרת יש 52 קלפים""" deck1 = DeckOfCards() self.assertEqual(len(deck1.cards), 52) def test_shuffle(self): """בודקת האם החפיסה לפני הערבוב שווה לחפיסה חדשה, ובודקת האם החפיסה אחרי הערבוב לא שווה לחפיסה חדשה לפני ערבוב""" deck1 = DeckOfCards() self.assertEqual(deck1.cards, self.tester.show()) self.tester.shuffle() self.assertNotEqual(deck1.cards, self.tester.show()) @patch("games_cards.DeckOfCards.DeckOfCards.random_for_deal_one", return_value=1) def test_deal_one1(self, mock_rand): """בודקת אם הפונקציה מחזירה לי בהגרלה את אינדקס 1 האם זה באמת אותו קלף""" c = self.tester.cards[1] b = self.tester.deal_one() print(c, b) self.assertEqual(b, c) def test_deal_one2(self): """בודקת לי האם הקלף שהוצאתי כבר לא נמצא בחפיסה""" a = self.tester.deal_one() self.assertNotIn(a, self.tester.show()) def test_deal_one3(self): """בודק לי האם החפיסה ירדה בכמות באחד אחרי שהוצאתי קלף""" len1 = len(self.tester.show()) self.tester.deal_one() len2 = len(self.tester.show()) self.assertEqual(len1 - 1, len2) def test_show(self): """בודקת האם הקלף הספיציפי נמצא בתוך חפיסת הקלפים""" self.card1 = Card(1, 1) self.assertIn(self.card1, self.tester.show())
class TestDeckOfCards(TestCase): def setUp(self): print("Set_up") self.d_card = DeckOfCards() self.c1 = Card(1, "Diamond") def tearDown(self): print("Tear_down") def test__init___1(self): # check the len of the deck cards self.assertEqual(52, (len(self.d_card.pack))) def test_shuffle_dec(self): # check the shuffle of the deck cards after "shuffle_dec" # var_1 = self.d_card.pack[1] # self.d_card.shuffle_dec() # var_2 = self.d_card.pack[1] # self.assertNotEqual(var_1, var_2) temp_list = self.d_card.pack.copy() self.d_card.shuffle_dec() self.assertNotEqual(self.d_card.pack, temp_list) def test_deal_one(self): # check the deck cards after "deal_one" and check the number of card after that self.assertNotIn(self.d_card.deal_one(), self.d_card.pack) self.assertEqual(51, (len(self.d_card.pack))) def test_new_game_1(self): # check the creation of the pack cards after "new_game" with the number of card in the pack self.d_card.new_game() self.assertEqual(52, len(self.d_card.pack)) def test_new_game_2(self): # check the shuffle of the pack cards after "new_game" temp_list = self.d_card.pack.copy() self.d_card.new_game() self.assertNotEqual(temp_list, self.d_card.pack) def test_print(self): # check the print of the pack show_pack = "pack- [ 1 Diamond, 2 Diamond, 3 Diamond, 4 Diamond, 5 Diamond, 6 Diamond, 7 Diamond, 8 Diamond, 9 Diamond, 10 Diamond, 11 Diamond, 12 Diamond, 13 Diamond, 1 Spade , 2 Spade , 3 Spade , 4 Spade , 5 Spade , 6 Spade , 7 Spade , 8 Spade , 9 Spade , 10 Spade , 11 Spade , 12 Spade , 13 Spade , 1 Heart , 2 Heart , 3 Heart , 4 Heart , 5 Heart , 6 Heart , 7 Heart , 8 Heart , 9 Heart , 10 Heart , 11 Heart , 12 Heart , 13 Heart , 1 Club , 2 Club , 3 Club , 4 Club , 5 Club , 6 Club , 7 Club , 8 Club , 9 Club , 10 Club , 11 Club , 12 Club , 13 Club ]" self.assertEqual(show_pack, self.d_card.__repr__())
class CardGame: def __init__(self, number_of_cards=5): if number_of_cards > 13: raise ValueError( "the number of cards need to be lost of 13 and big of 5") if number_of_cards < 5: raise ValueError( "the number of cards need to be lost of 13 and big of 5") self.number_of_cards = number_of_cards self.deckOfCards = DeckOfCards() self.players = [] def deal_cards(self): # Distribution cards to players if self.number_of_cards > 0: for player in self.players: for i in range(0, self.number_of_cards): player.add_card(self.deckOfCards.deal_one()) else: print("error - Need to deal more cards than the number of rounds") def create_players(self, money): # Create 4 players for i in range(0, 4): name = f"p_{i}" self.players.append(Player(name, money, self.number_of_cards)) def new_game_n(self): # Create a deck of cards, hadar money, create players and deal cards to the players self.deckOfCards.new_game() money = randrange(5000, 10001) self.create_players(money) self.deal_cards() def game_move(self, move_number): # Performs a game round (draws, compares, adds and returns a winner) self.reduce_players_money(move_number) move_cards = self.get_move_cards() # print(f" R O U N D - - - {i} \n p_1- {move_cards[0]} p_2- {move_cards[1]} p_3- {move_cards[2]} p_4- {move_cards[3]}") self.print_players_cards(move_cards, move_number) winning_card = self.compare_cards(move_cards) winning_player = self.players[move_cards.index(winning_card)] winning_player.add_amount(move_number * 100 * 4) print(f"The wining in this round is {winning_player.name}") print(f" -- E N D R O U N D -- \n\n\n") def reduce_players_money(self, i): # Attracting money from all players for player in self.players: player.reduce_amount(i * 100) def get_move_cards(self): # Gets a list of cards for a game round listt = [] for i in range(0, 4): varr = self.players[i].get_card() # varr = self.players[i].get_card() listt.append(varr) return listt def compare_cards(self, cards_to_compare): # Compare cards and return a list from the big to the small temp_list = cards_to_compare.copy() temp_list.sort(reverse=True) return temp_list[0] # m def win(self): # return the players with most money # m win = 0 win_player = [] for player in self.players: if player.money >= win: win = player.money win_player.append(player.name) return win_player, win # m def __repr__(self): # Print card game return f"players - {self.players}\n cards - {self.deckOfCards}\n number_of_cards - {self.number_of_cards}" def print_players_cards(self, move_cards, move_number): # Print players, money and card of the round i = 0 print(f"R O U N D - - - {move_number} \n") for player in self.players: print(player.name, "-", move_cards[i], player.money) i = i + 1
def test_deal_one(self): """copy the deck of cards to 'a' and remove if the card we removed appears on the copied list""" deck = DeckOfCards() a = deck.list_cards.copy() self.assertIn(deck.deal_one(), a)
def set_hand(self, DeckOfCards): # Pulling a cards from a deck of cards for i in range(0, self.number_of_cards): self.cards.append(DeckOfCards.deal_one())