我試圖使用OOP創建二十一點遊戲和每一個我稱之爲21點或半身功能時,它重印玩家的手和值經銷商。我想它與Hand類正在初始化有關,但我不確定那裏的工作是什麼。Python的酒杯OOP - 呼叫胸部和大酒杯類功能不斷重印卡值
代碼下面的手+類變量:
class Hand(object):
def __init__(self):
self.hand = []
def add_card(self, card):
self.hand.append(card)
return self.hand
def __str__(self):
hand = [str(card) for card in self.hand]
print 'Current hand is: ',hand
def get_value(self):
value = 0
for card in self.hand:
print(card)
value += card.value
for card in self.hand:
if card.card == 'Ace' and value > 21:
value -= 10
return value
def bust(self):
if self.get_value() > 21:
print ('Busted!')
else:
pass
def hit_or_stand(self):
self.hit = 'Hit'
self.stand = 'Stand'
self.prompter = raw_input('Would you like to hit or stand? ').lower()
return self.prompter
def show_hand(self):
for card in self.hand:
print(str(card))
def show_dealerHand(self):
print('Dealers Hand: '+ str(self.hand[0]) + ' ' + '[X]')
def blackjack(self):
self.bj = False
if self.get_value() == 21:
self.bj = True
print ('Blackjack!')
else:
pass
def dealerHit(self):
if self.get_value < 17:
self.add_card(card)
else:
pass
這裏是運行遊戲的主要功能:
def runGame():
player1 = Player()
deck1 = Deck()
playerHand = Hand()
dealerHand = Hand()
# deal out cards to player
i = 0
while i < 2:
playerHand.add_card(deck1.drawCard())
dealerHand.add_card(deck1.drawCard())
i += 1
playerHand.__str__()
dealerHand.show_dealerHand()
player_move = playerHand.hit_or_stand()
if player_move == playerHand.hit.lower():
playerHand.add_card(deck1.drawCard())
while dealerHand.get_value() < 17:
dealerHand.add_card(deck1.drawCard())
if playerHand.bust():
print('Dealer wins!')
elif dealerHand.bust():
print('Player wins!')
if playerHand.blackjack() and dealerHand.blackjack():
print('Both the Player and the Dealer have a blackjack!')
player1.winnings += player1.amount
elif playerHand.get_value() > dealerHand.get_value():
print('Player wins')
player1.winnings += player1.amount * 2
elif playerHand.get_value() < dealerHand.get_value():
print('Dealer wins')
player1.winnings -= player1.amount
get_value函數是什麼?和'show_dealerHand'?等,好像有在'Hand' – noamgot
類缺少一些方法@noamgot我編輯的代碼,以手類 – user2899245