【发布时间】:2017-05-27 21:13:10
【问题描述】:
所以我在 python 中编写二十一点脚本,但遇到了手中的 A 的问题(有些手可能有 1 个 A,2 个 A 或击中后,3 个 A 或最多 4 个),问题是找到最大的和小于或等于 21。这听起来很容易,但是当每个 ace 的值可以是 1 或 11、不能重新计算并且 ace 的数量变化时,就会出现复杂情况,例如 p>
aces = [[1, 11], [1, 11]] #could be aces = [[1,11]] or etc. where one ace is [1, 11]
所以我目前所拥有的对我不起作用,因为 for 循环的数量已经固定了一定数量的 ace...(在本例中为两个)
possiblehand = []
for ace1 in aces:
aces.remove(ace1) #To not re-count this current ace
for ace2 in aces:
handtotal = currenthandsum + ace1 + ace2 #two for-loops fixes two aces, but the amount of aces is varying. The error of adding lists exists also, but was not able get rid of this yet still add up all the combinations.
if handtotal <= 21:
possiblehand.append(handtotal)
hand = 0
for possible in possiblehand:
if possible > hand:
hand = possible #just getting the greatest value in this list
这被固定为手中正好有两个 A,并且存在我实际上添加两个列表的错误,但我想将 [1, 11] 的所有组合相加,以获得有多少个 A。 (例如,对于 20: 11, 1, 1, 7,其中 7 不是 ace)
那么我该如何解决这个问题呢?
【问题讨论】: