1
1
import React , { Component } from 'react' ;
2
2
import cs from 'classnames' ;
3
3
4
- import { some } from 'lodash' ;
5
-
6
4
import './App.css' ;
7
5
8
6
const GRID_SIZE = 40 ;
9
7
const TICK_RATE = 100 ;
10
-
11
- var GRID_ARRAY = [ ] ;
8
+ const GRID_ARRAY = [ ] ;
12
9
13
10
for ( var i = 0 ; i <= GRID_SIZE ; i ++ ) {
14
11
GRID_ARRAY . push ( i ) ;
@@ -39,7 +36,10 @@ const isPosition = (x, y, diffX, diffY) =>
39
36
x === diffX && y === diffY ;
40
37
41
38
const getRandomCoordinate = ( ) =>
42
- ( { x : Math . floor ( Math . random ( ) * GRID_SIZE ) , y : Math . floor ( Math . random ( ) * GRID_SIZE ) } ) ;
39
+ ( {
40
+ x : Math . floor ( Math . random ( ) * GRID_SIZE ) ,
41
+ y : Math . floor ( Math . random ( ) * GRID_SIZE ) ,
42
+ } ) ;
43
43
44
44
// TODO make own some, use compose
45
45
const isSnake = ( x , y , snakeCoordinates ) => {
@@ -49,23 +49,20 @@ const isSnake = (x, y, snakeCoordinates) => {
49
49
}
50
50
}
51
51
return false ;
52
- }
52
+ } ;
53
53
54
54
// TODO compose instead: direction ticks
55
55
// TODO make last a previous compose step
56
56
const applySnakePosition = ( prevState ) => {
57
- // TODO babel stage
58
- // const [...snakeCoordinatesWithoutLast, lastCoordinate] = prevState.snake.coordinates;
59
-
60
- // const snakeCoordinatesWithoutLast = prevState.snake.coordinates.slice()
61
-
62
57
const isSnakeEating = getIsSnakeEating ( prevState ) ;
63
58
64
59
const snakeHead = DIRECTION_TICKS [ prevState . controls . direction ] (
65
60
prevState . snake . coordinates [ 0 ] . x ,
66
61
prevState . snake . coordinates [ 0 ] . y ,
67
62
) ;
68
63
64
+ // TODO babel stage
65
+ // const [...snakeCoordinatesWithoutLast, lastCoordinate] = prevState.snake.coordinates;
69
66
const snakeTail = isSnakeEating
70
67
? prevState . snake . coordinates
71
68
: prevState . snake . coordinates . slice ( 0 , prevState . snake . coordinates . length - 1 ) ;
@@ -74,15 +71,13 @@ const applySnakePosition = (prevState) => {
74
71
? getRandomCoordinate ( )
75
72
: prevState . snack . coordinate ;
76
73
77
- console . log ( snackCoordinate ) ;
78
-
79
74
return {
80
75
snake : {
81
- coordinates : [ snakeHead ] . concat ( snakeTail )
76
+ coordinates : [ snakeHead ] . concat ( snakeTail ) , // babel
82
77
} ,
83
78
snack : {
84
- coordinate : snackCoordinate
85
- }
79
+ coordinate : snackCoordinate ,
80
+ } ,
86
81
} ;
87
82
} ;
88
83
@@ -93,7 +88,7 @@ const doChangeDirection = (direction) => () => ({
93
88
} ) ;
94
89
95
90
const getIsSnakeEating = ( { snake, snack } ) =>
96
- isPosition ( snake . coordinates [ 0 ] . x , snake . coordinates [ 0 ] . y , snack . coordinate . x , snack . coordinate . y )
91
+ isPosition ( snake . coordinates [ 0 ] . x , snake . coordinates [ 0 ] . y , snack . coordinate . x , snack . coordinate . y ) ;
97
92
98
93
class App extends Component {
99
94
constructor ( props ) {
@@ -110,7 +105,6 @@ class App extends Component {
110
105
coordinate : getRandomCoordinate ( ) ,
111
106
} ,
112
107
} ;
113
-
114
108
}
115
109
116
110
componentDidMount ( ) {
@@ -136,7 +130,7 @@ class App extends Component {
136
130
}
137
131
138
132
render ( ) {
139
- const { snake, snack } = this . state
133
+ const { snake, snack } = this . state ;
140
134
return (
141
135
< div >
142
136
< Grid
@@ -169,21 +163,7 @@ const Row = ({ snake, snack, y }) =>
169
163
/> ) }
170
164
</ div >
171
165
172
- const Cell = ( {
173
- snake,
174
- snack,
175
- x,
176
- y,
177
- } ) => {
178
- const cellCs = cs (
179
- "grid-cell" ,
180
- {
181
- "grid-cell-snake" : isSnake ( x , y , snake . coordinates ) ,
182
- "grid-cell-snack" : isPosition ( x , y , snack . coordinate . x , snack . coordinate . y ) ,
183
- }
184
- ) ;
185
-
186
- return < div className = { cellCs } /> ;
187
- }
166
+ const Cell = ( { snake, snack, x, y } ) =>
167
+ < div className = { getCellCs ( sname , snack , x , y ) } /> ;
188
168
189
169
export default App ;
0 commit comments