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 __init__(self, width, height, caption): pygame.init() self.width = width self.height = height pygame.display.set_mode((width, height), DOUBLEBUF | OPENGL) pygame.display.set_caption(caption) gluPerspective(45, (self.width / self.height), 0.1, 200.0) glEnable(GL_LIGHTING) glShadeModel(GL_SMOOTH) self.clock = pygame.time.Clock() self.FPS = 60 self.on = True self.ogl = Graphics() player1 = player.Player() player1.setParent(self.ogl) self.ogl.AddObject(player1, "Player", "Main Player") en1 = enemy.BasicEnemy(0, 0, -10) self.ogl.AddObject(en1, "Enemy", "Enemy 1") #en1 = enemy.BasicEnemy(0,0,20) #self.ogl.AddObject(en1, "Enemy","Enemy 2") #en1 = enemy.BasicEnemy(20,20,0) #self.ogl.AddObject(en1, "Enemy","Enemy 3") #en1 = enemy.BasicEnemy(0,-20,0) #self.ogl.AddObject(en1, "Enemy","Enemy 4") #en2 = enemy.BasicEnemy(10,0,-20) #self.ogl.AddObject(en2, "Enemy","Enemy 2") self.loading = Text2D(20, 20, "Loading textures...", 35, (255, 0, 0)) self.ogl.Add2D(self.loading, "Loading") """py1 = PhysicObject(2.0,2.0,2.0,0.0,0.0,-50.0,10.0,(0,255,0),"object 1",self.ogl)
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])
def __init__(self, m, n): self.board = np.zeros((m, n)) self.horizontal_edges = [] self.vertical_edges = [] self.dimensions = (m, n) self.p1 = play.Player(1, colors.RED) self.p2 = play.Player(2, colors.BLUE) self.curr_player = self.p1 self.alpha = -math.inf self.beta = math.inf # m rows, n columns # each row is of the form (n * k, (n+1)k - 1) for i in range(m): self.horizontal_edges.append( [Edge(i * n + j, i * n + j + 1) for j in range(n - 1)]) for i in range(m - 1): self.vertical_edges.append( [Edge(i * n + j, (i + 1) * n + j) for j in range(n)]) self.boxes = [] for i in range(m - 1): row = [] for j in range(n - 1): edges = [ self.horizontal_edges[i][j], self.horizontal_edges[i + 1][j], self.vertical_edges[i][j], self.vertical_edges[i][j + 1] ] box = Box(edges) for edge in box.edges: edge.add_box(box) row.append(box) self.boxes.append(row) # at this point, all edges have boxes that they're a part of, and all # boxes are a collection of edges # now need a set of all available edges to start self.available_edges = [] for edge_list in self.horizontal_edges + self.vertical_edges: for edge in edge_list: self.available_edges.append(edge)
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 create_player_one(self, color, **kwargs): # Creates player one, and sets the position of the player to the top-left corner of the screen. # This is where the powerups the player collects will display. x = powerup.Powerup.width / 2 y = powerup.Powerup.height / 2 name = settings.PLAYER_ONE_NAME key_up = settings.PLAYER_ONE_KEY_UP key_down = settings.PLAYER_ONE_KEY_DOWN key_unleash_energy = settings.PLAYER_ONE_KEY_UNLEASH_ENERGY joy_unleash_energy = settings.PLAYER_ONE_JOY_UNLEASH_ENERGY player_one = player.Player(x, y, name, key_up, key_down, key_unleash_energy, joy_unleash_energy, kwargs.get("gamepad_id", None), color, self.player_one_ai) return player_one
def create_player_two(self, color, **kwargs): # Creates player two, and sets the position of the player to the bottom-right corner of the screen. # This is where the powerups the player collects will display. x = settings.SCREEN_WIDTH - (powerup.Powerup.width / 2) - powerup.Powerup.width y = settings.SCREEN_HEIGHT - (powerup.Powerup.height / 2) - powerup.Powerup.height name = settings.PLAYER_TWO_NAME key_up = settings.PLAYER_TWO_KEY_UP key_down = settings.PLAYER_TWO_KEY_DOWN key_unleash_energy = settings.PLAYER_TWO_KEY_UNLEASH_ENERGY joy_unleash_energy = settings.PLAYER_TWO_JOY_UNLEASH_ENERGY player_two = player.Player(x, y, name, key_up, key_down, key_unleash_energy, joy_unleash_energy, kwargs.get("gamepad_id", None), color, self.player_two_ai) return player_two
class ScoreTeam(unittest.TestCase): player1 = player.Player('') player2 = player.Player('') def test_team_score_1(self): '''Team which called trump and wins 3 tricks gets 1 point''' team1 = team.Team(self.player1, self.player2) team1.calledTrump = True team1.tricks = 3 team1.scoreHand() self.assertEqual(team1.gameScore, 1) 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) def test_team_score_3(self): '''Team which didn't call trump and wins 3 tricks gets 2 points''' team1 = team.Team(self.player1, self.player2) team1.calledTrump = False team1.tricks = 3 team1.scoreHand() self.assertEqual(team1.gameScore, 2) 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_5(self): '''Team which calls trump, goes alone, and wins 3 tricks gets 1 point''' team1 = team.Team(self.player1, self.player2) team1.calledTrump = True team1.tricks = 3 team1.alone = True team1.scoreHand() self.assertEqual(team1.gameScore, 1) 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_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_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()
def test_new_player_1(self): '''New player should have name they set''' player1 = player.Player('Jimothy') self.assertEqual(player1.name, 'Jimothy')
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)
def test_new_player_3(self): '''New player have error raised if they use reserved namespace''' with self.assertRaises(ValueError): player.Player(NOT_HELD)
def test_new_player_2(self): '''New player should be allowed blank names''' player1 = player.Player('') self.assertEqual(player1.name, '')
Game loop : * handle event * update game state * draw screen """ # initialize pygame pygame.init() # Clock object for FPS management fps_clock = pygame.time.Clock() # region Assets # Window screen = pygame.display.set_mode((1280, 800)) screenHelper = ScreenHelper(screen) player = player.Player(screenHelper) hive = enemyhive.EnemyHive(screenHelper) bulletHandler = bulletHandler.BulletHandler(screenHelper) # endregion # region Game loop while True: # Events keys = pygame.key.get_pressed() if keys[K_UP]: player.move("Up") if keys[K_LEFT]: player.move("Left") if keys[K_DOWN]: player.move("Down")
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)