Esempio n. 1
0
 def resetHandState(self):
     self.winner = -1
     self.tripComplete = False
     self.delayedAction = False
     self.extension = False
     self.discardPile = []
     if len(self.players) == 4:
         self.target = 1000
         self.extensionPossible = False
     else:
         self.target = 700
         self.extensionPossible = True
     self.deck = Deck()
     # No hands yet.
     for player in self.players:
         player.hand = []
     # Reset per-hand team variables
     for team in self.teams:
         team.reset()
Esempio n. 2
0
    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()