Exemple #1
0
class Game:
    """ゲームの情報を持つ."""
    def __init__(self):
        self.deck = Deck()
        self.dealer = Player('Dealer', self.deck, [])
        self.player = Player('Player', self.deck, [])

    #TODO: 読み取りだけに専念するべき?
    def begin(self):
        random.shuffle(self.deck)
        for _ in range(2):
            self.player.hit()
            self.dealer.hit()

    @property
    def turn_is_over(self):
        return (self.dealer.point >= result.BLACKJACK
                or self.player.point >= result.BLACKJACK)

    @property
    def game_is_over(self):
        return (self.player.frozen() or self.turn_is_over)

    @property
    def game_result(self):
        if not self.game_is_over:
            return None
        return result.judge_from_point(self.dealer.point, self.player.point)
Exemple #2
0
class TestPlayer(unittest.TestCase):
    def setUp(self):
        self.player = Player()

    def test_hand_property(self):
        self.player.hand = "A♣"
        self.assertEqual(self.player.hand, ["A♣"])

    def test_show_hand_one_card(self):
        """Test show_hand output with one card"""
        import sys
        from io import StringIO
        self.player.hand = "A♣"

        saved_stdout = sys.stdout
        try:
            fake_out = StringIO()
            sys.stdout = fake_out
            self.player.show_hand()
            output = fake_out.getvalue().strip()
            self.assertEqual(output, '1 card: A♣')
        finally:
            sys.stdout = saved_stdout

    def test_show_hand_more_than_two_card(self):
        """Test show_hand output with more than two cards"""
        import sys
        from io import StringIO
        self.player.hand = ["A♣", "2♣", "3♣"]

        saved_stdout = sys.stdout
        try:
            fake_out = StringIO()
            sys.stdout = fake_out
            self.player.show_hand()
            output = fake_out.getvalue().strip()
            self.assertEqual(output, '3 cards: A♣, 2♣, 3♣')
        finally:
            sys.stdout = saved_stdout

    def test_hit_card(self):
        deck = ["A♣", "2♣", "3♣"]
        self.player.hit(deck)
        self.assertEqual(self.player.hand, ["A♣"])
        self.assertEqual(len(deck), 2)
Exemple #3
0
class TestPlayer(unittest.TestCase):
    def setUp(self):
        self.player = Player()

    def test_hand_property(self):
        self.player.hand = "A♣"
        self.assertEqual(self.player.hand, ["A♣"])

    def test_show_hand_one_card(self):
        """Test show_hand output with one card"""
        import sys
        from io import StringIO
        self.player.hand = "A♣"

        saved_stdout = sys.stdout
        try:
            fake_out = StringIO()
            sys.stdout = fake_out
            self.player.show_hand()
            output = fake_out.getvalue().strip()
            self.assertEqual(output, '1 card: A♣')
        finally:
            sys.stdout = saved_stdout

    def test_show_hand_more_than_two_card(self):
        """Test show_hand output with more than two cards"""
        import sys
        from io import StringIO
        self.player.hand = ["A♣", "2♣", "3♣"]

        saved_stdout = sys.stdout
        try:
            fake_out = StringIO()
            sys.stdout = fake_out
            self.player.show_hand()
            output = fake_out.getvalue().strip()
            self.assertEqual(output, '3 cards: A♣, 2♣, 3♣')
        finally:
            sys.stdout = saved_stdout

    def test_hit_card(self):
        deck = ["A♣", "2♣", "3♣"]
        self.player.hit(deck)
        self.assertEqual(self.player.hand, ["A♣"])
        self.assertEqual(len(deck), 2)