File tree 4 files changed +74
-8
lines changed
4 files changed +74
-8
lines changed Original file line number Diff line number Diff line change 1
1
function ship ( plateau ) {
2
- var self = square ( plateau . initialShipPosition ( ) , 15 ) ;
2
+ var shipSize = 15 ;
3
+ var self = square ( plateau . initialShipPosition ( ) , shipSize ) ;
3
4
self . position = function ( newPosition ) {
4
5
return self . corner = newPosition || self . corner ;
5
- }
6
+ } ;
7
+ self . size = function ( ) {
8
+ return shipSize ;
9
+ } ;
6
10
7
11
self . element = $ ( "<div>" )
8
12
. css ( "position" , "absolute" )
@@ -16,16 +20,27 @@ function ship(plateau) {
16
20
self . element
17
21
. css ( "top" , self . corner . y )
18
22
. css ( "left" , self . corner . x ) ;
19
- } ;
23
+ }
20
24
updatePosition ( ) ;
21
25
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
+
22
35
self . moveLeft = function ( ) {
23
36
self . position ( self . position ( ) . shift ( { x : - 10 , y : 0 } ) ) ;
37
+ fixBoundaries ( ) ;
24
38
updatePosition ( ) ;
25
39
} ;
26
40
27
41
self . moveRight = function ( ) {
28
42
self . position ( self . position ( ) . shift ( { x : 10 , y : 0 } ) ) ;
43
+ fixBoundaries ( ) ;
29
44
updatePosition ( ) ;
30
45
} ;
31
46
Original file line number Diff line number Diff line change @@ -4,10 +4,15 @@ function square(corner, side) {
4
4
self . side = side ;
5
5
6
6
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 ;
11
16
} ;
12
17
13
18
return self ;
Original file line number Diff line number Diff line change @@ -31,6 +31,34 @@ describe("ship", function() {
31
31
32
32
expect ( s . corner . x ) . toEqual ( 0 ) ;
33
33
} ) ;
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
+ } ) ;
34
62
} ) ;
35
63
36
64
describe ( "right" , function ( ) {
@@ -46,10 +74,16 @@ describe("ship", function() {
46
74
47
75
function fakePlateau ( ) {
48
76
return {
77
+ side : 31 ,
49
78
add : function ( ) { } ,
50
79
initialShipPosition : function ( ) {
51
80
return point ( 10 , 20 ) ;
52
- }
81
+ } ,
82
+ corner : point ( 5 , 15 ) ,
83
+ contains : function ( ) {
84
+ return true ;
85
+ } ,
86
+ corners : function ( ) { }
53
87
} ;
54
88
}
55
89
} ) ;
Original file line number Diff line number Diff line change @@ -12,4 +12,16 @@ describe("square", function() {
12
12
expect ( s . corners ( ) [ 2 ] ) . toBeEqual ( point ( 5 , 6 ) ) ;
13
13
expect ( s . corners ( ) [ 3 ] ) . toBeEqual ( point ( 2 , 6 ) ) ;
14
14
} ) ;
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
+ } ) ;
15
27
} ) ;
You can’t perform that action at this time.
0 commit comments