编程的乐趣——精选推荐


2023年12月21日发(作者:skype安卓手机版)

这是 python 程序:import randomclass Cannon(object):def __init__(self, name, accuracy): = cy = = = Nonedef setTarget(self, target): = targetdef hit(self):result = m(0, 1)if result < cy:#print "hit!" = Falsereturn Trueelse:#print "miss!"return Falsedef __str__(self):return 'This cannon is named ' + + ", accuracy " + str(cy)def testCannon():myCannon = Cannon('A', 0.5)target = Cannon('target', 0)hitcount, misscount = 0, 0

for i in range(0,500):get(target)if () == 1:hitcount = hitcount + 1else:misscount = misscount + 1print "hit " + str(hitcount) + " miss " + str(misscount)class Game(object):def __init__(self):s = []def addCannon(self, newCannon):if newCannon in s:raise ValueError('Duplicate cannon')else:(newCannon)def removeCannon(self, cannon):if cannon in s:(cannon)else:raise ValueError('Cannon not found')def bestPlayer(self):if len(s) == 0:return Nonebestcannon = s[0]for cannon in s:if cy > cannon:bestcannon = cannonreturn bestcannondef secondBest(self):

if len(s) < 2:return Nonebestcannon = ayer()secondBest = s[0]if secondBest == bestcannon:secondBest = s[1]for cannon in s:if (cy < cy) and (bestcannon != cannon):secondBest = cannonreturn secondBestdef endGame(self):#print "there are " + str(len(s)) + "cannons right now."if len(s) <= 1:return Trueelse:return Falsedef winner(self):if not e():returnif len(s) == 0:#print "no winner"return Noneelse:return s[0]def printCannons(self):for cannon in s:print cannondef setTargets(self):best = ayer()

secondBest = Best()for cannon in s:get(best)get(secondBest)#for cannon in s:# print str(cannon) + ' target is ' + str()def fire(self):if e():print "Game ended."return gets()for cannon in s:()for i in range(len(s)-1, -1, -1):#print iif not s[i].alive:#print "hahaha this one dead " + str(cannon)del s[i]#annons()def play(self):while not e():()#winner = ()def testGame():game = Game()cannonA = Cannon('A', 1.0/2)cannonB = Cannon('B', 1.0/3)cannonC = Cannon('C', 1.0/6)non(cannonA)

non(cannonB)non(cannonC)annons()e()print "best " + str(ayer())print "2nd best " + str(Best())print#Cannon(cannonA)#print ayer()()def gameRuns():totalRuns = 300000aCount, bCount, cCount, nowinner = 0, 0, 0, 0for i in range(totalRuns):game = Game()cannonA = Cannon('A', 1.0/2)cannonB = Cannon('B', 1.0/3)cannonC = Cannon('C', 1.0/6)non(cannonA)non(cannonB)non(cannonC)()winner = ()if winner == cannonA:aCount = aCount + 1elif winner == cannonB:bCount = bCount + 1elif winner == cannonC:cCount = cCount + 1


本文发布于:2024-09-21 23:30:59,感谢您对本站的认可!

本文链接:https://www.17tex.com/fanyi/19960.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:手机   作者
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议