Created
November 6, 2017 16:51
-
-
Save mimetaur/b24285c4a2b6475e0f84c32cb8094fbd to your computer and use it in GitHub Desktop.
Dinner Table Guest from A.K. Dewdney's simulation
This file contains hidden or 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
| class Guest(object): | |
| def __init__(self, name, index, x, y, ideal_distances): | |
| self.name = name | |
| self.index = index | |
| self.x = x | |
| self.y = y | |
| self.target_x = x | |
| self.target_y = y | |
| self.ideal_distances = ideal_distances | |
| self.presum = 0 | |
| self.sum = 0 | |
| def update(self, guests): | |
| self.presum = self.sum | |
| self.sum = 0 | |
| for i in range(8): | |
| otherGuest = guests[i] | |
| distance = self.distanceTo(otherGuest) | |
| self.sum += abs( distance - self.ideal_distances[i]) | |
| if (self.sum < self.presum): | |
| self.target_x = otherGuest.x | |
| self.target_y = otherGuest.y | |
| if (self.target_x > self.x): | |
| self.x += 1 | |
| if (self.target_x < self.x): | |
| self.x -= 1 | |
| if (self.target_y > self.y): | |
| self.y += 1 | |
| if (self.target_y < self.y): | |
| self.y -= 1 | |
| # TODO need to translate the distance algorithm on page 156 | |
| # TODO need to figure out keeping guests from walking through tables or walls | |
| # self.x += translate(self.target_x - self.x, 0, 30, -1, 1) | |
| # self.y += translate(self.target_y - self.y, 0, 30, -1, 1) | |
| def distanceTo(self, otherGuest): | |
| return math.sqrt(math.pow( self.x - otherGuest.x, 2) + math.pow( self.y - otherGuest.y, 2)) | |
| def __repr__(self): | |
| tmpl = Template("$name is at: [$x,$y] and has target of [$target_x][$target_y]") | |
| return tmpl.substitute(name=self.name, x=self.x, y=self.y, target_x=self.target_x, target_y=self.target_y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment