Python实现国外赌场热门游戏Craps(双骰子)

 更新时间:2015年03月31日 17:20:27   投稿:hebedich   我要评论
这篇文章主要介绍了Python实现国外赌场热门游戏Craps(双骰子)的源码及运行方法,十分简单,有需要的小伙伴可以参考下。

运行方法:

    1. 打开python2 IDLE;
    2. 输入 from craps import *
    3. 按提示输入运行命令。例如,玩游戏就输入play();查看余额就输入check_bankroll();
        自动玩看胜率就输入auto()

craps.py

import random
point_set = False
bet = 10
bankroll = 1000
sim_win = 0
sim_lose = 0
print """
     Welcome to the 'Seven Star' casino!
     You are playing craps now,
     your started bankroll is '$1000',
     the started bet is '$10',
     command: 
       play(): "Rolling the dices"
       check_bankroll(): "Checking your current balance"
       all_in(): Showing "hand"
       set_bet(): "Setting a new bet"
       game(): "Check your game status"
       auto(): "It can be played automatically for you until reach a specific bankroll"
"""
def roll():
  d1 = random.randrange(1,7)
  d2 = random.randrange(1,7)
  print "You rolled", d1, "+", d2, "=", d1+d2
  return d1 + d2
def play():
  global point_set, bankroll, point
  global sim_win, sim_lose
  if bankroll < bet:
    print "Sorry, you can't play since you don't have enough money!"
    print """Do you wanna get more money?
        1: Yes
        2: No
       """
    choice = raw_input(">>")
    if choice == str(1):
      money = raw_input("How much do you wanna get?")
      bankroll += int(money)
      print "Your current bankroll is: ", bankroll
    if choice == str(2):
      print "Thanks for playing! See you next time!"
  else:
    if not point_set:
      print
      print "New game. Your bet is: ", bet
    # for the first roll
    r = roll()
    if not point_set:
      if r in (7, 11):
        bankroll += bet
        sim_win += 1
        print "Congratz! You Won! Your bankroll is: ", bankroll
      elif r in (2, 3, 12):
        bankroll -= bet
        sim_lose += 1
        print "Oops! You lost! Your bankroll is: ", bankroll
      else:
        point = r
        point_set = True
        print "Your point is", "[", point, "]"
    # for subsequence rolls
    elif r == 7:
      bankroll -= bet
      sim_lose += 1
      point_set = False
      print "You crapped out! Your bankroll is: ", bankroll 
    elif r == point:
      bankroll += bet
      sim_win += 1
      point_set = False
      print "You made your point! Your bankroll is: ", bankroll
def set_bet(inp):
  global bet, bankroll, point_set
  print
  if point_set:
    print "WARNING!"
    print "The game has started, you will lose half of your bet if resetting your bet!"
    prompt = raw_input("""
      1: Yes, I am wanna reset my bet!
      2: No, I don't wanna reset my bet!
              """)
    if prompt == "1":
      point_set = False
      bankroll -= bet/2
      print "Forfeiting current bet. Your bankroll is: ", bankroll
    else:
      pass
  bet = int(inp)
  print "New bet size is: ", bet
def all_in():
    set_bet(bankroll)
def check_bankroll():
  global bet
  print "Your current balance is: ", bankroll
def game():
  total = sim_win + sim_lose
  percent = float(sim_win)/total * 100
  print "So far, the games that you have been playing are: ", total 
  print "Won ", sim_win
  print "Lost ", sim_lose
  print "Overall, you have %d%% to win!" %percent
def auto():
  game_status = True
  purpose = raw_input("How much are you gonna reach? ")
  while game_status:
    play()
    if bankroll == int(purpose) or bankroll == 0:
      game_status = False
  game()

以上所述就是本文的全部内容了,希望能够对大家学习Python有所帮助。

相关文章

  • 使用Python写个小监控

    使用Python写个小监控

    最近使用python写了个小监控,为什么使用python?简单、方便、好管理,Python如何实现简单的小监控,感兴趣的小伙伴们可以参考一下
    2016-01-01
  • python使用PyQt5的简单方法

    python使用PyQt5的简单方法

    这篇文章主要介绍了python使用PyQt5的简单方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-02-02
  • python实现二维数组的对角线遍历

    python实现二维数组的对角线遍历

    这篇文章主要为大家详细介绍了python实现二维数组的对角线遍历,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • 在Python程序和Flask框架中使用SQLAlchemy的教程

    在Python程序和Flask框架中使用SQLAlchemy的教程

    SQLAlchemy为Python程序与SQL语句之间建立了映射,是Python操作金沙国际官网的利器,这里我们将来看在Python程序和Flask框架中使用SQLAlchemy的教程,需要的朋友可以参考下
    2016-06-06
  • Python中实现单例模式的n种方式和原理

    Python中实现单例模式的n种方式和原理

    这篇文章主要介绍了Python中实现单例模式的n种方式和原理,需要的朋友可以参考下
    2018-11-11
  • Python封装shell命令实例分析

    Python封装shell命令实例分析

    这篇文章主要介绍了Python封装shell命令,实例分析了Python将各种常用shell命令封装进一个类中以便调用的方法,非常具有实用价值,需要的朋友可以参考下
    2015-05-05
  • python的pytest框架之命令行参数详解(下)

    python的pytest框架之命令行参数详解(下)

    这篇文章主要介绍了python的pytest框架之命令行参数详解,今天将继续更新其他一些命令选项的使用,和pytest收集测试用例的规则,需要的朋友可以参考下
    2019-06-06
  • Python PyCharm如何进行断点调试

    Python PyCharm如何进行断点调试

    这篇文章主要介绍了Python PyCharm如何进行断点调试,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • 浅谈Python 对象内存占用

    浅谈Python 对象内存占用

    下面小编就为大家带来一篇浅谈Python 对象内存占用。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-07-07
  • PyCharm鼠标右键不显示Run unittest的解决方法

    PyCharm鼠标右键不显示Run unittest的解决方法

    今天小编就为大家分享一篇PyCharm鼠标右键不显示Run unittest的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-11-11

最新评论