def getHandCode(herohand, table):
  handscore = 0
  herohandplus = herohand.list_rep() + table.list_rep()
  evaluated = hands.evaluate_hand(herohandplus)

  if evaluated[2] > 3:
    handscore = evaluated[2]
  elif evaluated[2] == 3:
    high_pair = evaluated[1][0]
    highest_card = True
    for e in evaluated[1]:
      if high_pair < e:
        highest_card = False
    if highest_card:
      handscore = features['2-pair-good']
    else:
      handscore = features['2-pair-bad']
  elif evaluated[2] == 2:
    high_pair = evaluated[1][0]
    num_card_greater = 0
    for e in evaluated[1]:
      if high_pair < e:
        num_card_greater += 1
    
    if num_card_greater == 0:
      handscore = features['high-pair']
    elif num_card_greater == 1:
      handscore = features['middle-pair']
    else:
      handscore = features['low-pair']
  elif evaluated[2] == 1:
    straightchance = checkstraight(herohandplus)
    # evaluating for flush draw
    if checkflush4(herohandplus):
      handscore = features['flush-draw-4']
    # evaluating for straight draw
    elif straightchance >= 2:
      handscore = features['straight-draw-good']
    elif straightchance == 1:
      handscore = features['straight-draw-bad']
    else:
      hand_strength = preflop_sim.getPreflopStrength(herohand)
      win_ratio = hand_strength[0] / (hand_strength[0] + hand_strength[2])

      if win_ratio > REALLYGOODHAND:
        handscore = features['really-good-high']
      elif win_ratio > GOODHAND:
        handscore = features['good-high']
      elif win_ratio > MIDDLEHAND:
        handscore = features['middle-high']
      elif win_ratio > BADHAND:
        handscore = features['bad-high']
      else: 
        handscore = features['really-bad-high']

  return handscore
def getHandCode(herohand, table):
  handscore = 0
  herohandplus = herohand.list_rep() + table.list_rep()
  evaluated = hands.evaluate_hand(herohandplus)

  if evaluated[2] > 3:
    handscore = evaluated[2]
  elif evaluated[2] == 3:
    high_pair = evaluated[1][0]
    highest_card = True
    for e in evaluated[1]:
      if high_pair < e:
        highest_card = False
    if highest_card:
      handscore = afterflop_sim.features['2-pair-good']
    else:
      handscore = afterflop_sim.features['2-pair-bad']
  elif evaluated[2] == 2:
    high_pair = evaluated[1][0]
    num_card_greater = 0
    for e in evaluated[1]:
      if high_pair < e:
        num_card_greater += 1
    
    if num_card_greater == 0:
      handscore = afterflop_sim.features['high-pair']
    elif num_card_greater == 1:
      handscore = afterflop_sim.features['middle-pair']
    else:
      handscore = afterflop_sim.features['low-pair']
  elif evaluated[2] == 1:
    hand_strength = preflop_sim.getPreflopStrength(herohand)
    win_ratio = hand_strength[0] / (hand_strength[0] + hand_strength[2])

    if win_ratio > afterflop_sim.REALLYGOODHAND:
      handscore = features['really-good-high']
    elif win_ratio > afterflop_sim.GOODHAND:
      handscore = features['good-high']
    elif win_ratio > afterflop_sim.MIDDLEHAND:
      handscore = features['middle-high']
    elif win_ratio > afterflop_sim.BADHAND:
      handscore = features['bad-high']
    else: 
      handscore = features['really-bad-high']

  return handscore
    def play_preflop(
            self,
            hand,
            money_in,
            money_required,
            big_blind,
            max_bet,
            position=False,
            small_blind=True):
        kUndercut = .33  # chance that hero bets small with a really good hand
        kLimp = .3  # chance that hero limps in with a poor hand
        kGoodHand = .1  # really good hand differential
        kBetFactor = 4
        kConstantBetFactor = .75
        kCallPercent = .3  # percent of big blind that the raise is for us just to call
        kLowHandConstant = .08  # makes it more likely to play beter hands
        kStretchFactor = 1.2
        kGoodRatio = .56
        kReallyGoodRatio = .66
        final_bet = 0

        hand_strength = preflop_sim.getPreflopStrength(hand)
        win_ratio = hand_strength[0] / (hand_strength[0] + hand_strength[2])
        cost_benefit_ratio = (money_required - money_in) / \
            float(money_required)

        # correcting for advantageous position
        if position:
            win_ratio = win_ratio * (1 + kPositionAdvantage)
        else:
            win_ratio = win_ratio * (1 - kPositionDisadvantage)

        if small_blind:
            if win_ratio > cost_benefit_ratio:
                # 'bluff' by calling with a really good hand
                if random.random() < kUndercut:
                    final_bet = money_required - money_in

                else:
                    bet = (win_ratio + kConstantBetFactor) * \
                        big_blind + money_required - money_in
                    bet = randomPermute(bet, kRandomFactor)
                    if bet - money_required < kCallPercent * big_blind:
                        final_bet = money_required - money_in
                    else:
                        final_bet = int(bet) - money_in
            else:
                if randomPermute(
                        win_ratio,
                        kRandomFactor) + kLowHandConstant > cost_benefit_ratio or random.random() < kLimp:
                    final_bet = money_required - money_in
                else:
                    final_bet = -1
        else:
            # use random_permute to sometimes play slightly worse hands as good
            # hands
            if randomPermute(win_ratio, kRandomFactor) > kGoodRatio:
                if random.random() < kUndercut:
                    final_bet = money_required - money_in
                else:
                    tentative_bet = (win_ratio + kConstantBetFactor) * big_blind + \
                        win_ratio * (money_required - money_in) * kBetFactor
                    if tentative_bet < money_required - money_in:
                        if win_ratio > kReallyGoodRatio:
                            final_bet = int(
                                randomPermute(
                                    (money_required - money_in) * 2,
                                    kRandomFactor))
                        else:
                            final_bet = money_required - money_in
                    else:
                        final_bet = int(tentative_bet)
            else:
                # kStretchFactor to err on the side of calling a bit
                if cost_benefit_ratio < randomPermute(
                        win_ratio, kRandomFactor) * kStretchFactor:
                    final_bet = money_required - money_in
                else:
                    final_bet = -1

        # this is just to make sure we're not overambitious with betting
        if final_bet > max_bet:
            final_bet = max_bet

        self.explanation += "Before the flop, hero's hand had a win ratio of " + \
            str(win_ratio) + " and a cost-benefit ratio of " + str(cost_benefit_ratio) + " and we bet " + str(final_bet) + " as a result. "

        return final_bet