Пример #1
0
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!")
Пример #2
0
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
Пример #3
0
 def test_invalid_name__init__(self):
     """cases when the name is not only letters"""
     player = Player("!!!")
     self.assertEqual("unnamed player", player.name)
     player = Player("123")
     self.assertEqual("unnamed player", player.name)
     player = Player("o!h!a!d123")
     self.assertEqual("unnamed player", player.name)
Пример #4
0
 def test_get_winner_player1_win(self):
     """checks thw winner is player 1"""
     player1 = Player("ohad")
     player2 = Player("polly")
     game = CardGame(player1.name, player2.name)
     game.player2.player_hand = [Card(8, 3), Card(9, 4), Card(13, 4)]
     game.player1.player_hand = [Card(7, 3)]
     winner = game.get_winner()
     self.assertEqual(player1.name, winner)
Пример #5
0
 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()
Пример #6
0
 def test_get_winner_draw(self):
     """checks if the result is draw"""
     player1 = Player("ohad")
     player2 = Player("polly")
     game = CardGame(player1.name, player2.name)
     game.player2.player_hand = [Card(8, 3), Card(9, 4), Card(13, 4)]
     game.player1.player_hand = [Card(7, 3), Card(10, 4), Card(12, 4)]
     winner = game.get_winner()
     self.assertEqual(None, winner)
Пример #7
0
 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!")  # אם המספר אינו תיקני
Пример #8
0
 def test_show(self):
     """checks the method are really shows the player name and deck"""
     player = Player("ohad")
     self.assertEqual(player.show(), player.__str__())
Пример #9
0
 def test_add_card(self):
     """checks the function is adds card to the players' deck"""
     player = Player("aaa")
     card = Card(4, 3)
     player.add_card(card)
     self.assertIn(card, player.player_hand)
Пример #10
0
 def test_get_card(self):
     """checks the function removing card from players' deck"""
     player = Player("aaa")
     player.player_hand = [Card(9, 3), Card(11, 2), Card(13, 1)]
     player.get_card()
     self.assertEqual(len(player.player_hand), 2)
Пример #11
0
 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)
Пример #12
0
 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)
Пример #13
0
 def test_card_number__init__(self):
     """checks what happened if the cards number>26"""
     player = Player("Ohad", 29)
     self.assertEqual(player.cards_number, 26)
Пример #14
0
class TestPlayer(TestCase):

    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()


    def tearDown(self):
        print("Tear_down")


    def test_set_hand(self):
        # check that after "set_hand" the card in player's cards
        with patch('games_cards.DeckOfCards.DeckOfCards.deal_one') as mocked_deal_one:
            mocked_deal_one.return_value = self.c1
            self.p_1.set_hand(self.dc)
            self.assertIn(self.c1, self.p_1.cards)

    def test_get_card_1(self):
        # check that the card from the list
        self.p_1.cards = [self.c1, self.c2, self.c3, self.c4]
        temp_list = self.p_1.cards.copy()
        card = self.p_1.get_card()
        self.assertIn(card, temp_list)

    def test_get_card_2(self):
        # check that after "get_card" the card not in player's cards
        self.p_1.cards = [self.c1, self.c2, self.c3, self.c4]
        card = self.p_1.get_card()
        self.assertNotIn(card, self.p_1.cards)

    def test_get_card_3(self):
        # check that "get_card" with list empty is ERROR
        self.p_1.cards = []
        self.assertEqual("ERROR", self.p_1.get_card())

    def test_add_card(self):
        # check that after "add_card" the card in player's cards
        self.p_1.add_card(self.c1)
        self.assertIn(self.c1, self.p_1.cards)

    def test_reduce_amount_1(self):
        # check tha change after "reduce_amount"
        self.p_1.reduce_amount(100)
        self.assertEqual(self.p_1.money, 4900)

    def test_reduce_amount_2(self):
        # check tha change after "reduce_amount"
        self.p_1.reduce_amount(100000)
        print(self.p_1.money)
        self.assertEqual(self.p_1.money, 5000)

    def test_add_amount_1(self):
        # check tha change after "reduce_amount"
        self.p_1.add_amount(100)
        self.assertEqual(self.p_1.money, 5100)

    def test_add_amount_2(self):
        # check tha change after "reduce_amount"
        self.p_1.add_amount(100000)
        self.assertEqual(self.p_1.money, 105000)
Пример #15
0
 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()
Пример #16
0
class TestPlayer(TestCase):
    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)

    @mock.patch("games_cards.DeckOfCards.DeckOfCards.deal_one",
                return_value=Card(2, 1))
    def test_set_hand(self, mock_1):
        """בודקת האם CARD נמצא בPLAYER_DECK"""
        card = Card(2, 1)
        self.player_1.set_hand(self.deck_cards)
        self.assertIn(card, self.player_1.player_deck)

    def test_set_hand_2(self):
        """בודקת האם בPLAYER DECK לא יהיה יותר מ26 קלפים"""
        self.player_2.set_hand(self.deck_cards)
        self.assertEqual(26, len(self.player_2.player_deck))

    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)

    @mock.patch("games_cards.Player.Player.random_get_card", return_value=1)
    def test_get_card(self, mock_rand):
        """בדיקה האם פונקציה מחזירה קלף אשר נמצא במקום השני ברשימת PLAYER_DECK """
        self.player_1.player_deck = [self.card_1, self.card_2]
        self.assertIs(self.card_2, self.player_1.get_card())

    def test_add_card(self):
        """בודקת האם ADD_CARD מוסיפה CARD1"""
        self.player_1.set_hand(self.deck_cards)
        self.player_1.add_card(self.card_1)
        self.assertIn(self.card_1, self.player_1.player_deck)
Пример #17
0
 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)
Пример #18
0
from games_cards.CardGame import CardGame

from games_cards.Player import Player

player1 = Player(input("enter player 1 name,(name must be letters only): "))
player2 = Player(input("enter player 2 name,(name must be letters only): "))
game = CardGame(player1.name, player2.name)
print(game.player1.show())
print(game.player2.show())

for i in range(10):
    print(f"Round {i+1}")
    a = (game.player1.get_card())
    b = (game.player2.get_card())
    print(a)
    print(b)
    if a > b:
        game.player2.add_card(b)
        game.player2.add_card(a)
        print(f"the winner is {player1.name}")

    if a < b:
        game.player1.add_card(b)
        game.player1.add_card(a)
        print(f"the winner is {player2.name}")

print(f" The winner of the game is : {game.get_winner()}")
Пример #19
0
 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))
Пример #20
0
 def test_valid_name__init__(self):
     """checks valid name"""
     player = Player("ohad")
     self.assertEqual("ohad", player.name)