2016-11-16 6 views
1

私の黒いジャックコードは非常に基本的ですが、非常にスムーズに走っていますが、スピードバンプに走りました。したがってここに私は。 Whileループで別のカードを送るために "Hit"を呼び出すと、DECKはすべてのループに対して同じカードをインスタンス化します。最初の2枚とヒットカードは常に異なりますが、Wh​​ileループ内(プレイヤーが「滞在」と言い、別のカードを望んでいないと終了するように設定されています)のヒットカード内は同じです。Python Black Jackゲームの変形

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と(2つの菱形)、3C(3クラブ) ヒット: 7S ヒット7S 7S(7枚のスペード) ヒット7S ヒット... 滞在は:どのようにあなたを見ることができますした。私は、Whileループのヒットが異なる、任意のアイデアを継続する必要があります。

答えて

1

問題は、ヒットカードをプログラムの開始時に1回だけ生成することです。

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

のようなものに

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

からあなたのコードを変更すると、それは各ヒットに異なるカードを出力ようになります。

+2

これは実質的に置換でサンプリングされるので、各カードは常に同じ確率で描画されますが、多くのヒットがあります。これにより、カードのカウントが不可能になります。よりリアルなブラックジャックのシミュレーションをしたい場合は、Nデッキのすべてのカードを含むリストを生成し、シャッフルして(random.shuffleはトリックを行います)、次のカードのインデックスを引き続けるか、ポップしてリストからカードを1枚取り出します。 –

+0

ありがとうございました。 +1 – Rep

+0

.shuffle()を提案してくれてありがとう、私はそれを知らなかった – mmenschig