Exemplo n.º 1
0
class PlayerTests(unittest.TestCase):
    def setUp(self):
        self.player_1 = False
        self.player_2 = False
        self.deck = False

    def test_human_player_is_not_bot(self):
        self.player_1 = HumanPlayer()
        self.player_2 = Bot

        self.assertFalse(isinstance(self.player_1, self.player_2))

    def test_two_diff_not_equal(self):
        self.player_1 = HumanPlayer("Jeffery")
        self.player_2 = HumanPlayer("Robert")
        self.assertNotEqual(self.player_1, self.player_2)

    def test_two_diff_same_name_not_equal(self):
        self.player_1 = HumanPlayer("Jeffery")
        self.player_2 = HumanPlayer("Jeffery")
        self.assertNotEqual(self.player_1, self.player_2)

    def test_draw_hand(self):
        self.deck = Deck()
        self.deck.initialize()

        self.player_1 = HumanPlayer()

        # Make sure the deck is 52 cards (one full deck)
        self.assertIs(self.deck.currentAmount(), 52)

        # Player draws a full hand from the deck
        self.player_1.drawHand(self.deck)

        # Make sure after drawing that the deck takes 7
        # 	cards away from its full total
        self.assertIs(self.deck.currentAmount(), 45)

        # Assert that the player actually has a hand
        self.assertIsNot(self.player_1.getHand(), [])

        # At least for Go Fish, the hand should be 7
        # 	cards big when the player is starting out
        self.assertIs(self.player_1.countHand(), 7)

    def tearDown(self):
        self.player_1 = False
        self.player_2 = False
        self.deck = False
Exemplo n.º 2
0
class EndPhaseEngineTests(unittest.TestCase):
    def setUp(self):
        self.humanPlayer = HumanPlayer().randomName()
        self.botPlayer = Bot()
        self.engine = TestGoFishEngine(True)

    # Insert Tests Here
    def testEndPhaseCorrect(self):
        # Set a player up with a correct guess, then test and see if that guess got reset.
        # 	This needs to happen before a player takes a turn
        self.humanPlayer.setGuess(True)

        self.engine.endPhase(self.humanPlayer)

        self.assertTrue(self.humanPlayer.gotGuess() == False)

    def testEndPhaseCorrectOneTrick(self):
        # Set a player up with a correct guess and a hand that contains one trick.
        hand = [
            Card("A", "Spades"),
            Card("A", "Hearts"),
            Card("A", "Diamonds"),
            Card("A", "Clubs")
        ]

        self.humanPlayer.takeRelevantCards(hand)
        self.assertTrue(self.humanPlayer.countHand() == 4)
        self.humanPlayer.setGuess(True)
        self.assertTrue(self.humanPlayer.gotGuess() == True)

        self.engine.endPhase(self.humanPlayer)

        self.assertTrue(self.humanPlayer.gotGuess() == False)
        self.assertTrue(self.humanPlayer.countHand() == 0)
        self.assertTrue(self.humanPlayer.getTricks() == 1)
        print self.engine.getMasterTrickCount()
        self.assertTrue(self.engine.getMasterTrickCount() == 1)

    def testEndPhaseCorrectMultipleTricks(self):
        hand = spawnDupCards(["A", "J", "K", "Q"],
                             ["Diamonds", "Spades", "Hearts", "Clubs"])
        self.humanPlayer.takeRelevantCards(hand)
        self.assertTrue(self.humanPlayer.countHand() == 16)
        self.humanPlayer.setGuess(True)

        self.assertTrue(self.humanPlayer.gotGuess() == True)

        self.engine.endPhase(self.humanPlayer)

        self.assertTrue(self.humanPlayer.gotGuess() == False)
        self.assertTrue(self.humanPlayer.countHand() == 0)
        self.assertTrue(self.humanPlayer.getTricks() == 4)
        print self.engine.getMasterTrickCount()
        self.assertTrue(self.engine.getMasterTrickCount() == 4)

    def testEndPhaseCorrectEndGame(self):
        hand = spawnDupCards(["3"], ["Diamonds", "Spades", "Hearts", "Clubs"])
        self.humanPlayer.takeRelevantCards(hand)
        self.assertTrue(self.humanPlayer.countHand() == 4)
        self.humanPlayer.setGuess(True)

        self.assertTrue(self.humanPlayer.gotGuess() == True)

        self.engine.trickCount = 12
        self.assertTrue(self.engine.getMasterTrickCount() == 12)
        self.engine.setPlayers([self.humanPlayer])

        self.engine.endGameLoop(self.humanPlayer)
        self.assertTrue(self.engine.getMasterTrickCount() == 13)
        self.assertTrue(self.engine.endGame == True)

        output = sys.stdout.getvalue().strip()

        winningPhrase = """Congratulations %s, you have won the epic game of Go Fish with a trick count of %s. Make sure to tell all of your other friends (if you have any) that you won one of the most childish games in all the land!""" % (
            self.humanPlayer, 1)
        self.assertEquals(output, winningPhrase)

    def testEndPhaseIncorrectNoTricks(self):
        self.humanPlayer.takeRelevantCards([Card("A", "Spades")])
        self.engine.setPlayers([self.humanPlayer, self.botPlayer])
        self.assertTrue(self.engine.getPlayerAmount() == 2)
        self.assertTrue(self.engine._getPlayerIndex() == 0)
        self.engine.endPhase(self.humanPlayer)
        self.assertTrue(self.engine._getPlayerIndex() == 1)

    def testEndPhaseIncorrectOneTrick(self):
        hand = [
            Card("A", "Spades"),
            Card("A", "Hearts"),
            Card("A", "Diamonds"),
            Card("A", "Clubs")
        ]

        self.humanPlayer.takeRelevantCards(hand)
        self.engine.setPlayers([self.humanPlayer, self.botPlayer])
        self.assertTrue(self.humanPlayer.countHand() == 4)
        self.assertTrue(not self.humanPlayer.gotGuess())

        self.engine.endPhase(self.humanPlayer)

        self.assertTrue(not self.humanPlayer.gotGuess())
        self.assertTrue(self.humanPlayer.countHand() == 0)
        self.assertTrue(self.humanPlayer.getTricks() == 1)
        print self.engine.getMasterTrickCount()
        self.assertTrue(self.engine.getMasterTrickCount() == 1)

    def testEndPhaseIncorrectMultipleTricks(self):
        hand = spawnDupCards(["A", "J", "K", "Q"],
                             ["Diamonds", "Spades", "Hearts", "Clubs"])
        self.humanPlayer.takeRelevantCards(hand)
        self.engine.setPlayers([self.humanPlayer, self.botPlayer])
        self.assertTrue(self.humanPlayer.countHand() == 16)
        self.assertTrue(not self.humanPlayer.gotGuess())

        self.engine.endPhase(self.humanPlayer)

        self.assertTrue(not self.humanPlayer.gotGuess())
        self.assertTrue(self.humanPlayer.countHand() == 0)
        self.assertTrue(self.humanPlayer.getTricks() == 4)
        print self.engine.getMasterTrickCount()
        self.assertTrue(self.engine.getMasterTrickCount() == 4)

    def testEndPhaseIncorrectEndGame(self):
        hand = spawnDupCards(["3"], ["Diamonds", "Spades", "Hearts", "Clubs"])
        self.humanPlayer.takeRelevantCards(hand)
        self.assertTrue(self.humanPlayer.countHand() == 4)

        self.assertTrue(not self.humanPlayer.gotGuess())

        self.engine.trickCount = 12
        self.assertTrue(self.engine.getMasterTrickCount() == 12)
        self.engine.setPlayers([self.humanPlayer])

        self.engine.endGameLoop(self.humanPlayer)
        self.assertTrue(self.engine.getMasterTrickCount() == 13)
        self.assertTrue(self.engine.endGame == True)

        output = sys.stdout.getvalue().strip()

        winningPhrase = """Congratulations %s, you have won the epic game of Go Fish with a trick count of %s. Make sure to tell all of your other friends (if you have any) that you won one of the most childish games in all the land!""" % (
            self.humanPlayer, 1)
        self.assertEquals(output, winningPhrase)

    def tearDown(self):
        self.humanPlayer = None
        self.botPlayer = None
        self.engine = None
Exemplo n.º 3
0
class TradingPhaseEngineTests(unittest.TestCase):
    def setUp(self):
        self.humanPlayer = HumanPlayer().randomName()
        self.botPlayer = Bot()
        self.engine = TestGoFishEngine(True)

    def test_trading_phase_accept_single_card(self):
        # Steps:
        # 1. Player initialization
        # 	a. Set Chosen Player
        #	b. Set Chosen Card
        # 	c. Initialize Chosen Player's hand with chosen card
        # 	d. run tests

        # The ole 5 of clubs :)

        ask_card = Card(5, "Clubs")
        self.humanPlayer.setChosenPlayer(self.botPlayer)
        self.humanPlayer.setChosenCard(ask_card)
        self.botPlayer.hand.append(ask_card)
        self.engine.tradingPhase(self.humanPlayer)

        output = sys.stdout.getvalue().strip()
        split_output = output.split('\n')
        output1 = split_output[0]
        output2 = split_output[1]

        acceptOutput1 = "%s: \"Hey %s, Do you have any %ss?\"" % (
            self.humanPlayer.getName(), self.botPlayer, ask_card.getRank())
        self.assertTrue(
            output1 == acceptOutput1,
            "Your output is not the same as what I am expecting\
												\nPlayer: %s\n\
												Chosen Player: %s\n\
												Chosen Card: %s" %
            (self.humanPlayer.getName(), self.botPlayer, ask_card.getRank()))

        self.assertFalse(checkStringForBullshit(output2),
                         "You got some bullshit in your output 2")
        self.assertTrue(
            len(self.botPlayer.getGiveArray()) == 0,
            "Bot Player's Give Array: %s" % self.botPlayer.getGiveArray())
        self.assertTrue(
            len(self.botPlayer.getHand()) == 0,
            "Bot Player's Hand %s" % self.botPlayer.showHand())

        self.assertFalse(
            len(self.humanPlayer.getHand()) == 0,
            "Human Player's hand %s" % self.humanPlayer.showHand())

    def test_trading_phase_accept_multiple_cards(self):
        # The chosen player should have multiple of the `same` card.
        # 	I do not expect this to work right off the bat, but you know.
        ask_card = Card(5, "Hearts")
        bot_card_array = [
            Card(5, "Clubs"),
            Card(5, "Spades"),
            Card(5, "Diamonds")
        ]
        self.botPlayer.takeRelevantCards(bot_card_array)

        self.humanPlayer.setChosenPlayer(self.botPlayer)
        self.humanPlayer.setChosenCard(ask_card)
        self.engine.tradingPhase(self.humanPlayer)

        output = sys.stdout.getvalue().strip()
        split_output = output.split('\n')
        output1 = split_output[0]
        output2 = split_output[1]

        acceptOutput1 = "%s: \"Hey %s, Do you have any %ss?\"" % (
            self.humanPlayer.getName(), self.botPlayer, ask_card.getRank())
        self.assertTrue(
            output1 == acceptOutput1,
            "Your output is not the same as what I am expecting\
												\nPlayer: %s\n\
												Chosen Player: %s\n\
												Chosen Card: %s" %
            (self.humanPlayer.getName(), self.botPlayer, ask_card.getRank()))

        self.assertTrue(
            len(self.botPlayer.getGiveArray()) == 0,
            "Bot Player's Give Array: %s" % self.botPlayer.getGiveArray())
        self.assertTrue(self.botPlayer.countHand() == 0,
                        "Bot Player's Hand %s" % self.botPlayer.showHand())

        self.assertFalse(self.humanPlayer.countHand() == 0,
                         "Human Player's hand is not empty, as it should be")

    def test_trading_phase_reject_no_cards(self):
        # Player should draw a card from the deck after this.
        # 	Don't forget to load up the engine with a deck.
        self.engine.setDeck()

        ask_card = Card(10, "Clubs")
        bot_hand_card_array = [
            Card(5, "Clubs"),
            Card(5, "Spades"),
            Card(5, "Diamonds")
        ]
        self.botPlayer.takeRelevantCards(bot_hand_card_array)

        self.humanPlayer.setChosenPlayer(self.botPlayer)
        self.humanPlayer.setChosenCard(ask_card)

        self.assertTrue(self.humanPlayer.countHand() == 0)

        self.engine.tradingPhase(self.humanPlayer)

        output = sys.stdout.getvalue().strip()
        split_output = output.split('\n')
        output1 = split_output[0]
        output2 = split_output[1]

        acceptOutput1 = "%s: \"Hey %s, Do you have any %ss?\"" % (
            self.humanPlayer.getName(), self.botPlayer, ask_card.getRank())
        self.assertTrue(
            output1 == acceptOutput1,
            "Your output is not the same as what I am expecting\
												\nPlayer: %s\n\
												Chosen Player: %s\n\
												Chosen Card: %s" %
            (self.humanPlayer.getName(), self.botPlayer, ask_card.getRank()))

        self.assertTrue(self.humanPlayer.countHand() == 1)
        self.assertTrue(self.botPlayer.countHand() == 3)

    def test_trading_phase_reject_player_loss(self):
        from Deck import Deck

        self.engine.deck = Deck()
        self.engine.setPlayers([self.humanPlayer, self.botPlayer])

        self.assertTrue(self.humanPlayer in self.engine.getPlayers())

        ask_card = Card(10, "Clubs")

        bot_hand_card_array = [
            Card(5, "Clubs"),
            Card(5, "Spades"),
            Card(5, "Diamonds")
        ]

        self.botPlayer.takeRelevantCards(bot_hand_card_array)

        self.humanPlayer.setChosenPlayer(self.botPlayer)

        self.humanPlayer.setChosenCard(ask_card)

        self.assertTrue(self.humanPlayer.countHand() == 0)

        self.engine.tradingPhase(self.humanPlayer)

        output = sys.stdout.getvalue().strip()
        split_output = output.split('\n')

        output1 = split_output[0]
        output2 = split_output[1]
        output3 = split_output[2]

        acceptOutput1 = "%s: \"Hey %s, Do you have any %ss?\"" % (
            self.humanPlayer.getName(), self.botPlayer, ask_card.getRank())
        self.assertTrue(
            output1 == acceptOutput1,
            "Your output is not the same as what I am expecting\
												\nPlayer: %s\n\
												Chosen Player: %s\n\
												Chosen Card: %s" %
            (self.humanPlayer.getName(), self.botPlayer, ask_card.getRank()))

        acceptOutput3 = "Hey everyone, laugh at %s! They got kicked out of the game for losing!" % self.humanPlayer
        self.assertTrue(output3 == acceptOutput3)

        self.assertTrue(not self.humanPlayer in self.engine.getPlayers())

    def tearDown(self):
        self.humanPlayer = None
        self.botPlayer = None
        self.engine = None