Created
February 17, 2012 14:36
-
-
Save mizchi/1853835 to your computer and use it in GitHub Desktop.
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
require 'sugar' | |
Object.extend() | |
log = console.log | |
class WorkerMachine | |
constructor:-> | |
guess:-> | |
go_home:-> | |
class Event | |
constructor:(@x, @y)-> | |
class Flesh extends Event | |
class Poison extends Event | |
class Ant | |
constructor:(@x, @y)-> | |
@machine = new WorkerMachine | |
action:-> | |
class Map | |
constructor:(w, h)-> | |
@wall = @blank(w, h) | |
@events = [] | |
@ants = [] | |
# set wall | |
(((@wall[y][x] = if( | |
x is 0 or x is row.length-1 or y is 0 or y is @wall.length-1 | |
)then 1 else 0) for _ , x in row) for row, y in @wall) | |
@events.push ( | |
if Math.random()>0.5 | |
new Poison(Number.random(1, w), Number.random(1, h)) | |
else | |
new Flesh( Number.random(1, w), Number.random(1, h)) | |
) for _ in [1..10] | |
blank:(x,y)-> | |
((0 for m in [1..x]) for n in [1..y]) | |
is_collide:(x, y)-> | |
return @wall[~~y][~~x] | |
show : -> | |
((log (( | |
if w is 1 then "#" | |
else if (ev = @events.find (e)-> x is e.x and y is e.y) | |
if ev instanceof Flesh then 'f' | |
else if ev instanceof Poison then 'p' | |
else " ") for w, x in row).join('')) | |
) for row, y in @wall | |
ant = new Ant | |
ant.action() | |
map = new Map 30, 10 | |
map.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment