Ejemplo n.º 1
0
class Game(object):
    """Conducts a game between two players.
    """
    def __init__(self, strategy_one_factory, strategy_two_factory):
        self.deck = Deck()
        self.go_fish = Response()
        self.player_one = strategy_one_factory.get_player(self.deck.get_hand(7)) 
        self.player_two = strategy_two_factory.get_player(self.deck.get_hand(7)) 

    def _take_turn(self, p1, p2):
        """Allows p1 to make a guess against p2.

        In the event that p1's guess is correct, it gets to see p2's response 
        and continue guessing.
        """
        req = p1.request()
        resp = p2.respond(req)
        while (not resp.go_fish):
            if (len(p2.hand) == 0):
                # In the event of a run of guesses that empties the opponent's hand.
                p2.add_card(self.deck.get_card())
            p1.receive_response(resp)
            req = p1.request()
            resp = p2.respond(req)
        next_card = self.deck.get_card()
        resp.set_card(next_card)
        p1.receive_response(resp)

    def _outcome(self):
        return Outcome(self.player_one, self.player_two)
       
    def run(self):
        """Runs the game by allowing the players to take turns against one
           another.
        """
        try:
            while (True):
                 self._take_turn(self.player_one, self.player_two);
                 self._take_turn(self.player_two, self.player_one);
        except DeckEmptyError:
            return self._outcome()
        return self._outcome()
Ejemplo n.º 2
0
class DeckTests(unittest.TestCase):
    def setUp(self):
        self.d = Deck()
        
    def tearDown(self):
        pass

    def testDeckCreation(self):
        self.assertEqual(len(self.d.deck), 52)

    def testGetCardDeckSize(self):
        self.assertEqual(len(self.d.deck), 52)
        c = self.d.get_card()
        self.assertEqual(len(self.d.deck), 51)

    def testGetCard(self):
        self.assertEqual(len(self.d.deck), 52)
        self.assertTrue(self.d.get_card() not in self.d.deck)
   
    def testGetHand(self):
        hand = self.d.get_hand(7)
        self.assertTrue(len(hand), 6)
       
    def testGetHandNoDuplicates(self):
        hand = self.d.get_hand(52)
        no_dupes_hand = set(hand)
        self.assertTrue(len(no_dupes_hand), 52)

    def testGetTooManyCards(self):
        self.d.get_hand(52)
        with self.assertRaises(DeckEmptyError):
            self.d.get_card()

    def testGetHandTooBig(self):
        with self.assertRaises(DeckEmptyError):
            self.d.get_hand(53)