马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import random
# # 判断随机发发的卡牌是否在列表中
def judge(card, card_list):
if card in card_list:
return True
else:
return False
# 将随机数对应成卡牌点数
def transform(num):
if num > 40:
return 0.5
else:
return num // 4 + 1
# 判断大小 返回-1平局 除了-1情况,返回赢得数字
def who_win(num1, num2):
if num1 == num2:
return -1
elif num1 < 21 and num2 < 21:
return max(num1, num2)
elif num1 == 21:
return num1
elif num2 == 21:
return num2
elif num1 < 21 and num2 > 21:
return num1
elif num2 < 21 and num1 > 21:
return num2
elif num1 > 21 and num2 > 21:
if num2 - 21 > num1 - 21:
return num1
else:
return num2
exit_game = False
# 将所有卡片放入列表中
player_name = input("请输入你的名字")
while True:
while True:
# 玩家的总点数
play_sum = 0
# 机器的总点数
robot_sum = 0
# 存放所有的卡片
card_list = []
a = True
b = True
# 将卡牌放到列表中
for i in range(1, 55):
card_list.append(i)
# 如果玩家,机器都不要牌,退出循环
while a or b:
# 判断是否给玩家发牌
if a:
ask_card = input("是否要牌:(y/n)")
if ask_card == "y":
card = random.randint(1, 54)
if judge(card, card_list):
card_list.remove(card)
card = transform(card)
print("给玩家牌为" + str(card))
play_sum += card
else:
while card not in card_list:
card = random.randint(1, 54)
card_list.remove(card)
card = transform(card)
print("给机器牌为" + str(card))
play_sum += card
print("玩家现在点数为%0.1f" % (play_sum))
else:
a = False
# 判断是否给机器发牌
if b:
if robot_sum < 18:
card = random.randint(1, 54)
print("机器选择要牌。。。。。。。")
if judge(card, card_list):
card_list.remove(card)
card = transform(card)
print("给机器牌为" + str(card))
robot_sum += card
else:
while card not in card_list:
card = random.randint(1, 54)
card_list.remove(card)
card = transform(card)
print("给机器牌为" + str(card))
robot_sum += card
print("机器现在点数为%0.1f" % (robot_sum))
else:
print("机器不要牌")
b = False
print("机器点数为%0.1f" % (robot_sum))
print("玩家点数为%0.1f" % (play_sum))
if who_win(play_sum, robot_sum) == -1:
print("平局")
elif who_win(play_sum, robot_sum) == play_sum:
print("玩家获胜")
else:
print("机器获胜获胜")
ask_game = input("是否玩下一局")
if ask_game == "y":
print("进入下一局。。。。。")
else:
print("退出游戏")
exit_game = True
break
if exit_game:
break
|