[关闭]
@chengweihuang 2019-01-07T12:38:51.000000Z 字数 2288 阅读 585

骰子游戏

数字游戏


单人模式

  1. # encoding: utf-8
  2. __author__ = 'ChengweiHuang'
  3. __date__ = '2018/7/27 14:10'
  4. import random
  5. print('游戏开始,你有10000元')
  6. def roll_dice(numbers = 3,points = None):
  7. print('----- ---- ---- 摇骰子 ---- ---- -----')
  8. if points is None:
  9. points = []
  10. while numbers > 0:
  11. point = random.randrange(1,7)
  12. points.append(point)
  13. numbers = numbers - 1
  14. return points
  15. def roll_result(my_sun):
  16. if my_sun<=10:
  17. return '小'
  18. else:
  19. return '大'
  20. def game():
  21. money = 10000
  22. while True:
  23. stake=input('请输入下注金额')
  24. if int(stake)>money:
  25. print('余额不足,请重新下注!')
  26. continue
  27. else:
  28. your_choice = input('请下注,大 or 小:')
  29. numbers=roll_dice()
  30. print(numbers)
  31. my_sum=sum(numbers)
  32. print(my_sum)
  33. da_xiao=roll_result(my_sum)
  34. print(da_xiao)
  35. if your_choice==da_xiao:
  36. money=money+int(stake)
  37. print('恭喜你,这把赢了%s元,你的余额为%s'%(int(stake),money))
  38. if money>=100000:
  39. print('你运气太好,请出门右拐隔壁澳门皇家赌场。游戏结束')
  40. break
  41. else:
  42. money=money-int(stake)
  43. print('这把输了%s元,你的余额为%s'%(stake,money))
  44. if money==0:
  45. print('余额不足,游戏结束')
  46. break
  47. game()

多人模式

  1. import random
  2. import time
  3. class Gambler():
  4. '''
  5. 流浪赌徒类
  6. '''
  7. users = []
  8. def __init__(self, name,money,b_or_s=None, pour_money=None):
  9. self.name=name
  10. self.money = money
  11. self.pour_money = pour_money
  12. self.b_or_s=b_or_s
  13. self.__class__.users.append(self)
  14. @classmethod
  15. def dice(cls):
  16. '''
  17. 生成骰子 得到结果
  18. :return:
  19. '''
  20. cls.L = []
  21. for i in range(3):
  22. nums = random.randint(1, 6)
  23. cls.L.append(nums)
  24. cls.result = '大' if sum(cls.L) > 9 else '小'
  25. #下注方法
  26. def bottom_pour(self):
  27. '''
  28. 下注函数 输入金额,判断余额是否足够 ,输入 大或小
  29. :return:
  30. '''
  31. f = True
  32. while f:
  33. self.pour_money = int(input('请%s (余额%d)输入下注金额' % (self.name, self.money)))
  34. if self.pour_money > self.money:
  35. print('资金不足,请联系管理员充值')
  36. continue
  37. s = input('请%s,输入大小 1 表示大 2 表示小' % (self.name))
  38. self.b_or_s = '大' if s == '1' else '小'
  39. f = False
  40. # 计算
  41. def my_count(self):
  42. if self.b_or_s != self.__class__.result:
  43. self.pour_money = int(-self.pour_money)
  44. self.money = self.money + self.pour_money
  45. def out(self):
  46. print('账单--', self.name, ':', '余额**', self.money, '单注流水**', self.pour_money, '选择**', self.b_or_s)
  47. if self.money == 0:
  48. self.__class__.users.remove(self)
  49. print('%s 出局'%self.name)
  50. def mian():
  51. while 1:
  52. #生成骰子
  53. Gambler.dice()
  54. for i in Gambler.users:
  55. i.bottom_pour() # 下注
  56. print('疯狂计算中')
  57. time.sleep(3)
  58. print('结果 : %s %s'%(Gambler.L,Gambler.result))
  59. for j in Gambler.users:
  60. j.my_count() # 结算
  61. for k in Gambler.users:
  62. k.out() # 出局判断
  63. if len(Gambler.users) == 1:
  64. print('%s: 恕我直言,在座的各位都是辣鸡' % (Gambler.users[0].name))
  65. break
  66. if len(Gambler.users) == 0:
  67. print('又调皮了')
  68. break
  69. if __name__ == '__main__':
  70. while True:
  71. data = input('请输入玩家姓名和初始金额,以逗号分隔,回车结束')
  72. if data=='':
  73. break
  74. user=Gambler(data.split(',')[0],int(data.split(',')[1]))
  75. mian()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注