Blackjack minimax algorithm

独自空忆成欢 提交于 2019-12-24 05:12:43

问题


I am implementing a blackjack game with minimax tree which calculates the probabilities and play automatically depend on this probabilities.

Assume that, we play with 1 deck and the first game dealer takes : '5' and player takes '5 7' so the total score is 12 for player.

In this case, first I am trying to check all possible probabilities for player's stand decision.

If player stands :

My remain cards in deck like this : Structure of deck(K,V) K : card number, V: count of card

{1: 4, 2: 4, 3: 4, 4: 4, 5: 2, 6: 4, 7: 3, 8: 4, 9: 4, 10: 16}

Now, dealer should pass the number 17. Some examples can be like this :

5(base card) + 1(11) + 1 = 17  (possibility of this hand : 4/49 * 3/48)
5(base card) + 1(11) + 2 = 18  (possibility of this hand : 4/49 * 4/48)
......
5 (base card) + 10 + 1 + 1 = 17 (possibility of this hand : 16/49 * 4/48 * 3/48)

My question is, how can I calculate all this possibilities and calculate the final possibility of if player decision is standing. I cannot figure out how can I coded these combination of numbers.

EDIT :

I found out this code which calculate the possible combinations. It is similar what I look like. I need to change this for my problem, I hope I can do it.

def subset_sum(numbers, target, partial=[]):
    s = sum(partial)

    # check if the partial sum is equals to target
    if s == target: 
        print "sum(%s)=%s" % (partial, target)
    if s >= target:
        return  # if we reach the number why bother to continue

    for i in range(len(numbers)):
        n = numbers[i]
        remaining = numbers[i+1:]
        subset_sum(remaining, target, partial + [n]) 


if __name__ == "__main__":
    subset_sum([3,9,8,4,5,7,10],15)

    #Outputs:
    #sum([3, 8, 4])=15
    #sum([3, 5, 7])=15
    #sum([8, 7])=15
    #sum([5, 10])=15

回答1:


This game is not really a situation for minimax.

In minimax, two players make moves which are determined from a known position, and take into account the move of the other player in determining their best moves.

Since this example has two players, the Player (who does not actually move in the sense that he changes the state of the board except to decide whether to stand or not) and the Dealer (who only makes moves randomly), in an unknown board state with random options, minimax specifically cannot be used.

In this case, an algorithm that would work fairly well would be to start with the 5 and the added 7:

base = 5
cards = [7]

Using the following formula:

if sum(cards) + base < 16:
  hit()
else:
  if evalStand() > minStandChance:
    hit()

it is not necessary to calculate the card tree while true, since the player needs to get another card anyway.

After that, getting a probability of standing after that:

def evalStand():
  hand = base + sum(cards)
  sums = []
  for cardValue, count in cards.items():
    for i in range(count):
      sums.append(cardValue + hand)
  return len([i for i in sums if i <= 21])/float(len(sums))

Simply filtering out the ratio of possible hands that will still get the player <= 21. A commonly used strategy in this game is to creep up on 21. This emulates it. If the probability of standing after the next round is less than, say, 0.5, the player can stop getting cards.

Again, this situation is not good for minimax, but this should be a good alternate solution.

Edit:

As @NickLarsen has pointed out, minStandChance represents a heuristic. There is not a variable that is 100% accurate, but can be adjusted according to how much risk you want the AI to play with. (closer to 0 is risky, closer to 1 is conservative).



来源:https://stackoverflow.com/questions/31904468/blackjack-minimax-algorithm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!