Skip to content

Commit

Permalink
Adding coffee-script version of DrunkenCockroach
Browse files Browse the repository at this point in the history
  • Loading branch information
zdennis committed Jul 4, 2012
1 parent 5ff7ac4 commit a62a00e
Show file tree
Hide file tree
Showing 52 changed files with 11,579 additions and 0 deletions.
116 changes: 116 additions & 0 deletions coffee-script/drunken-cockroach.coffee
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,116 @@
extend = require 'xtend'
sets = require 'simplesets'
_ = require 'underscore'

Comparable =
compareTo: (other) ->
throw "Must supply where included to return -1, 0, or 1!"

isEqualTo: (other) ->
@compareTo(other) == 0

isGreaterThan: (other) ->
@compareTo(other) == 1

isLessThan: (other) ->
@compareTo(other) == -1

class Point
extend @prototype, Comparable

constructor: (@x, @y) ->

plus: (otherPoint) ->
new Point(@x + otherPoint.x, @y + otherPoint.y)

compareTo: (other) ->
if other.x > @x || other.y > @y then -1
else if other.x == @x && other.y == @y then 0
else 1

max: (other) ->
if @isGreaterThan(other) then @ else other

min: (other) ->
if @isLessThan(other) then @ else other


class Rectangle
constructor: (attrs = {}) ->
@originPoint = attrs.originPoint
@corner = attrs.corner

width: -> @corner.x
height: -> @corner.y

class Tile
extend @prototype, Comparable

constructor: (attrs = {}) ->
@location = attrs.location
@floorArea = attrs.floorArea

neighborAt: (deltaPoint) ->
new_tile = new Tile floorArea: @floorArea
wants_to_move_to = @location.plus deltaPoint
actually_moving_to = wants_to_move_to.max(@floorArea.originPoint).min(@floorArea.corner)
new_tile.location = actually_moving_to
new_tile

compareTo: (other) ->
if other.location.isEqualTo(@location) then 0
else -1

toString: ->
"(#{@location.x},#{@location.y})"


class DrunkenCockroach
constructor: ->
@tile = new Tile
@tilesVisited = []

directions: ->
directions = []
for x in [-1..1]
for y in [-1..1]
directions.push new Point(x,y)
@directions = -> directions
directions

walkWithin: (rectangle, startingPoint) ->
@tilesVisited = []
@tile = new Tile location: startingPoint, floorArea: rectangle
number_of_tiles = rectangle.width() * rectangle.height()

@tilesVisited.push @tile
directions = @directions()

computeSet = =>
simple_types = _.map @tilesVisited, (t) -> t.toString()
new sets.Set simple_types

while computeSet().size() < number_of_tiles
index = parseInt(Math.random() * directions.length)
delta_point = directions[index]
@tile = @tile.neighborAt(delta_point)
@tilesVisited.push @tile

numberOfSteps: ->
@tilesVisited.length

timesSteppedOn: (tile) ->
console.log "timesSteppedOn is not yet implemented"
#@tilesVisited.count(tile)


c = new DrunkenCockroach
results = []
for num in [1..10]
rectangle = new Rectangle
originPoint: new Point(1,1)
corner: new Point(5,5)
c.walkWithin rectangle, new Point(1,1)
results.push c.numberOfSteps()

console.log results
135 changes: 135 additions & 0 deletions coffee-script/drunken-cockroach.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,135 @@
(function() {
var Comparable, DrunkenCockroach, Point, Rectangle, Tile, c, extend, sets, _;
extend = require('xtend');
sets = require('simplesets');
_ = require('underscore');
Comparable = {
compareTo: function(other) {
throw "Must supply where included to return -1, 0, or 1!";
},
isEqualTo: function(other) {
return this.compareTo(other) === 0;
},
isGreaterThan: function(other) {
return this.compareTo(other) === 1;
},
isLessThan: function(other) {
return this.compareTo(other) === -1;
}
};
Point = (function() {
extend(Point.prototype, Comparable);
function Point(x, y) {
this.x = x;
this.y = y;
}
Point.prototype.plus = function(otherPoint) {
return new Point(this.x + otherPoint.x, this.y + otherPoint.y);
};
Point.prototype.compareTo = function(other) {
if (other.x > this.x || other.y > this.y) {
return -1;
} else if (other.x === this.x && other.y === this.y) {
return 0;
} else {
return 1;
}
};
Point.prototype.max = function(other) {
if (this.isGreaterThan(other)) {
return this;
} else {
return other;
}
};
Point.prototype.min = function(other) {
if (this.isLessThan(other)) {
return this;
} else {
return other;
}
};
return Point;
})();
Rectangle = (function() {
function Rectangle(_arg) {
this.originPoint = _arg.originPoint, this.corner = _arg.corner;
}
Rectangle.prototype.width = function() {
return this.corner.x;
};
Rectangle.prototype.height = function() {
return this.corner.y;
};
return Rectangle;
})();
Tile = (function() {
extend(Tile.prototype, Comparable);
function Tile(_arg) {
this.location = _arg.location, this.floorArea = _arg.floorArea;
}
Tile.prototype.neighborAt = function(deltaPoint) {
var actually_moving_to, new_tile, wants_to_move_to;
new_tile = new Tile({
floorArea: this.floorArea
});
wants_to_move_to = this.location + deltaPoint;
actually_moving_to = wants_to_move_to.max(this.floorArea.originPoint).min(this.floorArea.corner);
new_tile.location = actually_moving_to;
return new_tile;
};
Tile.prototype.compareTo = function(other) {
if (other.location.isEqualTo(this.location)) {
return 0;
} else {
return -1;
}
};
return Tile;
})();
DrunkenCockroach = (function() {
function DrunkenCockroach() {
this.tilesVisited = [];
}
DrunkenCockroach.prototype.directions = function() {
var directions, x, y;
directions = [];
for (x = -1; x <= 1; x++) {
for (y = -1; y <= 1; y++) {
directions.push(new Point(x, y));
}
}
return this.directions = function() {
return directions;
};
};
DrunkenCockroach.prototype.walkWithin = function(rectangle, startingPoint) {
var delta_point, directions, index, number_of_tiles, tile_set, _results;
this.tilesVisited = [];
this.tile = new Tile({
location: startingPoint,
floorArea: rectangle
});
number_of_tiles = rectangle.width() * rectangle.height();
this.tilesVisited.push(this.tile);
tile_set = new sets.Set(this.tilesVisited);
directions = this.directions();
_results = [];
while (tile_set.size() < number_of_tiles) {
index = Math.random() * directions.length + 1;
delta_point = directions[index];
this.tile = this.tile.neighborAt(delta_point);
_results.push(this.tilesVisited.push(this.tile));
}
return _results;
};
DrunkenCockroach.prototype.numberOfSteps = function() {
return this.tilesVisited.length;
};
DrunkenCockroach.prototype.timesSteppedOn = function(tile) {
return console.log("timesSteppedOn is not yet implemented");
};
return DrunkenCockroach;
})();
c = new DrunkenCockroach;
}).call(this);
1 change: 1 addition & 0 deletions coffee-script/node_modules/.bin/cake

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coffee-script/node_modules/.bin/coffee

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions coffee-script/node_modules/coffee-script/.npmignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coffee-script/node_modules/coffee-script/CNAME

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions coffee-script/node_modules/coffee-script/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions coffee-script/node_modules/coffee-script/README

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a62a00e

Please sign in to comment.