# Python学习---综合练习之Craps赌博游戏

## Craps赌博游戏

### 解法一：用分支-循环结构

import random
money = 1000
# 只要玩家有钱游戏就可以继续!
while money > 0:
print('玩家总资产：', money)
while True:
debt = int(input('请下注： '))
if 0 < debt <= money: # (if debt >0 and debt <= money)
break
face1 = random.randint(1, 6)
face2 = random.randint(1, 6)
first_point = face1 + face2
print('玩家摇出了%d点' % first_point)
go_on = False
if first_point == 7 or first_point == 11:
print('玩家胜!')
money += debt
elif first_point == 2 or first_point == 3 or first_point == 12:
print('庄家胜!')
money -= debt
else:
go_on = True
while go_on:
face1 = random.randint(1, 6)
face2 = random.randint(1, 6)
current_point = face1 + face2
print('玩家摇出了%d点' % current_point)
if current_point == 7:
print('庄家胜!')
money -= debt
go_on = False
elif current_point == first_point:
print('玩家胜!')
money += debt
go_on = False
print('你已经破产，游戏结束！')

### 解法二：用定义新函数方法

import random

# 定义摇骰子的函数，传入骰子的个数，返回摇出的点数。
def roll_dice(n=2):  # 默认值，这里给了，里面的就根据这里给的值，然后才会传进去，不设的情况，默认值为2.
total = 0
for _ in range(n):
total += random.randint(1, 6)

money = 1000
# 只要玩家有钱游戏就可以继续。
while money > 0:
print('玩家总资产为： ', money)
while True:
debt = int(input('请下注： '))
if 0 < debt <= money: # (if debt >0 and debt <= money)
break
first_point = roll_dice()
print('玩家摇出了%d点' % first_point)
go_on = False
if first_point == 7 or first_point == 11:
print('玩家胜!')
money += debt
elif first_point == 2 or first_point == 3 or first_point == 12:
print('庄家胜!')
money -= debt
else:
go_on = True
while go_on:
current_point = roll_dice()
print('玩家摇出了%d点' % current_point)
if current_point == 7:
print('庄家胜!')
money -= debt
go_on = False
elif current_point == first_point:
print('玩家胜!')
money += debt
go_on = False

if __name__ == '__main__':
print('你已经破产，游戏结束！')