2017-11-30 4 views
1

これは21またはブラックジャックのゲームのコードです。最後のラインでは、勝ったゲームの数と失われたゲームの数は表示されませんが、それを修正する方法を理解することはできません。誰かがこれを訂正するのを助けることができる?私はこれをPython 3.6.1で行った。ラインを越えたゲームが機能していません。勝って失われたゲームを正しく表示しないのはなぜですか?

P.S.ここで改善できることが他にもある場合は、フィードバックが高く評価されます。私はすべてのモジュールを持っていないので、

import random 
from random import choice 
import time 
import colorama 
from colorama import Fore, Back, Style 
name = str(input("Welcome to " + Fore.YELLOW + "21" + Style.RESET_ALL + "! I'll be your dealer. \nWhat's your name?")) 
instructions="" 
while instructions!="N" and instructions!="Y": 
    instructions=str(input("Would you like to see the Instructions? Y/N")) 
    if instructions!="N" and instructions!="Y": 
    print("That is not a valid answer.") 
    else: 
    time.sleep(1) 
if instructions=="Y": 
    print(Fore.GREEN + "Instructions:" + Style.RESET_ALL + "\nYour objective is to get as close as you can to " + Fore.YELLOW + "21" + Style.RESET_ALL + ".\n>Each round, you will be dealt two standard cards. You must add these and the person with the closest total wins. \n>You will choose whether to 'hit' or 'miss' every round. If you hit, you get dealt another card. However, be careful as going over " + Fore.YELLOW + "21 " + Style.RESET_ALL + "causes you to bust and lose! \n>Picture cards are worth 10 points each.\n>Aces are 1 or 11, randomly.") 
    time.sleep(1) 
else: 
    print("") 
rounds=int(input("How many rounds do you want to play " + name + "?")) 
gamesLost=0 
gamesWon=0 
def twentyone(): 
    roun=0 
    for i in range(0,rounds): 
    gamesLost=0 
    gamesWon=0 
    roun+=1 
    print("Round " + str(roun)) 
    cards=['Ace','Ace','Ace','Ace','2','2','2','2','3','3','3','3','4','4','4','4','5','5','5','5','6','6','6','6','7','7','7','7','8','8','8','8','9','9','9','9','10','10','10','10','Jack','Jack','Jack','Jack','Queen','Queen','Queen','Queen','King','King','King','King'] 
    cardVal = {'Ace':random.randint(1,11),'King':10,'Queen':10,'Jack':10,'10':10,'9':9,'8':8,'7':7,'6':6,'5':5,'4':4,'3':3,'2':2} 
    card1=random.choice(cards) 
    cards.remove(card1) 
    card2=random.choice(cards) 
    cards.remove(card2) 
    hands=(str(card1) + ", " + str(card2)) 
    playerHand = cardVal[card1]+cardVal[card2] 
    print("Your hand is " + hands + ". Your hand is worth " + str(playerHand) + ".") 
    hit="" 
    while hit!="H" and hit!="S" and hit!="h" and hit!="s": 
     hit=str(input("Do you want to hit (H) or stay (S)?")) 
     if hit!="H" and hit!="S" and hit!="h" and hit!="s": 
     print("That is not a valid answer.") 
     else: 
     time.sleep(0.01) 
    while hit=="H" or hit=="h": 
     card=random.choice(cards) 
     cards.remove(card) 
     playerHand+=cardVal[card] 
     hands=(hands + ", " + str(card)) 
     print("Your hand is " + hands + ". Your hand is worth " + str(playerHand) + ".") 
     if playerHand>21: 
     break 
     else: 
     time.sleep(0.01) 
     hit=str(input("Do you want to hit (H) or stay (S)?")) 
    if playerHand>21: 
     print("Sorry, you bust!") 
     gamesLost+=1 
     i+=1 
    else: 
     time.sleep(0.01) 
    cpuCard1=random.choice(cards) 
    cards.remove(cpuCard1) 
    cpuCard2=random.choice(cards) 
    cards.remove(cpuCard2) 
    handsCPU=(str(cpuCard1) + ", " + str(cpuCard2)) 
    computerHand=cardVal[cpuCard1]+cardVal[cpuCard2] 
    while computerHand<17: 
     if computerHand<17: 
     cpuCard=random.choice(cards) 
     cards.remove(cpuCard) 
     computerHand+=cardVal[cpuCard] 
     handsCPU=(handsCPU + ", " + str(cpuCard)) 
     else: 
     time.sleep(0.01) 
    if computerHand>21: 
     print("I'm bust! You win.") 
     gamesWon+=1 
     i+=1 
    elif computerHand>playerHand: 
     print("My hand is " + str(handsCPU) + ". It is worth " + str(computerHand) + ". You lose.") 
     gamesLost+=1 
     i+=1 
    else: 
     print("My hand is " + str(handsCPU) + ". It is worth " + str(computerHand) + ". You win.") 
     gamesLost+=1 
     i+=1 
twentyone() 
print("\nGame Over!") 
print("You won " + str(gamesWon) + " and lost " + str(gamesLost) + ".") 
again=str(input("Do you want to go again? Y/N")) 
if again=="Y": 
    rounds=int(input("How many rounds do you want to play " + name + "?")) 
    twentyone() 
elif again=="N": 
    print("Have a nice day.") 
else: 
    print("Have a nice day.") 
+2

問題を再現するために必要な最小限の量に含めるコードを減らすよう努力してください。 [最小限の完全かつ検証可能なサンプルを作成する方法](https://stackoverflow.com/help/mcve)を参照してください。 – glibdud

答えて

2

残念ながら、私はそれを自分自身をテストすることはできません。しかし、私はあなたの問題は、ラインプリントがあなたが勝ち、毎回0試合を失ったということです。

それはあなたがすべての新しいゲームであなたのゲームのカウンターをリセットしているように見えます:

for i in range(0,rounds): 
    gamesLost=0 
    gamesWon=0 

あなたは1つのゲームをプレイ、レコードゲームが優勝し、失われたときにループし、次の試合のために0に両方をリセットやり直すループの直前 - 両方のカウンタを初期化しているので、両方のラインを削除しても効果があります。または、ループの前に行を移動し、重複している場合は別のコピーを削除することもできます。しかし、関数内にカウンタを定義する方が良い方法です。

関連する問題