Created
April 11, 2014 08:26
-
-
Save rarous/10449809 to your computer and use it in GitHub Desktop.
#6 coding dojo
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
var white = 'x'; | |
var black = 'y'; | |
var north = {o:"N"}; | |
var east = {o:"E"}; | |
var south = {o:"S"}; | |
var west = {o:"W"}; | |
north.right = function() { return east; } | |
north.left = function() { return west; } | |
east.right = function() { return south;} | |
east.left = function() { return north;} | |
south.right = function() { return west;} | |
south.left = function() { return east;} | |
west.right = function() { return north;} | |
west.left = function() { return south;} | |
function anyOrientation() { | |
return north; | |
} | |
function anyPosition() { | |
return [0,0]; | |
} | |
var Ant = function () { | |
this.orientation = north; | |
}; | |
Ant.prototype = { | |
move: function (plane) { | |
var color = plane.getColorAtPosition(this.position); | |
var newColor = color == black ? white : black; | |
plane.setColorAtPosition(this.position, newColor); | |
this.position = [1,1]; | |
this.orientation = this.orientation.right(); | |
// this.orientation = color == white | |
// ? this.orientation.right() | |
// : this.orientation.left(); | |
} | |
}; | |
var Plane = function() {}; | |
Plane.prototype = { | |
setColorAtPosition: function (position, color) { | |
this[position] = color; | |
}, | |
getColorAtPosition: function (position) { | |
return this[position] || white; | |
} | |
}; | |
// At a white square, turn 90° right, flip the color of the square, move forward one unit | |
// At a black square, turn 90° left, flip the color of the square, move forward one unit | |
describe("langton's ant", function() { | |
describe("At a white square", function() { | |
var ant; | |
var plane; | |
var position; | |
var originalOrientation; | |
beforeEach(function() { | |
ant = new Ant(); | |
plane = new Plane(); | |
position = anyPosition(); | |
originalOrientation = anyOrientation(); | |
plane.setColorAtPosition(position, white); | |
}); | |
it("turn 90° right", function() { | |
ant.orientation = originalOrientation; | |
ant.move(plane); | |
expect(ant.orientation). | |
toBe(originalOrientation.right()); | |
}); | |
it("flip the color of the square", function() { | |
ant.position = position; | |
ant.move(plane); | |
expect(plane.getColorAtPosition(position)). | |
toBe(black); | |
}); | |
it("move forward one unit", function() { | |
ant.position = position; | |
ant.move(plane); | |
expect(ant.position).not.toBe(position); | |
}); | |
}); | |
xdescribe("At a black square", function() { | |
var ant; | |
var plane; | |
var position; | |
var originalOrientation; | |
beforeEach(function() { | |
ant = new Ant(); | |
plane = new Plane(); | |
position = anyPosition(); | |
originalOrientation = anyOrientation(); | |
plane.setColorAtPosition(position, black); | |
}); | |
it('turn 90° left', function() { | |
ant.orientation = originalOrientation; | |
ant.move(plane); | |
expect(ant.orientation). | |
toBe(originalOrientation.left()); | |
}); | |
}); | |
describe("orientation", function() { | |
it('supports turn 90 right', function() { | |
expect(north.right()).toBe(east); | |
expect(east.right()).toBe(south); | |
expect(south.right()).toBe(west); | |
expect(west.right()).toBe(north); | |
}); | |
it('supports turn 90 left', function() { | |
expect(north.left()).toBe(west); | |
expect(east.left()).toBe(north); | |
expect(south.left()).toBe(east); | |
expect(west.left()).toBe(south); | |
}); | |
}); | |
describe("plane", function() { | |
var plane; | |
beforeEach(function() { | |
plane = new Plane(); | |
}); | |
it('has default color white', function() { | |
expect(plane.getColorAtPosition(anyPosition())). | |
toBe(white); | |
}); | |
it('supports change of color at position', function() { | |
expect(north.left()).toBe(west); | |
expect(east.left()).toBe(north); | |
expect(south.left()).toBe(east); | |
expect(west.left()).toBe(south); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My Pleasure to write you,
My name is Favor Williams,
My email address is
( [email protected])
Am interested to know
more about you,
Contact me for my
photo and other
important issue via,
[email protected]