@@ -56,16 +56,29 @@ const getSnakeHead = (snake) =>
5656const getSnakeWithoutStub = ( snake ) =>
5757 snake . coordinates . slice ( 0 , snake . coordinates . length - 1 ) ;
5858
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+
5971const getIsSnakeEating = ( { snake, snack } ) =>
6072 isPosition ( getSnakeHead ( snake ) . x , getSnakeHead ( snake ) . y , snack . coordinate . x , snack . coordinate . y ) ;
6173
62- const getCellCs = ( snake , snack , x , y ) =>
74+ const getCellCs = ( isGameOver , snake , snack , x , y ) =>
6375 cs (
6476 'grid-cell' ,
6577 {
6678 'grid-cell-border' : isBorder ( x , y ) ,
6779 'grid-cell-snake' : isSnake ( x , y , snake . coordinates ) ,
6880 '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 ) ,
6982 }
7083 ) ;
7184
@@ -95,6 +108,12 @@ const applySnakePosition = (prevState) => {
95108 } ;
96109} ;
97110
111+ const applyGameOver = ( prevState ) => ( {
112+ playground : {
113+ isGameOver : true
114+ } ,
115+ } ) ;
116+
98117const doChangeDirection = ( direction ) => ( ) => ( {
99118 playground : {
100119 direction,
@@ -108,6 +127,7 @@ class App extends Component {
108127 this . state = {
109128 playground : {
110129 direction : DIRECTIONS . RIGHT ,
130+ isGameOver : false ,
111131 } ,
112132 snake : {
113133 coordinates : [ getRandomCoordinate ( ) ] ,
@@ -137,13 +157,16 @@ class App extends Component {
137157 }
138158
139159 onTick = ( ) => {
140- this . setState ( applySnakePosition ) ;
160+ getIsSnakeOutside ( this . state . snake ) || getIsSnakeClumy ( this . state . snake )
161+ ? this . setState ( applyGameOver )
162+ : this . setState ( applySnakePosition ) ;
141163 }
142164
143165 render ( ) {
144166 const {
145167 snake,
146168 snack,
169+ playground,
147170 } = this . state ;
148171
149172 return (
@@ -152,25 +175,27 @@ class App extends Component {
152175 < Grid
153176 snake = { snake }
154177 snack = { snack }
178+ isGameOver = { playground . isGameOver }
155179 />
156180 </ div >
157181 ) ;
158182 }
159183}
160184
161- const Grid = ( { snake, snack } ) =>
185+ const Grid = ( { isGameOver , snake, snack } ) =>
162186 < div >
163187 { GRID . map ( y =>
164188 < Row
165189 y = { y }
166190 key = { y }
167191 snake = { snake }
168192 snack = { snack }
193+ isGameOver = { isGameOver }
169194 />
170195 ) }
171196 </ div >
172197
173- const Row = ( { snake, snack, y } ) =>
198+ const Row = ( { isGameOver , snake, snack, y } ) =>
174199 < div className = "grid-row" >
175200 { GRID . map ( x =>
176201 < Cell
@@ -179,11 +204,12 @@ const Row = ({ snake, snack, y }) =>
179204 key = { x }
180205 snake = { snake }
181206 snack = { snack }
207+ isGameOver = { isGameOver }
182208 />
183209 ) }
184210 </ div >
185211
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 ) } />
188214
189215export default App ;
0 commit comments