簡體   English   中英

退出我的BlackJack游戲

[英]Exit my BlackJack game

需要一些幫助來退出此游戲...幾乎可以按照我想要的方式工作,但是如果玩家選擇“ stay”作為選項,而房屋也選擇了“ stay”選項,那么游戲就只是在持續不斷的停留,直到用戶點擊為止。 如果房子和玩家決定同時停留,我希望游戲進入end_game(house_cards, new_player_hand)函數。 另外,如果您在此程序中發現任何其他愚蠢的錯誤或完全無效,請告訴我! 也對缺乏可讀性感到抱歉...我需要添加一些評論!

import random
import numpy as np
def start_game():
    while True:
        Choice = raw_input("Do you Want to Play Black Jack? Y/N?\n").lower()
        if Choice == "y":
            shuffle(cards)
            deal(cards)
            player_turn(player_hand, cards)
        elif Choice == "n":
            exit()
        else:
            print "Please Choose Y or N"
def shuffle(cards):
    cards = [x for x in cards for y in range(4)]
    return cards


def player_turn(player_hand, cards):
    Choice_1 = raw_input("Would you like to hit, fold, or stay?\n").lower()
    if Choice_1 == "hit":
        hit(player_hand,cards)
    elif Choice_1 == "fold":
        print "Lame... You lose!"
        start_game()
    elif Choice_1 == "stay":
        house_ai(house_hand, cards)
    else:
        print "please choose to hit, fold, or stay\n"
    return player_hand, cards

def hit(player_hand, cards): 
    global new_player_hand
    rand_card = random.choice(cards)
    player_hand.append(rand_card)
    new_player_hand = player_hand
    print new_player_hand
    if np.sum(new_player_hand) > 21:
        print "You went over 21!"
    elif np.sum(player_hand) < 21:
        Choice_3 = raw_input("Would you like to hit or stay?\n").lower()
        if Choice_3 == "hit":
            hit(new_player_hand, cards)
        elif Choice_3 == "stay":
            print "Okay house turn!"
            house_ai(house_hand, cards)
        else:
            print "Choose hit or stay"
    elif np.sum(new_player_hand) == 21:
        print "You win!"
    return player_hand, cards , new_player_hand


def house_ai(house_hand, cards):
    global house_cards
    house_cards = np.sum(house_hand)
    if house_cards == 17: #house stays
        print "House stays."    
        player_turn(player_hand, cards)
    elif house_cards > 17 and house_cards <21:
        print "House stays."
        player_turn(player_hand, cards)
    elif house_cards < 17:
        print "House hits."
        house_less(house_hand, house_cards, cards)  
    return house_hand, house_cards, cards

def house_less(house_hand, house_cards, cards):
    if np.sum(house_hand) < 17:
        new_card = random.sample(set(cards), 1)
        print "The House hits."
        house_hand.extend(new_card)
        print house_hand
        house_less(house_hand, house_cards, cards)
    elif np.sum(house_hand) > 21:
        print "The House hits."
        house_greater(house_cards)
    elif np.sum(house_hand) == 17:
        player_turn(player_hand, cards)
    elif np.sum(house_hand) > 17 and house_cards < 21:
        player_turn(player_hand, cards)
    elif np.sum(house_hand) == 21:
        house_wins(house_cards)
    return house_hand, house_cards, cards

def house_greater(house_cards):
    print "You Win! The house went over 21!"
    return house_cards

def end_game(house_cards, new_player_hand):
    if new_player_hand > house_cards:
        print "YOU WIN! YOU HAVE HIGHER CARDS THAN THE HOUSE"
    elif new_player_hand < house_cards:
        print "YOU LOSE! HOUSE HAS HIGGHER CARDS"
    else:
        assert False

def house_wins(house_cards):
    if house_cards == 21:
        print house_hand
        print " Sorry the House wins!"
    return house_cards


def deal(cards):
    global player_hand
    global house_hand
    player_hand = random.sample(set(cards),2)
    house_hand = random.sample(set(cards),2)
    print "Here are your cards!", player_hand


    return player_hand
    return house_hand
cards = [1, 2, 3, 4, 5, 6, 7, 8, 9 ,10 , 10, 10, 10]

start_game()    

謝謝你們的任何回應!

您可以在這里查看我如何實現二十一點模擬器: https//github.com/skyl/broken-knuckles

您的第一個問題似乎是您要讓玩家命中,然后發牌手可以命中,然后玩家又可以命中? 那不是二十一點的工作方式。 每個玩家在庄家完全玩之前都已經完全玩了。 所以,一旦發牌人完成,您就結束了比賽,看看誰贏了。 當房子呆着時,游戲結束了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM