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用来获得图片exif信息的库实例分析
Mar 16 Python
python中pygame针对游戏窗口的显示方法实例分析(附源码)
Nov 11 Python
Python实现并行抓取整站40万条房价数据(可更换抓取城市)
Dec 14 Python
Python实现获取照片拍摄日期并重命名的方法
Sep 30 Python
Python实现批量读取图片并存入mongodb数据库的方法示例
Apr 02 Python
Python求两点之间的直线距离(2种实现方法)
Jul 07 Python
python画图——实现在图上标注上具体数值的方法
Jul 08 Python
python 并发编程 阻塞IO模型原理解析
Aug 20 Python
python实现智能语音天气预报
Dec 02 Python
python 读取串口数据的示例
Nov 09 Python
如何用Python徒手写线性回归
Jan 25 Python
python sleep和wait对比总结
Feb 03 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
DC这些乐高系列动画电影你看过几部?
2020/04/09 欧美动漫
php中使用临时表查询数据的一个例子
2013/02/03 PHP
一个简洁的PHP可逆加密函数(分享)
2013/06/06 PHP
浅析ThinkPHP中execute和query方法的区别
2014/06/13 PHP
destoon找回管理员密码的方法
2014/06/21 PHP
CentOS安装php v8js教程
2015/02/26 PHP
php中执行系统命令的方法
2015/03/21 PHP
JS读取cookies信息(记录用户名)
2012/01/10 Javascript
js判断FCKeditor内容是否为空的两种形式
2013/05/14 Javascript
超级简单的jquery操作表格方法
2014/12/15 Javascript
Javascript通过overflow控制列表闭合与展开的方法
2015/05/15 Javascript
Angular中$compile源码分析
2016/01/28 Javascript
仿百度换肤功能的简单实例代码
2016/07/11 Javascript
jquery实现垂直和水平菜单导航栏
2020/08/27 Javascript
详解handlebars+require基本使用方法
2016/12/21 Javascript
前端页面文件拖拽上传模块js代码示例
2017/05/19 Javascript
JavaScript canvas实现围绕旋转动画
2017/11/18 Javascript
解决Jquery下拉框数据动态获取的问题
2018/01/25 jQuery
JS中promise化微信小程序api
2018/04/12 Javascript
vue2.0移动端滑动事件vue-touch的实例代码
2018/11/27 Javascript
angularjs http与后台交互的实现示例
2018/12/21 Javascript
JS实现移动端双指缩放和旋转方法
2019/12/13 Javascript
js将日期格式转换为YYYY-MM-DD HH:MM:SS
2020/09/18 Javascript
[02:57]DOTA2亚洲邀请赛 SECRET战队出场宣传片
2015/02/07 DOTA
Python使用 Beanstalkd 做异步任务处理的方法
2018/04/24 Python
详解Python3除法之真除法、截断除法和下取整对比
2019/05/23 Python
VSCode中自动为Python文件添加头部注释
2019/11/14 Python
日本高端护肤品牌:Tatcha
2016/08/29 全球购物
雅高酒店中国:Accorhotels.com China
2018/03/26 全球购物
学生出入校管理制度
2014/01/16 职场文书
大学生社会实践自我鉴定
2014/03/24 职场文书
学生实习证明范文
2014/09/28 职场文书
2015年乡镇流动人口工作总结
2015/05/12 职场文书
2015社区个人工作总结范文
2015/05/13 职场文书
刑事附带民事起诉状
2015/05/19 职场文书
2015年三年级班主任工作总结
2015/05/21 职场文书