Skip to content

Commit

Permalink
Code as at end of songkick Dojo
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwynne committed Jan 27, 2009
1 parent 7557648 commit ee1b1c2
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 10 deletions.
74 changes: 71 additions & 3 deletions langstons-ant/models/ant.js
@@ -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]();
}
19 changes: 17 additions & 2 deletions langstons-ant/models/world.js
@@ -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
69 changes: 65 additions & 4 deletions langstons-ant/spec/models/ant_spec.js
@@ -1,6 +1,6 @@
// Rules:
// White square: paint it black, go left
// Black square: paint it white, go right
// White square: paint it black, turn left
// Black square: paint it white, turn right

Screw.Unit(function() {

Expand All @@ -21,13 +21,74 @@ Screw.Unit(function() {
});

it("should move down one square", function() {
expect(ant.location()).to(equal, [4, 5])
expect(ant.location()).to(equal, [5, 4])
});

it("should have painted the square it was on black", function() {
expect(world.isBlack(5, 5)).to(be_true)
expect(world.isBlack(5, 5)).to(be_true)
})

describe("then a second step", function() {
before(function() {
ant.step()
});

it("should move right one square", function() {
expect(ant.location()).to(equal, [6, 4]);
});

it("should have painted the square it was on black", function() {
expect(world.isBlack(5, 4)).to(be_true)
});

describe("then a third step", function() {
before(function() {
ant.step()
});

it("should move up one square", function() {
expect(ant.location()).to(equal, [6, 5]);
});

it("should have painted the square it was on black", function() {
expect(world.isBlack(6, 4)).to(be_true)
});

describe("then a fourth step", function() {
before(function() {
ant.step()
});

it("should move back to square one", function() {
expect(ant.location()).to(equal, [5, 5]);
});

it("should have painted the square it was on black", function() {
expect(world.isBlack(6, 5)).to(be_true)
});

describe("then a fifth step", function() {
before(function() {
ant.step()
});

it("should move up one", function() {
expect(ant.location()).to(equal, [5, 6]);
});

it("should have painted the square it was on white", function() {
expect(world.isBlack(5, 5)).to(be_false)
});

});

});


});

});

});

// TODO:
Expand Down
39 changes: 39 additions & 0 deletions langstons-ant/spec/models/world_spec.js
@@ -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);
});

});

});

});
2 changes: 1 addition & 1 deletion langstons-ant/spec/suite.html
Expand Up @@ -24,7 +24,7 @@
<script src="../models/world.js"></script>

<script src="models/ant_spec.js"></script>

<script src="models/world_spec.js"></script>
</head>
<body></body>
</html>

0 comments on commit ee1b1c2

Please sign in to comment.