(编号为2-10的卡的值应分别为2-10.J,Q和K应为10,A应为1或11,具体取决于手的值。)
如何为这些值分配牌组?此外,游戏需要3轮。我这样做的方式只有一轮。如何让游戏进行三次,同时跟踪玩家的赢/输?
有人可以解释一下如何以一种简单的方式做到这一点吗?
答案 0 :(得分:1)
这是一个完整的实现
看看手。核心
import random
class Card:
def __init__(self,rank,suite):
self.rank = rank
self.suite = suite
def Rank(self):
return "Ace Two Three Four Five Six Seven Eight Nine Ten Jack Queen King".split()[self.rank]
def Suite(self):
return "Hearts Spades Clubs Diamonds".split()[self.suite]
def __str__(self):
#print "Get Self:",type(self)
#print "Dir:",dir(self)
#return "OF"
return self.Rank()+" of "+ self.Suite()
class Hand:
def __init__(self):
self.cards = []
def Score(self):
aces_ct = 0
score = 0
for c in self.cards:
if c.rank == 0:
aces_ct += 1
score += 11
if 0 < c.rank < 9:
score += c.rank+1
else:
score += 10
while score > 21 and aces_ct > 0:
score -= 10
aces_ct -= 1
return score
def add(self,card):
self.cards.append(card)
def Show(self,show_only=None):
if not show_only:
for k in self.cards:
print "%s"%k
else:
if isinstance(show_only,int):
print "%s"%self.cards[show_only]
elif isinstance(show_only,(list,tuple)):
for idx in show_only:
print "%s"%self.cards[idx]
class deck:
def __init__(self):
self.cards = []
for i in range(4):
for j in range(13):
self.cards.append(Card(j,i))
random.shuffle(self.cards)
def shuffle(self):
random.shuffle(self.cards)
def pop(self):
return self.cards.pop()
if __name__ == "__main__":
d = deck()
player_hand = Hand()
dealer_hand = Hand()
player_hand.add(d.pop())
dealer_hand.add(d.pop())
player_hand.add(d.pop())
dealer_hand.add(d.pop())
print "Player Score :",player_hand.Score()
player_hand.Show()
print "\n\nDealer Score :",dealer_hand.Score()
dealer_hand.Show()
答案 1 :(得分:0)
您可以将汽车表示为元组:表示套装的字符和表示值的数字。我还要为元组添加第三个值,它的二十一点值(例如一个国王和一个女王都算作10)。
card = ('D', 11, 10) # card is queen of diamonds
你也可以自己上课代表卡片。
class Card(object):
def __init__(self, suit, number):
self.suit = suit
self.number = number
self.value = (number if 2 <= number <= 9 else 10)
当然,你必须特别考虑王牌的价值。
答案 2 :(得分:0)
您应该动态计算总数。此外,您需要某种方式来存储每个玩家的钱。现在,没有办法知道钱的分配,因为你只有一个。