Advertisement
SciQuest_csp

Langton's Ant, Ant

Mar 31st, 2012
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.26 KB | None | 0 0
  1. Ant = class()
  2.  
  3. function Ant:init(dir, x, y, rules)
  4.     self.dir = dir
  5.     self.x = x
  6.     self.y = y
  7.     self.rules = {}
  8.     for k,v in pairs(rules) do
  9.         table.insert(self.rules, k, v)
  10.     end
  11. end
  12.  
  13. function Ant:go(board)
  14.     self.x = self:wrap(self.x, board.width)
  15.     self.y = self:wrap(self.y, board.height)
  16.  
  17.     local p = grey
  18.     p.r, p.g, p.b, p.a = board:get(self.x, self.y)
  19.     for k,v in pairs(self.rules) do
  20.         if colorEqual(colors[k], p) then
  21.             newColor = k + 1
  22.             if #self.rules < newColor then
  23.                 newColor = 1
  24.             end
  25.            
  26.             board:set(self.x, self.y, colors[newColor])
  27.            
  28.             if v == L then
  29.                 self.dir = self.dir:rotate90()
  30.             else
  31.                 self.dir = -self.dir:rotate90()
  32.             end
  33.            
  34.             self.x = self.x + self.dir.x
  35.             self.y = self.y + self.dir.y
  36.             return
  37.         end        
  38.     end
  39.  
  40.     self.x = self.x + self.dir.x
  41.     self.y = self.y + self.dir.y
  42. end
  43.  
  44. function Ant:wrap(pos, bound)
  45.     if pos < 1 then
  46.         return bound
  47.     elseif bound < pos then
  48.         return 1
  49.     end
  50.     return pos
  51. end
  52.  
  53. function Ant:draw()
  54. end
  55. function Ant:touched(touch)
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement