到目前为止,这是我的代码:
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中制作二十一点游戏,我不知道接近这个的正确方法。请以最好的方式帮助我。
答案 0 :(得分:1)
不确定你要做什么,但这是一种生成洗牌的方法:
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)
答案 1 :(得分:0)
学习爱情列表,并使用数字来表示卡片,而不是字符串。这是一个简单的卡片类,可以很好地工作:
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
答案 2 :(得分:0)
课程可以很好地代表卡片和游戏
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("%r\nHit?"%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()
(无论如何都是这样的)