-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEcosystem.py
More file actions
186 lines (144 loc) · 5.77 KB
/
Ecosystem.py
File metadata and controls
186 lines (144 loc) · 5.77 KB
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
186
#This program was written by Bill Neubauer
import random
def main():
#Generate a river
Snohomish = Ecosystem()
#Insert the animals
Fish(Snohomish)
Fish(Snohomish)
Fish(Snohomish)
Bear(Snohomish)
Bear(Snohomish)
print(Snohomish)
for x in range(0, 20):
Snohomish.GenerateMoves()
print(Snohomish)
class Ecosystem:
def __init__(self):
self.river = []
for i in range(10):
self.river.append(None)
#Sequentially generate moves for each animal
def GenerateMoves(self):
i = 0
while( i < len(self.river) ):
x = 0
if not self.river[i] == None:
x = self.river[i].Move()
if x == 1:
i += 1
i += 1
#Can set rules of where next new valid index would be
def ValidRandomIndex(self):
newIndex = random.randint(0, len(self.river)-1)
while not self.river[newIndex] == None:
newIndex = random.randint(0, len(self.river)-1)
return newIndex
def __str__(self):
string = "[ "
for animal in self.river:
string = string + " " + str(animal) + ", "
return string + " ]"
class Fish:
#Will be born in at a random point in the river where space allows
def __init__(self, ecosystem):
self.ecosystem = ecosystem
self.lastIndex = -1
self.index = ecosystem.ValidRandomIndex()
self.ecosystem.river[self.index] = self
#Will have three scenarios:
# It will move and will have no collisions.
# It will move and there will be a collision.
# It will collide into the same type
# Will tell other fish to move back to where they were
# Will not move, will birth a new fish.
# It will collide into other type
#Returns how much it moved by
def Move(self):
newIndex = self.index + random.randint(-1,1)
#If fish goes out of bounds
if newIndex < 0 or newIndex > len(self.ecosystem.river):
print("\t[Fish] Went out of bounds, did not move")
lastIndex = self.index
return 0
if self.ecosystem.river[newIndex] == None or self.index == newIndex:
print("\t[Fish] Moving from " + str(self.index) +" to " + str(newIndex))
self.ecosystem.river[self.index] = None
self.lastIndex = self.index
self.index = newIndex
self.ecosystem.river[newIndex] = self
return (self.index - self.lastIndex)
if str(self.ecosystem.river[newIndex]) == "Fish":
print("\t[Fish to Fish]: Collided with " + str(self.index) + " and " + str(newIndex))
self.ecosystem.river[newIndex].GoBack()
lastIndex = self.index
Fish(self.ecosystem)
return 0
if str(self.ecosystem.river[newIndex]) == "Bear":
print("\t[Fish to Bear]: Collided with " + str(self.index) + " and " + str(newIndex))
self.Kill()
return
#Will go back to it's last index
def GoBack(self):
self.ecosystem.river[self.index] = None
self.ecosystem.river[self.lastIndex] = self
self.index = self.lastIndex
return
#Fish will die
def Kill(self):
self.ecosystem.river[self.index] = None
self = None
return
def __str__(self):
return "Fish"
class Bear:
#Will be born in at a random point in the river where space allows
def __init__(self, ecosystem):
self.ecosystem = ecosystem
self.lastIndex = -1
self.index = ecosystem.ValidRandomIndex()
self.ecosystem.river[self.index] = self
#Will have three scenarios:
# It will move and will have no collisions.
# It will move and there will be a collision.
# It will collide into the same type
# Will tell other bear to move back to where they were
# Will not move, will birth a new bear.
# It will collide into other type
#Returns how much it moved by
def Move(self):
newIndex = self.index + random.randint(-1,1)
#If fish goes out of bounds
if newIndex < 0 or newIndex > len(self.ecosystem.river):
print("\t[Bear] Went out of bounds, did not move")
lastIndex = self.index
return 0
if self.ecosystem.river[newIndex] == None or self.index == newIndex:
print("\t[Bear] Moving from " + str(self.index) +" to " + str(newIndex))
self.ecosystem.river[self.index] = None
self.lastIndex = self.index
self.index = newIndex
self.ecosystem.river[newIndex] = self
return (self.index - self.lastIndex)
if str(self.ecosystem.river[newIndex]) == "Bear":
print("\t[Bear to Bear]: Collided with " + str(self.index) + " and " + str(newIndex))
self.ecosystem.river[newIndex].GoBack()
self.lastIndex = self.index
Fish(self.ecosystem)
return 0
if str(self.ecosystem.river[newIndex]) == "Fish":
print("\t[Bear to Fish]: Collided with " + str(self.index) + " and " + str(newIndex))
self.ecosystem.river[newIndex].Kill()
self.lastIndex = self.index
self.index = newIndex
self.ecosystem.river[self.index] = self
return (self.index - self.lastIndex)
#Will go back to it's last index
def GoBack(self):
self.ecosystem.river[self.index] = None
self.ecosystem.river[self.lastIndex] = self
self.index = self.lastIndex
return
def __str__(self):
return "Bear"
main()