[英]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.