@@ -3,12 +3,12 @@ import cs from 'classnames';
33
44import './App.css' ;
55
6- const GRID_SIZE = 40 ;
6+ const GRID_SIZE = 20 ;
77const TICK_RATE = 100 ;
8- const GRID_ARRAY = [ ] ;
8+ const GRID = [ ] ;
99
1010for ( let i = 0 ; i <= GRID_SIZE ; i ++ ) {
11- GRID_ARRAY . push ( i ) ;
11+ GRID . push ( i ) ;
1212}
1313
1414const KEY_CODES_MAPPER = {
@@ -38,24 +38,20 @@ const getIsGameOver = (state) =>
3838const getIsAllowedToChangeDirection = ( state , e ) =>
3939 ! getIsGameOver ( state ) && KEY_CODES_MAPPER [ e . keyCode ] ;
4040
41+ const getRandomNumberFromRange = ( min , max ) =>
42+ Math . floor ( Math . random ( ) * ( max - min + 1 ) + min ) ;
43+
4144const getRandomCoordinate = ( ) =>
4245 ( {
43- x : Math . floor ( Math . random ( ) * GRID_SIZE ) ,
44- y : Math . floor ( Math . random ( ) * GRID_SIZE ) ,
46+ x : getRandomNumberFromRange ( 1 , GRID_SIZE - 1 ) ,
47+ y : getRandomNumberFromRange ( 1 , GRID_SIZE - 1 ) ,
4548 } ) ;
4649
4750const isPosition = ( x , y , diffX , diffY ) =>
4851 x === diffX && y === diffY ;
4952
50- // TODO make own some, use compose
51- const isSnake = ( x , y , snakeCoordinates ) => {
52- for ( var i = 0 ; i < snakeCoordinates . length ; i ++ ) {
53- if ( isPosition ( snakeCoordinates [ i ] . x , snakeCoordinates [ i ] . y , x , y ) ) {
54- return true ;
55- }
56- }
57- return false ;
58- } ;
53+ const isSnake = ( x , y , snakeCoordinates ) =>
54+ snakeCoordinates . filter ( coordinate => isPosition ( coordinate . x , coordinate . y , x , y ) ) . length ;
5955
6056const getSnakeHead = ( snake ) =>
6157 snake . coordinates [ 0 ] ;
@@ -69,8 +65,8 @@ const getSnakeTail = (snake) =>
6965const getIsSnakeOutside = ( snake ) =>
7066 getSnakeHead ( snake ) . x >= GRID_SIZE ||
7167 getSnakeHead ( snake ) . y >= GRID_SIZE ||
72- getSnakeHead ( snake ) . x < 0 ||
73- getSnakeHead ( snake ) . y < 0 ;
68+ getSnakeHead ( snake ) . x <= 0 ||
69+ getSnakeHead ( snake ) . y <= 0 ;
7470
7571const getIsSnakeClumy = ( snake ) =>
7672 isSnake ( getSnakeHead ( snake ) . x , getSnakeHead ( snake ) . y , getSnakeTail ( snake ) ) ;
@@ -105,7 +101,7 @@ const applySnakePosition = (prevState) => {
105101
106102 return {
107103 snake : {
108- coordinates : [ snakeHead ] . concat ( snakeTail ) , // babel
104+ coordinates : [ snakeHead , ... snakeTail ] ,
109105 } ,
110106 snack : {
111107 coordinate : snackCoordinate ,
@@ -125,7 +121,8 @@ const getCellCs = (isGameOver, snake, snack, x, y) =>
125121 {
126122 'grid-cell-snake' : isSnake ( x , y , snake . coordinates ) ,
127123 'grid-cell-snack' : isPosition ( x , y , snack . coordinate . x , snack . coordinate . y ) ,
128- 'grid-cell-hit' : isGameOver && isPosition ( x , y , getSnakeHead ( snake ) . x , getSnakeHead ( snake ) . y )
124+ 'grid-cell-border' : x === 0 || y === 0 || x === GRID_SIZE || y === GRID_SIZE ,
125+ 'grid-cell-hit' : isGameOver && isPosition ( x , y , getSnakeHead ( snake ) . x , getSnakeHead ( snake ) . y ) ,
129126 }
130127 ) ;
131128
@@ -196,41 +193,27 @@ class App extends Component {
196193
197194const Grid = ( { isGameOver, snake, snack } ) =>
198195 < div className = "grid" >
199- < Border grid = { GRID_ARRAY } />
200- { GRID_ARRAY . map ( y => < Row
201- y = { y }
202- key = { y }
203- snake = { snake }
204- snack = { snack }
205- isGameOver = { isGameOver }
206- /> ) }
207- < Border grid = { GRID_ARRAY } />
196+ { GRID . map ( y => < Row
197+ y = { y }
198+ key = { y }
199+ snake = { snake }
200+ snack = { snack }
201+ isGameOver = { isGameOver }
202+ /> ) }
208203 </ div >
209204
210205const Row = ( { isGameOver, snake, snack, y } ) =>
211206 < div className = "grid-row" >
212- < Block />
213- { GRID_ARRAY . map ( x => < Cell
207+ { GRID . map ( x => < Cell
214208 x = { x }
215209 y = { y }
216210 key = { x }
217211 snake = { snake }
218212 snack = { snack }
219213 isGameOver = { isGameOver }
220214 /> ) }
221- < Block />
222- </ div >
223-
224- const Border = ( { grid } ) =>
225- < div className = "grid-row" >
226- < Block />
227- { grid . map ( v => < Block key = { v } /> ) }
228- < Block />
229215 </ div >
230216
231- const Block = ( ) =>
232- < div className = "grid-cell grid-cell-border" />
233-
234217const Cell = ( { isGameOver, snake, snack, x, y } ) =>
235218 < div className = { getCellCs ( isGameOver , snake , snack , x , y ) } /> ;
236219
0 commit comments