初始状态怎么弄?都在第1个格上么?
鄙人还是先说一下思路吧:
这个问题难点在于这两位大侠的相遇计算, 1-19格子需要来回的走,略复杂。
假如弄一个数组 arr = [1..18 19 18..2](环) 这样,不就是循环了吗? arr[index % size] 就是当前所在的格子号。
(index是数组下标而不是格子号)
index对于小李飞刀来说 每次 index ++, 对于朱(楚?)留香来说,每次 index += 2 (跳)?
arr[index % size] 两个人的格子号相等的时候就干一架就可以了? 血量啥的弄个变量记着就可以了。
这样?
1 class Person(object): 2 def __init__(self, name, power, skill, blood): 3 self.name = name 4 self.power = power 5 self.skill = skill 6 self.blood = blood 7 8 # calculate attack damage 9 def attackDamage(self): 10 return (.5 * self.power + .5 * self.skill) * (self.blood + 10) / 100 11 12 if __name__ == '__main__': 13 x = Person("X", 10, 10, 100) 14 z = Person("Z", 20, 10, 100) 15 16 # generate grid arr 17 gridArr = [i for i in range(1, 20)] + [i for i in range(18, 1, -1)] 18 gridSize = len(gridArr) 19 # start index 20 xstep = 3 21 zstep = 0 22 23 # meet count 24 count = 100 25 26 i = 0 27 xGridValue = None 28 zGridValue = None 29 while i < count: 30 i = i + 1 31 xstep = xstep + 1 32 zstep = zstep + 2 33 xGridValue = gridArr[xstep % gridSize] 34 zGridValue = gridArr[zstep % gridSize] 35 if xGridValue == zGridValue : 36 # generate attack damage before attack happen 37 xAttackDamage = x.attackDamage() 38 zAttackDamage = z.attackDamage() 39 40 if x.blood > 0 and z.blood >0: 41 z.blood = z.blood - xAttackDamage 42 x.blood = x.blood - zAttackDamage 43 44 print("%s %3d %3d %3d %3d" %(x.name, xGridValue, x.power , x.skill , x.blood )) 45 print("%s %3d %3d %3d %3d" %(z.name, zGridValue, z.power , z.skill , z.blood )) 46
死了也会走,但是不会攻击了,
没验证是不是正确,只是验证了一下自己的思路