1 实验题目
1.1 游戏介绍
游戏开始,首先玩家选择 Big or Small(押大小),选择完成后开始摇三个 骰子计算总值,11<= 总值 <=18 为"大",3<= 总值 <= 10 为"小"。然后告诉玩家猜对或是猜错,玩家初始金额是 1000 元,赔率是 1 倍,让玩家输入赌的金额,分别用两个函数完成 掷骰子任务和计算三个骰子点数大小,然后创建开始游戏,让用户输入猜大小,并定义什么是对的什么是错的,对的输出“你赢了!”,并获得相应的金额;错的输出“你好笨!”, 扣掉相应金额。
1.2 实验要求
编写程序,模拟掷骰子游戏。
2 运行结果
3 源代码
# 掷骰子游戏
import random
class Game:
def __init__(self):
self.money = 1000
self.rate = 1
self.one = 0
self.two = 0
self.three = 0
def player(self):
print('Big or Small?')
choose = input('请输入Big或者Small:')
while choose != 'Big' and choose != 'Small':
choose = input('输入错误,请按照规则重新输入:')
if choose == 'Big':
return 1
else:
return 0
def setThreeDice(self):
self.one = random.randint(1, 6)
self.two = random.randint(1, 6)
self.three = random.randint(1, 6)
def getOne(self):
return int(self.one)
def getTwo(self):
return int(self.two)
def getThree(self):
return int(self.three)
def calculate(self):
self.setThreeDice()
s = self.getOne() + self.getTwo() + self.getThree()
return s
def begin(self):
print('欢迎进入掷骰子游戏!你的已有金额:1000')
guess = 0
flag = self.player()
s = self.calculate()
if 11 <= s <= 18:
guess = 1
if 3 <= s <= 10:
guess = 0
if guess == flag:
print('三个骰子点数分别为:{:d},{:d},{:d}'.format(self.one, self.two, self.three))
print('你赢了!')
self.money += self.money
print('金额变为:{:d}'.format(self.money))
else:
print('三个骰子点数分别为:{:d},{:d},{:d}'.format(self.one, self.two, self.three))
print('你好笨!')
self.money -= self.money
print('金额变为:{:d}'.format(self.money))
if __name__ == '__main__':
game = Game()
game.begin()