退出我的BlackJack游戏

时间:2015-02-15 01:37:55

标签: python

需要一些帮助退出这个游戏...它几乎按照我想要的方式工作,但如果玩家选择"留下"作为一种选择,虽然房子也选择了"停留"然后游戏只是在无休止的循环中停留直到用户点击。我希望游戏能够进入 如果房屋和玩家决定同时停留,则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()    

感谢任何回复人员!

1 个答案:

答案 0 :(得分:0)

您可以在此处查看我如何实施二十一点模拟器:https://github.com/skyl/broken-knuckles

你的第一个问题似乎是你让玩家被击中,然后经销商可以击中,那么玩家可以再次击中?这不是二十一点的运作方式。每个玩家在经销商完全玩之前完全玩。所以,一旦经销商完成,你就结束比赛,看看谁赢了。当房子停留时,游戏就结束了。

相关问题