def makeMove(self, gameState): print 'Opponents:' for opponent in gameState.opponents: print opponent print 'Us:' print gameState.us print 'Cards left in deck: ' + str(gameState.cardsLeft) print 'Target: ' + str(gameState.target) + ' miles' print 'Your hand: ', print Cards.cardsToStrings(gameState.hand) print 'Valid moves:' for i in range(len(gameState.validMoves)): print str(i) + ':', print gameState.validMoves[i] moveint = -1 while moveint not in range(len(gameState.validMoves)): move = raw_input('Enter move index: ') try: moveint = int(move) except ValueError: moveint = -1 return gameState.validMoves[int(move)]
def playHand(self): self.resetHandState() # Fresh deck self.deck = Deck() # Deal hands for player in self.players: player.hand = self.draw(player, 6) if self.transcriptWriter: self.transcriptWriter.writeHandStart() currentPlayerNumber = 0 # Normally this is currentPlayerNumber + 1, but in the case of a coup # fourre it gets changed to give the player another turn nextPlayerNumber = 1 while True: currentPlayer = self.players[currentPlayerNumber] currentTeam = self.teams[currentPlayer.teamNumber] drewCardThisMove = None try: drewCardThisMove = self.draw(currentPlayer) currentPlayer.hand.append(drewCardThisMove) except IndexError: self.delayedAction = True if self.debug: print 'Hand contents:', print Cards.cardsToStrings(currentPlayer.hand) if len(currentPlayer.hand) == 0: totalHandSize = 0 for player in self.players: totalHandSize += len(player.hand) if totalHandSize == 0: # Everyone's out of cards - game over break else: # Pass and let someone else go currentPlayerNumber = nextPlayerNumber nextPlayerNumber += 1 if nextPlayerNumber >= len(self.players): nextPlayerNumber = 0 continue state = self.makeState(currentPlayer) state.findValidPlays() # Store a copy of this locally, just in case the AI changes it validMoves = state.validMoves move = currentPlayer.ai.makeMove(state) # Replace invalid moves with a random discard if move not in validMoves: print 'Warning: invalid play' move = Move(Move.DISCARD, currentPlayer.hand[0]) if self.debug: print currentPlayer, print move sanitizedPlayer = copy(currentPlayer) sanitizedPlayer.hand = [] sanitizedPlayer.ai = None self.notifyPlayers(sanitizedPlayer, move) oldTarget = self.target self.handleMove(currentPlayer, currentTeam, move) if self.transcriptWriter: extensionWasDeclared = self.target > oldTarget self.transcriptWriter.writeMove(currentPlayer.number, drewCardThisMove, move, extensionWasDeclared) if self.tripComplete: break # Remove the card from the player's hand del currentPlayer.hand[currentPlayer.hand.index(move.card)] if self.debug: for team in self.teams: print team print '' # Go on to the next player. If a safety got played, nextPlayerNumber # got changed and will cause us to break out of the normal rotation currentPlayerNumber = nextPlayerNumber nextPlayerNumber += 1 if nextPlayerNumber >= len(self.players): nextPlayerNumber = 0 if self.debug: print 'Hand complete' if self.transcriptWriter: self.transcriptWriter.writeHandEnd() self.computeHandScores()