def hardcode_handstrength(x): """input shape: (b,m,18)""" B, M, C = x.size() hands = x[:, :, :8] boards = x[:, :, 8:] ranks = [] for j in range(B): for i in range(M): hand = [[int(hands[j, i, c]), int(hands[j, i, c + 1])] for c in range(0, len(hands[j, i, :]), 2)] board = [[int(boards[j, i, c]), int(boards[j, i, c + 1])] for c in range(0, len(boards[j, i, :]), 2)] hand_en = [encode(c) for c in hand] board_en = [encode(c) for c in board] ranks.append( torch.tensor(hand_rank(hand_en, board_en), dtype=torch.float)) return torch.stack(ranks).view(B, M, 1)
def resolve_outcome(self): """Gets all hands, gets all handranks,finds and counts lowest (strongest) hands. Assigns the pot to them.""" hands,positions = self.players.return_active_hands() if len(positions) > 1: # encode hands en_hands = [] for hand in hands: en_hand = [encode(c) for c in hand] en_hands.append(en_hand) en_board = [encode(self.board[i*2:(i*2)+2]) for i in range(0,len(self.board)//2)] hand_ranks = [hand_rank(hand,en_board) for hand in en_hands] best_hand = np.min(hand_ranks) winner_mask = np.where(best_hand == hand_ranks)[0] winner_positions = np.array(positions)[winner_mask] for i,position in enumerate(positions): self.players[position].handrank = hand_ranks[i] for winner in winner_positions: self.players[winner].stack += self.pot / len(winner_mask) else: self.players[positions[0]].stack += self.pot
def testHoldemWinner(self): en_hand = [cb.encode(card) for card in self.holdem_hand] en_hand2 = [cb.encode(card) for card in self.holdem_hand2] en_board = [cb.encode(card) for card in self.board] assert cb.holdem_winner(en_hand, en_hand2, en_board) == -1
def testHoldemHandrank(self): en_hand = [cb.encode(card) for card in self.holdem_hand] en_board = [cb.encode(card) for card in self.board] assert cb.holdem_hand_rank(en_hand, en_board) == 3304
def testHandrank(self): en_hand = [cb.encode(card) for card in self.omaha_hand] en_board = [cb.encode(card) for card in self.board] assert cb.hand_rank(en_hand, en_board) == 2985
def testWinner(self): en_hand = [cb.encode(card) for card in self.omaha_hand] en_hand2 = [cb.encode(card) for card in self.omaha_hand2] en_board = [cb.encode(card) for card in self.board] assert cb.winner(en_hand, en_hand2, en_board) == -1
def testDecode(self): en_hand = [cb.encode(card) for card in self.omaha_hand] hand = [cb.decode(card) for card in en_hand] assert hand == [[7, 's'], [5, 'c'], [14, 'h'], [10, 'h']]
def testEncode(self): en_hand = [cb.encode(card) for card in self.omaha_hand] assert en_hand == [2102541, 557831, 268446761, 16787479]