Skip to content

Commit c168e12

Browse files
committed
added boundaries check to ship while moving left or right
1 parent b223b44 commit c168e12

File tree

4 files changed

+74
-8
lines changed

4 files changed

+74
-8
lines changed

js/Ship.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
function ship(plateau) {
2-
var self = square(plateau.initialShipPosition(), 15);
2+
var shipSize = 15;
3+
var self = square(plateau.initialShipPosition(), shipSize);
34
self.position = function(newPosition) {
45
return self.corner = newPosition || self.corner;
5-
}
6+
};
7+
self.size = function() {
8+
return shipSize;
9+
};
610

711
self.element = $("<div>")
812
.css("position", "absolute")
@@ -16,16 +20,27 @@ function ship(plateau) {
1620
self.element
1721
.css("top", self.corner.y)
1822
.css("left", self.corner.x);
19-
};
23+
}
2024
updatePosition();
2125

26+
function fixBoundaries() {
27+
if (!plateau.contains(self.corners()[1])) {
28+
self.position().x = plateau.corners()[1].x- self.size() + 1;
29+
}
30+
if (!plateau.contains(self.position())) {
31+
self.position().x = plateau.corner.x;
32+
}
33+
}
34+
2235
self.moveLeft = function() {
2336
self.position(self.position().shift({x: -10, y: 0}));
37+
fixBoundaries();
2438
updatePosition();
2539
};
2640

2741
self.moveRight = function() {
2842
self.position(self.position().shift({x: 10, y: 0}));
43+
fixBoundaries();
2944
updatePosition();
3045
};
3146

js/Square.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ function square(corner, side) {
44
self.side = side;
55

66
self.corners = function() {
7-
return [point(corner.x, corner.y),
8-
point(corner.x + side - 1, corner.y),
9-
point(corner.x + side - 1, corner.y + side - 1),
10-
point(corner.x, corner.y + side - 1)];
7+
return [point(self.corner.x, self.corner.y),
8+
point(self.corner.x + self.side - 1, self.corner.y),
9+
point(self.corner.x + self.side - 1, self.corner.y + self.side - 1),
10+
point(self.corner.x, self.corner.y + self.side - 1)];
11+
};
12+
13+
self.contains = function(aPoint) {
14+
return aPoint.x >= self.corner.x &&
15+
aPoint.x < self.corner.x + side;
1116
};
1217

1318
return self;

spec/ShipSpec.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,34 @@ describe("ship", function() {
3131

3232
expect(s.corner.x).toEqual(0);
3333
});
34+
it("should not go beyond plateau's right most limit", function() {
35+
var p = fakePlateau();
36+
spyOn(p, 'contains').andCallFake(function(corner) {
37+
return !(corner.equals(s.corners()[1]));
38+
});
39+
spyOn(p, 'corners').andReturn(
40+
[null, point(40, 45)]
41+
);
42+
43+
var s = ship(p);
44+
s.moveRight();
45+
46+
expect(s.position().x).toBe(26);
47+
});
48+
it("should not go beyond plateau's left most limit", function() {
49+
var p = fakePlateau();
50+
spyOn(p, 'contains').andCallFake(function(corner) {
51+
return !(corner.equals(s.corners()[0]));
52+
});
53+
spyOn(p, 'corners').andReturn(
54+
[point(5, 15), point(19, 15)]
55+
);
56+
57+
var s = ship(p);
58+
s.moveLeft();
59+
60+
expect(s.position().x).toBe(5);
61+
});
3462
});
3563

3664
describe("right", function () {
@@ -46,10 +74,16 @@ describe("ship", function() {
4674

4775
function fakePlateau() {
4876
return {
77+
side: 31,
4978
add: function() {},
5079
initialShipPosition: function () {
5180
return point(10, 20);
52-
}
81+
},
82+
corner: point(5, 15),
83+
contains: function() {
84+
return true;
85+
},
86+
corners: function() {}
5387
};
5488
}
5589
});

spec/SquareSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,16 @@ describe("square", function() {
1212
expect(s.corners()[2]).toBeEqual(point(5, 6));
1313
expect(s.corners()[3]).toBeEqual(point(2, 6));
1414
});
15+
it("should know when a point is inside of it", function() {
16+
var s = square(point(1, 1), 2);
17+
18+
expect(s.contains(point(1, 1))).toBeTruthy();
19+
expect(s.contains(point(2, 2))).toBeTruthy();
20+
});
21+
it("should know when a point is not inside of it", function() {
22+
var s = square(point(1, 1), 2);
23+
24+
expect(s.contains(point(0, 1))).toBeFalsy();
25+
expect(s.contains(point(3, 2))).toBeFalsy();
26+
});
1527
});

0 commit comments

Comments
 (0)