@@ -56,16 +56,29 @@ const getSnakeHead = (snake) =>
56
56
const getSnakeWithoutStub = ( snake ) =>
57
57
snake . coordinates . slice ( 0 , snake . coordinates . length - 1 ) ;
58
58
59
+ const getSnakeTail = ( snake ) =>
60
+ snake . coordinates . slice ( 1 ) ;
61
+
62
+ const getIsSnakeOutside = ( snake ) =>
63
+ getSnakeHead ( snake ) . x >= GRID_SIZE ||
64
+ getSnakeHead ( snake ) . y >= GRID_SIZE ||
65
+ getSnakeHead ( snake ) . x <= 0 ||
66
+ getSnakeHead ( snake ) . y <= 0 ;
67
+
68
+ const getIsSnakeClumy = ( snake ) =>
69
+ isSnake ( getSnakeHead ( snake ) . x , getSnakeHead ( snake ) . y , getSnakeTail ( snake ) ) ;
70
+
59
71
const getIsSnakeEating = ( { snake, snack } ) =>
60
72
isPosition ( getSnakeHead ( snake ) . x , getSnakeHead ( snake ) . y , snack . coordinate . x , snack . coordinate . y ) ;
61
73
62
- const getCellCs = ( snake , snack , x , y ) =>
74
+ const getCellCs = ( isGameOver , snake , snack , x , y ) =>
63
75
cs (
64
76
'grid-cell' ,
65
77
{
66
78
'grid-cell-border' : isBorder ( x , y ) ,
67
79
'grid-cell-snake' : isSnake ( x , y , snake . coordinates ) ,
68
80
'grid-cell-snack' : isPosition ( x , y , snack . coordinate . x , snack . coordinate . y ) ,
81
+ 'grid-cell-hit' : isGameOver && isPosition ( x , y , getSnakeHead ( snake ) . x , getSnakeHead ( snake ) . y ) ,
69
82
}
70
83
) ;
71
84
@@ -95,6 +108,12 @@ const applySnakePosition = (prevState) => {
95
108
} ;
96
109
} ;
97
110
111
+ const applyGameOver = ( prevState ) => ( {
112
+ playground : {
113
+ isGameOver : true
114
+ } ,
115
+ } ) ;
116
+
98
117
const doChangeDirection = ( direction ) => ( ) => ( {
99
118
playground : {
100
119
direction,
@@ -108,6 +127,7 @@ class App extends Component {
108
127
this . state = {
109
128
playground : {
110
129
direction : DIRECTIONS . RIGHT ,
130
+ isGameOver : false ,
111
131
} ,
112
132
snake : {
113
133
coordinates : [ getRandomCoordinate ( ) ] ,
@@ -137,13 +157,16 @@ class App extends Component {
137
157
}
138
158
139
159
onTick = ( ) => {
140
- this . setState ( applySnakePosition ) ;
160
+ getIsSnakeOutside ( this . state . snake ) || getIsSnakeClumy ( this . state . snake )
161
+ ? this . setState ( applyGameOver )
162
+ : this . setState ( applySnakePosition ) ;
141
163
}
142
164
143
165
render ( ) {
144
166
const {
145
167
snake,
146
168
snack,
169
+ playground,
147
170
} = this . state ;
148
171
149
172
return (
@@ -152,25 +175,27 @@ class App extends Component {
152
175
< Grid
153
176
snake = { snake }
154
177
snack = { snack }
178
+ isGameOver = { playground . isGameOver }
155
179
/>
156
180
</ div >
157
181
) ;
158
182
}
159
183
}
160
184
161
- const Grid = ( { snake, snack } ) =>
185
+ const Grid = ( { isGameOver , snake, snack } ) =>
162
186
< div >
163
187
{ GRID . map ( y =>
164
188
< Row
165
189
y = { y }
166
190
key = { y }
167
191
snake = { snake }
168
192
snack = { snack }
193
+ isGameOver = { isGameOver }
169
194
/>
170
195
) }
171
196
</ div >
172
197
173
- const Row = ( { snake, snack, y } ) =>
198
+ const Row = ( { isGameOver , snake, snack, y } ) =>
174
199
< div className = "grid-row" >
175
200
{ GRID . map ( x =>
176
201
< Cell
@@ -179,11 +204,12 @@ const Row = ({ snake, snack, y }) =>
179
204
key = { x }
180
205
snake = { snake }
181
206
snack = { snack }
207
+ isGameOver = { isGameOver }
182
208
/>
183
209
) }
184
210
</ div >
185
211
186
- const Cell = ( { snake, snack, x, y } ) =>
187
- < div className = { getCellCs ( snake , snack , x , y ) } />
212
+ const Cell = ( { isGameOver , snake, snack, x, y } ) =>
213
+ < div className = { getCellCs ( isGameOver , snake , snack , x , y ) } />
188
214
189
215
export default App ;
0 commit comments