Advertisement
lbromo

Langton's ant python implementation

Apr 6th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.01 KB | None | 0 0
  1. from random import randint
  2. from tkinter import *
  3. from time import sleep
  4. from random import randint
  5.  
  6. SQ_SIZE = 800
  7. SQ_STEP = 10
  8.  
  9. gridMap = {}
  10.  
  11. class Color(object):
  12.     """
  13.    Color, should be an Enum, but this was written in Python3.3
  14.    """
  15.     BLACK = 'black'
  16.     WHITE = 'white'
  17.  
  18. class Direction(object):
  19.     """
  20.    Direction, should be an Enum, but this was written in Python3.3
  21.    """
  22.     UP = 90
  23.     LEFT = 180
  24.     DOWN = 270
  25.     RIGHT = 0
  26.  
  27. class Coord(object):
  28.     """
  29.    Defines a coordinate with a (x, y) point.
  30.    Method get_key() returns a string on the form (x,y)
  31.    """
  32.     x,y = int, int
  33.     def __init__(self, x, y):
  34.         self.x = x
  35.         self.y = y
  36.    
  37.     def get_key(self):
  38.         """
  39.        returns a string on the form (x,y)
  40.        """
  41.         return str.format("{0},{1}", self.x, self.y)
  42.  
  43. class Point(object):
  44.     """
  45.    A Point defines an object on the Canvas (note only rectangles as of now)
  46.    """
  47.     ID = str
  48.     top_left = Coord
  49.     bot_right = Coord
  50.     color = object
  51.  
  52.     def __init__(self, ID, x, y, color):
  53.         self.ID = ID
  54.         self.top_left = Coord(x, y)
  55.         self.bot_right = Coord(x + SQ_STEP, y + SQ_STEP)
  56.         self.color = color
  57.        
  58.         w.create_rectangle(self.top_left.x, self.top_left.y, self.bot_right.x, self.bot_right.y, fill=self.color)
  59.        
  60.     def updateColor(self):
  61.         """
  62.        Flips the coler of the object from balck to white and vica versa
  63.        """
  64.         if self.color is Color.BLACK:
  65.             self.color = Color.WHITE
  66.         elif self.color is Color.WHITE:
  67.             self.color = Color.BLACK
  68.         else:
  69.             print("Error in updating colors")
  70.        
  71.         w.create_rectangle(self.top_left.x, self.top_left.y, self.bot_right.x, self.bot_right.y, fill=self.color)
  72.  
  73. class Ant(object):
  74.     """
  75.    The object of them all!! Walks around a flips colors of Points
  76.    """
  77.     direction, locationKey = object, Coord
  78.  
  79.     def __init__(self, direction = None, loctaionKey = None):
  80.         if direction is None and loctaionKey is None:
  81.             self.direction = Direction.UP
  82.             self.locationKey = Coord(400, 400)
  83.         elif direction is not None and loctaionKey is None:
  84.             self.direction = direction
  85.             self.locationKey = Coord(400, 400)
  86.         elif direction is not None and loctaionKey is not None:
  87.             self.direction = direction
  88.             self.locationKey = loctaionKey
  89.        
  90.         gridMap[self.locationKey.get_key()].updateColor()
  91.    
  92.     def move(self):        
  93.         """
  94.        Take a step, and flip a point
  95.        """
  96.         if gridMap[self.locationKey.get_key()].color is Color.BLACK:
  97.             turn = 90
  98.         else:
  99.             turn = 270
  100.            
  101.         self.direction = (self.direction + turn) % 360
  102.        
  103.         if self.direction == Direction.UP:
  104.             self.locationKey.y += -SQ_STEP
  105.         elif self.direction == Direction.DOWN:
  106.             self.locationKey.y += SQ_STEP
  107.         elif self.direction == Direction.LEFT:
  108.             self.locationKey.x += -SQ_STEP
  109.         elif self.direction == Direction.RIGHT:
  110.             self.locationKey.x += SQ_STEP
  111.    
  112.         gridMap[self.locationKey.get_key()].updateColor()
  113.        
  114.  
  115. def run():
  116.     """
  117.    Thread for ant walking
  118.    """
  119.     ant = Ant(Direction.UP, Coord(int(SQ_SIZE / 2 + 10), int(SQ_SIZE / 2 + 10)))
  120.     for i in range(0,11000):
  121.         ant.move()
  122.         if not i % 1:
  123.             w.update_idletasks()
  124.             sleep(0.01)
  125.    
  126.     print("POST SCRIPT")
  127.     w.postscript(file="Langton's ant.eps")
  128.     print("Jobs done !")
  129.  
  130. if __name__ == '__main__':
  131.    
  132.     master = Tk()
  133.     w = Canvas(master, width=SQ_SIZE, height=SQ_SIZE)
  134.     w.pack()
  135.    
  136.    
  137.     for x in range(0, SQ_SIZE, SQ_STEP):
  138.         for y in range (0, SQ_SIZE, SQ_STEP):
  139.             ID = Coord(x,y)
  140.             gridMap[ID.get_key()] = Point(ID, x, y, Color.WHITE)
  141.                
  142.     w.after(500, run)        
  143.     w.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement