def scoreHands(self): for i in range(self.dealer + 1, self.dealer + 1 + len(self.players)): if self.checkWin(): break player = self.players[i % len(self.players)] score = getScore(player.hand, self.starter, self.verbose) player.pips += score if self.verbose: print("Scoring {}'s hand: ".format(player.getName()) + cardsString(player.hand) + " + " + str(self.starter)) print("\t{}'s hand scored {}".format(player.getName(), score)) if not (self.checkWin()): cribScore = getScore(self.crib, self.starter, self.verbose) self.players[self.dealer].pips += cribScore if self.verbose: print("In {}'s crib: ".format(self.players[ self.dealer].getName()) + cardsString(self.crib) + " + " + str(self.starter)) print("{} scored {} in the crib!\n\n".format( self.players[self.dealer].getName(), cribScore)) for player in self.players: player.learnFromHandScores([ getScore(self.players[0].hand, self.starter, False), getScore(self.players[1].hand, self.starter, False), getScore(self.crib, self.starter, False) ], self.gameState())
def show(self): print('{}:'.format(self.getName())) print('Hand:' + cardsString(sorted(self.playhand))) print('Crib throw:' + cardsString(self.cribThrow))
def throwCribCards(self, numCards, gameState): cribCards = [] self.cribThrow = [] cardIndices = list(range(0, len(self.hand))) maxValue = -np.inf if gameState['dealer'] == self.number - 1: dealerFlag = 1 else: dealerFlag = 0 self.throwString = "DeepPeg ({}) is considering a hand of: {}".format( self.number, cardsString(self.hand)) if dealerFlag == 1: self.throwString = "{}. Own crib.\n".format(self.throwString) else: self.throwString = "{}. Opponent's crib.\n".format( self.throwString) self.opponentEstimate = np.ones(13) for i in range(len(self.hand)): idx = self.hand[i].getRank().value - 1 self.opponentEstimate[idx] = self.opponentEstimate[idx] - 0.25 for combination in combinations(cardIndices, len(self.hand) - numCards): handCards = [] thrownCards = [] for i in range(0, len(cardIndices)): if i in combination: handCards.append(Card(self.hand[i].rank, self.hand[i].suit)) else: thrownCards.append( Card(self.hand[i].rank, self.hand[i].suit)) q = 0 possibleThrows = list(self.cribCardCombinations.keys()) possibleThrows.remove("00") throwProbabilities = list(self.cribCardCombinations.values()) throwProbabilities.remove(self.cribCardCombinations["00"]) if not throwProbabilities: possibleThrows = [ "AA", "22", "33", "44", "55", "66", "77", "88", "99", "TT", "JJ", "QQ", "KK" ] throwProbabilities = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] throwProbabilities = throwProbabilities / np.sum( throwProbabilities) for i in range(self.numSims): oppThrow = np.random.choice(possibleThrows, p=throwProbabilities) oppCards = [] for j in range(2): cardRank = oppThrow[j:j + 1] if cardRank == "A": cardRank = 1 elif cardRank == "T": cardRank = 10 elif cardRank == "J": cardRank = 11 elif cardRank == "Q": cardRank = 12 elif cardRank == "K": cardRank = 13 oppCards.append( Card(int(cardRank), np.random.randint(1, 5))) q = q + self.throwValue( self.getThrowingFeatures(handCards, thrownCards, oppCards, dealerFlag)) self.throwString = "{}\t{}: {}\n".format(self.throwString, cardsString(thrownCards), q) if q > maxValue: maxValue = q cribCards = [] cribCards.append(thrownCards.pop()) cribCards.append(thrownCards.pop()) self.throwString = "{}I chose to throw: {}\n\n".format( self.throwString, cardsString(cribCards)) for i in range(0, len(cribCards)): for j in range(0, len(self.hand)): if cribCards[i].isIdentical(self.hand[j]): self.cribThrow.append( Card(self.hand[j].rank, self.hand[j].suit)) self.hand.pop(j) break if (self.verbose): print("{} threw {} cards into the crib".format( self.getName(), numCards)) super().createPlayHand() # Note which cards have been thrown into the crib cardKeys = list() for i in range(2): cardKeys.append(str(cribCards[i].getRank().value)) if cardKeys[i] == '1': cardKeys[i] = 'A' elif cardKeys[i] == '10': cardKeys[i] = 'T' elif cardKeys[i] == '11': cardKeys[i] = 'J' elif cardKeys[i] == '12': cardKeys[i] = 'Q' elif cardKeys[i] == '13': cardKeys[i] = 'K' if cribCards[0].getRank().value <= cribCards[1].getRank().value: cardKey = cardKeys[0] + cardKeys[1] else: cardKey = cardKeys[1] + cardKeys[0] if cardKey in self.cribCardCombinations: self.cribCardCombinations[ cardKey] = self.cribCardCombinations[cardKey] + 1 else: self.cribCardCombinations[cardKey] = 1 self.cribCardCombinations["00"] = self.cribCardCombinations["00"] + 1 return cribCards
def throwCribCards(self, numCards, gameState): cribCards = [] cardScores = np.zeros(len(self.hand)) if gameState['dealer'] == (self.number - 1): dealerFlag = True else: dealerFlag = False self.throwString = "Myrmidon ({}) is considering a hand of: {}".format( self.number, cardsString(self.hand)) if dealerFlag: self.throwString = "{}. Own crib.\n".format(self.throwString) else: self.throwString = "{}. Opponent's crib.\n".format( self.throwString) # Score the cards that would be left in the player's hand for combination in combinations(self.hand, len(self.hand) - numCards): for i in range(0, self.numSims): starterCard = self.randomStarter() score = getScore(list(combination), starterCard, False) for j in range(0, len(self.hand)): if self.hand[j] in combination: cardScores[j] += score # Score the cards that would be thrown in the crib for combination in combinations(self.hand, numCards): for i in range(0, self.numSims): starterCard = self.randomStarter() score = getScore(list(combination), starterCard, False) for j in range(0, len(self.hand)): if self.hand[j] in combination: if dealerFlag: # We can worry less about keeping the card if it # will score points for us in the crib cardScores[j] -= score if self.hand[j].rank == 5: cardScores[j] += 2 else: # We should keep cards that will score points for # our opponents in the crib cardScores[j] += score for i in range(len(self.hand)): self.throwString = "{}\t{}: {}\n".format(self.throwString, str(self.hand[i]), cardScores[i]) # Pick the lowest scoring cards to throw for i in range(0, numCards): lowIndex = min(range(len(cardScores)), key=cardScores.__getitem__) cribCards.append(self.hand.pop(lowIndex)) cardScores = np.delete(cardScores, lowIndex) self.throwString = "{}I chose to throw: {}\n\n".format( self.throwString, cardsString(cribCards)) if (self.verbose): print("Myrmidon ({}) threw {} cards into the crib".format( self.number, numCards)) super().createPlayHand() self.cribThrow = cribCards return cribCards
def show(self): for player in self.players: print(player) print("Cut: " + str(self.starter)) print("Crib: " + cardsString(self.crib))
def play(self): if self.verbose: print("{} dealt this hand.".format(self.players[self.dealer].getName())) if not(self.critic is None): self.critic.playhand = [] for i in range(0,4): self.critic.playhand.append(self.players[0].playhand[i]) #self.players[0].show() #self.critic.show() self.playorder = [] # Starting player is not the dealer toPlay = (self.dealer + 1) % len(self.players) self.playorder = [] # as long as any player has cards in hand, and the game isn't over while (any(len(player.playhand) > 0 for player in self.players)) and (not (self.checkWin())): self.inplay = [] # those cards that affect the current count count = 0 # the current count goCounter = 0 # a counter for the number of consecutive "go"s while (count < 31) and (goCounter < 2) and (not (self.checkWin())): if self.verbose: print("It is {}'s turn. Score is ".format(self.players[toPlay].getName()) + self.scoreString()) # Call on agent to choose a card if toPlay == 0 and not(self.critic is None): criticCard = self.critic.playCard(self.gameState()) if not(criticCard is None): self.critic.playhand.append(criticCard) else: criticCard = None playedCard = self.players[toPlay].playCard(self.gameState()) if playedCard is None: if goCounter == 0: goCounter = 1 else: goCounter = 2 self.players[toPlay].pips += 1 if self.verbose: print("{} scores 1 for the go.\n".format(self.players[toPlay].getName())) else: if not(criticCard is None): if not(criticCard.isIdentical(playedCard)): self.players[0].explainPlay() self.critic.explainPlay() else: print("{} agrees with {}'s play.".format(self.critic.getName(),self.players[0].getName())) self.critic.removeCard(playedCard) count += playedCard.value() self.inplay.append(playedCard) self.playorder.append(playedCard) if self.verbose: print("\t{}: ".format(count) + cardsString(self.inplay)) self.players[toPlay].pips += scoreCards(self.inplay, self.verbose) goCounter = 0 toPlay = ((toPlay + 1) % len(self.players)) # Allow agent to learn from the previous round of plays self.players[toPlay].learnFromPegging(self.gameState()) if goCounter == 2: # A go has happened for player in self.players: player.go(self.gameState()) if count == 31: pass # A 31 has happened for player in self.players: player.thirtyOne(self.gameState()) if self.checkWin(): # Someone won for player in self.players: player.endOfGame(self.gameState()) if self.verbose: print('Game Over!')