Code
Code
Code
def copyMat(m):
return [[m[i][j] for j in range(len(m[0]))] for i in range(len(m))]
###
class Ocean :
def __init__(self,width=50,height=50):
self.width,self.height = width,height
self.grid = [[None]*height for _ in range(width)]
self.animals = []
self.states = []
self.animalCount = []
# do not change the names of the attributes or the show function won't work
def __str__(self):
def conv(v):
if v == None : return "0"
else : return str(v.num)
res = ""
for i in range(self.width):
res += "| "
for j in range(self.height):
res += conv(self.grid[i][j]) + " "
res += "|\n"
return res
def initialize(self):
for i in range (self.width):
for j in range (self.height):
p=rd.randint(0,9)
if p==0:
newShark=Shark(i,j)
self.animals.append(Shark)
self.grid[i][j] = newShark
elif p<4:
newFish=Fish(i,j)
self.animals.append(Fish)
self.grid[i][j] = newFish
self.states.append(copyMat(self.grid))
self.animalCount.append(self.count())
def show(self):
# used for the animation do not touch !
fig = plt.figure(1)
mapsList = []
for k in range(len(self.states)):
for i in range(self.width):
for j in range(self.height):
if self.states[k][i][j] == None :
self.states[k][i][j] = colorTable[0]
else :
self.states[k][i][j] = colorTable[self.states[k][i][j].num]
img = plt.matshow(self.states[k],fignum=1)
mapsList.append([img])
anim =
animation.ArtistAnimation(fig,mapsList,interval=100,blit=True,repeat=False)
plt.show()
def surroundings(self,pos):
i=pos[0]
j=pos[1]
check=[[i,(j-1)%len(self.grid)],[i,(j+1)%len(self.grid)],[(i-
1)%len(self.grid),j],[(i+1)%len(self.grid),j]]
resfish=[0,[]]
resf=0
resnone=[0,[]]
ress=0
resn=0
resshark=[0,[]]
for (x,y) in check:
l=self.grid[x][y]
if l is None:
resn+=1
resnone[0]=resn
resnone[1].append([x,y])
elif l.num == 2:
ress+=1
resshark[0]=ress
resshark[1].append([x,y])
else:
resf+=1
resfish[0]=resf
resfish[1].append([x,y])
return (resfish,resnone,resshark)
def removeDeads(self):
pass
# complete here (remove pass)
def tick(self):
rd.shuffle(self.animals)
# complete here
self.states.append(copyMat(self.grid))
self.animalCount.append(self.count())
def count(self):
# used for plotting the number of fishes and sharks
nbF = 0
nbS = 0
for animal in self.animals :
if animal.num == 1 :
nbF += 1
else :
nbS += 1
return nbF,nbS
### surroundings test
#(this test only works for surroundings, do not use it for other functions)
# sea = Ocean(3,3)
# fish = Fish(0,0)
# shark = Shark(0,0)
# sea.grid[0][2] = fish
# sea.grid[1][2] = fish
# sea.grid[2][1] = fish
# sea.grid[2][2] = fish
# sea.grid[1][0] = shark
# sea.grid[1][1] = shark
# print(sea)
# print(sea.surroundings((1,1)))
# print(sea.surroundings((1,2)))
###
class Animal :
def __init__(self,x,y):
self.pos=[x,y]
self.reprod=0
self.alive=True
def move(self,ocean,d,cellType):
pass
# complete here (remove pass)
def reproduce(self,ocean,oldpos):
pass
# complete here (remove pass)
###
class Fish(Animal):
num = 1
reproductionTreshold = 4
###
class Shark(Animal):
num = 2
reproductionTreshold = 12
###
# for testing once the last function (tick) has been implemented
#nbIter = 100
#atlantic = Ocean()
#atlantic.initialize()
#for _ in range(nbIter):
# atlantic.tick()
#atlantic.show()
def LV():
x = [i for i in range(20,nbIter+1)]
plt.plot(x,fishes,color='green')
plt.plot(x,sharks,color='blue')
plt.show()