class TestDeckOfCards(TestCase): def setUp(self): self.deck1 = DeckOfCards() self.copy_deck = self.deck1.deck.copy() print('The test is starting') def tearDown(self): print("The test completed") def test_init(self): list1 = [] for i in self.deck1.deck: if type(i) == Card: if i not in list1: list1.append(i) self.assertEqual(len(list1), 52) def test_shuffle_deck(self): self.assertNotEqual(self.deck1.shuffle_deck(), self.copy_deck) self.assertCountEqual(self.deck1.deck, self.copy_deck) def test_deal_one(self): self.assertNotIn(self.deck1.deal_one(), self.deck1.deck) self.assertEqual(len(self.deck1.deck) + 1, len(self.copy_deck)) deck2 = DeckOfCards() deck2.deck = [Card(2, u'\u2666')] deck2.deal_one() self.assertEqual(len(deck2.deck), 0) self.assertIsInstance(deck2.deck, list) self.assertIs(deck2.deal_one(), None)
def __init__(self, name1, name2, num_cards = 10): if type(num_cards) != int: raise TypeError('Invalid Type, int type is needed') else: self.num_cards = num_cards self.player1 = Player(name1, num_cards) self.player2 = Player(name2, num_cards) self.game_deck = DeckOfCards() self.new_game()
def test_add_card(self): self.player1.add_card(self.r_card) self.assertIn(self.r_card,self.player1.pack) self.assertEqual(self.player1.len_pack + 1, len(self.player1.pack)) with self.assertRaises(TypeError): self.player1.add_card(4) with self.assertRaises(TypeError): self.player1.add_card('Four') deck1 = DeckOfCards() with self.assertRaises(TypeError): self.player1.add_card(deck1)
class CardGame(): # מחלקה שמגדירה את משחק הקלפים וחוקיו. # מתודה שמקבלת את שמות שתי השחקנים ומספר הקלפים שיש לחלק לכל שחקן # המתודה יוצרת חבילת קלפים אקראית חדשה def __init__(self, name1, name2, num_cards = 10): if type(num_cards) != int: raise TypeError('Invalid Type, int type is needed') else: self.num_cards = num_cards self.player1 = Player(name1, num_cards) self.player2 = Player(name2, num_cards) self.game_deck = DeckOfCards() self.new_game() # מתודה שמערבבת את חבילת הקלפים ומחלקת קלפים לשתי השחקנים # המתודה פועלת רק כאשר המשחק עוד לא התחיל, נקראית מהקונסטרקטור # במידה והמתודה תופעל באמצע המשחק, תחזיר הודעת שגיאה def new_game(self): if len(self.game_deck.deck)==52: self.game_deck.shuffle_deck() self.player1.set_hand(self.game_deck) self.player2.set_hand(self.game_deck) else: raise SystemError("the game is already began") # מתודה שמכריעה מי הזוכה במשחק ומדפיסה הודעה בהתאם def get_winner(self): if len(self.player1.pack)<len(self.player2.pack): return self.player1.name, print(f'The winner is: {self.player1.name}') elif len(self.player1.pack)>len(self.player2.pack): return self.player2.name,print(f'The winner is: {self.player2.name}') else: return None,print("There is no winner - tied")
def test_deal_one(self): self.assertNotIn(self.deck1.deal_one(), self.deck1.deck) self.assertEqual(len(self.deck1.deck) + 1, len(self.copy_deck)) deck2 = DeckOfCards() deck2.deck = [Card(2, u'\u2666')] deck2.deal_one() self.assertEqual(len(deck2.deck), 0) self.assertIsInstance(deck2.deck, list) self.assertIs(deck2.deal_one(), None)
def test_set_hand(self): deck1 = DeckOfCards() self.player2.set_hand(deck1) deck2 = DeckOfCards() self.player3.set_hand(deck2) deck3 = DeckOfCards() self.player4.set_hand(deck3) self.assertEqual(len(self.player2.pack),3) self.assertEqual(len(self.player3.pack),0) self.assertEqual(len(self.player4.pack),26) deck4 = DeckOfCards() deck4.deck = [] player_1 = Player('name', 10) player_1.set_hand(deck4) self.assertEqual(len(player_1.pack), 0)
def setUp(self): self.deck1 = DeckOfCards() self.copy_deck = self.deck1.deck.copy() print('The test is starting')