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循環點擊不同,任何想法。
請記住,這是有效的取樣替換,所以每張卡片將始終有相同的機會被繪製,但無論你做出多少命中。這使得卡片計數不可能。如果你想對二十一點進行更逼真的模擬,你可以生成一個包含N套牌的所有牌的列表,隨機播放(random.shuffle完成這個技巧),並保留要繪製的下一張牌上的索引,或者使用彈出刪除並從列表中獲取一張卡。 –
謝謝,幫了很多! +1 – Rep
感謝您提供.shuffle(),我沒有意識到這一點 – mmenschig