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


Posted in Python onMarch 31, 2015

运行方法:

    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开发之函数定义实例分析
Nov 12 Python
Python中的CSV文件使用&quot;with&quot;语句的方式详解
Oct 16 Python
Python实现E-Mail收集插件实例教程
Feb 06 Python
python将excel转换为csv的代码方法总结
Jul 03 Python
Python封装成可带参数的EXE安装包实例
Aug 24 Python
基于python求两个列表的并集.交集.差集
Feb 10 Python
Pycharm 安装 idea VIM插件的图文教程详解
Feb 21 Python
python+selenium+Chrome options参数的使用
Mar 18 Python
Python SMTP配置参数并发送邮件
Jun 16 Python
高考考python编程是真的吗
Jul 20 Python
python eventlet绿化和patch原理
Nov 21 Python
Python编程源码报错解决方法总结经验分享
Oct 05 Python
通过代码实例展示Python中列表生成式的用法
Mar 31 #Python
使用Python实现一个简单的项目监控
Mar 31 #Python
详解Python中内置的NotImplemented类型的用法
Mar 31 #Python
python计算N天之后日期的方法
Mar 31 #Python
使用Python3中的gettext模块翻译Python源码以支持多语言
Mar 31 #Python
python根据出生日期获得年龄的方法
Mar 31 #Python
用Python进行一些简单的自然语言处理的教程
Mar 31 #Python
You might like
Re:从零开始的异世界生活 第2季 开播啦
2020/07/24 日漫
杏林同学录(一)
2006/10/09 PHP
php完全过滤HTML,JS,CSS等标签
2009/01/16 PHP
php去除二维数组的重复项方法
2015/11/03 PHP
PHP检测数据类型的几种方法(总结)
2017/03/04 PHP
PHP使用数组实现矩阵数学运算的方法示例
2017/05/29 PHP
PHP去除空数组且数组键名重置的讲解
2019/02/28 PHP
发一个自己用JS写的实用看图工具实现代码
2008/07/26 Javascript
jQuery实现列表自动滚动循环滚动展示新闻
2014/08/22 Javascript
js获取浏览器基本信息大全
2014/11/27 Javascript
JQuery实现的购物车功能(可以减少或者添加商品并自动计算价格)
2015/01/13 Javascript
浅谈JS使用[ ]来访问对象属性
2016/09/21 Javascript
Angular2 多级注入器详解及实例
2016/10/30 Javascript
AngularJS打开页面隐藏显示表达式用法示例
2016/12/25 Javascript
JS三级联动代码格式实例详解
2019/12/30 Javascript
原生js实现密码强度验证功能
2020/03/18 Javascript
原生JS实现九宫格抽奖
2020/09/13 Javascript
浅谈vant组件Picker 选择器选单选问题
2020/11/04 Javascript
Vue实现多页签组件
2021/01/14 Vue.js
wxpython中自定义事件的实现与使用方法分析
2016/07/21 Python
Python基于OpenCV实现视频的人脸检测
2018/01/23 Python
对python中Matplotlib的坐标轴的坐标区间的设定实例讲解
2018/05/25 Python
用Python一键搭建Http服务器的方法
2018/06/01 Python
Python模块future用法原理详解
2020/01/20 Python
在python中实现求输出1-3+5-7+9-......101的和
2020/04/02 Python
Django日志及中间件模块应用案例
2020/09/10 Python
使用canvas绘制超炫时钟
2014/12/17 HTML / CSS
利用纯html5绘制出来的一款非常漂亮的时钟
2015/01/04 HTML / CSS
塑料制成的可水洗的编织平底鞋和鞋子:Rothy’s
2018/09/16 全球购物
英国时尚配饰、珠宝和服装网站:KJ Beckett
2020/01/23 全球购物
生产总经理岗位职责
2013/12/19 职场文书
物流管理专业毕业生自荐信
2014/03/04 职场文书
小学综治宣传月活动总结
2014/07/02 职场文书
务工证明怎么写
2015/06/18 职场文书
新郎父亲婚礼致辞
2015/07/27 职场文书
Android RecyclerView实现九宫格效果
2022/06/28 Java/Android