鱼C论坛

 找回密码
 立即注册
查看: 2051|回复: 1

[原创] python做的摇骰子吹牛小游戏

[复制链接]
发表于 2018-7-22 01:31:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
开发版本:python3.6.5

【注意一下,有一个附件需要下载然后把后缀.txt改成.pkl文件,与保存的.py文件放在同一文件夹下。】

我之前是自学的python基础太差,7月初才开始学习小甲鱼老师的课程。学到38课,来交个作业。
开发时间断断续续大概1周,有的算法和BUG真是搞到头秃。

电脑超弱AI,主要像急着想看后面的课程就没深入研究,但是摇骰子这种拼运气的东西,还是会有输的时候哦。
由于没有编程经验,代码应该是非常不成熟,还请各位大佬多多包含,多多指教,有BUG的话欢迎反馈。

  1. import pickle
  2. import random as ra


  3. class Bones:
  4.     # 单个骰子roll点
  5.     def roll(self):
  6.         self.result = ra.randint(1,6)
  7.         return self.result
  8.       
  9. class Rules:
  10.     # 开牌分数计算
  11.     def showdown(self,lis):
  12.         global score
  13.         self.newlis = lis[:]

  14.         for each in lis:
  15.             k = list(set(each))
  16.             if len(k) == 1:
  17.                 special = 1
  18.                
  19.             if len(k) == 2 and each.count(k[1]) + each.count(1) == 5 and call_one == 0:
  20.                 special = 1
  21.                
  22.             elif len(set(each)) == 5:
  23.                 special = 2

  24.             else:
  25.                 special = 0
  26.                 pass   

  27.             if special == 1:
  28.                 score += 10
  29.                 self.newlis.remove(each)
  30.                     
  31.             elif special == 2:               
  32.                 score  += 0
  33.                 self.newlis.remove(each)
  34.                
  35.             elif special == 0:
  36.                 score += each.count(guess[1])
  37.                 if call_one != 1:
  38.                     score += each.count(1)                    
  39.                 else:
  40.                     pass
  41.         return score
  42. class Banker(Bones):
  43.     # 电脑roll点
  44.     def play(self):
  45.         for bone in range(5):
  46.             B_result.append(self.roll())

  47.     # 电脑开牌
  48.     def guess(self):
  49.         self.num = guess[0] + 1
  50.         self.points = guess[1]
  51.         guess[0] = self.num
  52.         guess[1] = self.points
  53.         print("电脑叫【%d】个【%d】\n"%(guess[0],guess[1]))
  54.         return guess

  55. class Player(Bones):
  56.     # 玩家roll点
  57.     def play(self):
  58.         for i in range(5):
  59.             P_result.append(self.roll())
  60.    
  61.     #玩家开牌
  62.     def guess(self):
  63.         self.num = 0
  64.         self.points = 0
  65.         while self.points == 0:
  66.             self.num = int(input("请输入骰子数(请输入一个非0的整数):"))
  67.             self.points = int(input("请叫点数(请输入一个大于0且小于等于6的整数):"))      
  68.             if self.points == 1:
  69.                     call_one = 1
  70.             if len(guess) == 0:
  71.                     guess.extend([self.num,self.points])
  72.                     break
  73.             else:
  74.                     if self.points > guess[1] and self.num >= guess[0]:
  75.                         guess[0] = self.num
  76.                         guess[1] = self.points
  77.                         break
  78.                
  79.                     elif self.points == guess[1] and self.num > guess[0]:
  80.                         guess[0] = self.num
  81.                         guess[1] = self.points
  82.                         break
  83.                
  84.                     elif self.points < guess[1] and self.num > guess[0]:
  85.                         guess[0] = self.num
  86.                         guess[1] = self.points
  87.                         break
  88.                
  89.                     else:
  90.                         self.num = 0
  91.                         self.points =0
  92.                         print("请从【%d+1】个【%d】或者【%d】个【%d+1】开始叫。"%(guess[0],guess[1],guess[0],guess[1]))
  93.                         continue
  94.         
  95.         print("玩家叫【%d】个【%d】"%(guess[0],guess[1]))
  96.         return guess

  97.     #游戏帮助
  98.     def game_help(self,h_list):
  99.         for i in h_list:
  100.             print(i)      

  101.     #退出游戏
  102.     def game_out(self):
  103.         try:
  104.             1 == 0

  105.         except KeyboardInterrupt:
  106.             pass
  107.             
  108.         
  109. B_result = []
  110. P_result = []
  111. two_players = [B_result,P_result]
  112. score    = 0
  113. guess     = []
  114. start_play = 0
  115. take_turns = 0
  116. call_one = 0
  117. special  = 0
  118. player_money = 100
  119. CPU_money = 100

  120. #调用PKL文件
  121. pkl = open("game_rule.pkl","rb")
  122. h_file = pickle.load(pkl)

  123. #主程序
  124. while player_money >= 0:
  125.     if start_play is not 1:
  126.         B = Banker()
  127.         P = Player()
  128.         Show = Rules()
  129.         B_result.clear()
  130.         P_result.clear()
  131.         guess.clear()
  132.         call_one = 0
  133.         special = 0
  134.         take_turns =0
  135.         score = 0
  136.         print("游戏开始,请摇骰子")
  137.         take_turns = 1
  138.         B.play()
  139.         P.play()
  140.         print("你的骰子点数为:%s" % P_result)
  141.         while True:
  142.             try:
  143.                 P_choice = int(input("输入【0】继续;输入【1】开牌;输入【5】查看帮助;输入【9】退出:"))
  144.                 if P_choice == 0:   
  145.                     P.guess()
  146.                     if guess[1] > 6 or guess[1] < 0:
  147.                         print("最小1点,最大6点")
  148.                         guess.clear()
  149.                         continue
  150.                     elif guess[0] < 1:
  151.                         print("不能少于1个")
  152.                         guess.clear()
  153.                         continue
  154.                     if guess[1] == 1:
  155.                         call_one = 1
  156.                     take_turns = 1
  157.                 elif P_choice == 1:
  158.                     if guess:
  159.                         All_result = B_result + P_result
  160.                         Show.showdown(two_players)
  161.                         take_turns = 2
  162.                         print("结果是:\n 你的骰子为:%s\n 电脑的骰子为:%s\n "%(P_result,B_result))
  163.                         print("骰子点数【%d】共有【%d】个\n"%(guess[1],score))
  164.                         break
  165.                     else:
  166.                         print("点数不叫你就开牌,你484傻?")
  167.                         continue
  168.                 elif P_choice == 5:
  169.                     P.game_help(h_list = h_file)
  170.                     continue

  171.                 elif P_choice == 9:
  172.                     P.game_out()

  173.                 else:
  174.                     print("输入错误!")
  175.                     continue
  176.             except (OSError,TypeError,ImportError):
  177.                 print("系统错误!")
  178.                 continue

  179.             except:
  180.                 print("输入错误!")
  181.                 continue
  182.             
  183.             if guess[1] in B_result:
  184.                     B.guess()
  185.                     take_turns = 2
  186.             else:
  187.                 All_result = B_result + P_result
  188.                 Show.showdown(two_players)
  189.                 take_turns = 1
  190.                 print("电脑说:" + ra.choice(["我不信,开你!","你输定了!","你骗不了我..."]))
  191.                 print("结果是:\n 你的骰子为:%s\n 电脑的骰子为:%s\n "%(P_result,B_result))
  192.                 print("【%d】为【%d】个\n "%(guess[1],score))
  193.                 break
  194.         
  195.         if score >= guess[0]:
  196.             if take_turns == 1:
  197.                 player_money += 10
  198.                 CPU_money -= 10
  199.                 start_play = 0
  200.                 print("恭喜,玩家胜。")
  201.                 print("玩家金币:%d\n电脑金币:%d\n"%(player_money,CPU_money))
  202.                
  203.             elif take_turns == 2:
  204.                 player_money -= 10
  205.                 CPU_money += 10
  206.                 start_play = 0
  207.                 print("嗷欧~玩家输了")
  208.                 print("玩家金币:%d\n电脑金币:%d\n"%(player_money,CPU_money))

  209.         else:
  210.             if take_turns == 1:
  211.                 player_money -= 10
  212.                 CPU_money += 10
  213.                 start_play = 0
  214.                 print("嗷欧~玩家输了")
  215.                 print("玩家金币:%d\n电脑金币:%d\n"%(player_money,CPU_money))

  216.             elif take_turns ==2:
  217.                 player_money += 10
  218.                 CPU_money -= 10
  219.                 start_play = 0
  220.                 print("恭喜,玩家胜。")
  221.                 print("玩家金币:%d\n电脑金币:%d\n"%(player_money,CPU_money))

  222.         if player_money <= 0:
  223.             print("玩家金币:%d\n电脑金币:%d\n"%(player_money,CPU_money))
  224.             print("555555,没钱了,回家吃土吧……")
  225.             break
  226.         elif CPU_money <= 0:
  227.             print("玩家金币:%d\n电脑金币:%d"%(player_money,CPU_money))
  228.             print("恭喜玩家大获全胜!\n游戏结束。")
  229.             break
  230.         else:
  231.             continue
复制代码


game_rule.txt

1.82 KB, 下载次数: 20

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-7-22 13:43:54 | 显示全部楼层
之前不知道可以上传压缩文件,不好意思。
补上补上

骰子.zip

2.95 KB, 下载次数: 16

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 12:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表