import random
from Queue import Queue
class Deck:
def __init__(self, decks=1, shuffle=4):
使用队列系统模拟被绘制和丢弃的卡片:参数甲板:要使用的甲板数:param shuffle:洗牌的次数:返回:
self.deck = Queue()
self.shuffle_times = shuffle
self.suits = ['spades', 'clubs', 'hearts', 'diamonds']
self.cards = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
for deck in range(decks):
for suit in self.suits:
for card in self.cards:
self.deck.put([card, suit])
def draw(self):
:返回:甲板上的随机卡
card = self.deck.get()
return card
def shuffle(self):
洗牌n次:返回:无
if self.shuffle_times > 0:
temp = list(self.deck.queue)
[random.shuffle(temp) for _ in range(self.shuffle_times)]
self.deck = Queue()
for card in temp:
self.deck.put(card)
class Blackjack:
def __init__(self, decks=1, shuffle=1):
:参数甲板:要使用的甲板数:param shuffle:洗牌的次数:返回:无
self.num_decks = decks
self.deck = Deck(self.num_decks, shuffle)
self.deck.shuffle()
self.cards_dict = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8,
'9': 9, '10': 10, 'J': 10, 'Q': 10, 'K': 10, 'A': 11}
self.high_low = {'2': 1, '3': 1, '4': 1, '5': 1, '6': 1, '7': 0, '8': 0, '9': 0,
'10': -1, 'J': -1, 'Q': -1, 'K': -1, 'A': -1}
self.games = 0
self.draws = 0
self.computer_win = 0
self.player_win = 0
self.running_count = 0
self.player_hand = []
self.computer_hand = []
self.player_total = 0
self.computer_total = 0
def batlle_bot(self):
使用二十一点的基本策略来决定要采取哪些措施。能够区分软手和硬手
if 'A' in [card[0] for card in self.player_hand]:
if self.player_total <= 17:
return 'h'
elif self.player_total >= 19:
return 's'
elif self.player_total == 18 and 2 <= self.computer_total <= 8:
return 's'
elif self.player_total == 18 and self.computer_total >= 9:
return 'h'
else:
if self.player_total <= 11:
return 'h'
elif self.player_total >= 17:
return 's'
elif self.player_total == 12 and 2 <= self.computer_total <= 3:
return 'h'
elif 12 <= self.player_total <= 16 and 4 <= self.computer_total <= 6:
return 's'
elif 13 <= self.player_total <= 16 and 2 <= self.computer_total <= 3:
return 's'
elif 4 <= self.player_total <= 16 and self.computer_total >= 7:
return 'h'
def total(self, hand):
"""
Helper to check for aces in the hand and calculates the value of the hand
:param hand: list of cards in the hand
:return: value of the hand
"""
aces = hand.count(11)
hand_value = sum(hand)
if hand_value > 21 and aces > 0:
while aces > 0 and hand_value > 21:
hand_value -= 10
aces -= 1
return hand_value
def high_low_count(self, card):
跟踪卡计数的高/低计数:参数卡:卡值:返回:根据高/低计数的卡值
self.running_count += self.high_low[card[0]]
return card
score = (0, 0, 0, 0)
num_cycles = 1000
num_decks = 6
for i in range(num_cycles):
game = Blackjack(num_decks).battle_bot=True
score = tuple([x + y for x, y in zip(score, game)])
games, draws, player_win, computer_win = score
print('Player win = {}'.format(player_win))
print('Computer win = {}'.format(computer_win))
print('Number of games = {}'.format(games))
print('Draws = {}'.format(draws))
try:
print('Win/Loss = {:.2f}'.format(player_win / computer_win))
except:
print('Win/Loss = {}'.format(max(player_win, computer_win)))
print('n--------------------n')
该程序应打印球员获胜的次数,经销商获胜以及多少平局。
您的问题在这里:
game = Blackjack(num_decks).battle_bot=True
score = tuple([x + y for x, y in zip(score, game)])
您刚分配了game
和Blackjack(num_decks).battle_bot
具有值True
,然后尝试使用zip
score
(a tuple
,fine)和 game
(a bool
, CC_8,不可能)。
我不知道您在这里的意图,即使您打电话给battle_bot
,它也会返回一个字母字符串,因此您的zip
只会产生一个单个输出,而不是您似乎认为您'的四个值将能够在下一行上解开包装(四个具有不同含义的值不少,而产生scores
的代码似乎以对多种含义没有意义的方式对待它们)。
您需要考虑您的程序逻辑,除了发布有关的小错误之外,这里存在太多的逻辑问题。