|

楼主 |
发表于 2016-11-24 12:04:13
|
显示全部楼层
本帖最后由 jerryxjr1220 于 2016-11-24 14:03 编辑
源代码如下:
- from time import time
- import random
- class blackjack:
- def __init__(self):
- global cards
- self.hands = [cards.pop()%13+1,cards.pop()%13+1]
- def call(self):
- global cards
- self.hands.append(cards.pop()%13+1)
- def calc(self):
- flag = 0
- total = 0
- for each in self.hands:
- if each == 1:
- flag = 1
- if each >= 10:
- total += 10
- else:
- total += each
- if total <= 11 and flag:
- total += 10
- return total
- p1,p2 = 0,0
- for i in range(100000):
- cards = list(range(1,53))
- random.shuffle(cards)
- player1 = blackjack()
- player2 = blackjack()
- while player1.calc() <= 14:
- player1.call()
- while player2.calc() <= 20:
- player2.call()
- if (player1.calc()<=21 and player1.calc()>player2.calc()) or (player1.calc()<=21 and player2.calc()>21):
- p1 += 1
- if (player2.calc()<=21 and player2.calc()>player1.calc()) or (player2.calc()<=21 and player1.calc()>21):
- p2 += 1
- print (p1*100/(p1+p2),p2*100/(p1+p2))
复制代码
每次统计用10万次随机牌局,结果还是比较稳定的。
模拟过程中未考虑black jack直接获胜的情况,并且排除了平局的情况。
|
|