def __init__(self, hand, player): self.hand = hand self.player = player self.nCards = len(hand) if self.nCards <= 3: self.type = 1 elif self.nCards == 4: if gameLogic.isFourOfAKind(hand): self.type = 2 else: self.type = 1 elif self.nCards == 5: if gameLogic.isStraight(hand): if gameLogic.isFlush(hand): self.type = 4 else: self.type = 1 elif gameLogic.isFlush(hand): self.type = 2 else: self.type = 3
def fiveCardOptions(handOptions, prevHand=[], prevType=0): #prevType = 0 - no hand, you have control and can play any 5 card # = 1 - straight # = 2 - flush # = 3 - full house # = 4 - straight flush validInds = np.zeros((nActions[4], ), dtype=int) c = 0 cardInds = np.zeros((5, ), dtype=int) #reuse #first deal with straights if prevType == 2 or prevType == 3 or prevType == 4: pass else: if len(handOptions.straights) > 0: for straight in handOptions.straights: nC = straight.size for i1 in range(nC - 4): #first index of hand val1 = handOptions.cards[straight[i1]].value cardInds[0] = handOptions.cards[straight[i1]].indexInHand for i2 in range(i1 + 1, nC - 3): val2 = handOptions.cards[straight[i2]].value if val1 == val2: continue if val2 > val1 + 1: break cardInds[1] = handOptions.cards[ straight[i2]].indexInHand for i3 in range(i2 + 1, nC - 2): val3 = handOptions.cards[straight[i3]].value if val3 == val2: continue if val3 > val2 + 1: break cardInds[2] = handOptions.cards[ straight[i3]].indexInHand for i4 in range(i3 + 1, nC - 1): val4 = handOptions.cards[straight[i4]].value if val4 == val3: continue if val4 > val3 + 1: break cardInds[3] = handOptions.cards[ straight[i4]].indexInHand for i5 in range(i4 + 1, nC): val5 = handOptions.cards[ straight[i5]].value if val5 == val4: continue if val5 > val4 + 1: break #import pdb; pdb.set_trace() cardInds[4] = handOptions.cards[ straight[i5]].indexInHand if prevType == 1: if handOptions.cHand[ cardInds[4]] < prevHand[4]: continue validInds[c] = fiveCardIndices[ cardInds[0]][cardInds[1]][cardInds[2]][ cardInds[3]][cardInds[4]] c += 1 #now deal with flushes (easier) if prevType == 4: pass else: if len(handOptions.flushes) > 0: for flush in handOptions.flushes: #all combinations of flush are allowable nC = flush.size for i1 in range(nC - 4): cardInds[0] = handOptions.cards[flush[i1]].indexInHand for i2 in range(i1 + 1, nC - 3): cardInds[1] = handOptions.cards[flush[i2]].indexInHand for i3 in range(i2 + 1, nC - 2): cardInds[2] = handOptions.cards[ flush[i3]].indexInHand for i4 in range(i3 + 1, nC - 1): cardInds[3] = handOptions.cards[ flush[i4]].indexInHand for i5 in range(i4 + 1, nC): cardInds[4] = handOptions.cards[ flush[i5]].indexInHand if prevType == 2: if prevHand[4] > handOptions.cHand[ cardInds[4]]: handBeingPlayed = handOptions.cHand[ cardInds] if gameLogic.isStraight( handBeingPlayed ): #its a straight flush so wins pass else: continue if prevType == 3: #needs to be a straight flush to beat handBeingPlayed = handOptions.cHand[ cardInds] if gameLogic.isStraight( handBeingPlayed ): #its a straight flush so wins pass else: continue validInds[c] = fiveCardIndices[ cardInds[0]][cardInds[1]][cardInds[2]][ cardInds[3]][cardInds[4]] c += 1 #now deal with full houses if prevType == 4: pass else: if prevType == 3: threeVal = gameLogic.cardValue(prevHand[2]) nPairs = handOptions.nPairs nThree = handOptions.nThreeOfAKinds if nPairs > 0 and nThree > 0: for pair in handOptions.pairs: pVal = handOptions.cards[pair[0]].value for three in handOptions.threeOfAKinds: tVal = handOptions.cards[three[0]].value if tVal == pVal: continue if pVal > tVal: cardInds[0] = handOptions.cards[three[0]].indexInHand cardInds[1] = handOptions.cards[three[1]].indexInHand cardInds[2] = handOptions.cards[three[2]].indexInHand cardInds[3] = handOptions.cards[pair[0]].indexInHand cardInds[4] = handOptions.cards[pair[1]].indexInHand else: cardInds[0] = handOptions.cards[pair[0]].indexInHand cardInds[1] = handOptions.cards[pair[1]].indexInHand cardInds[2] = handOptions.cards[three[0]].indexInHand cardInds[3] = handOptions.cards[three[1]].indexInHand cardInds[4] = handOptions.cards[three[2]].indexInHand if prevType == 3: if threeVal > tVal: continue validInds[c] = fiveCardIndices[cardInds[0]][cardInds[1]][ cardInds[2]][cardInds[3]][cardInds[4]] c += 1 if c > 0: return validInds[0:c] else: return -1
def returnAvailableActions(self): currHand = self.currentHands[self.playersGo] availableActions = np.zeros((enumerateOptions.nActions[5] + 1, )) if self.control == 0: #allow pass action availableActions[enumerateOptions.passInd] = 1 prevHand = self.handsPlayed[self.goIndex - 1].hand nCardsToBeat = len(prevHand) if nCardsToBeat > 1: handOptions = gameLogic.handsAvailable(currHand) if nCardsToBeat == 1: options = enumerateOptions.oneCardOptions( currHand, prevHand, 1) elif nCardsToBeat == 2: options = enumerateOptions.twoCardOptions( handOptions, prevHand, 1) elif nCardsToBeat == 3: options = enumerateOptions.threeCardOptions( handOptions, prevHand, 1) elif nCardsToBeat == 4: if gameLogic.isFourOfAKind(prevHand): options = enumerateOptions.fourCardOptions( handOptions, prevHand, 2) else: options = enumerateOptions.fourCardOptions( handOptions, prevHand, 1) else: if gameLogic.isStraight(prevHand): if gameLogic.isFlush(prevHand): options = enumerateOptions.fiveCardOptions( handOptions, prevHand, 4) else: options = enumerateOptions.fiveCardOptions( handOptions, prevHand, 1) elif gameLogic.isFlush(prevHand): options = enumerateOptions.fiveCardOptions( handOptions, prevHand, 2) else: options = enumerateOptions.fiveCardOptions( handOptions, prevHand, 3) if isinstance(options, int): #no options - must pass return availableActions for option in options: index = enumerateOptions.getIndex(option, nCardsToBeat) availableActions[index] = 1 return availableActions else: #player has control. handOptions = gameLogic.handsAvailable(currHand) oneCardOptions = enumerateOptions.oneCardOptions(currHand) twoCardOptions = enumerateOptions.twoCardOptions(handOptions) threeCardOptions = enumerateOptions.threeCardOptions(handOptions) fourCardOptions = enumerateOptions.fourCardOptions(handOptions) fiveCardOptions = enumerateOptions.fiveCardOptions(handOptions) for option in oneCardOptions: index = enumerateOptions.getIndex(option, 1) availableActions[index] = 1 if not isinstance(twoCardOptions, int): for option in twoCardOptions: index = enumerateOptions.getIndex(option, 2) availableActions[index] = 1 if not isinstance(threeCardOptions, int): for option in threeCardOptions: index = enumerateOptions.getIndex(option, 3) availableActions[index] = 1 if not isinstance(fourCardOptions, int): for option in fourCardOptions: index = enumerateOptions.getIndex(option, 4) availableActions[index] = 1 if not isinstance(fiveCardOptions, int): for option in fiveCardOptions: index = enumerateOptions.getIndex(option, 5) availableActions[index] = 1 return availableActions
def randomOption(self): cHand = self.currentHands[self.playersGo] if self.control == 0: prevHand = self.handsPlayed[self.goIndex - 1].hand nCards = len(prevHand) if nCards > 1: handOptions = gameLogic.handsAvailable(cHand) if nCards == 1: options = enumerateOptions.oneCardOptions(cHand, prevHand, 1) elif nCards == 2: options = enumerateOptions.twoCardOptions( handOptions, prevHand, 1) elif nCards == 3: options = enumerateOptions.threeCardOptions( handOptions, prevHand, 1) elif nCards == 4: if gameLogic.isFourOfAKind(prevHand): options = enumerateOptions.fourCardOptions( handOptions, prevHand, 2) else: options = enumerateOptions.fourCardOptions( handOptions, prevHand, 1) else: if gameLogic.isStraight(prevHand): if gameLogic.isFlush(prevHand): options = enumerateOptions.fiveCardOptions( handOptions, prevHand, 4) else: options = enumerateOptions.fiveCardOptions( handOptions, prevHand, 1) elif gameLogic.isFlush(prevHand): options = enumerateOptions.fiveCardOptions( handOptions, prevHand, 2) else: options = enumerateOptions.fiveCardOptions( handOptions, prevHand, 3) if isinstance(options, int): nOptions = -1 else: nOptions = len(options) ind = random.randint(0, nOptions) if ind == nOptions or isinstance(options, int): return -1 #pass else: return (options[ind], nCards) else: #we have control - choose from any option handOptions = gameLogic.handsAvailable(cHand) oneCardOptions = enumerateOptions.oneCardOptions(cHand) twoCardOptions = enumerateOptions.twoCardOptions(handOptions) threeCardOptions = enumerateOptions.threeCardOptions(handOptions) fourCardOptions = enumerateOptions.fourCardOptions(handOptions) fiveCardOptions = enumerateOptions.fiveCardOptions(handOptions) if isinstance(oneCardOptions, int): n1 = 0 else: n1 = len(oneCardOptions) if isinstance(twoCardOptions, int): n2 = 0 else: n2 = len(twoCardOptions) if isinstance(threeCardOptions, int): n3 = 0 else: n3 = len(threeCardOptions) if isinstance(fourCardOptions, int): n4 = 0 else: n4 = len(fourCardOptions) if isinstance(fiveCardOptions, int): n5 = 0 else: n5 = len(fiveCardOptions) nTot = n1 + n2 + n3 + n4 + n5 ind = random.randint(0, nTot - 1) if ind < n1: return (oneCardOptions[ind], 1) elif ind < (n1 + n2): return (twoCardOptions[ind - n1], 2) elif ind < (n1 + n2 + n3): return (threeCardOptions[ind - n1 - n2], 3) elif ind < (n1 + n2 + n3 + n4): return (fourCardOptions[ind - n1 - n2 - n3], 4) else: return (fiveCardOptions[ind - n1 - n2 - n3 - n4], 5)
def updateNeuralNetworkInputs(self, prevHand, cPlayer): self.fillNeuralNetworkHand(cPlayer) nPlayer = cPlayer - 1 if nPlayer == 0: nPlayer = 4 nnPlayer = nPlayer - 1 if nnPlayer == 0: nnPlayer = 4 nnnPlayer = nnPlayer - 1 if nnnPlayer == 0: nnnPlayer = 4 nCards = self.currentHands[cPlayer].size cardsOfNote = np.intersect1d(prevHand, np.arange(45, 53)) nPlayerInd = 22 * 13 nnPlayerInd = nPlayerInd + 27 nnnPlayerInd = nnPlayerInd + 27 #next player self.neuralNetworkInputs[nPlayer][nPlayerInd:(nPlayerInd + 13)] = 0 self.neuralNetworkInputs[nPlayer][nPlayerInd + nCards - 1] = 1 #number of cards #next next player self.neuralNetworkInputs[nnPlayer][nnPlayerInd:(nnPlayerInd + 13)] = 0 self.neuralNetworkInputs[nnPlayer][nnPlayerInd + nCards - 1] = 1 #next next next player self.neuralNetworkInputs[nnnPlayer][nnnPlayerInd:(nnnPlayerInd + 13)] = 0 self.neuralNetworkInputs[nnnPlayer][nnnPlayerInd + nCards - 1] = 1 for val in cardsOfNote: self.neuralNetworkInputs[nPlayer][nPlayerInd + 13 + (val - 45)] = 1 self.neuralNetworkInputs[nnPlayer][nnPlayerInd + 13 + (val - 45)] = 1 self.neuralNetworkInputs[nnnPlayer][nnnPlayerInd + 13 + (val - 45)] = 1 #prevHand phInd = nnnPlayerInd + 27 + 16 self.neuralNetworkInputs[nPlayer][phInd:] = 0 self.neuralNetworkInputs[nnPlayer][phInd:] = 0 self.neuralNetworkInputs[nnnPlayer][phInd:] = 0 self.neuralNetworkInputs[cPlayer][phInd:] = 0 nCards = prevHand.size if nCards == 2: self.neuralNetworkInputs[nPlayer][nPlayerInd + 21] = 1 self.neuralNetworkInputs[nnPlayer][nnPlayerInd + 21] = 1 self.neuralNetworkInputs[nnnPlayer][nnnPlayerInd + 21] = 1 value = int(gameLogic.cardValue(prevHand[1])) suit = prevHand[1] % 4 self.neuralNetworkInputs[nPlayer][phInd + 19] = 1 self.neuralNetworkInputs[nnPlayer][phInd + 19] = 1 self.neuralNetworkInputs[nnnPlayer][phInd + 19] = 1 elif nCards == 3: self.neuralNetworkInputs[nPlayer][nPlayerInd + 22] = 1 self.neuralNetworkInputs[nnPlayer][nnPlayerInd + 22] = 1 self.neuralNetworkInputs[nnnPlayer][nnnPlayerInd + 22] = 1 value = int(gameLogic.cardValue(prevHand[2])) suit = prevHand[2] % 4 self.neuralNetworkInputs[nPlayer][phInd + 20] = 1 self.neuralNetworkInputs[nnPlayer][phInd + 20] = 1 self.neuralNetworkInputs[nnnPlayer][phInd + 20] = 1 elif nCards == 4: self.neuralNetworkInputs[nPlayer][nPlayerInd + 23] = 1 self.neuralNetworkInputs[nnPlayer][nnPlayerInd + 23] = 1 self.neuralNetworkInputs[nnnPlayer][nnnPlayerInd + 23] = 1 value = int(gameLogic.cardValue(prevHand[3])) suit = prevHand[3] % 4 if gameLogic.isTwoPair(prevHand): self.neuralNetworkInputs[nPlayer][phInd + 21] = 1 self.neuralNetworkInputs[nnPlayer][phInd + 21] = 1 self.neuralNetworkInputs[nnnPlayer][phInd + 21] = 1 else: self.neuralNetworkInputs[nPlayer][phInd + 22] = 1 self.neuralNetworkInputs[nnPlayer][phInd + 22] = 1 self.neuralNetworkInputs[nnnPlayer][phInd + 22] = 1 elif nCards == 5: #import pdb; pdb.set_trace() if gameLogic.isStraight(prevHand): self.neuralNetworkInputs[nPlayer][nPlayerInd + 24] = 1 self.neuralNetworkInputs[nnPlayer][nnPlayerInd + 24] = 1 self.neuralNetworkInputs[nnnPlayer][nnnPlayerInd + 24] = 1 value = int(gameLogic.cardValue(prevHand[4])) suit = prevHand[4] % 4 self.neuralNetworkInputs[nPlayer][phInd + 23] = 1 self.neuralNetworkInputs[nnPlayer][phInd + 23] = 1 self.neuralNetworkInputs[nnnPlayer][phInd + 23] = 1 if gameLogic.isFlush(prevHand): self.neuralNetworkInputs[nPlayer][nPlayerInd + 25] = 1 self.neuralNetworkInputs[nnPlayer][nnPlayerInd + 25] = 1 self.neuralNetworkInputs[nnnPlayer][nnnPlayerInd + 25] = 1 value = int(gameLogic.cardValue(prevHand[4])) suit = prevHand[4] % 4 self.neuralNetworkInputs[nPlayer][phInd + 24] = 1 self.neuralNetworkInputs[nnPlayer][phInd + 24] = 1 self.neuralNetworkInputs[nnnPlayer][phInd + 24] = 1 elif gameLogic.isFullHouse(prevHand): self.neuralNetworkInputs[nPlayer][nPlayerInd + 26] = 1 self.neuralNetworkInputs[nnPlayer][nnPlayerInd + 26] = 1 self.neuralNetworkInputs[nnnPlayer][nnnPlayerInd + 26] = 1 value = int(gameLogic.cardValue(prevHand[2])) suit = -1 self.neuralNetworkInputs[nPlayer][phInd + 25] = 1 self.neuralNetworkInputs[nnPlayer][phInd + 25] = 1 self.neuralNetworkInputs[nnnPlayer][phInd + 25] = 1 else: value = int(gameLogic.cardValue(prevHand[0])) suit = prevHand[0] % 4 self.neuralNetworkInputs[nPlayer][phInd + 18] = 1 self.neuralNetworkInputs[nnPlayer][phInd + 18] = 1 self.neuralNetworkInputs[nnnPlayer][phInd + 18] = 1 self.neuralNetworkInputs[nPlayer][phInd + value - 1] = 1 self.neuralNetworkInputs[nnPlayer][phInd + value - 1] = 1 self.neuralNetworkInputs[nnnPlayer][phInd + value - 1] = 1 if suit == 1: self.neuralNetworkInputs[nPlayer][phInd + 13] = 1 self.neuralNetworkInputs[nnPlayer][phInd + 13] = 1 self.neuralNetworkInputs[nnnPlayer][phInd + 13] = 1 elif suit == 2: self.neuralNetworkInputs[nPlayer][phInd + 14] = 1 self.neuralNetworkInputs[nnPlayer][phInd + 14] = 1 self.neuralNetworkInputs[nnnPlayer][phInd + 14] = 1 elif suit == 3: self.neuralNetworkInputs[nPlayer][phInd + 15] = 1 self.neuralNetworkInputs[nnPlayer][phInd + 15] = 1 self.neuralNetworkInputs[nnnPlayer][phInd + 15] = 1 elif suit == 0: self.neuralNetworkInputs[nPlayer][phInd + 16] = 1 self.neuralNetworkInputs[nnPlayer][phInd + 16] = 1 self.neuralNetworkInputs[nnnPlayer][phInd + 16] = 1 #general - common to all hands. cardsRecord = np.intersect1d(prevHand, np.arange(37, 53)) endInd = nnnPlayerInd + 27 for val in cardsRecord: self.neuralNetworkInputs[1][endInd + (val - 37)] = 1 self.neuralNetworkInputs[2][endInd + (val - 37)] = 1 self.neuralNetworkInputs[3][endInd + (val - 37)] = 1 self.neuralNetworkInputs[4][endInd + (val - 37)] = 1 #no passes. self.neuralNetworkInputs[nPlayer][phInd + 26] = 1 self.neuralNetworkInputs[nnPlayer][phInd + 26] = 1 self.neuralNetworkInputs[nnnPlayer][phInd + 26] = 1 self.neuralNetworkInputs[nPlayer][phInd + 27:] = 0 self.neuralNetworkInputs[nnPlayer][phInd + 27:] = 0 self.neuralNetworkInputs[nnnPlayer][phInd + 27:] = 0