-
Notifications
You must be signed in to change notification settings - Fork 0
/
Animal.py
185 lines (160 loc) · 5.54 KB
/
Animal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from Queue import queue
import random
from PathFind import Node, a_star
class animal:
"""Class to manage the animal in the simulation"""
def __init__(self, x, y, animal_range, speed):
self.x = x
self.y = y
self.animal_range = animal_range
self.speed = speed
self.hunger = 10
self.food_near = []
self.searching_for_food = True
self.move_queue = queue()
self.moves = ""
def move_to_food(self):
#Boolean for determining if animal has found any food
has_path = False
if self.move_queue:
#If the animal has a queue, they have found a food
has_path = True
moved = 0
for i in range(self.speed):
empty_queue = False
moves = self.move_queue.de_q()
try:
new_q = moves[1:]
except TypeError as e:
if not has_path:
self.searching_for_food = True
try:
try:
move = moves[0]
except TypeError as t:
empty_queue = True
except IndexError as e:
empty_queue = True
if empty_queue:
"""
If the animal has a path set, an empty queue means
they have reached their food and should exit
"""
if has_path:
break
move = random.choice(["U", "D", "L", "R"])
self.searching_for_food = True
self.move_queue = queue()
self.move_queue.add(new_q)
if move == "U":
self.y += 1
elif move == "D":
self.y -= 1
elif move == "L":
self.x -= 1
elif move == "R":
self.x += 1
def reproduce(self, restricted_spots, size, population):
x_interval = 1
y_interval = 0
new_animals = []
for i in range(2):
while True:
new_x = random.randint(1, size)
new_y = random.randint(1, size)
try:
if restricted_spots[new_x] != new_y:
break
except KeyError as e:
break
###Create the attributes of the new animals###
if population > 10:
mutation_chance = 50
elif population < 7 and population > 3:
mutation_chance = 20
else:
mutation_chance = 5
speed_rand = random.randint(1, mutation_chance)
if speed_rand == 1:
speed = random.randint(1, 5)
else:
speed = self.speed
range_rand = random.randint(1, mutation_chance)
if range_rand == 1:
animal_range = random.randint(5, 10)
else:
animal_range = self.animal_range
#############################################
restricted_spots[new_x] = new_y
new_animal = animal(new_x, new_y, animal_range, speed)
new_animals.append(new_animal)
return new_animals
def eat(self, foods):
x, y = -1, -1
for food in foods:
if food.x == self.x and food.y == self.y:
print (f"📗📗📗{self} yum x: {self.x} y:{self.y}")
self.searching_for_food = True
self.hunger += food.type
x = food.x
y = food.y
self.food_near = []
self.move_queue = queue()
break
new_foods = []
for food in foods:
if food.x != x or food.y != y:#Fix to error where too much food gets removed
new_foods.append(food)
return new_foods, self.searching_for_food
def find_food(self, board):
self.searching_for_food = False
x_area = range(self.x - int(self.animal_range), self.x + int(self.animal_range))
y_area = range(self.y - int(self.animal_range), self.y + int(self.animal_range))
area = [x_area, y_area]
for x in x_area:
for y in y_area:
try:
if board[x][y] == 1 and x > 0 and y > 0:
food = [x, y]
self.food_near.append(food)
except IndexError as e:
pass
def end_found(self, moves, x, y, foodx, foody):
for move in moves:
if move == "U":
y += 1
elif move == "D":
y -= 1
elif move == "L":
x -= 1
elif move == "R":
x += 1
if x == foodx and y == foody:
return True
else:
return False
def valid_path(self, moves, x, y):
for move in moves:
if move == "U":
y += 1
elif move == "D":
y -= 1
elif move == "L":
x -= 1
elif move == "R":
x += 1
if x < 100 and x > 0 and y < 100 and y > 0:
return True
else:
return False
def find_best_path(self, food, width, height):
# Find the best path using the a star algorithm
path = a_star(width, height, [self.x, self.y], food)
#Add the current route to the q
new_q = queue()
new_q.add(path)
self.move_queue = new_q
if __name__ == '__main__':
a = animal(5, 5, 5, 5)
a.find_food()
#print(a.find_best_path(7, 7))