class CardGame: def __init__(self, name1="player-1", name2="player-2", num_cards=10): """מקבלת כמה קלפים לחלק לכל שחקן, מגדירה 2 שחקנים, יוצרת חפיסה ומתחילה משחק חדש""" if type(num_cards) == int: # נכנסת רק כאשר המספר הוא שלם ותקין if 1 <= num_cards: # בודקת שכמות הקלפים היא תיקנית ולא פחות מ1 if num_cards > 26: # אם כמות הקלפים לחלוקה גדול מ26 היא תחלק רק 26 קלפים ותעדכן את המשתמש print("only 26 cards dealt") num_cards = 26 self.num_cards = num_cards self.player_1 = Player(name1, self.num_cards) self.player_2 = Player(name2, self.num_cards) self.deck_cards = DeckOfCards() self.new_game() else: exit("ERROR!") # אם הכמות היא פחות מ1 תדפיס שגיאה else: exit("ERROR!") # אם המספר אינו תיקני def get_winner(self): """מחזירה את השם של השחקן עם הרשימה הקצרה ביותר או אם הם שווים אז none""" if len(self.player_1.player_deck) < len(self.player_2.player_deck): return self.player_1.name elif len(self.player_1.player_deck) == len(self.player_2.player_deck): return None else: return self.player_2.name def new_game(self): """מערבבת את החפיסה השלימה ומחלקת קלפים לשני השחקנים, במידה והפונקציה הופעלה במהלך המשחק היא תציג שגיאה""" if len(self.deck_cards.show()) == 52: self.deck_cards.shuffle() self.player_1.set_hand(self.deck_cards) self.player_2.set_hand(self.deck_cards) else: exit("Error!")
class CardGame: def __init__(self, name1, name2, num_cards=10): self.player1 = Player(name1) self.player2 = Player(name2) self.num_cards = num_cards self.deck = DeckOfCards() self.new_game() def new_game(self): """"shuffling the deck if the deck is full, giving to the players cards else giving an error """ if len(self.deck.list_cards) == 52: self.deck.shuffle() self.player1.set_hand(self.deck) self.player2.set_hand(self.deck) else: print("Error, deck is not full cant start a new game") def get_winner(self): """check and pronounce who is the winner return None if its draw""" if len(self.player1.player_hand) > len(self.player2.player_hand): return self.player2.name elif len(self.player1.player_hand) < len(self.player2.player_hand): return self.player1.name elif len(self.player1.player_hand) == len(self.player2.player_hand): return None
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 __init__(self, name1="player-1", name2="player-2", num_cards=10): """מקבלת כמה קלפים לחלק לכל שחקן, מגדירה 2 שחקנים, יוצרת חפיסה ומתחילה משחק חדש""" if type(num_cards) == int: # נכנסת רק כאשר המספר הוא שלם ותקין if 1 <= num_cards: # בודקת שכמות הקלפים היא תיקנית ולא פחות מ1 if num_cards > 26: # אם כמות הקלפים לחלוקה גדול מ26 היא תחלק רק 26 קלפים ותעדכן את המשתמש print("only 26 cards dealt") num_cards = 26 self.num_cards = num_cards self.player_1 = Player(name1, self.num_cards) self.player_2 = Player(name2, self.num_cards) self.deck_cards = DeckOfCards() self.new_game() else: exit("ERROR!") # אם הכמות היא פחות מ1 תדפיס שגיאה else: exit("ERROR!") # אם המספר אינו תיקני
def setUp(self): print("Set_up") self.c1 = Card(1, "Diamond") self.c2 = Card(2, "Diamond") self.c3 = Card(3, "Diamond") self.c4 = Card(4, "Diamond") self.p_1 = Player("p_1", 5000, 5) self.list = [self.c1, self.c2, self.c3, self.c4] self.dc = DeckOfCards()
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__())
def test_shuffle(self, mock_shuffle): """checking the list is shuffling the deck""" deck = DeckOfCards() deck.shuffle() mock_shuffle.assert_called_once_with(deck.list_cards)
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 setUp(self): self.tester = DeckOfCards()
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())
def test_shuffle(self): """בודקת האם החפיסה לפני הערבוב שווה לחפיסה חדשה, ובודקת האם החפיסה אחרי הערבוב לא שווה לחפיסה חדשה לפני ערבוב""" deck1 = DeckOfCards() self.assertEqual(deck1.cards, self.tester.show()) self.tester.shuffle() self.assertNotEqual(deck1.cards, self.tester.show())
def test_sum_cards(self): """בודקת האם בחפיסה חדשה שנוצרת יש 52 קלפים""" deck1 = DeckOfCards() self.assertEqual(len(deck1.cards), 52)
def test_set_hand_all_cards_set(self): """checks the game will played for 10 rounds""" deck = DeckOfCards() player = Player('Ohad') player.set_hand(deck) self.assertEqual(len(player.player_hand), player.cards_number)
def test_set_hand1(self, mock_deal_one): """checks the player really get cards from the deck""" deck = DeckOfCards() player = Player('Ohad') player.set_hand(deck) self.assertEqual(player.player_hand.count(Card(11, 2)), 10)
def setUp(self): print("Set_up") self.d_card = DeckOfCards() self.c1 = Card(1, "Diamond")
def test_set_hand_3(self): """בודקת אם נכנס לחפיסה משהו שהוא לא אובייקט Card""" deck_cards = DeckOfCards() deck_cards.cards.append(1) # מכניסים סתם מספר במקום אובייקט with self.assertRaises(SystemExit): self.player_1.set_hand(deck_cards)
def test_show(self): """checks the method are really shows the full deck""" deck = DeckOfCards() self.assertEqual(deck.show(), deck.list_cards)
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 __init__(self, name1, name2, num_cards=10): self.player1 = Player(name1) self.player2 = Player(name2) self.num_cards = num_cards self.deck = DeckOfCards() self.new_game()
def setUp(self): self.player_1 = Player("dima", 2) self.player_2 = Player("shahar", 27) self.deck_cards = DeckOfCards() self.card_1 = Card(1, 3) self.card_2 = Card(2, 1)
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())