2016-11-16 81 views
1

我的黑杰克代码是非常基本的,但运行相当顺利,但是我遇到了减速带。因此,我在这里。当我打电话给我时,在我的While循环中向我发送另一张卡片,对于每个循环,DECK实例化相同的卡片。前两张和Hit卡总是不同,但是在While循环中(当玩家说“停留”并且不需要另一张卡时设置为结束),Hit卡保持不变。Python黑杰克游戏变种

import random 
import itertools 
SUITS = 'cdhs' 
RANKS = '23456789TJQKA' 
DECK = tuple(''.join(card) for card in itertools.product(RANKS, SUITS)) 
hand = random.sample(DECK, 2) 
hit = random.sample(DECK, 1) 

print("Welcome to Jackpot Guesser! ") 
c = input("Would you like to play Black Jack or play Slots? ") 
if c == 'Black Jack': 
    print() 
    print("Welcome to Black Jack! Here are the rules: ") 
    print("Black Jack is a game of whit, skill with a bit of Luck. You will start with 2 card and try to achieve a total of 21 points. \n Each cards worth is equal to its number, face cards are worth 10 and the Ace can be 1 or 11 points. You decide. \n You can decide to -Stay- and take the points you have or -Hit- to get another card. If you get 21 its Black Jack and you win. \n If no one gets 21, the highest points win, but if you go over 21 you -Bomb- and lose everything. \n Becarful not to Bomb and goodluck out there! Remember, you got to know when to Hit, when to Stay and when to walk away! \n") 
    print(hand) 
    print() 

    g = 'swag' 
    while g != 'Stay': 
     g = input(("What would you like to do, Stay or Hit: ")) 
     if g == 'Hit': 
      print(hit) 
     elif g == 'Stay': 
      print("Lets see how you did!") 
     else: 
      print("test3") 
elif c == 'Slots': 
      print("test") 
else: 
    print("test2") 

EX:手:TD(两颗钻石),3C(3俱乐部) 命中:7S(7张黑桃) 打7S 打7S 打7S ... 住宿:让我们看看如何你没有。我需要继续While循环点击不同,任何想法。

回答

1

问题在于,在程序启动期间,您只生成一次匹配卡。从

if g == 'Hit': 
     print(hit) 

的东西改变你的代码像

if g == 'Hit': 
     hit = random.sample(DECK, 1) 
     print(hit) 

将使每个命中输出不同的卡。

+2

请记住,这是有效的取样替换,所以每张卡片将始终有相同的机会被绘制,但无论你做出多少命中。这使得卡片计数不可能。如果你想对二十一点进行更逼真的模拟,你可以生成一个包含N套牌的所有牌的列表,随机播放(random.shuffle完成这个技巧),并保留要绘制的下一张牌上的索引,或者使用弹出删除并从列表中获取一张卡。 –

+0

谢谢,帮了很多! +1 – Rep

+0

感谢您提供.shuffle(),我没有意识到这一点 – mmenschig