Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
193 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,80 @@ | ||
function Ant(world) { | ||
this.world = world; | ||
this.x = this.y = world.size / 2; | ||
with (this) { | ||
this.world = world; | ||
this.x = this.y = world.size / 2; | ||
this.NORTH = 0; | ||
this.EAST = 1; | ||
this.SOUTH = 2; | ||
this.WEST = 3; | ||
this.direction = SOUTH; | ||
|
||
this.directions = [ | ||
function() { | ||
y++; | ||
}, | ||
function() { | ||
x++; | ||
}, | ||
function() { | ||
y--; | ||
}, | ||
function() { | ||
x--; | ||
} | ||
] | ||
|
||
this.black = function() { | ||
return { | ||
toggleColour: function() { | ||
world.paintWhite(x, y) | ||
}, | ||
rotate: function() { | ||
direction += 1; | ||
if(direction > WEST) { | ||
direction = NORTH | ||
} | ||
} | ||
} | ||
}(); | ||
|
||
this.white = function() { | ||
return { | ||
toggleColour: function() { | ||
world.paintBlack(x, y) | ||
}, | ||
rotate: function() { | ||
direction -= 1; | ||
if(direction < NORTH) { | ||
direction = WEST | ||
} | ||
} | ||
} | ||
}(); | ||
|
||
this.currentColour = function() { | ||
if (world.isBlack(x,y)) { | ||
return black; | ||
} | ||
else { | ||
return white; | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
Ant.prototype.location = function() { | ||
with (this) return [x, y]; | ||
} | ||
|
||
Ant.prototype.step = function() { | ||
|
||
with(this) { | ||
currentColour().toggleColour(); | ||
moveForward(); | ||
currentColour().rotate(); | ||
} | ||
} | ||
|
||
Ant.prototype.moveForward = function() { | ||
this.directions[this.direction](); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,24 @@ | ||
function World(size) { | ||
this.size = size; | ||
this.rows = new Array(size); | ||
for (var i = 0; i < this.rows.length; i++) { | ||
this.rows[i] = new Array(size); | ||
}; | ||
} | ||
|
||
World.prototype.isBlack = function(x, y) { | ||
return this.rows[x][y] == true; | ||
} | ||
|
||
World.prototype.paintBlack = function(x, y) { | ||
this.rows[x][y] = true; | ||
} | ||
|
||
World.prototype.paintWhite = function(x, y) { | ||
this.rows[x][y] = false; | ||
} | ||
|
||
|
||
// TODO: | ||
// isBlack | ||
// paintBlack(x,y) | ||
// paintWhite(x,y) | ||
// paintAllBlack |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
Screw.Unit(function() { | ||
|
||
describe("World", function() { | ||
describe("with 2x2 world", function () { | ||
before(function() { | ||
world = new World(2); | ||
}); | ||
|
||
describe("after painting a square black", function() { | ||
before(function() { | ||
world.paintBlack(0,0); | ||
}); | ||
|
||
it("should be black", function() { | ||
expect(world.isBlack(0,0)).to(be_true); | ||
}); | ||
|
||
it("another square should be white", function() { | ||
expect(world.isBlack(0,1)).to(be_false); | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
describe("With one square", function() { | ||
before(function() { | ||
world = new World(1); | ||
}); | ||
|
||
it("should start off white", function() { | ||
expect(world.isBlack(0,0)).to(be_false); | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains 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