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

yipeiwu_com5年前Python基础

运行方法:

    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有所帮助。

相关文章

PYQT5开启多个线程和窗口,多线程与多窗口的交互实例

PYQT5开启多个线程和窗口,多线程与多窗口的交互实例

每点击一次按钮,弹出一个对话框(子窗口),同时开启一个子线程来执行任务并更新对话框内容,关闭对话框则关闭对应子线程 1. 建立一个简单的主界面和一个自定义对话框 from PyQt...

python-docx修改已存在的Word文档的表格的字体格式方法

搞了好几天的表格字体格式,一直想找一种能直接一次性修改表格所有字体格式的方法(函数),但是无论用什么方法都无法修改表格字体的格式,原因应该是已存在的文档本身就具有某种格式限制,制约着里面...

python树的同构学习笔记

python树的同构学习笔记

一、题意理解 给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构的”。现给定两棵树,请你判断它们是否是同构的。 输入格式:输入给出2棵二叉树的信...

Python中类似于jquery的pyquery库用法分析

本文实例讲述了Python中类似于jquery的pyquery库用法。分享给大家供大家参考,具体如下: pyquery:一个类似于jquery的Python库 pyquery可以使你在x...

Python 3.7新功能之dataclass装饰器详解

前言 Python 3.7 将于今年夏天发布,Python 3.7 中将会有许多新东西: 各种字符集的改进 对注释的推迟评估 以及对dataclass的支持 最激动人心的...