def getReward(self, state, action, transitionState):
        isDone, isFirst, isDoubleDown, hasAce, hardCount, dealerSoftCount = transitionState
        multiplier = 2 if isDoubleDown else 1
        if hardCount > 21:
            return -2 * multiplier

        softCount = hardCount + 10 if hasAce and hardCount <= 11 else hardCount
        if isDone:
            if isFirst and softCount == 21:
                return multiplier
            # Simulate the dealer's actions
            dealerAgent = DealerAgent()
            dealerCardValue = dealerSoftCount - 1 if dealerSoftCount != 11 else 0
            card = Card(0, dealerCardValue)
            dealerHand = Hand(1)
            dealerHand.addCard(card)
            deck = Deck(1, 4, 13)
            dealerHand.addCard(deck.take())
            while dealerAgent.getNextAction(None, dealerHand) == Actions.HIT:
                dealerHand.addCard(deck.take())
            return (
                multiplier
                if softCount > dealerHand.getSoftCount()
                else (0 if softCount == dealerHand.getSoftCount() else -multiplier)
            )
        else:
            return 0
Exemplo n.º 2
0
 def test_4CardHand(self):
     hand = Hand(0)
     hand.addCard(Card(1, 6))
     hand.addCard(Card(1, 4))
     hand.addCard(Card(1, 2))
     hand.addCard(Card(1, 0))
     dealer = DealerAgent()
     self.assertEqual(Actions.HIT, dealer.getNextAction(None, hand))
Exemplo n.º 3
0
    def getReward(self, state, action, transitionState):
        isDone, isFirst, isDoubleDown, hasAce, hardCount, dealerSoftCount = transitionState
        multiplier = 2 if isDoubleDown else 1
        if hardCount > 21:
            return -2 * multiplier

        softCount = hardCount + 10 if hasAce and hardCount <= 11 else hardCount
        if isDone:
            if isFirst and softCount == 21:
                return multiplier
            # Simulate the dealer's actions
            dealerAgent = DealerAgent()
            dealerCardValue = dealerSoftCount - 1 if dealerSoftCount != 11 else 0
            card = Card(0, dealerCardValue)
            dealerHand = Hand(1)
            dealerHand.addCard(card)
            deck = Deck(1, 4, 13)
            dealerHand.addCard(deck.take())
            while dealerAgent.getNextAction(None, dealerHand) == Actions.HIT:
                dealerHand.addCard(deck.take())
            return multiplier if softCount > dealerHand.getSoftCount() else (
                0 if softCount == dealerHand.getSoftCount() else -multiplier)
        else:
            return 0
Exemplo n.º 4
0
        elif playerAgentString == "StandingAgent":
            playerAgents.append(StandingAgent())
        elif playerAgentString == "NoBustAgent":
            playerAgents.append(NoBustAgent())
        elif playerAgentString == "HumanAgent":
            playerAgents.append(HumanAgent())
        elif playerAgentString == "ValueIterationAgent":
            if not iterations:
                print "Number of iterations must be specified with ValueIterationAgent"
                sys.exit(1)
            playerAgents.append(ValueIterationAgent(iterations))
        else:
            print "Unrecognized agent {0}".format(playerAgentString)
            sys.exit(1)

    dealerAgent = DealerAgent()

    if trainingRounds > 0:
        trainingAgents = filter(lambda x: x.needsTraining(), playerAgents)
        print "Training ({0} rounds)...".format(trainingRounds)
        game = Game(dealerAgent, trainingAgents)
        game.executeGame(trainingRounds)

    for playerAgent in playerAgents:
        playerAgent.trainingOver()

    print "Testing ({0} rounds)...".format(realRounds)
    game = Game(dealerAgent, playerAgents)
    game.executeGame(realRounds)
    print game.resultString()
    for (playerAgentString, playerAgent) in zip(playerAgentStrings, playerAgents):
Exemplo n.º 5
0
 def test_Soft17(self):
     hand = Hand(0)
     hand.addCard(Card(1, 6))
     hand.addCard(Card(1, 0))
     dealer = DealerAgent()
     self.assertEqual(Actions.STAND, dealer.getNextAction(None, hand))