2013-01-17 39 views
-3

我在python中创建黑色杰克游戏。函数循环时,如何将结尾货币变量移动到函数顶部?Python - 黑杰克程序

print "Welcome to BlackJack" 

def run(): 

    import random 
    from random import choice 
    import sys 
    money = 500 

变量'钱'会根据玩法是赢还是输。当播放选择再次播放时,我希望结尾变量成为开始变量。

raw_input("Press <ENTER> To Begin") 
    print "You have $",money,"in your bank." 
    bet = raw_input("How much would you like to bet?") 

    b = int(bet) 

    cards = [1,2,3,4,5,6,7,8,9,10,10,10,10]*4 

    c1 = choice(cards) 
    cards.remove(c1) 

    c2 = choice(cards) 
    cards.remove(c2) 

    psum = c1 + c2 

    print "You were dealt a",c1,"and a",c2,"for a sum of",psum, 
    print "\n" 
    hs = " " 

    while psum < 21 and "s" not in hs: 
     hs = raw_input("Hit or Stand (h or s): ").lower() 
     if "h" in hs: 
      c3 = choice(cards) 
      cards.remove(c3) 
      psum = psum + c3 
      print "You were dealt a",c3,"for a sum of",psum, 
      print "\n" 
     elif "s" in hs: 
      print "Your final sum is",psum, 

    print "\n" 

    if psum > 21: 
     print "Bust!" "\n" "You lose." "\n" 
     money = money - b 
     print "You now have $",money,"in your bank." 
    elif psum == 21: 
     print "You got a BlackJack!" "\n" "You win!" "\n" 
     money = money + b 
     print "You now have $",money,"in your bank." 
    else: 
     print "Dealer's turn" 

    if psum < 21: 
     c4 = choice(cards) 
     cards.remove(c4) 

     c5 = choice(cards) 
     cards.remove(c5) 

     dsum = c4 + c5 

     while dsum < 17: 
      c6 = choice(cards) 
      cards.remove(c6) 
      dsum = dsum + c6 

     if dsum > 21: 
      print "Dealer's final sum is",dsum,"\n" 
      print "Dealer bust! You win!" "\n" 
      money = money + b 
      print "You now have $",money,"in your bank." 
     elif dsum < psum: 
      print "Dealer's final sum is",dsum,"\n" 
      print "You win!" "\n" 
      money = money + b 
      print "You now have $",money,"in your bank." 
     elif dsum == psum: 
      print "Dealer's final sum is",dsum,"\n" 
      print "Draw." "\n" 
      print "You have $",money,"in your bank." 
     else: 
      print "Dealer's sum is",dsum,"\n" 
      print "You lose." "\n" 
      money = money - b 
      print "You now have $",money,"in your bank." 


    yn = raw_input("Would you like to play again? (y or n): ") 

    if "y" in yn: 
     print "\n" * 5 
     run() 
    else: 
     print "\n" "Your total winnings is $",money, 
     sys.exit()   

run()  

回答

0

定义你的函数是这样的:

def run(startingFunds = None): 

    <brilliant code> 

    money = 500 if startingFunds is None else startingFunds 

    <brilliant code> 

    if "y" in yn: 
     print "\n" * 5 
     run(money) 

关于第二个想法,做iamnotmaynard暗示什么,并把while循环围绕它。但是我仍然会采取startingFunds作为参数传递给功能。

(PS:他得到检查:))

2

而不是调用每个玩家选择再次上场时间run(),你应该把所有的代码在一个循环中,当玩家选择它打破“不”。这样money变量将继续保持其价值。

编辑:将代码移动到单独的方法中(例如,清洁和可维护的代码)肯定是有利的。 deal_a_hand(),并且每次将money变量传递给它(您可能需要使用return money的方法),但最好从主方法的循环中调用它,而不是使用不必要的递归。一般而言,除非使程序更高效或更容易编写,否则您不需要调用自己的方法,即使如此,您也必须考虑递归的深度。

+0

除了美学以外,其他任何原因都比另一个更好吗? – BenDundee

+0

循环的一个优点是每次调用run()时,它都会将另一轮变量放入堆栈,并通过不必要的“进口”,而循环只保留一组变量。 – iamnotmaynard

+0

@BenDundee是的,函数调用堆栈不会通过run()一直调用自身来保持构建。 –

1

最简单的,这是将参数添加到run

def run(money): 

删除行money = 500,呼叫run如环run(money)run(500)的第一次。

我建议去掉从run“再玩一轮”的逻辑,这样

def run_single_hand(money): 
    # <code to run hand, change value of money> 
    return money 

def play_hands(): 
    starting_money = 500 
    money = starting_money 
    money = run_single_hand(money) 
    while True: 
     # <code to ask if they would like to play again 
     if again: 
      run_single_hand(money) 
     else: 
      print 'thank you, you made a profit of %d' % money - starting_money 
      break 

因为这避免了递归问题(这样做我认为最终会以N在堆栈上呼吁run第一种方式)并且仍然很好地考虑你的代码。

例如,你可以修改这个来替代run_single_hand来做扑克。对于这个例子来说,这看起来微不足道,但对于更复杂的项目来说,它是一个很好的代码模式

+0

这比较好,因为控制玩家开始游戏的金额比较容易。 – BenDundee