@thphd #131587 假设牌洗的均匀。以下为程序(Python),原谅我的渣排版。如果要测试的话请注意缩进。多谢码农朋友们帮我修正排版
import random
import math
def total(hand):
aces=hand.count(11)
t = sum(hand)
if t > 21 and aces > 0:
while aces > 0 and t > 21:
t -= 10
aces -= 1
return t ## hand=[11,11,6], total(hand) is 18
def deal(player,cards):
rc=random.choice(cards)
player.append(rc)
cards.remove(rc)
number_deck=8
a=[0,0,0,0,0,0,0,0,0,0] # cards left
b=[0,0,0,0,0,0,0,0,0,0] # percentage
prb=[-2,-1,1,1,2,2,2,1,0,0] # each card take away
exp_value=exp=0
for x in range(10):
a[int(x)]=4*number_deck
a[0]=4*number_deck*4 #initialization
while True:
string=input("please enter:")
if string=='me': #me calculate the mathematical expectation of stand or hit once, if hit once has larger than 100% exp, then player should double if they can; if both expectation below 50%, player should surrender if they can
stand_or_hit=0 # stand=0 hit_once=1
compare=[0,0]
my_cards=[]
dealer_cards=[]
my_cards_string=input("my cards:")
dealer_cards_string=input("dealer cards:")
for x in my_cards_string:
if x>='0' and x<='9':
if int(x)<2:
my_cards.append(int(x)+10)
else:
my_cards.append(int(x))
if len(dealer_cards_string) < 2:
if dealer_cards_string>='0' and dealer_cards_string<='9':
if int(dealer_cards_string)<2:
dealer_cards.append(int(dealer_cards_string)+10)
else:
dealer_cards.append(int(dealer_cards_string))
cards_ini=[]
for x in range(10):
for s in range(a[x]):
if x<2:
cards_ini.append(x+10)
else:
cards_ini.append(x)
while stand_or_hit<=1:
try_times=0
exp_return=0
while try_times<30000:
my=list(my_cards)
dealer=list(dealer_cards)
cards=list(cards_ini)
if stand_or_hit==1:
deal(my,cards)
deal(dealer,cards)
total_dealer=total(dealer)
while True:
if total_dealer>=17:
break
else:
deal(dealer,cards)
total_dealer=total(dealer)
total_my=total(my)
if total_my>21:exp_return+=0
elif total_dealer>21: exp_return+=2
elif dealer==[10,11] or dealer==[11,10]:exp_return+=0
elif total_my>total_dealer: exp_return+=2
elif total_my==total_dealer: exp_return+=1
else: exp_return+=0
try_times+=1
compare[stand_or_hit]=round(exp_return/300,1) #bet 100, expected return
stand_or_hit+=1
print(compare)
if string=='split': # calculate split expectation
compare=[0,0]
my_cards=[]
dealer_cards=[]
stand_or_split=0
my_cards_string=input("my cards:")
dealer_cards_string=input("dealer cards:")
for x in my_cards_string:
if x>='0' and x<='9':
if int(x)<2:
my_cards.append(int(x)+10)
else:
my_cards.append(int(x))
if len(dealer_cards_string) < 2:
if dealer_cards_string>='0' and dealer_cards_string<='9':
if int(dealer_cards_string)<2:
dealer_cards.append(int(dealer_cards_string)+10)
dealer_cards.append(int(dealer_cards_string))
cards_ini=[]
for x in range(10):
for s in range(a[x]):
if x<2:
cards_ini.append(x+10)
else:
cards_ini.append(x)
try_times=0
exp_return=0
my1=[]
my2=[]
my1.append(my_cards[0])
my2.append(my_cards[1]) #my two hands
while stand_or_split <= 1:
try_times=0
exp_return=0
while try_times<30000:
myhand1=list(my1)
myhand2=list(my2)
dealer=list(dealer_cards)
cards=list(cards_ini)
if stand_or_split==1:
deal(myhand1,cards)
deal(myhand2,cards)
deal(dealer,cards)
total_dealer=total(dealer)
while True:
if total_dealer>=17:
break
else:
deal(dealer,cards)
total_dealer=total(dealer)
total_my1=total(myhand1)
if total_my1>21:exp_return+=0
elif total_dealer>21: exp_return+=2
elif dealer==[10,11] or dealer==[11,10]:exp_return+=0
elif total_my1>total_dealer: exp_return+=2
elif total_my1==total_dealer: exp_return+=1
else: exp_return+=0
total_my2=total(myhand2)
if total_my2>21:exp_return+=-1
elif total_dealer>21: exp_return+=1
elif dealer==[10,11] or dealer==[11,10]:exp_return+=0 #Return of split for dealer blackjack
elif total_my2>total_dealer: exp_return+=1
elif total_my1==total_dealer: exp_return+=0
else: exp_return+=-1
else:
total_dealer=total(dealer)
while True:
if total_dealer>=17:
break
else:
deal(dealer,cards)
total_dealer=total(dealer)
total_my=total(my_cards)#unsplit hands
if total_my>21:exp_return+=0
elif total_dealer>21: exp_return+=2
elif dealer==[10,11] or dealer==[11,10]:exp_return+=0
elif total_my>total_dealer: exp_return+=2
elif total_my==total_dealer: exp_return+=1
else: exp_return+=0
try_times+=1
compare[stand_or_split]=round(exp_return/300,1) #bet 100, expected return
stand_or_split+=1
print(compare) #bet 100, expected return
elif string>='a' and string<='z': # to exit, press a nonletter. for example "/"
pass
else:
for x in string:
if x<='9' and x>='0':
a[int(x)]=a[int(x)]-1
exp+=prb[int(x)]
for x in range(10):
b[int(x)]=round(100*a[int(x)]/sum(a)*13-100,1)
b[0]=round(100/4*a[0]/sum(a)*13-100,1)
n=2
stt='10'+'\t'+'A'+'\t'
while n<10:
stt=stt+str(n)+'\t'
n+=1
print(stt)
n=0
stt=''
while n<10:
stt=stt+str(a[n])+'\t'
n+=1
print(stt)
n=0
stt=''
while n<10:
stt=stt+str(b[n])+'\t'
n+=1
exp_value=round(exp*52/sum(a),1)
print(stt)
print("totla cards:"+str(sum(a))+' '+"expect_value:"+str(exp_value))
if b[0]>8.3:
print("insurance OK")