这是我到目前为止的代码:
import sys
import os
import random
Question():
os.system('cls')
SQ=input('Do you want to play blackjack y/n')
if(SQ == y or SQ == Y):
StartGame()
if(SQ == n or SQ == N):
sys.exit()
if(SQ != n and SQ != N and SQ != y and SQ != Y):
print('You did answer the question with a y or a n which correspond to yes and no accordingly')
Question()
Question()
StartGame():
slot1=False
slot2=False
slot3=False
slot4=False
slot5=False
slot6=False
slot7=False
slot8=False
slot9=False
slot10=False
slot11=False
slot12=False
slot13=False
slot14=False
slot15=False
slot16=False
slot17=False
slot18=False
slot19=False
slot20=False
slot21=False
slot22=False
slot22=False
slot23=False
Slot24=False
slot25=False
slot26=False
slot27=False
Slot28=False
slot29=False
slot30=False
slot31=False
slot32=False
slot33=False
slot34=False
slot35=False
slot36=False
slot37=False
slot38=False
slot39=False
slot40=False
slot41=False
slot42=False
slot43=False
slot44=False
slot45=False
slot46=False
slot47=False
slot48=False
slot49=False
slot50=False
slot51=False
slot52=False
aceHEART = randrange(1, 52)
aceHEART
我不明白将插槽和随机数生成器放在一起进行随机洗牌的正确方法。 我怎样才能做到它不会尝试在一个插槽中放置多张卡。 我也不知道如何以更有效的方式管理这些卡。 我正在用python制作一个二十一点游戏,我不知道解决这个问题的正确方法。 请尽你所能帮助我。
不确定你想做什么,但这里有一种方法可以生成一副洗牌:
ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suite = ['clubs', 'hearts', 'spades', 'diamonds']
deck = [r + ' ' + s for r in ranks for s in suite]
random.shuffle(deck)
或与对象:
class Card(object):
def __init__(self, rank, suite):
self.rank = rank
self.suite = suite
deck = [Card(r,s) for r in ranks for s in suite]
random.shuffle(deck)
学会爱列表,用数字来表示卡片,而不是字符串。下面是一个简单的卡片类,应该可以很好地工作:
class Card(object):
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __str__(self):
return "23456789TJQKA"[self.rank] + "cdhs"[self.suit]
然后创建只是牌列表的套牌和手牌:
deck = [ Card[r,s] for r in range(13) for s in range(4) ]
random.shuffle(deck)
例如,为什么要使用手牌和牌组列表,因为发牌很简单:
hand.append(deck.pop())
为什么要使用数字而不是字符串来表示卡片等级,以便可以添加和比较它们。您可以在上面的 Mihai 代码中添加一个"值"成员,这会有所帮助。在我的身上,你只需要稍微调整一下数字:
def bjTotal(hand):
total = 0
hasAce, isSoft = False, False
for card in hand:
if card.rank == 12:
hasAce = True
total += 1
elif card.rank > 7:
total + 10
else:
total += card.rank + 2
if hasAce and total < 12:
isSoft = True
total += 10
return total, isSoft
类在表示纸牌和游戏方面做得很好
import random
class Card:
def __init__(self,rank,suit):
self.rank = rank
self.suit = suit
def rankName(self):
return "A23456789TJQK"[self.rank]
def suitName(self):
return "HCDS"[self.suit]
def __int__(self):
if self.rank > 8: return 10
if self.rank == 0:return 11
return self.rank + 1
def __eq__(self,other):
try:
return self.rank == other.rank
except:
return self.rank == other
def __str__(self):
return self.rankName() +self.suitName()
def __repr__(self):
return "<Card: %s>"%self
class Deck:
def __init__(self,cards=None):
if cards is None:
cards = [Card(rank,suit) for rank in range(13) for suit in range(4)]
self.cards = cards
random.shuffle(self.cards)
def draw(self):
return self.cards.pop(0)
def __add__(self,other):
return Deck(self.cards+other.cards)
class Hand:
def __init__(self):
self.cards = []
def __int__(self):
total = sum(map(int,self.cards))
aces = self.cards.count(0)
while aces > 0 and total > 21:
aces -= 1
total -= 10
return total
def put(self,card):
self.cards.append(card)
def __str__(self):
return ", ".join(map(str,self.cards))
def __repr__(self):
return "<Hand %s>"%self.cards
有了课程后,您现在可以开始构建游戏了
class Game:
def __init__(self,n_players=1,use_dealer=True):
self.main_deck = Deck()+Deck() # 2 deck shoe
self.n_players = n_players
self.dealer = use_dealer
def play_hand(self,hand):
while int(hand) <= 21 and raw_input("%rnHit?"%hand)[0].lower() == "y" :
hand.put(self.main_deck.draw())
if int(hand) > 21:
print "BUST!!!"
def play_game(self):
current_player = 0
hands = [Hand() for _ in range(self.n_players+self.dealer)]
for i in range(2):
for hand in hands:
hand.put(self.main_deck.draw())
while current_player < len(hands) - self.dealer:
self.play_hand(hands[current_player])
current_player += 1
if self.dealer:
while int(hands[-1]) < 17:
hands[-1].put(self.main_deck.draw())
print "DEALER HITS:",hands[-1]
print "FINAL SCORES:"
print "n".join("%s. %r %d"%(i,h,h) for i,h in enumerate(hands))
game = Game()
game.play_game()
(反正类似的东西)