【发布时间】:2016-09-02 16:05:02
【问题描述】:
我正在尝试在 python 中调试二十一点游戏,但是一旦发牌,我无法弄清楚如何通过 King 实现 Ace。我需要一个提示或什么...我是否忽略了一些明显的东西?
MAX = 21
# main function
def main():
# Local variables
hand1 = 0
hand2 = 0
deck = create_deck()
while hand1 <= 21 and hand2 <= 21:
card1, value1 = deck.popitem()
hand1 = update_hand_value(hand1, value1, card1)
card2, value2 = deck.popitem()
hand2 = update_hand_value(hand2, value2, card2)
print('Player 1 was dealt', card1)
print('Player 2 was dealt', card2)
print()
# Determine the winner.
if hand1 > MAX and hand2 > MAX:
print("There is no winner.")
elif hand1 > 21:
print("Player 2 wins.")
else:
print("Player 1 wins.")
def create_deck():
# Set up local variables
suits = ['Spades', 'Hearts', 'Clubs', 'Diamonds']
# unused currently, needs to be implemented
special_values = {'Ace': 1, 'King': 10, 'Queen': 10, 'Jack': 10}
numbers = ['Ace', 'King', 'Queen', 'Jack']
for i in range(2, 11):
numbers.append(str(i))
# Initialize deck
deck = {}
for suit in suits:
for num in numbers:
# Values 2-10.
if num.isnumeric():
deck[num + ' of ' + suit] = int(num)
else:
deck[num + ' of ' + suit] = str(num)
# values 1 and 11
return deck
def update_hand_value(hand, value, card):
if not card == 'Ace':
return hand, value
# Adding 11 would cause to go over the maximum.
elif hand > 10:
# Value is 1 by default.
return hand, value
else:
return hand + 11
# Call the main function.
main()
# Call the main function.
main()
在玩家获胜之前一直发牌。
示例输出:
玩家 1 获得了 2 颗钻石 玩家 2 得到了黑桃 8
玩家 1 获得了 6 颗钻石 玩家 2 获得了 5 颗钻石
玩家 1 得到了 4 个俱乐部 玩家 2 获得了 8 个方块
玩家 1 得到了 7 个俱乐部 玩家 2 得到了黑桃 3
玩家 1 获胜。
编辑: 我已经修改了我的程序,但出现了一个新错误。
Traceback(最近一次调用最后一次): 文件“C:/Users/Sean/PycharmProjects/SeanPython/ISY150/Demo/Blackjack.py”,第 99 行,在 主要的() 文件“C:/Users/Sean/PycharmProjects/SeanPython/ISY150/Demo/Blackjack.py”,第 17 行,在 main 而hand1
我不明白,据我所知,在第 17 行,我没有将元组与 int 进行比较...
【问题讨论】:
-
不得不编辑我的程序。另外,我想这个程序必须以固定的方式完成。我实际上有另一个工作二十一点程序。这只需要以这种方式完成。我必须调试它作为练习。
标签: python list function blackjack playing-cards