Permalink
Browse files

Adding coffee-script version of DrunkenCockroach

  • Loading branch information...
1 parent 5ff7ac4 commit a62a00e68aa76326e0b8d2e1e4065b3c5a4408d8 @zdennis committed Jul 4, 2012
Showing with 11,579 additions and 0 deletions.
  1. +116 −0 coffee-script/drunken-cockroach.coffee
  2. +135 −0 coffee-script/drunken-cockroach.js
  3. +1 −0 coffee-script/node_modules/.bin/cake
  4. +1 −0 coffee-script/node_modules/.bin/coffee
  5. +11 −0 coffee-script/node_modules/coffee-script/.npmignore
  6. +1 −0 coffee-script/node_modules/coffee-script/CNAME
  7. +22 −0 coffee-script/node_modules/coffee-script/LICENSE
  8. +51 −0 coffee-script/node_modules/coffee-script/README
  9. +78 −0 coffee-script/node_modules/coffee-script/Rakefile
  10. +7 −0 coffee-script/node_modules/coffee-script/bin/cake
  11. +7 −0 coffee-script/node_modules/coffee-script/bin/coffee
  12. +44 −0 coffee-script/node_modules/coffee-script/extras/jsl.conf
  13. +92 −0 coffee-script/node_modules/coffee-script/lib/coffee-script/browser.js
  14. +111 −0 coffee-script/node_modules/coffee-script/lib/coffee-script/cake.js
  15. +167 −0 coffee-script/node_modules/coffee-script/lib/coffee-script/coffee-script.js
  16. +500 −0 coffee-script/node_modules/coffee-script/lib/coffee-script/command.js
  17. +606 −0 coffee-script/node_modules/coffee-script/lib/coffee-script/grammar.js
  18. +77 −0 coffee-script/node_modules/coffee-script/lib/coffee-script/helpers.js
  19. +11 −0 coffee-script/node_modules/coffee-script/lib/coffee-script/index.js
  20. +788 −0 coffee-script/node_modules/coffee-script/lib/coffee-script/lexer.js
  21. +2,986 −0 coffee-script/node_modules/coffee-script/lib/coffee-script/nodes.js
  22. +138 −0 coffee-script/node_modules/coffee-script/lib/coffee-script/optparse.js
  23. +683 −0 coffee-script/node_modules/coffee-script/lib/coffee-script/parser.js
  24. +261 −0 coffee-script/node_modules/coffee-script/lib/coffee-script/repl.js
  25. +349 −0 coffee-script/node_modules/coffee-script/lib/coffee-script/rewriter.js
  26. +146 −0 coffee-script/node_modules/coffee-script/lib/coffee-script/scope.js
  27. +47 −0 coffee-script/node_modules/coffee-script/package.json
  28. +106 −0 coffee-script/node_modules/simplesets/README.md
  29. +35 −0 coffee-script/node_modules/simplesets/examples/hello.js
  30. +333 −0 coffee-script/node_modules/simplesets/lib/simplesets.js
  31. +24 −0 coffee-script/node_modules/simplesets/package.json
  32. +180 −0 coffee-script/node_modules/simplesets/test/testsets.js
  33. +3 −0 coffee-script/node_modules/underscore/.npmignore
  34. +1 −0 coffee-script/node_modules/underscore/CNAME
  35. +22 −0 coffee-script/node_modules/underscore/LICENSE
  36. +19 −0 coffee-script/node_modules/underscore/README.md
  37. BIN coffee-script/node_modules/underscore/favicon.ico
  38. +2,109 −0 coffee-script/node_modules/underscore/index.html
  39. +1 −0 coffee-script/node_modules/underscore/index.js
  40. +28 −0 coffee-script/node_modules/underscore/package.json
  41. BIN coffee-script/node_modules/underscore/raw/underscore.psd
  42. +32 −0 coffee-script/node_modules/underscore/underscore-min.js
  43. +1,059 −0 coffee-script/node_modules/underscore/underscore.js
  44. +19 −0 coffee-script/node_modules/xtend/LICENCE
  45. +4 −0 coffee-script/node_modules/xtend/Makefile
  46. +60 −0 coffee-script/node_modules/xtend/README.md
  47. +17 −0 coffee-script/node_modules/xtend/browser/index.js
  48. +15 −0 coffee-script/node_modules/xtend/index.js
  49. +46 −0 coffee-script/node_modules/xtend/package.json
  50. +14 −0 coffee-script/node_modules/xtend/support/compile.js
  51. +14 −0 coffee-script/package.json
  52. +2 −0 ruby/drunken-cockroach.rb
@@ -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
@@ -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);

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit a62a00e

Please sign in to comment.