-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,51 @@ | ||
from life import Ant, Food, Land | ||
from math import sqrt, pi | ||
from math import pi | ||
class AntWorld: | ||
''' | ||
It contains the World Model | ||
''' | ||
def __init__(self, numOfAnts=1, width=800, length=800): | ||
self.numOfAnts = numOfAnts | ||
self.width = width | ||
self.length = length | ||
self.land = Land(width, length) | ||
self.ants = [Ant(self.land) for i in range(numOfAnts)] | ||
self.food = Food(self.land) | ||
self.land.ants = [Ant(self.land) for i in range(numOfAnts)] | ||
self.land.food = Food(self.land) | ||
self.ants = self.land.ants | ||
self.food = self.land.food | ||
|
||
def run(self): | ||
for ant in self.ants: | ||
ant.walk() | ||
self.checkBoundary(ant) | ||
ant.patrol() | ||
self.checkBoundary(ant, True) | ||
ant.leaveSignal() | ||
|
||
def checkSuccess(self): | ||
for ant in self.ants: | ||
if self.getDistance(ant, self.food) < 15: | ||
return True | ||
# for ant in self.ants: | ||
# if self.land.getDistanceAB(ant, self.food) < 15: | ||
# return True | ||
return False | ||
|
||
def getFoodPosition(self): | ||
return (self.food.x, self.food.y) | ||
|
||
def getDistance(self, lifea, lifeb): | ||
return sqrt((lifea.x - lifeb.x) ** 2 + (lifea.y - lifeb.y) ** 2) | ||
|
||
def checkBoundary(self, ant, bounce=False): | ||
if (bounce == True): | ||
#Rule 1 Touch wall = bounce back like light | ||
if (int(ant.x) >= self.width - 1): | ||
if (int(ant.x) >= self.land.width - 1): | ||
ant.facingAngle = -ant.facingAngle - pi | ||
ant.x = self.width - 1 | ||
ant.x = self.land.width - 1 | ||
elif (int(ant.x) <= 0): | ||
ant.facingAngle = -ant.facingAngle - pi | ||
ant.x = 0 | ||
|
||
elif (int(ant.y) >= self.length - 1): | ||
elif (int(ant.y) >= self.land.length - 1): | ||
ant.facingAngle = -ant.facingAngle | ||
ant.y = self.length - 1 | ||
ant.y = self.land.length - 1 | ||
|
||
elif (int(ant.y) <= 0): | ||
ant.facingAngle = -ant.facingAngle | ||
ant.y = 0 | ||
ant.facingAngle %= 2 * pi | ||
else: | ||
#Rule 2 Touch wall = cross | ||
ant.x %= self.width | ||
ant.x %= self.land.width | ||
ant.y %= self.length | ||
|