def handStrength(cardsOnBoardObjects, cardObject1, cardObject2, cards, evaluator): ahead, tied, behind = 0, 0, 0 ourRank = evaluate(evaluator, cardsOnBoardObjects, cardObject1, cardObject2) possibilitiesForOpponents = getAllPairsFromCards(cards) for opponentCard1, opponentCard2 in possibilitiesForOpponents: oppRank = evaluate(evaluator, cardsOnBoardObjects, opponentCard1, opponentCard2) if ourRank < oppRank: ahead += 1 elif ourRank == oppRank: tied += 1 else: behind += 1 handstrength = (ahead + tied / 2) / float(ahead + tied + behind) return handstrength
def worker(evaluator, cardsOnBoardObjects ,turnCard, riverCard, cardObject1, cardObject2, opponentCard1, opponentCard2, currentIndex, return_dict): '''worker function''' opponentRankHere = evaluate(evaluator, cardsOnBoardObjects + [turnCard, riverCard], opponentCard1, opponentCard2) # opponentRankHere = evaluate(cardsOnBoardObjects + [turnCard], opponentCard1, opponentCard2) myRankHere = evaluate(evaluator, cardsOnBoardObjects + [turnCard, riverCard], cardObject1, cardObject2) if myRankHere < opponentRankHere: return_dict[currentIndex]["ahead"] += 1 elif myRankHere == opponentRankHere: return_dict[currentIndex]["tied"] += 1 else: return_dict[currentIndex]["behind"] += 1 print return_dict
def declare_action(self, valid_actions, hole_card, round_state): choice = self.__choice_action(valid_actions) action = choice["action"] amount = choice["amount"] if action == "raise": amountToRaise = 40 amount = amountToRaise #rand.randrange(amount["min"], max(amount["min"], amount["max"]) + 1) opponentAgressivnessScore = opponentAgressivness(self) opponentAgressivnessScoreLast7Round = opponentAgressivness(self, 2) card1InString, card2InString = hole_card[0], hole_card[1] cardObject1 = cardToCardObject(card1InString) cardObject2 = cardToCardObject(card2InString) pot, levelOfBetting, cardsOnBoard, roundCount, isOnSmallblind, isOnBigBlind, smallBlindAmount, bigBlindAmount, stackOfPlayer, stackOfOpponent = extractInfoFromState(self.uuid, round_state) cardsOnBoardObjects = getBoardToCardsObject(cardsOnBoard) allCardsObjectOnBoard = cardsOnBoardObjects[:2 + levelOfBetting - 1] evaluator = Evaluator() if len(cardsOnBoardObjects) > 0: # if there is any card on board - so after flop every case handStrengthLibrary = evaluate(evaluator, allCardsObjectOnBoard, cardObject1, cardObject2) else: # preFLop handStrengthLibrary = 0 sklanskyClass = getSklanskyClass(card1InString, card2InString) cards = getAllCards() cards.remove(cardObject1) cards.remove(cardObject2) for boardCard in cardsOnBoardObjects: cards.remove(boardCard) evaluator = Evaluator() if len(cardsOnBoardObjects) > 0: HS = handStrength(cardsOnBoardObjects, cardObject1, cardObject2, cards, evaluator) else: HS = 0 EHS, PPOT, NPOT = 0,0,0 # if len(cardsOnBoardObjects) > 0: # if there is any card on board - so after flop every case # EHS, HS, PPOT, NPOT = effectiveHandStrength(cardsOnBoardObjects, handStrength, cardObject1, cardObject2) # else: # EHS, HS, PPOT, NPOT = 0, 0, 0, 0 # handle -BigBlind and smallBlind if levelOfBetting == 1 and action != "fold": if isOnSmallblind: realAmountToPlay = amount - smallBlindAmount elif isOnBigBlind: realAmountToPlay = amount - bigBlindAmount else: # no blind realAmountToPlay = amount else: realAmountToPlay = amount addRowToHistory(self.historyDF, [self.hashOfGame, self.uuid, hole_card[0], hole_card[1], action, realAmountToPlay, pot, levelOfBetting, cardsOnBoard, roundCount, isOnSmallblind, isOnBigBlind , smallBlindAmount, bigBlindAmount, stackOfPlayer, stackOfOpponent, sklanskyClass, EHS, HS, PPOT, NPOT, handStrengthLibrary, opponentAgressivnessScore, opponentAgressivnessScoreLast7Round]) return action, amount
def declare_action(self, valid_actions, hole_card, round_state): # valid_actions format => [raise_action_info, call_action_info, fold_action_info] # print valid_actions opponentAgressivnessScore = opponentAgressivness(self) opponentAgressivnessScoreLast7Round = opponentAgressivness(self, 7) card1InString, card2InString = hole_card[0], hole_card[1] cardObject1 = cardToCardObject(card1InString) cardObject2 = cardToCardObject(card2InString) call_action_info = valid_actions[1] action = call_action_info["action"] amount = call_action_info["amount"] pot, levelOfBetting, cardsOnBoard, roundCount, isOnSmallblind, isOnBigBlind, smallBlindAmount, bigBlindAmount, stackOfPlayer, stackOfOpponent = extractInfoFromState( self.uuid, round_state) cardsOnBoardObjects = getBoardToCardsObject(cardsOnBoard) allCardsObjectOnBoard = cardsOnBoardObjects[:2 + levelOfBetting - 1] evaluator = Evaluator() print cardsOnBoardObjects if len( cardsOnBoardObjects ) > 0: # if there is any card on board - so after flop every case handStrength = evaluate(evaluator, allCardsObjectOnBoard, cardObject1, cardObject2) else: # preFLop handStrength = 0 sklanskyClass = getSklanskyClass(card1InString, card2InString) if len( cardsOnBoardObjects ) > 0: # if there is any card on board - so after flop every case EHS, HS, PPOT, NPOT = effectiveHandStrength( cardsOnBoardObjects, handStrength, cardObject1, cardObject2) else: EHS, HS, PPOT, NPOT = 0, 0, 0, 0 #handle -BigBlind and smallBlind if levelOfBetting == 1: if isOnSmallblind: realAmountToPlay = amount - smallBlindAmount elif isOnBigBlind: realAmountToPlay = amount - bigBlindAmount else: #no blind realAmountToPlay = amount else: realAmountToPlay = amount addRowToHistory(self.historyDF, [ self.hashOfGame, self.uuid, hole_card[0], hole_card[1], action, realAmountToPlay, pot, levelOfBetting, cardsOnBoard, roundCount, isOnSmallblind, isOnBigBlind, smallBlindAmount, bigBlindAmount, stackOfPlayer, stackOfOpponent, sklanskyClass, EHS, HS, PPOT, NPOT, handStrength, opponentAgressivnessScore, opponentAgressivnessScoreLast7Round ]) # print(self.historyDF) return action, amount # action returned here is sent to the poker engine
def effectiveHandStrength(cardsOnBoardObjects, currentHandStrength, cardObject1, cardObject2): ## https://en.wikipedia.org/wiki/Poker_Effective_Hand_Strength_(EHS)_algorithm #TODO # 1, potencial extremne dobrych kariet (? x < 500) TODO # 2, potencial extremne zlych kariet (? x > 6000) TODO # 3, potencial na turne TODO # 4, celkovy potencial => zaklad EHS DONE # 5, NPOT is the Negative POTential DONE # 6, PPOT is the Positive POTential DONE # 7, HS is the current Hand Strength DONE # 8, potential before FLOP TODO # 10, myRank = currentHandStrength forbiddenCardInSimulation = [cardObject1] + [cardObject2] + cardsOnBoardObjects if cardsOnBoardObjects[0] is not None: # if there is any card on board - so after flop every case HAND_POTENTIAL_TOTAL = {"ahead": 0, "tied": 0, "behind": 0} HAND_POTENTIAL = { "ahead": {"ahead": 0, "tied": 0, "behind": 0}, "tied": {"ahead": 0, "tied": 0, "behind": 0}, "behind": {"ahead": 0, "tied": 0, "behind": 0}, } STR_RANKS = ["2", "3", "4", "5", "6", "7", "8"]# "9", "T", "J", "Q", "K", "A"] colors = ["c", "d", "s", "h"]#, "s", ] cards = getAllCards() cards.remove(cardObject1) cards.remove(cardObject2) for boardCard in cardsOnBoardObjects: cards.remove(boardCard) evaluator = Evaluator() HS = handStrength(cardsOnBoardObjects, cardObject1, cardObject2, cards, evaluator) start = time.clock() alreadyComputedOpponentsCardsHashes = [] wasAlreadyTurn = len(cardsOnBoardObjects) == 4 wasAlreadyRiver = len(cardsOnBoardObjects) == 5 if wasAlreadyRiver: return HS, HS, HS, HS # from multiprocessing import Manager, Process, Pool # import multiprocessing # multiprocessing.freeze_support() # manager = Manager() # return_dict = manager.dict(HAND_POTENTIAL) # jobs = [] # pool = Pool(processes=4) # mgr = Manager() # return_dict = mgr.dict() possibilitiesForOpponents = getAllPairsFromCards(cards) for opponentCard1, opponentCard2 in possibilitiesForOpponents: hashOfCards = hash(str(cardObject1) + str(cardObject2)) hashOfCardsOpponents = hash(str(opponentCard1) + str(opponentCard2))# + str(isSameColorOfCardsOpponent)) hashOfCardsReverseOrderOpponents = hash(str(opponentCard2) + str(opponentCard1))# + str(isSameColorOfCardsOpponent)) # Card.print_pretty_cards([cardObject2, cardObject1] + cardsOnBoardObjects) # Card.print_pretty_cards([opponentCard1, cardObject2]) goFurther = opponentCard1 not in forbiddenCardInSimulation \ and opponentCard2 not in forbiddenCardInSimulation\ and opponentCard1 != opponentCard2 \ and hashOfCardsReverseOrderOpponents not in alreadyComputedOpponentsCardsHashes \ and hashOfCardsOpponents not in alreadyComputedOpponentsCardsHashes if goFurther: alreadyComputedOpponentsCardsHashes.append(hashOfCardsOpponents) alreadyComputedOpponentsCardsHashes.append(hashOfCardsReverseOrderOpponents) opponentRank = evaluate(evaluator, cardsOnBoardObjects, opponentCard1, opponentCard2) if myRank < opponentRank: index = "ahead" elif myRank == opponentRank: index = "tied" else: index = "behind" HAND_POTENTIAL_TOTAL[index] += 1 if not wasAlreadyTurn: for turnCard, riverCard in possibilitiesForOpponents: if turnCard == opponentCard1 or turnCard == opponentCard2 or riverCard == opponentCard1 or riverCard == opponentCard2: continue # Card.print_pretty_cards(cardsOnBoardObjects + [turnCard, riverCard] + [opponentCard1, opponentCard2]) opponentRankHere = evaluate(evaluator, cardsOnBoardObjects + [turnCard, riverCard], opponentCard1, opponentCard2) # opponentRankHere = evaluate(cardsOnBoardObjects + [turnCard], opponentCard1, opponentCard2) # Card.print_pretty_cards(cardsOnBoardObjects + [turnCard, riverCard] + [cardObject1, cardObject2]) myRankHere = evaluate(evaluator, cardsOnBoardObjects + [turnCard, riverCard], cardObject1, cardObject2) if myRankHere < opponentRankHere: HAND_POTENTIAL[index]["ahead"] += 1 elif myRankHere == opponentRankHere: HAND_POTENTIAL[index]["tied"] += 1 else: HAND_POTENTIAL[index]["behind"] += 1 HAND_POTENTIAL_TOTAL[index] += 1 else: #just river card for riverCard in cards: if riverCard == opponentCard1 or riverCard == opponentCard2: continue myRankHere = evaluate(cardsOnBoardObjects + [riverCard], cardObject1, cardObject2) opponentRankHere = evaluate(cardsOnBoardObjects + [riverCard], opponentCard1, opponentCard2) if myRankHere < opponentRankHere: HAND_POTENTIAL[index]["ahead"] += 1 elif myRankHere == opponentRankHere: HAND_POTENTIAL[index]["tied"] += 1 else: HAND_POTENTIAL[index]["behind"] += 1 HAND_POTENTIAL_TOTAL[index] += 1 # pool.map(worker, (evaluator, cardsOnBoardObjects ,turnCard, riverCard, cardObject1, cardObject2, opponentCard1, # opponentCard2, index, return_dict)) # # p = Process(target=worker, args=(evaluator, cardsOnBoardObjects ,turnCard, riverCard, cardObject1, cardObject2, opponentCard1, # # opponentCard2, index, return_dict)) # # jobs.append(p) # # p.start() # # # # # end = time.clock() # # print end - start, "TIME" # # print "1", cardsOnBoardObjects + [turnCard, riverCard], opponentCard1, opponentCard2 # # print "2", cardsOnBoardObjects + [turnCard, riverCard], cardObject1, cardObject2 # # # Mark pool as closed -- no more tasks can be added. # pool.close() # print "pool to run" # # Wait for tasks to exit # pool.join() # print return_dict # exit() # print("jobs are inn") # for proc in jobs: # proc.join() # print return_dict # exit() # print HAND_POTENTIAL print time.clock() - start, "TIME" print("karty") Card.print_pretty_cards([cardObject2, cardObject1] ) print "board" Card.print_pretty_cards(cardsOnBoardObjects) HP = HAND_POTENTIAL PPOT = (HP["behind"]["ahead"] + HP["behind"]["tied"] / 2 + HP["tied"]["ahead"] / 2) / float(HAND_POTENTIAL_TOTAL["behind"] + HAND_POTENTIAL_TOTAL["tied"]) NPOT = (HP["ahead"]["behind"] + HP["tied"]["behind"] / 2 + HP["ahead"]["tied"] / 2) / float(HAND_POTENTIAL_TOTAL["ahead"] + HAND_POTENTIAL_TOTAL["tied"]) print HP["ahead"]["behind"] , HP["tied"]["behind"] , HP["ahead"]["tied"] ,HAND_POTENTIAL_TOTAL["ahead"] , HAND_POTENTIAL_TOTAL["tied"] print "NPOT", NPOT print "PPOT", PPOT print "HS", HS EHS = HS*(1 - NPOT) + (1 - HS)*PPOT print "EHS", EHS return EHS, HS, PPOT, NPOT