def spawnExtraPlayers(playerType, amount, names=None): # Returns: Array with amount of players specified. Returns object if amount == 1 if isinstance(playerType, Bot): # Spawn bots if amount == 1: return Bot() else: return [Bot() for i in range(amount)] else: # Spawn humans if amount == 1: return HumanPlayer().randomName() else: return [HumanPlayer().randomName() for i in range(amount)]
def test_decision_phase_human_to_human_pass(self): test_card = Card("2", None) extraPlayer = spawnExtraPlayers(HumanPlayer(), 1) f1 = sys.stdin f = open('../../test_data/decision_phase/decision_phase_test_#2.txt', 'r') sys.stdin = f self.humanPlayer.hand.append(test_card) self.engine.setPlayers([self.humanPlayer, extraPlayer]) self.engine.initialize() self.assertIs(self.engine.getPlayerAmount(), 2) self.engine.decisionPhase(self.humanPlayer) f.close() sys.stdin = f1 self.assertIs(self.humanPlayer.getChosenPlayer(), extraPlayer) self.assertEqual(self.humanPlayer.getChosenCard(), test_card)
def setUp(self): self.humanPlayer = HumanPlayer().randomName() self.botPlayer = Bot() self.engine = TestGoFishEngine(True)
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.handCount() == 0, "Bot Player's Hand %s" % self.botPlayer.showHand()) self.assertFalse(self.humanPlayer.handCount() == 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.handCount() == 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.handCount() == 1) self.assertTrue(self.botPlayer.handCount() == 3) def test_trading_phase_reject_player_loss(self): from Modules.Cards.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.handCount() == 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
class InitialPhaseEngineTests(unittest.TestCase): def setUp(self): self.humanPlayer = HumanPlayer().randomName() self.botPlayer = Bot() self.engine = TestGoFishEngine(True) def test_initial_phase(self): # What is necessary for the initial phase to pass? # 1. Initially, since I am setting no tricks, the output must show that there are 0 tricks # 2. I will give a custom hand, but the output must self.engine.setPlayers([self.humanPlayer]) self.engine.initialize() # Make sure only one player self.assertIs(self.engine.getPlayerAmount(), 1) # Want to get the first hand that could possibly be dealt. # this will be the hand that the player gets, supposedly acceptHand = self.engine.returnFirstHand() # Make sure I'm not actually drawing from the deck before the player draws self.assertIs(self.engine.getDeck().currentAmount(), 52) # These variables contain strings which will will be what # this test is looking to assert equality to acceptTrickOutput = "You currently have 0 tricks" acceptHandOutput = "%s" % acceptHand # Deal out the hands now self.engine.dealHands() # This goes through the initial phase of the engine self.engine.initialPhase(self.humanPlayer) # Full output output = sys.stdout.getvalue().strip() # Splitting the output is a nice parse to apply to # variables print output givenTrickOutput = output.split('\n')[0] givenHandOutput = output.split('\n')[1] self.assertEqual(givenTrickOutput, acceptTrickOutput) self.assertEqual(givenHandOutput, acceptHandOutput) def test_initial_phase_multiple_tricks(self): # Player is going to start out with 2 tricks on this initial phase test for i in range(2): # Add a trick to a hand. self.humanPlayer.addPlayerTrick() self.engine.setPlayers([self.humanPlayer]) self.engine.initialize() acceptTrickOutput = "You currently have 2 tricks" self.engine.dealHands() self.engine.initialPhase(self.humanPlayer) givenTrickOutput = sys.stdout.getvalue().strip().split('\n')[0] self.assertEqual(givenTrickOutput, acceptTrickOutput) def tearDown(self): self.humanPlayer = None self.botPlayer = None self.engine = None
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.handCount() == 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.handCount() == 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.handCount() == 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.handCount() == 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.handCount() == 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.handCount() == 4) self.assertTrue(not self.humanPlayer.gotGuess()) self.engine.endPhase(self.humanPlayer) self.assertTrue(not self.humanPlayer.gotGuess()) self.assertTrue(self.humanPlayer.handCount() == 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.handCount() == 16) self.assertTrue(not self.humanPlayer.gotGuess()) self.engine.endPhase(self.humanPlayer) self.assertTrue(not self.humanPlayer.gotGuess()) self.assertTrue(self.humanPlayer.handCount() == 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.handCount() == 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
class DecisionPhaseEngineTests(unittest.TestCase): def setUp(self): self.humanPlayer = HumanPlayer().randomName() self.botPlayer = Bot() self.engine = TestGoFishEngine(True) def test_decision_phase_human_to_bot_pass(self): # Human to bot case test_card = Card("2", None) f1 = sys.stdin f = open('../../test_data/decision_phase/decision_phase_test_#1.txt', 'r') sys.stdin = f self.humanPlayer.hand.append(test_card) self.engine.setPlayers([self.humanPlayer, self.botPlayer]) self.engine.initialize() self.assertIs(self.engine.getPlayerAmount(), 2) self.engine.decisionPhase(self.humanPlayer) f.close() sys.stdin = f1 self.assertIs(self.humanPlayer.getChosenPlayer(), self.botPlayer) self.assertEqual(self.humanPlayer.getChosenCard(), test_card) def test_decision_phase_human_to_human_pass(self): test_card = Card("2", None) extraPlayer = spawnExtraPlayers(HumanPlayer(), 1) f1 = sys.stdin f = open('../../test_data/decision_phase/decision_phase_test_#2.txt', 'r') sys.stdin = f self.humanPlayer.hand.append(test_card) self.engine.setPlayers([self.humanPlayer, extraPlayer]) self.engine.initialize() self.assertIs(self.engine.getPlayerAmount(), 2) self.engine.decisionPhase(self.humanPlayer) f.close() sys.stdin = f1 self.assertIs(self.humanPlayer.getChosenPlayer(), extraPlayer) self.assertEqual(self.humanPlayer.getChosenCard(), test_card) def test_decision_phase_bot_to_bot_pass(self): test_card = Card("2", None) extraPlayer = spawnExtraPlayers(Bot(), 1) f1 = sys.stdin f = open('../../test_data/decision_phase/decision_phase_test_#3.txt', 'r') sys.stdin = f self.botPlayer.hand.append(test_card) self.engine.setPlayers([self.botPlayer, extraPlayer]) self.engine.initialize() self.assertIs(self.engine.getPlayerAmount(), 2) self.engine.decisionPhase(self.botPlayer) f.close() sys.stdin = f1 self.assertIs(self.botPlayer.getChosenPlayer(), extraPlayer) self.assertEqual(self.botPlayer.getChosenCard(), test_card) def test_decision_phase_bot_to_human_pass(self): test_card = Card("2", "Hearts") f1 = sys.stdin f = open('../../test_data/decision_phase/decision_phase_test_#3.txt', 'r') sys.stdin = f self.botPlayer.hand.append(test_card) self.engine.setPlayers([self.botPlayer, self.humanPlayer]) self.engine.initialize() self.assertIs(self.engine.getPlayerAmount(), 2) self.engine.decisionPhase(self.botPlayer) f.close() sys.stdin = f1 self.assertIs(self.botPlayer.getChosenPlayer(), self.humanPlayer) self.assertEqual(self.botPlayer.getChosenCard(), test_card) # How can I do wrong inputs in the decision phase? # Input a player choice # that is out of the bounds of the array (Player inputs 0 or > len(choiceList)) # Input non valid card values (A card rank that is not applicable, say 11 or 12 (jack or queen)) def test_decision_phase_fail_player_choice(self): acceptOutput = "Error: That is not one of the player choices" test_card = Card("2", "Hearts") f1 = sys.stdin f = open('../../test_data/decision_phase/decision_phase_test_#4.txt', 'r') sys.stdin = f self.humanPlayer.hand.append(test_card) self.engine.setPlayers([self.humanPlayer, self.botPlayer]) self.assertIs(self.engine.getPlayerAmount(), 2) self.engine.decisionPhase(self.humanPlayer) output = sys.stdout.getvalue().strip() # Error output when choice is 0 givenErrorOutput1 = output.split('\n')[3] # Error output when choice is >= len(choiceList) givenErrorOutput2 = output.split('\n')[5] f.close() sys.stdin = f1 self.assertEqual(givenErrorOutput1, acceptOutput) self.assertEqual(givenErrorOutput2, acceptOutput) def test_decision_phase_fail_card_choice(self): acceptOutput1 = "Error: That is not an acceptable card rank. Please choose again." acceptOutput2 = "Error: You don't even have any of those cards in your hand! Try again." test_card = Card("2", "Hearts") f1 = sys.stdin f = open('../../test_data/decision_phase/decision_phase_test_#5.txt', 'r') sys.stdin = f self.humanPlayer.hand.append(test_card) self.engine.setPlayers([self.humanPlayer, self.botPlayer]) self.assertIs(self.engine.getPlayerAmount(), 2) self.engine.decisionPhase(self.humanPlayer) output = sys.stdout.getvalue().strip() # Error output when player asks for unacceptable card givenErrorOutput1 = output.split('\n')[3] # Error output when player asks for card that # is not even in their hand. givenErrorOutput2 = output.split('\n')[5] f.close() sys.stdin = f1 self.assertEqual(givenErrorOutput1, acceptOutput1) self.assertEqual(givenErrorOutput2, acceptOutput2) def tearDown(self): self.humanPlayer = None self.botPlayer = None self.engine = None