def run(): player1 = player.Player('') player2 = player.Player('') player3 = player.Player('') player4 = player.Player('') team1 = team.Team(player1, player2) team2 = team.Team(player3, player4) current_game = game.Game(team1, team2)
class HandEval(unittest.TestCase): player1 = player.Player('') player2 = player.Player('') player3 = player.Player('') player4 = player.Player('') team1 = team.Team(player1, player2) team2 = team.Team(player3, player4) current_game = game.Game(team1, team2) def test_evaluate_hand(self): ''' Trump Hearts, 9 of Hearts beats A of Spades, A of Clubs, A of Diamonds''' card1 = card.Card(HEARTS, NINE) card2 = card.Card(SPADES, A) card3 = card.Card(CLUBS, A) card4 = card.Card(DIAMONDS, A) self.current_game.trump = HEARTS self.current_game.played = [card1, card2, card3, card4] result = self.current_game.evaluateHand() self.assertEqual(result, card1) def test_evaluate_hand_2(self): ''' Trump Hearts, 9 of Hearts beats A of Spades, A of Clubs, A of Diamonds''' card1 = card.Card(SPADES, A) card2 = card.Card(HEARTS, NINE) card3 = card.Card(CLUBS, A) card4 = card.Card(DIAMONDS, A) self.current_game.trump = HEARTS self.current_game.played = [card1, card2, card3, card4] result = self.current_game.evaluateHand() self.assertEqual(result, card2) def test_evaluate_hand_3(self): ''' Trump Clubs, 9 of Hearts beats A of Spades, K of Spades, A of Diamonds, when 9 played first''' card1 = card.Card(HEARTS, NINE) card2 = card.Card(SPADES, A) card3 = card.Card(SPADES, K) card4 = card.Card(DIAMONDS, A) self.current_game.trump = CLUBS self.current_game.played = [card1, card2, card3, card4] result = self.current_game.evaluateHand() self.assertEqual(result, card1) def test_evaluate_hand_4(self): ''' Trump Clubs, J of Spades beats A of Diamonds, K of Hearts, A of Clubs, when A of Diamonds played first''' card1 = card.Card(DIAMONDS, A) card2 = card.Card(CLUBS, A) card3 = card.Card(HEARTS, K) card4 = card.Card(SPADES, J) self.current_game.trump = CLUBS self.current_game.played = [card1, card2, card3, card4] result = self.current_game.evaluateHand() self.assertEqual(result, card4)
class RuleEval(unittest.TestCase): player1 = player.Player('') player2 = player.Player('') player3 = player.Player('') player4 = player.Player('') team1 = team.Team(player1, player2) team2 = team.Team(player3, player4) current_game = game.Game(team1, team2) # method to compare if two instances of cards have same suit and symbol # returns a Boolean # expects arguments # card1, card2 as instances of card def cardsEqual(self, card1, card2): return ((card1.suit == card2.suit) and (card1.symbol == card2.symbol)) # method should return if a card is valid # returns a Boolean # expects arguments # card as instance of card def cardValid(self, card): return ((card.suit in SUITS) and (card.symbol in SYMBOLS)) def test_generate_deck(self): '''Deck should contain 24 unique, valid cards''' self.current_game.generateDeck() result = [] for card in self.current_game.deck: if self.cardValid(card) and (len( list(filter(lambda x: self.cardsEqual(x, card), result)))) == 0: result.append(card) self.assertEqual(24, len(result)) def test_start_round_1(self): '''Each player should have 5 cards in hand''' self.current_game.startRound() for player in [self.player1, self.player2, self.player3, self.player4]: with self.subTest(player=player): self.assertEqual(len(player.hand), 5) def test_start_round_2(self): '''Faceup card should be different from any card in players hands''' self.current_game.startRound() result = False # result indicates whether card has appeared in a hand for player in [self.player1, self.player2, self.player3, self.player4]: for card in player.hand: if self.cardsEqual(card, self.current_game.faceup): result = True self.assertEqual(result, False)
def test_team_score_7(self): '''Team which gets 2 tricks gets 0 points''' team1 = team.Team(self.player1, self.player2) team1.calledTrump = True team1.tricks = 2 team1.scoreHand() self.assertEqual(team1.gameScore, 0)
def test_team_score_4(self): '''Team which calls trump and wins 5 tricks gets 2 points''' team1 = team.Team(self.player1, self.player2) team1.calledTrump = False team1.tricks = 5 team1.scoreHand() self.assertEqual(team1.gameScore, 2)
def test_team_score_2(self): '''Team which called trump and wins 4 tricks gets 1 point''' team1 = team.Team(self.player1, self.player2) team1.calledTrump = True team1.tricks = 4 team1.scoreHand() self.assertEqual(team1.gameScore, 1)
class DecideTrump(unittest.TestCase): player1 = player.Player('') player2 = player.Player('') player3 = player.Player('') player4 = player.Player('') team1 = team.Team(player1, player2) team2 = team.Team(player3, player4) current_game = game.Game(team1, team2) card1 = card.Card(CLUBS, NINE, player1) card2 = card.Card(CLUBS, A, player1) card3 = card.Card(SPADES, Q, player1) card4 = card.Card(DIAMONDS, TEN, player1) card5 = card.Card(HEARTS, K, player1) card6 = card.Card(DIAMONDS, J) card7 = card.Card(CLUBS, Q, player2) card8 = card.Card(HEARTS, NINE, player2) def test_decide_trump_face_up_1(self): '''Returns false if player does not have any of suit in hannd''' self.player1.hand = [ self.card3, self.card4, self.card5, self.card6, self.card8 ] result = self.player1.decideTrumpFaceUp( self.card1, [self.player1, self.player2, self.player3, self.player4]) self.assertEqual(result, False) def test_decide_trump_face_up_2(self): '''In current stubbbed implementation, should always return True if valid''' self.player1.hand = [ self.card2, self.card4, self.card5, self.card6, self.card8 ] result = self.player1.decideTrumpFaceUp( self.card1, [self.player1, self.player2, self.player3, self.player4]) self.assertEqual(result, True) def test_decide_trump_face_down_1(self): '''Returns any suit except the suit which was face up''' self.player1.hand = [ self.card2, self.card4, self.card5, self.card6, self.card8 ] result = self.player1.decideTrumpFaceDown( self.card1, [self.player1, self.player2, self.player3, self.player4]) self.assertIn(result, [SPADES, DIAMONDS, HEARTS])
class OnTeam(unittest.TestCase): player1 = player.Player('') player2 = player.Player('') player3 = player.Player('') player4 = player.Player('') team1 = team.Team(player1, player2) team2 = team.Team(player3, player4) def test_on_team_1(self): '''Should return True for player on team''' result = self.team1.onTeam(self.player1) self.assertEqual(result, True) def test_on_team_2(self): '''Should return False for player on team''' result = self.team1.onTeam(self.player3) self.assertEqual(result, False)
def test_team_score_6(self): '''Team which calls trump, goes alone, and wins 5 tricks gets 4 points''' team1 = team.Team(self.player1, self.player2) team1.calledTrump = True team1.tricks = 5 team1.alone = True team1.scoreHand() self.assertEqual(team1.gameScore, 4)
def test_team_score_8(self): '''Team with more than 5 tricks should raise an error''' team1 = team.Team(self.player1, self.player2) team1.tricks = 6 with self.assertRaises(ValueError): team1.scoreHand()
class PlayCard(unittest.TestCase): player1 = player.Player('') player2 = player.Player('') player3 = player.Player('') player4 = player.Player('') team1 = team.Team(player1, player2) team2 = team.Team(player3, player4) current_game = game.Game(team1, team2) card1 = card.Card(CLUBS, NINE, player1) card2 = card.Card(CLUBS, A, player1) card3 = card.Card(SPADES, Q, player1) card4 = card.Card(DIAMONDS, TEN, player1) card5 = card.Card(HEARTS, K, player1) card6 = card.Card(DIAMONDS, J) card7 = card.Card(CLUBS, Q, player2) card8 = card.Card(HEARTS, NINE, player2) def test_play_card_1(self): '''Any card in hand should be valid if first to play, function should return any card from hand''' self.current_game.trump = SPADES # self.current_game.played = [] -> for reference, not required for function invocation available_cards = [ self.card1, self.card2, self.card3, self.card4, self.card5 ] self.player1.hand = copy.copy(available_cards) result = self.player1.play(self.current_game.trump, None) self.assertIn( result, available_cards ) # as card is removed from hand, need to use copy of hand def test_play_card_2(self): '''Card which is played should be appropriately removed from the hand''' self.current_game.trump = SPADES # self.current_game.played = [] -> for reference, not required for function invocation available_cards = [ self.card1, self.card2, self.card3, self.card4, self.card5 ] self.player1.hand = copy.copy( [self.card1, self.card2, self.card3, self.card4, self.card5]) result = self.player1.play(self.current_game.trump, None) self.assertNotIn( result, self.player1.hand ) # as card is removed from hand, test properly removed def test_play_card_3(self): '''Should only play one of the clubs cards when clubs are led''' self.current_game.trump = SPADES # self.current_game.played = [card7] -> for reference, not required for function invocation self.player1.hand = [ self.card1, self.card2, self.card3, self.card4, self.card5 ] result = self.player1.play(self.current_game.trump, CLUBS) self.assertIn(result, [self.card1, self.card2]) def test_play_card_4(self): '''Should only play a heart or the Jack of diamonds when hearts are led and trump''' self.current_game.trump = HEARTS # self.current_game.played = [card8] -> for reference, not required for function invocation self.player1.hand = [ self.card2, self.card3, self.card4, self.card5, self.card6 ] result = self.player1.play(self.current_game.trump, HEARTS) self.assertIn(result, [self.card5, self.card6]) def test_play_card_5(self): '''Should play any card when cannot follow suit''' self.current_game.trump = HEARTS # self.current_game.played = [card8] -> for reference, not required for function invocation available_cards = [ self.card1, self.card2, self.card4, self.card5, self.card6 ] self.player1.hand = copy.copy(available_cards) result = self.player1.play(self.current_game.trump, SPADES) self.assertIn(result, available_cards) def test_select_card_1(self): '''selectCard method should raise exception if list of cards empty''' with self.assertRaises(ValueError): self.player1.selectCard([]) def test_can_play_1(self): '''canPlay method should return True for J of Diamonds, trump clubs, suitLed Diamonds''' result = self.player1.canPlay(self.card6, CLUBS, DIAMONDS) self.assertEqual(result, True) def test_can_play_2(self): '''canPlay method should return False for J of Diamonds, trump clubs, suitLed Hearts''' result = self.player1.canPlay(self.card6, CLUBS, HEARTS) self.assertEqual(result, False) def test_can_play_3(self): '''canPlay method should return True for J of Diamonds, trump Hearts, suitLed Hearts''' result = self.player1.canPlay(self.card6, HEARTS, HEARTS) self.assertEqual(result, True) def test_can_play_4(self): '''canPlay method should return False for J of Diamonds, trump Hearts, suitLed Diamonds''' result = self.player1.canPlay(self.card6, HEARTS, DIAMONDS) self.assertEqual(result, False)
class CardEval(unittest.TestCase): player1 = player.Player('') player2 = player.Player('') player3 = player.Player('') player4 = player.Player('') team1 = team.Team(player1, player2) team2 = team.Team(player3, player4) current_game = game.Game(team1, team2) def test_evaluate_cards(self): '''10 beats 9, same suit as led, neither trump''' card1 = card.Card(SPADES, NINE) card2 = card.Card(SPADES, TEN) self.current_game.trump = HEARTS result = self.current_game.evaluateCards(card1, card2, SPADES) self.assertEqual(result, card2) def test_evaluate_cards_2(self): '''J beats 10, same suit as led, neither trump''' card1 = card.Card(SPADES, TEN) card2 = card.Card(SPADES, J) self.current_game.trump = HEARTS result = self.current_game.evaluateCards(card1, card2, SPADES) self.assertEqual(result, card2) def test_evaluate_cards_3(self): '''Should raise error when comparing two non-suit led, non-trump cards''' card1 = card.Card(SPADES, TEN) card2 = card.Card(SPADES, J) self.current_game.trump = HEARTS with self.assertRaises(ValueError): self.current_game.evaluateCards(card1, card2, DIAMONDS) def test_evaluate_cards_4(self): '''9 beats 10, 9 follows suit & 10 doesn't, neither trump''' card1 = card.Card(DIAMONDS, NINE) card2 = card.Card(SPADES, TEN) self.current_game.trump = HEARTS result = self.current_game.evaluateCards(card1, card2, DIAMONDS) self.assertEqual(result, card1) def test_evaluate_cards_5(self): '''9 beats 10, 10 follows suit & 9 doesn't, 9 trump''' card1 = card.Card(HEARTS, NINE) card2 = card.Card(DIAMONDS, TEN) self.current_game.trump = HEARTS result = self.current_game.evaluateCards(card1, card2, DIAMONDS) self.assertEqual(result, card1) def test_evaluate_cards_6(self): '''J beats A, A follows suit & J does not, J right bower''' card1 = card.Card(DIAMONDS, A) card2 = card.Card(HEARTS, J) self.current_game.trump = HEARTS result = self.current_game.evaluateCards(card1, card2, DIAMONDS) self.assertEqual(result, card2) def test_evaluate_cards_7(self): '''J beats A, A follows suit & J does not, J left bower''' card1 = card.Card(DIAMONDS, A) card2 = card.Card(DIAMONDS, J) self.current_game.trump = HEARTS result = self.current_game.evaluateCards(card1, card2, DIAMONDS) self.assertEqual(result, card2) def test_evaluate_cards_8(self): '''J beats A, both trump, J left bower''' card1 = card.Card(HEARTS, A) card2 = card.Card(DIAMONDS, J) self.current_game.trump = HEARTS result = self.current_game.evaluateCards(card1, card2, SPADES) self.assertEqual(result, card2) def test_evaluate_cards_9(self): '''J1 beats J2, both trump, J1 right bower and J2 left bower''' card1 = card.Card(HEARTS, J) card2 = card.Card(DIAMONDS, J) self.current_game.trump = HEARTS result = self.current_game.evaluateCards(card1, card2, SPADES) self.assertEqual(result, card1) def test_evaluate_cards_10(self): '''A beats Q, both trump''' card1 = card.Card(CLUBS, A) card2 = card.Card(CLUBS, Q) self.current_game.trump = CLUBS result = self.current_game.evaluateCards(card1, card2, SPADES) self.assertEqual(result, card1) def test_evaluate_cards_11(self): '''Should raise error if comparing same cards, trump''' card1 = card.Card(CLUBS, A) card2 = card.Card(CLUBS, A) self.current_game.trump = CLUBS with self.assertRaises(ValueError): self.current_game.evaluateCards(card1, card2, DIAMONDS) def test_evaluate_cards_12(self): '''Should raise error if comparing same cards, not trump''' card1 = card.Card(CLUBS, A) card2 = card.Card(CLUBS, A) self.current_game.trump = DIAMONDS with self.assertRaises(ValueError): self.current_game.evaluateCards(card1, card2, DIAMONDS) def test_evaluate_cards_13(self): '''Q beats J, same suit as led, neither trump''' card1 = card.Card(SPADES, J) card2 = card.Card(SPADES, Q) self.current_game.trump = HEARTS result = self.current_game.evaluateCards(card1, card2, SPADES) self.assertEqual(result, card2) def test_evaluate_cards_14(self): '''Q beats J, same suit as led, neither trump''' card1 = card.Card(SPADES, Q) card2 = card.Card(SPADES, K) self.current_game.trump = HEARTS result = self.current_game.evaluateCards(card1, card2, SPADES) self.assertEqual(result, card2) def test_evaluate_cards_15(self): '''Q beats J, same suit as led, neither trump''' card1 = card.Card(SPADES, K) card2 = card.Card(SPADES, A) self.current_game.trump = HEARTS result = self.current_game.evaluateCards(card1, card2, SPADES) self.assertEqual(result, card2)