|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import random
- class Turtle:
- def initial_position(self):
- self.x = random.randint(0,10)
- self.y = random.randint(0,10)
- return [self.x, self.y]
- def move(self):
- self.x -= random.choice([-2, -1, 1, 2])
- self.y -= random.choice([-2, -1, 1, 2])
- if self.x <= 0:
- self.x += random.choice([1,2])
- elif self.x >=10:
- self.x -= random.choice([1,2])
- if self.y <= 0:
- self.y += random.choice([1,2])
- elif self.y >= 10:
- self.y -= random.choice([1,2])
- def move_position(self):
- self.initial_position = [self.x, self.y]
- return self.initial_position
- class Fish:
- def initial_position(self):
- self.x = random.randint(0,10)
- self.y = random.randint(0,10)
- def move(self):
- self.x -= random.choice([-1, 1])
- self.y -= random.choice([-1, 1])
- if self.x <= 0:
- self.x += 1
- elif self.x >=10:
- self.x -= 1
- if self.y <= 0:
- self.y += 1
- elif self.y >= 10:
- self.y -= 1
- def move_position(self):
- self.initial_position = [self.x, self.y]
- return self.initial_position
- tt = Turtle()
- tt.initial_position()
- fish_position_list = [None for i in range(10)]
- fish_list = []
- for i in range(10):
- exec('fish' + str(i) + '= Fish()')
- for i in range(10):
- exec('fish' + str(i) + '.initial_position()')
- fish_list.append(i)
- stamina =100
- count = 0
- pool = []
- ch = 0
- while stamina:
- stamina -= 1
- for i in fish_list:
- if i not in pool:
- exec('fish' + str(i) +'.move()')
- exec('fish_position_list[i] =' + "fish" + str(i) + ".move_position()")
- tt.move()
- pos = tt.move_position()
- ch += 1
- if pos in fish_position_list:
- n = fish_position_list.count(pos)
- stamina += 20 * n
- if stamina > 100:
- stamina = 100
- count += n
- for j in range(n):
- a = fish_position_list.index(pos)
- fish_position_list[a] = None
- pool.append(fish_list[a])
- print(fish_position_list)
- if count == 10:
- print('---TURTLE WIN !---')
- print('剩余体力%d,共捕食%d次' % (stamina, ch))
- break
- else:
- print('跑了%d次这么久,小乌龟累死啦,饿死啦..' % ch)
复制代码 |
|