马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
开发版本:python3.6.5
【注意一下,有一个附件需要下载然后把后缀.txt改成.pkl文件,与保存的.py文件放在同一文件夹下。】
我之前是自学的python基础太差,7月初才开始学习小甲鱼老师的课程。学到38课,来交个作业。
开发时间断断续续大概1周,有的算法和BUG真是搞到头秃。
电脑超弱AI,主要像急着想看后面的课程就没深入研究,但是摇骰子这种拼运气的东西,还是会有输的时候哦。
由于没有编程经验,代码应该是非常不成熟,还请各位大佬多多包含,多多指教,有BUG的话欢迎反馈。
import pickle
import random as ra
class Bones:
# 单个骰子roll点
def roll(self):
self.result = ra.randint(1,6)
return self.result
class Rules:
# 开牌分数计算
def showdown(self,lis):
global score
self.newlis = lis[:]
for each in lis:
k = list(set(each))
if len(k) == 1:
special = 1
if len(k) == 2 and each.count(k[1]) + each.count(1) == 5 and call_one == 0:
special = 1
elif len(set(each)) == 5:
special = 2
else:
special = 0
pass
if special == 1:
score += 10
self.newlis.remove(each)
elif special == 2:
score += 0
self.newlis.remove(each)
elif special == 0:
score += each.count(guess[1])
if call_one != 1:
score += each.count(1)
else:
pass
return score
class Banker(Bones):
# 电脑roll点
def play(self):
for bone in range(5):
B_result.append(self.roll())
# 电脑开牌
def guess(self):
self.num = guess[0] + 1
self.points = guess[1]
guess[0] = self.num
guess[1] = self.points
print("电脑叫【%d】个【%d】\n"%(guess[0],guess[1]))
return guess
class Player(Bones):
# 玩家roll点
def play(self):
for i in range(5):
P_result.append(self.roll())
#玩家开牌
def guess(self):
self.num = 0
self.points = 0
while self.points == 0:
self.num = int(input("请输入骰子数(请输入一个非0的整数):"))
self.points = int(input("请叫点数(请输入一个大于0且小于等于6的整数):"))
if self.points == 1:
call_one = 1
if len(guess) == 0:
guess.extend([self.num,self.points])
break
else:
if self.points > guess[1] and self.num >= guess[0]:
guess[0] = self.num
guess[1] = self.points
break
elif self.points == guess[1] and self.num > guess[0]:
guess[0] = self.num
guess[1] = self.points
break
elif self.points < guess[1] and self.num > guess[0]:
guess[0] = self.num
guess[1] = self.points
break
else:
self.num = 0
self.points =0
print("请从【%d+1】个【%d】或者【%d】个【%d+1】开始叫。"%(guess[0],guess[1],guess[0],guess[1]))
continue
print("玩家叫【%d】个【%d】"%(guess[0],guess[1]))
return guess
#游戏帮助
def game_help(self,h_list):
for i in h_list:
print(i)
#退出游戏
def game_out(self):
try:
1 == 0
except KeyboardInterrupt:
pass
B_result = []
P_result = []
two_players = [B_result,P_result]
score = 0
guess = []
start_play = 0
take_turns = 0
call_one = 0
special = 0
player_money = 100
CPU_money = 100
#调用PKL文件
pkl = open("game_rule.pkl","rb")
h_file = pickle.load(pkl)
#主程序
while player_money >= 0:
if start_play is not 1:
B = Banker()
P = Player()
Show = Rules()
B_result.clear()
P_result.clear()
guess.clear()
call_one = 0
special = 0
take_turns =0
score = 0
print("游戏开始,请摇骰子")
take_turns = 1
B.play()
P.play()
print("你的骰子点数为:%s" % P_result)
while True:
try:
P_choice = int(input("输入【0】继续;输入【1】开牌;输入【5】查看帮助;输入【9】退出:"))
if P_choice == 0:
P.guess()
if guess[1] > 6 or guess[1] < 0:
print("最小1点,最大6点")
guess.clear()
continue
elif guess[0] < 1:
print("不能少于1个")
guess.clear()
continue
if guess[1] == 1:
call_one = 1
take_turns = 1
elif P_choice == 1:
if guess:
All_result = B_result + P_result
Show.showdown(two_players)
take_turns = 2
print("结果是:\n 你的骰子为:%s\n 电脑的骰子为:%s\n "%(P_result,B_result))
print("骰子点数【%d】共有【%d】个\n"%(guess[1],score))
break
else:
print("点数不叫你就开牌,你484傻?")
continue
elif P_choice == 5:
P.game_help(h_list = h_file)
continue
elif P_choice == 9:
P.game_out()
else:
print("输入错误!")
continue
except (OSError,TypeError,ImportError):
print("系统错误!")
continue
except:
print("输入错误!")
continue
if guess[1] in B_result:
B.guess()
take_turns = 2
else:
All_result = B_result + P_result
Show.showdown(two_players)
take_turns = 1
print("电脑说:" + ra.choice(["我不信,开你!","你输定了!","你骗不了我..."]))
print("结果是:\n 你的骰子为:%s\n 电脑的骰子为:%s\n "%(P_result,B_result))
print("【%d】为【%d】个\n "%(guess[1],score))
break
if score >= guess[0]:
if take_turns == 1:
player_money += 10
CPU_money -= 10
start_play = 0
print("恭喜,玩家胜。")
print("玩家金币:%d\n电脑金币:%d\n"%(player_money,CPU_money))
elif take_turns == 2:
player_money -= 10
CPU_money += 10
start_play = 0
print("嗷欧~玩家输了")
print("玩家金币:%d\n电脑金币:%d\n"%(player_money,CPU_money))
else:
if take_turns == 1:
player_money -= 10
CPU_money += 10
start_play = 0
print("嗷欧~玩家输了")
print("玩家金币:%d\n电脑金币:%d\n"%(player_money,CPU_money))
elif take_turns ==2:
player_money += 10
CPU_money -= 10
start_play = 0
print("恭喜,玩家胜。")
print("玩家金币:%d\n电脑金币:%d\n"%(player_money,CPU_money))
if player_money <= 0:
print("玩家金币:%d\n电脑金币:%d\n"%(player_money,CPU_money))
print("555555,没钱了,回家吃土吧……")
break
elif CPU_money <= 0:
print("玩家金币:%d\n电脑金币:%d"%(player_money,CPU_money))
print("恭喜玩家大获全胜!\n游戏结束。")
break
else:
continue
|