def makeMove(self, node): """The makeMove method that overrides the agent base class's makeMove; simply asks the user to input a valid action. :node: The current state at which we're moving. :returns: A (numCards, whichCard) tuple representing the action. """ if self.idx in node.finished: print( 'Congratulations, you\'re out of cards and were number {} ' 'to finish!'.format(node.finished.index(self.idx) + 1)) return (0, -1) print 'Cards left for each player: {}'.format(node.numRemaining) print 'Your hand: {}'.format( map(lambda c: cards.cardRepr[c], cards.cardDictToList(self.hand))) if node.topCard: topN, topC = node.topCard topStr = '{} {}'.format(topN, cards.cardRepr[topC]) else: topStr = 'Empty' print 'Top card: {}'.format(topStr) legalActions = self.getAllActions(node) print 'Valid actions: {}'.format( ['{} {}'.format(n, cards.cardRepr[c]) for n, c in legalActions]) mnAgent = maxN.MaxNAgent(self.idx, self.hand) mnN, mnC = mnAgent.makeMove(node) print 'The Max^n agent recommends playing {} {}'.format( mnN, cards.cardRepr[mnC]) action = None while action not in legalActions: actStr = raw_input( 'Your move. Input as a space-separated tuple of numCards and ' 'whichCard representing the action (that is, ' '"numCards whichCard" without the quotes). To pass, enter ' '"0 P", "P", or "pass". So, what\'s your move? ') if actStr.lower() in ('pass', 'p', '0p'): action = (0, -1) else: try: numStr, cardStr = actStr.split() numCards = int(numStr) encodedNum = cards.cardRepr.index(cardStr.upper()) except Exception: continue # if number not valid, continue the loop again if encodedNum == -1: continue whichCard = int(encodedNum) action = (numCards, whichCard) return action
def newGame(self, ordering): deck = cards.allCards() hands = cards.dealHands(deck, 52/self.numPlayers) for i in xrange(self.numPlayers): self.agents[i].hand = hands[i] # swap cards if ordering: for i in xrange(2): high = ordering.index(i) low = ordering.index(self.numPlayers - i - 1) cards.swapCards(hands[high], hands[low], 2 - i) threes = collections.Counter() for p, hand in enumerate(hands): threes[p] += hand[0] whosTurn = np.random.choice(cards.cardDictToList(threes)) playedCards = [cards.noCards() for i in xrange(self.numPlayers)] self.initialState = state.State(playedCards, whosTurn, None, None, [])
def __init__(self, agents, hands=None, playedCards=None, whosTurn=None, topCard=None, lastPlayed=None, finished=[]): """Initializes the game with the agents listed as the players. :agents: Either a list of agent objects or a list of agent constructors. If constructors, the dealing and ID assignment will be done here; if objects, we simply assign the list to self.agents. :hands: list of hand dicts; if supplied, will be used as the hands for the agents. :playedCards: list of played card dicts; if supplied, will be used for the playedCards for the initialState. :whosTurn: index of agent whose turn it is; if supplied, the game will start with this player. topCard, lastPlayed, and finished are all parameters passed on to the initial state; for more information, see state.State's __init__. """ self.numPlayers = len(agents) if hands is None: deck = cards.allCards() hands = cards.dealHands(deck, 52/self.numPlayers) # if agents is a list of agent objects, set that to self.agents if all(isinstance(a, agent.Agent) for a in agents): self.agents = agents # otherwise, construct the agents from the list of agent constructors else: self.agents = [agentConstructor(i, hand) for i, (agentConstructor,hand) in enumerate(zip(agents, hands))] if whosTurn is None: # randomly choose a player with a 3 to start # (proportional to how many 3's they have) threes = collections.Counter() for p, hand in enumerate(hands): threes[p] += hand[0] whosTurn = np.random.choice(cards.cardDictToList(threes)) if playedCards is None: playedCards = [cards.noCards() for i in xrange(self.numPlayers)] self.initialState = state.State(playedCards, whosTurn, topCard, lastPlayed, finished)
import copy import cards, game import humanAgent, maxN, dummyAgent gm = game.Game([humanAgent.HumanAgent] + [maxN.MaxNAgent for i in xrange(3)]) origHands = [copy.deepcopy(agent.hand) for agent in gm.agents] print 'Your hand:', map(lambda c: cards.cardRepr[c], cards.cardDictToList(origHands[0])) print gm.playGame(verbose=True) print 'Original hands and strengths:' for hand in origHands: print sum(k * v for k, v in hand.items()), print hand
import copy import cards, game import humanAgent, maxN, dummyAgent gm = game.Game([humanAgent.HumanAgent] + [maxN.MaxNAgent for i in xrange(3)]) origHands = [copy.deepcopy(agent.hand) for agent in gm.agents] print 'Your hand:', map(lambda c: cards.cardRepr[c], cards.cardDictToList(origHands[0])) print gm.playGame(verbose=True) print 'Original hands and strengths:' for hand in origHands: print sum(k*v for k, v in hand.items()), print hand