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 相关文章推荐
Windows下Python的Django框架环境部署及应用编写入门
Mar 10 Python
基于Python的文件类型和字符串详解
Dec 21 Python
python使用pil库实现图片合成实例代码
Jan 20 Python
用python3教你任意Html主内容提取功能
Nov 05 Python
python自制包并用pip免提交到pypi仅安装到本机【推荐】
Jun 03 Python
python 实现将文件或文件夹用相对路径打包为 tar.gz 文件的方法
Jun 10 Python
python聚类算法解决方案(rest接口/mpp数据库/json数据/下载图片及数据)
Aug 28 Python
python 链接sqlserver 写接口实例
Mar 11 Python
python中reload重载实例用法
Dec 15 Python
pytorch 6 batch_train 批训练操作
May 28 Python
Python 正则模块详情
Nov 02 Python
python turtle绘图
May 04 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
Yii入门教程之目录结构、入口文件及路由设置
2014/11/25 PHP
深入理解PHP 数组之count 函数
2016/06/13 PHP
php mysql实现mysql_select_db选择数据库
2016/12/30 PHP
PHP+AJAX 投票器功能
2017/11/11 PHP
PHP Swoole异步MySQL客户端实现方法示例
2019/10/24 PHP
JQuery 1.4 中的Ajax问题
2010/01/23 Javascript
jquery 图片 上一张 下一张 链接效果(续篇)
2010/04/20 Javascript
jQuery中将函数赋值给变量的调用方法
2012/03/23 Javascript
使用js修改客户端注册表的方法
2013/08/09 Javascript
JS 实现导航栏悬停效果(续2)
2013/09/24 Javascript
Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
2013/11/14 Javascript
window.location不跳转的问题解决方法
2014/04/17 Javascript
javascript中setInterval的用法
2015/07/19 Javascript
Bootstrap每天必学之媒体对象
2015/11/30 Javascript
JavaScript实现选中文字提示新浪微博分享效果
2017/06/15 Javascript
vue.js中created方法作用
2018/03/30 Javascript
小程序实现抽奖动画
2020/04/16 Javascript
详解小程序如何避免多次点击,重复触发事件
2019/04/08 Javascript
ES6中let、const的区别及变量的解构赋值操作方法实例分析
2019/10/15 Javascript
python计算方程式根的方法
2015/05/07 Python
Python中py文件引用另一个py文件变量的方法
2018/04/29 Python
Python抽象和自定义类定义与用法示例
2018/08/23 Python
对python3标准库httpclient的使用详解
2018/12/18 Python
python截取两个单词之间的内容方法
2018/12/25 Python
Pytorch转tflite方式
2020/05/25 Python
Python3爬虫中pyspider的安装步骤
2020/07/29 Python
Python装饰器如何实现修复过程解析
2020/09/05 Python
html5+css3之制作header实例与更新
2020/12/21 HTML / CSS
详解HTML5 Canvas绘制时指定颜色与透明度的方法
2016/03/25 HTML / CSS
html5用video标签流式加载的实现
2020/05/20 HTML / CSS
Zalando Lounge瑞士:时尚与生活方式购物俱乐部
2020/03/12 全球购物
财务管理个人自荐书范文
2013/11/24 职场文书
目标管理责任书
2014/04/15 职场文书
2014年五四青年节演讲比赛方案
2014/04/22 职场文书
个人工作年终总结
2015/03/09 职场文书
2015财务年终工作总结范文
2015/05/22 职场文书