必须有更好的方法来做到这一点......任何想法?这是CS1的基本二十一点游戏。
困扰我的两件主要事情:
谢谢!任何帮助赞赏。
import random
import sys
# game, hand, deck
class Deck:
theDeck = []
card = 0
def createDeck(self):
for x in range(2, 15):
for y in range(1, 5):
self.theDeck.append(x)
def extractCard(self):
card = self.theDeck.pop(random.randrange(1, len(self.theDeck) - 1))
return card
class Game:
dealerTotal = [0, 0]
playerTotal = 0
deck = Deck()
deck.createDeck()
def reset(self):
self.dealerTotal = [0, 0]
self.playerTotal = 0
def dealerEnd(self):
if self.dealerTotal[0] == 21 or self.dealerTotal[1] == 21:
print("BLACKJACK! Game over.\n\nDEALER'S TURN")
self.reset()
return True
elif self.dealerTotal[0] > 21 and self.dealerTotal[1] > 21:
print("PLAYER WINS! Game over.\n\nDEALER'S TURN")
self.reset()
return True
return False
def playerEnd(self):
if self.playerTotal == 21:
print("BLACKJACK! Game over.\n\nDEALER'S TURN")
self.reset()
return True
elif self.playerTotal > 21:
print("DEALER WINS! Game over.\n\nDEALER'S TURN")
self.reset()
return True
return False
def dealerChangeTotal(self,hold):
self.dealerTotal[0] += hold
self.dealerTotal[1] += hold
print("Total ::"), self.dealerTotal
self.dealerEnd()
def playerChangeTotal(self,hold):
self.playerTotal += hold
print("Total ::"), self.playerTotal
self.playerEnd()
def play(self):
while len(self.deck.theDeck) > 0:
print("DEALER'S TURN")
if self.dealerTotal[0] >= 16 or self.dealerTotal[1] >= 16:
print("I can't take. Your go.")
while self.dealerTotal[0] < 16 or self.dealerTotal[1] < 16:
hold = self.deck.extractCard()
if hold == 14:
print("Card :: ace")
self.dealerTotal[0] += 1
self.dealerTotal[1] += 11
print("Total ::"), self.dealerTotal
if self.dealerEnd() == True:
break
elif hold == 13:
hold = 10
print("Card :: king")
self.dealerChangeTotal(hold)
if self.dealerEnd() == True:
break
elif hold == 12:
hold = 10
print("Card :: queen")
self.dealerChangeTotal(hold)
if self.dealerEnd() == True:
break
elif hold == 11:
hold = 10
print("Card :: jack")
self.dealerChangeTotal(hold)
if self.dealerEnd() == True:
break
else:
print("Card ::"), hold
self.dealerChangeTotal(hold)
if self.dealerEnd() == True:
break
print("\n\nPLAYER'S TURN")
while True:
hold = self.deck.extractCard()
if hold == 14:
print("Card :: ace")
self.playerTotal += input("Do you want a 1 or an 11? ")
print("Total ::"), self.playerTotal
response = raw_input("Stand or hit? ")
if response == "stand":
break
if self.playerEnd() == True:
break
elif hold == 13:
hold = 10
print("Card :: king")
self.playerChangeTotal(hold)
response = raw_input("Stand or hit? ")
if response == "stand":
break
if self.playerEnd() == True:
break
elif hold == 12:
hold = 10
print("Card :: queen")
self.playerChangeTotal(hold)
response = raw_input("Stand or hit? ")
if response == "stand":
break
if self.playerEnd() == True:
break
elif hold == 11:
hold = 10
print("Card :: jack")
self.playerChangeTotal(hold)
response = raw_input("Stand or hit? ")
if response == "stand":
break
if self.playerEnd() == True:
break
else:
print("Card ::"), hold
self.playerChangeTotal(hold)
response = raw_input("Stand or hit? ")
if response == "stand":
break
if self.playerEnd() == True:
break
print("\n\n")
#... Main?
game = Game()
game.play()
答案 0 :(得分:5)
在外部循环开始时创建一个标志为FALSE ...然后在内部循环中,当需要打破2个循环时将其设置为TRUE,你去,现在你将打破内部循环,并检查外部循环中的标志循环,如果它是真的,那么(外循环)也会突破。
while ...:
flag = 0
while ...:
# point where you want 2 loops to break
flag = 1
break
# break out of this too :P
if flag == 1:
break