def test__setUpCurseCards(self): self.mocker.StubOutWithMock(Game, '_makePile') Game._makePile(dominion_data.cards['curse'], dominion_rules.GAME_SETUP.CURSE_CARDS).AndReturn("curse pile") self.mocker.ReplayAll() game = Game() game._setUpCurseCards() self.assertEqual(game.SupplyArea.CursePile, "curse pile") self.mocker.VerifyAll()
def test__setUpTreasureCards(self): self.mocker.StubOutWithMock(Game, '_makePile') Game._makePile(dominion_data.cards['copper'], dominion_rules.GAME_SETUP.COPPER_CARDS).AndReturn("copper pile") Game._makePile(dominion_data.cards['silver'], dominion_rules.GAME_SETUP.SILVER_CARDS).AndReturn("silver pile") Game._makePile(dominion_data.cards['gold'], dominion_rules.GAME_SETUP.GOLD_CARDS).AndReturn("gold pile") self.mocker.ReplayAll() game = Game() game._setUpTreasureCards() self.assertEqual(game.SupplyArea.CopperPile, "copper pile") self.assertEqual(game.SupplyArea.SilverPile, "silver pile") self.assertEqual(game.SupplyArea.GoldPile, "gold pile") self.mocker.VerifyAll()
def test__setUpKingdomCards(self): dominion_data.decks['first-game'] = ['cellar', 'market', 'militia', 'mine', 'moat',] self.mocker.StubOutWithMock(Game, '_makePile') for cardName in dominion_data.decks['first-game']: Game._makePile(dominion_data.cards[cardName], dominion_rules.GAME_SETUP.KINGDOM_CARDS).AndReturn("%s pile" % cardName) self.mocker.ReplayAll() game = Game() game._setUpKingdomCards() self.assertEqual(len(game.SupplyArea.KingdomPiles), 5) self.assertEqual(game.SupplyArea.KingdomPiles['Cellar'], "cellar pile") self.assertEqual(game.SupplyArea.KingdomPiles['Market'], "market pile") self.assertEqual(game.SupplyArea.KingdomPiles['Militia'], "militia pile") self.assertEqual(game.SupplyArea.KingdomPiles['Mine'], "mine pile") self.assertEqual(game.SupplyArea.KingdomPiles['Moat'], "moat pile") self.mocker.VerifyAll()
def test_over__emptyProvincePile(self): game = Game() game.addPlayer(Player(name="Player 1")) game.addPlayer(Player(name="Player 2")) game.setUpGame() game.SupplyArea.ProvincePile = Pile() self.assertTrue(game.over())
def play_game(card_set, num_players, verbose): """Plays a game of Dominion, displaying the initial game state and then running the game simulation. """ agent_dict = {'Human Player 1': HMIAgent()} demo_game = Game(n_players=num_players, agents=agent_dict, card_set=card_set, verbose=verbose) demo_game.players[0].display_deck() print("") demo_game.players[0].display_hand() print("") demo_game.players[0].display_draw_pile() print("") demo_game.players[0].display_discard_pile() print("") demo_game.play_game()
def create_room(sid, data): username = data['username'] try: if data['client_type'] == 'browser': interaction_class = BrowserInteraction except KeyError: interaction_class = NetworkedCLIInteraction characters = string.ascii_uppercase + string.digits # Create a unique room ID room = ''.join(random.choice(characters) for i in range(4)) while room in games: room = ''.join(random.choice(characters) for i in range(4)) # Add the user to the room socketio.enter_room(sid, room) sids[sid] = (room, data) # Create the game object game = Game(socketio=socketio, room=room) # Add the game object to the global dictionary of games games[room] = game # Add the player to the game game.add_player(username, sid, interactions_class=interaction_class) socketio.send(f'{username} has created room {room}\n', room=room) return room # This activates the client's set_room() callback
def test_stability(): ''' Test completely CPU-driven games. Expansions and number of CPU players are randomly selected for each game. ''' game = Game() # Add a randomly selected set of expansions into the game num_expansions = random.randint(0, len(EXPANSIONS)) expansions_to_include = random.sample(EXPANSIONS, num_expansions) for expansion in expansions_to_include: game.add_expansion(expansion) # Add a random number (2-4) players into the game num_players = random.randint(2, 4) for _ in range(num_players): game.add_player(interactions_class=AutoInteraction) game.start()
def test_over__threeEmptyPiles(self): game = Game() game.addPlayer(Player(name="Player 1")) game.addPlayer(Player(name="Player 2")) game.setUpGame() game.SupplyArea.DuchyPile = Pile() for i in range(2): game.SupplyArea.KingdomPiles[i] = Pile() self.assertTrue(game.over())
def test__drawFirstHands(self): game = Game() game.addPlayer(Player(name="Player 1")) game.addPlayer(Player(name="Player 2")) estatePile = game._makePile(dominion_data.cards['estate'], dominion_rules.FIRST_DECK.ESTATE_CARDS) copperPile = game._makePile(dominion_data.cards['copper'], dominion_rules.FIRST_DECK.COPPER_CARDS) for player in game.Players: firstDeck = game._combinePiles([estatePile, copperPile]) player.DrawPile = firstDeck game._drawFirstHands() for player in game.Players: self.assertEqual(player.DrawPile.len(), 5) self.assertEqual(player.Hand.len(), 5)
def test__setUpInitialDecks(self): game = Game() game.addPlayer(Player(name="Player 1")) game.addPlayer(Player(name="Player 2")) game.SupplyArea.EstatePile = game._makePile(dominion_data.cards['estate'], 20) game.SupplyArea.CopperPile = game._makePile(dominion_data.cards['copper'], 20) game._setUpInitialDecks() for player in game.Players: self.assertEqual(player.DrawPile.len(), 10) self.assertEqual(game.SupplyArea.EstatePile.len(), (20 - (dominion_rules.FIRST_DECK.ESTATE_CARDS * 2))) self.assertEqual(game.SupplyArea.CopperPile.len(), (20 - (dominion_rules.FIRST_DECK.COPPER_CARDS * 2)))
def test__setUpVictoryCards(self): victoryCards = 10 self.mocker.StubOutWithMock(dominion_rules, 'getGameSetupVictoryCardCount') self.mocker.StubOutWithMock(Game, '_makePile') self.mocker.StubOutWithMock(Game, '_combinePiles') dominion_rules.getGameSetupVictoryCardCount(0).AndReturn(victoryCards) Game._makePile(dominion_data.cards['estate'], victoryCards).AndReturn("estate pile") Game._makePile(dominion_data.cards['duchy'], victoryCards).AndReturn("duchy pile") Game._makePile(dominion_data.cards['province'], victoryCards).AndReturn("province pile") Game._makePile(dominion_data.cards['estate'], 0).AndReturn("additional estates") Game._combinePiles(["estate pile","additional estates"]).AndReturn("updated estate pile") self.mocker.ReplayAll() game = Game() game._setUpVictoryCards() self.assertEqual(game.SupplyArea.EstatePile, "updated estate pile") self.assertEqual(game.SupplyArea.DuchyPile, "duchy pile") self.assertEqual(game.SupplyArea.ProvincePile, "province pile") self.mocker.VerifyAll()
def test_setUpGame(self): self.mocker.StubOutWithMock(Game, '_setUpTreasureCards') Game._setUpTreasureCards() self.mocker.StubOutWithMock(Game, '_setUpVictoryCards') Game._setUpVictoryCards() self.mocker.StubOutWithMock(Game, '_setUpCurseCards') Game._setUpCurseCards() self.mocker.StubOutWithMock(Game, '_setUpKingdomCards') Game._setUpKingdomCards() self.mocker.StubOutWithMock(Game, '_setUpInitialDecks') Game._setUpInitialDecks() self.mocker.StubOutWithMock(Game, '_drawFirstHands') Game._drawFirstHands() self.mocker.ReplayAll() game = Game() game.setUpGame() self.mocker.VerifyAll()
def test_addPlayer(self): game = Game() game.addPlayer(Player(name="Test Player")) self.assertEqual(len(game.Players), 1) self.assertEqual(game.Players[0].Name, "Test Player")
def test_over__notOver(self): game = Game() game.addPlayer(Player(name="Player 1")) game.addPlayer(Player(name="Player 2")) game.setUpGame() self.assertFalse(game.over())
def main(self): game = Game() playerOne = Player(name="Player One") playerTwo = Player(name="Player Two") game.addPlayer(playerOne) game.addPlayer(playerTwo) game.setUpGame() print "DEBUG: The game looks like this: %s" % game.__dict__ print "DEBUG: The SupplyArea looks like this:" print "Copper (%s)" % game.SupplyArea.CopperPile.len() print "Silver (%s)" % game.SupplyArea.SilverPile.len() print "Gold (%s)" % game.SupplyArea.GoldPile.len() print "Estate (%s)" % game.SupplyArea.EstatePile.len() print "Duchy (%s)" % game.SupplyArea.DuchyPile.len() print "Province (%s)" % game.SupplyArea.ProvincePile.len() print "Curse (%s)" % game.SupplyArea.CursePile.len() for pileName, pile in game.SupplyArea.KingdomPiles.iteritems(): print "%s (%s)" % (pileName, pile.len()) print "DEBUG: Here are the players' draw piles:" for player in game.Players: print "%s:" % player.Name print "DECK:" for card in player.DrawPile.Cards: print card.Name print "HAND:" for card in player.Hand.Cards: print card.Name while not game.over(): for player in game.Players: currentTurn = Turn() # Count the number of coins for card in player.Hand.Cards: currentTurn.Coins += card.Effects.get('coins', 0) print "-- %s --" % player.Name while currentTurn.ActionsLeft > 0 or currentTurn.BuysLeft > 0: # Choose a card to play cardNumber = None while cardNumber is None: # Display the player's hand and options print "You have %s actions, %s buys, and %s coins" % (currentTurn.ActionsLeft, currentTurn.BuysLeft, currentTurn.Coins) print "Your hand is" for i in range(player.Hand.len()): print "%s: %s" % (i, player.Hand.Cards[i].Name) # Get user input for the card to play try: cardNumber = input("Choose a card to play (enter the number), or 100 to buy, or 99 to end: ") except (IndexError, NameError) as e: print "That isn't an available option. Try again." cardNumber = None # Player is ending the turn ... if cardNumber == 99: break # Player is buying a card ... elif cardNumber == 100: # Make sure the player has enough buys if currentTurn.BuysLeft == 0: print "You don't have another buy" else: # Get player input for the card to buy buyCardName = None while buyCardName is None: try: print "The available cards are" for cardName, pile in game.SupplyArea.allPiles().iteritems(): print "%s (%s coins) (%s left)" % (cardName, pile.Cards[0].Cost, pile.len()) buyCardName = raw_input("Enter the name of the card you wish to buy: ") buyCard = game.SupplyArea.allPiles()[buyCardName].Cards[0] except (NameError, KeyError) as e: print "That isn't an option. Try again" buyCardName = None buyCardCost = game.SupplyArea.allPiles()[buyCardName].Cards[0].Cost # Make sure the player has enough coins if buyCardCost > currentTurn.Coins: print "You don't have enough coins to buy that card" else: # Buy the card currentTurn.Coins -= buyCardCost currentTurn.BuysLeft -= 1 player.DiscardPile.drop(game.SupplyArea.allPiles()[buyCardName].draw()) # Player is playing a card from the hand ... elif cardNumber < 100: try: card = player.Hand.Cards[int(cardNumber)] if card.Type.startswith("Action"): # Make sure the player has enough actions if currentTurn.ActionsLeft == 0: print "You don't have any actions left" else: currentTurn = self._applyCardEffects(card, currentTurn, player, game) else: currentTurn = self._applyCardEffects(card, currentTurn, player, game) player.discard(card) except IndexError, e: print "That isn't an option. Try again" print "Your turn is over" print "" player.discardHand() player.drawHand()