源码如下:
简单解释一些函数的作用
?首先是函数deal(),poker是一个从1到52的列表.每次调用deal()函数都会生成一个随机数
randint_1 = random.randint(1, 52),为了保证一副牌中的每一张牌只能出现一次,所以每一次都会调用remove函数删除randint_1,为了防止出现一个数被删除后调用remove的报错(举个例子,第一次调用deal函数拿到的牌是12, 12被删除, 第二次又是12, 这个时候删除12的话就会报错).
if randint_1 in poker:? ? ? 有效防止了上述问题的发生
别的函数有需要再补充.
import random
def deal(poker): # 发牌函数
while True:
randint_1 = random.randint(1, 52)
if randint_1 in poker:
poker.remove(randint_1)
return randint_1
break
def card_change_count(card): # 将牌转化成点数的函数
if not card < 1 and card <= 4:
count = 1
elif card >= 5 and not card > 8:
count = 2
elif card >= 9 and not card > 12:
count = 3
elif card >= 13 and not card > 16:
count = 4
elif card >= 17 and not card > 20:
count = 5
elif card >= 21 and not card > 24:
count = 6
elif card >= 25 and not card > 28:
count = 7
elif card >= 29 and not card > 32:
count = 8
elif card >= 33 and not card > 36:
count = 9
elif card >= 37 and not card > 52:
count = 10
return count
def addition(counts): # 专门用来计算点数之和的函数
count = 0
for i in counts[:]:
count += i
return count
def first_deal(your_count, banker_count, poker): # 第一次发牌的函数(庄家两张,一明一暗, 玩家两张)
d_w = False
print("开始游戏")
banker_count.append(card_change_count(deal(poker)))
banker_count.append(card_change_count(deal(poker)))
print(f"庄家的明牌是: {banker_count[0]}")
your_count.append(card_change_count(deal(poker)))
your_count.append(card_change_count(deal(poker)))
exchange_point(your_count, 0)
print(f"玩家的牌是: {your_count}")
if addition(your_count) == 21:
print("你的点数为21点,你赢了!")
d_w = True
return d_w
def continue_deal(your_count, poker): # 继续要牌函数
lose = False
all_count = addition(your_count)
count_3 = card_change_count(deal(poker))
print(f"你的新牌点数为: {count_3}")
if count_3 == 1 and all_count < 11:
print("A由1点变成11点")
count_3 = 11
your_count.append(count_3)
all_count = addition(your_count)
if all_count > 21:
exchange_point(your_count, 1)
all_count = addition(your_count)
if all_count > 21:
print(f"你的牌点数为:{all_count}, 超过21点.你输了")
lose = True
return lose
def exchange_point(your_count, signed): # A由1转换成11点或者11点转换成1点
if signed == 1:
for count in your_count:
if count == 11:
your_count.remove(count)
your_count.append(1)
print("点数超过21点, 且牌中包含A,故将A由11变成1点")
if signed == 0:
if your_count[0] == 1:
your_count[0] = 11
print("系统强制将牌中的A由1点转换为11点")
if your_count[1] == 1:
your_count[1] = 11
print("系统强制将牌中的A由1点转换为11点")
def lose_or_win(ai_count, all_count): # 最后判断是玩家赢了还是庄家赢了的函数
lose = False
if ai_count == all_count:
print("你的点数和庄家点数一样大,平局")
if ai_count > all_count and not ai_count >= 22:
print(f"庄家的点数:{ai_count},大于你的点数:{all_count}, 你输了")
lose = True
if ai_count < all_count:
print(f"庄家的点数:{ai_count},小于你的点数:{all_count}, 你赢了")
return lose
def run_game(): # 游戏运行的主题函数
poker = [i for i in range(1, 53)]
lose = False
your_count = []
banker_count = []
direct_win = first_deal(your_count, banker_count, poker)
all_count = addition(your_count)
while not direct_win:
all_count = addition(your_count)
print(f"\n你的点数为:{all_count}")
sign = input("是否需要继续要牌? 'y' or 'n'")
if sign == 'n':
print(f"你放弃要牌,并且你的点数为:{all_count}")
print("你的回合结束,下面开始庄家回合\n")
break
if sign == 'y':
lose = continue_deal(your_count, poker)
if lose:
break
if not lose and not direct_win:
exchange_point(banker_count, 0)
ai_count = addition(banker_count)
print(f"庄家的牌为:{banker_count}")
print(f"庄家的点数为: {ai_count}")
if ai_count < 17:
while ai_count < 17:
count_3 = card_change_count(deal(poker))
print(f"庄家选择继续要牌,庄家的新牌点数为: {count_3}")
if count_3 == 1 and ai_count < 11:
print("A由1点变成11点")
count_3 = 11
banker_count.append(count_3)
ai_count = addition(banker_count)
print(f"\n庄家的点数为:{ai_count}")
if ai_count > 21:
exchange_point(banker_count, 1)
ai_count = addition(banker_count)
print(f"庄家的点数为:{ai_count}")
if ai_count > 21:
print(f"庄家牌点数为:{ai_count},超过21点,你赢了")
break
if ai_count > all_count and ai_count > 16:
break
lose = lose_or_win(ai_count, all_count)
run_game()
|