예제 #1
0
파일: game.py 프로젝트: Kevinpgalligan/ctci
    def __init__(self, num_players):
        """
		num_players does not include the dealer.
		"""
        self.dealer = BlackJackPlayer()
        self.players = [BlackJackPlayer() for i in range(num_players)]
        self.deck = StandardDeck()
        self.deck.shuffle()
        self.active_player = 0
예제 #2
0
파일: game.py 프로젝트: Kevinpgalligan/ctci
class BlackJack(Game):
    """
	This is casino-style blackjack.
	Dealer is house.
	Play starts at dealer's left
	House always wins.
	"""
    def __init__(self, num_players):
        """
		num_players does not include the dealer.
		"""
        self.dealer = BlackJackPlayer()
        self.players = [BlackJackPlayer() for i in range(num_players)]
        self.deck = StandardDeck()
        self.deck.shuffle()
        self.active_player = 0

    def deal_hidden_card(self, player):
        player.hidden_card = self.deal()

    def deal_visible_card(self, player):
        player.visible_cards.append(self.deal())

    def start_game(self):
        for player in self.players:
            self.deal_hidden_card(player)
        self.deal_hidden_card(self.dealer)
        for player in self.players:
            self.deal_visible_card(player)
        self.deal_visible_card(self.dealer)

    def hit(self, player):
        player.visible_cards.append(self.deal())

    def print_table_stats(self):
        print("table looks like this:")
        print("-------")
        for i, player in enumerate(self.players):
            print(f"Player {i}:")
            print("Visible cards:")
            print([card for card in player.visible_cards])
            print(
                f"Showing score of {player.score_hand(include_hidden=False)}")
            print(f"Total score is {player.score_hand(include_hidden=True)}")
            print('-------')
        print("Dealer:")
        print(f"Visible cards:")
        print([card for card in self.dealer.visible_cards])
        print(
            f"Showing score of {self.dealer.score_hand(include_hidden=False)}")
        print(f"Total score is {self.dealer.score_hand(include_hidden=True)}")
예제 #3
0
파일: game.py 프로젝트: Kevinpgalligan/ctci
class Game:
    def __init__(self):
        self.players = []
        self.deck = StandardDeck()
        self.first_player = 0

    def deal(self):
        return self.deck.draw_card()
예제 #4
0
class KitTestCase(TestCase):
    def setUp(self):
        self.d = StandardDeck()
        self.d.shuffle()

    def tearDown(self):
        del self.d

    def test_init(self):
        m = self.d.pop()
        n = self.d.pop()
        o = self.d.pop()
        p = self.d.pop()
        street1 = TestStreet1(m, n)
        street2 = TestStreet2(o)
        TestKit()
        TestKit(street1)
        TestKit(street1, street2)
        with self.assertRaises(TypeError):
            TestKit(street2, street1)
        TestKit(m, n)
        TestKit(m, n, o)
        with self.assertRaises(ValueError):
            TestKit(m)
        with self.assertRaises(ValueError):
            TestKit(m, n, o, p)

    def test_contains(self):
        m = self.d.pop()
        n = self.d.pop()
        o = self.d.pop()
        p = self.d.pop()
        self.assertIn(m, TestKit(TestStreet1(m, n)))
        self.assertIn(o, TestKit(TestStreet1(m, n), TestStreet2(o)))
        self.assertNotIn(p, TestKit(TestStreet1(m, n), TestStreet2(o)))

    def test_append(self):
        m = self.d.pop()
        n = self.d.pop()
        o = self.d.pop()
        p = self.d.pop()
        b = TestKit()
        b.append(TestStreet1(m, n))
        self.assertIn(m, b)
        b.append(o)
        self.assertIn(o, b)
        with self.assertRaises(ValueError):
            b.append(TestStreet2(p))
        with self.assertRaises(ValueError):
            b.append(p)
예제 #5
0
class StreetTestCase(TestCase):
    def setUp(self):
        self.d = StandardDeck()
        self.d.shuffle()

    def tearDown(self):
        del self.d

    def test_init(self):
        m = self.d.pop()
        n = self.d.pop()
        o = self.d.pop()
        TestStreet1(m, n)
        with self.assertRaises(ValueError):
            TestStreet1(m)
        with self.assertRaises(ValueError):
            TestStreet1(m, n, o)
        with self.assertRaises(TypeError):
            TestStreet1(m, self.d)

    def test_contains(self):
        m = self.d.pop()
        n = self.d.pop()
        o = self.d.pop()
        self.assertIn(m, TestStreet1(m, n))
        self.assertIn(m, TestStreet1(n, m))
        self.assertNotIn(o, TestStreet1(m, n))
예제 #6
0
파일: game.py 프로젝트: Kevinpgalligan/ctci
 def __init__(self):
     self.players = []
     self.deck = StandardDeck()
     self.first_player = 0
예제 #7
0
from cards import StandardDeck

deck = StandardDeck()
deck.shuffle()
print(deck.deal())

예제 #8
0
 def setUp(self):
     self.d = StandardDeck()
     self.d.shuffle()
예제 #9
0
    def test_speed(self):
        timer = Timer()

        count = Counter()
        for i in range(SPEED_TEST_COUNT):
            deck = StandardDeck()
            deck.shuffle()
            pocket = Pocket(deck.pop(), deck.pop())
            board = Board(deck.pop(), deck.pop(), deck.pop(), deck.pop(), deck.pop())
            with timer:
                hand = HandIdentifier.identify(pocket, board)
            count.update([hand.__class__])

        for item, c in count.most_common():
            print(item, c)
        print('time:', timer.elapsed)
        print('per second:', round(SPEED_TEST_COUNT / timer.elapsed))