TaiSai.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import random
  2. class betSize(object):
  3. """
  4. 假设有足够多的钱 ,庄家愿意一直玩就不会输
  5. 1.初次下注基本金额足够少,本金1/100
  6. 2.之后每次下注为上一次金额的翻倍
  7. 3.直到第一次赢了后,回到步骤1
  8. """
  9. def __init__(self,count,principal):
  10. self.count = count
  11. self.principal = principal
  12. def gambling(self):
  13. """"""
  14. c =0
  15. for i in range(self.count):
  16. c =i+1
  17. #初次下注金额
  18. money = self.principal/100
  19. self.logStart(c, money)
  20. if not self.game(money):
  21. break
  22. self.logResult(c)
  23. def game(self,amount):
  24. """ 游戏开始, amount=下注金额 """
  25. #下注
  26. self.bet(amount)
  27. self.logGameStart(amount)
  28. if (not self.isWin()):
  29. #下轮下注金额
  30. nextAmount =amount * 2
  31. if(self.isLose(nextAmount)):
  32. self.lose()
  33. return False
  34. else:
  35. return self.game(nextAmount)
  36. else:
  37. self.win(amount)
  38. return True
  39. def bet(self,money):
  40. """下注 , money = 下注金额"""
  41. self.principal -= money
  42. return money
  43. def win(self,money):
  44. self.principal += money * 2
  45. self.logWin(money*2)
  46. def isLose(self,amount):
  47. return self.principal<=0 or self.principal<amount
  48. def lose(self):
  49. print("lose...剩余本金:%d" %(self.principal))
  50. def isWin(self):
  51. """ 每次买小 , 小于等于10当赢"""
  52. random.seed(random.randint(1, 100000))
  53. one = random.randint(1,6)
  54. two = random.randint(1,6)
  55. three = random.randint(1,6)
  56. result = one + two + three
  57. self.logGameResult(one,two,three,result,result<=10)
  58. return result <=10
  59. def logStart(self,i,money):
  60. print("第%d轮,本金%d ,初次下注金额:%d" %(i,self.principal,money))
  61. def logGameStart(self,money):
  62. print("下注金额:%d 剩余本金:%d" %(money,self.principal))
  63. def logWin(self,money):
  64. print("赢了,结束本轮,收入:%d 剩余本金:%d" %(money,self.principal))
  65. print("")
  66. def logGameResult(self,one,two,three,result,isWin):
  67. print("本轮结果:%d %d %d = %d %s" %(one ,two ,three ,result ,"赢" if isWin else "输"))
  68. def logResult(self,i):
  69. print("游戏%d轮结束 , 剩余本金:%d" %(i ,self.principal))
  70. if __name__ == '__main__':
  71. count = 100
  72. principal = 1000000
  73. bet = betSize(count,principal)
  74. bet.gambling()