2015-02-15 5 views
1

нужна помощь в выходе из этой игры ... Это почти работает так, как я этого хочу, но если игрок выбирает «остаться» в качестве опции, в то время как дом также выбирает вариант «остаться», то игра находится в бесконечном цикле пребывания до тех пор, пока пользователь не ударит. Я хочу, чтобы игра перешла к функции end_game(house_cards, new_player_hand), если дом и игрок решили одновременно оставаться в одном месте. Кроме того, если вы видите какие-либо другие глупые ошибки или общую неэффективность в этой программе, пожалуйста, скажите мне! Также жаль отсутствие читаемости ... Мне нужно добавить несколько комментариев!Выход из моей игры BlackJack

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()  

Благодарим за любые ответы ребята!

ответ

0

Вы можете посмотреть, как я реализовал блэкджек тренажер здесь: https://github.com/skyl/broken-knuckles

Ваша первая проблема, как представляется, в том, что вы позволяете игрок хит, то дилер может ударить, то игрок может попасть снова? Это не так, как работает блэкджек. Каждый игрок играет полностью, прежде чем дилер будет играть полностью. Итак, как только дилер закончен, вы заканчиваете игру и видите, кто выиграл. Когда дом остается, игра окончена.

+0

Wow thats right ... Я полностью забыл, что игра идет ... возможно, следовало бы исследовать ее немного больше. Думаю, что на самом деле решает мой вопрос, спасибо! Кроме того, ваш симулятор велик. Мне нужно научиться более объектно-ориентированному подходу. Если у вас есть какие-то советы, которые вы хотели бы бросить, они были бы очень благодарны. – tear728