@@ -13,53 +13,83 @@ for (let i = 0; i <= GRID_SIZE; i++) {
13
13
const isBorder = ( x , y ) =>
14
14
x === 0 || y === 0 || x === GRID_SIZE || y === GRID_SIZE ;
15
15
16
- const getCellCs = ( x , y ) =>
16
+ const isPosition = ( x , y , diffX , diffY ) =>
17
+ x === diffX && y === diffY ;
18
+
19
+ const getCellCs = ( snake , snack , x , y ) =>
17
20
cs (
18
21
'grid-cell' ,
19
22
{
20
23
'grid-cell-border' : isBorder ( x , y ) ,
24
+ 'grid-cell-snake' : isPosition ( x , y , snake . coordinate . x , snake . coordinate . y ) ,
25
+ 'grid-cell-snack' : isPosition ( x , y , snack . coordinate . x , snack . coordinate . y ) ,
21
26
}
22
27
) ;
23
28
24
29
class App extends Component {
25
30
constructor ( props ) {
26
31
super ( props ) ;
27
32
28
- this . state = { } ;
33
+ this . state = {
34
+ snake : {
35
+ coordinate : {
36
+ x : 20 ,
37
+ y : 10 ,
38
+ } ,
39
+ } ,
40
+ snack : {
41
+ coordinate : {
42
+ x : 25 ,
43
+ y : 10 ,
44
+ } ,
45
+ }
46
+ } ;
29
47
}
30
48
31
49
render ( ) {
50
+ const {
51
+ snake,
52
+ snack,
53
+ } = this . state ;
54
+
32
55
return (
33
56
< div className = "app" >
34
57
< h1 > Snake!</ h1 >
35
- < Grid />
58
+ < Grid
59
+ snake = { snake }
60
+ snack = { snack }
61
+ />
36
62
</ div >
37
63
) ;
38
64
}
39
65
}
40
66
41
- const Grid = ( ) =>
67
+ const Grid = ( { snake , snack } ) =>
42
68
< div >
43
69
{ GRID . map ( y =>
44
70
< Row
45
71
y = { y }
46
72
key = { y }
73
+ snake = { snake }
74
+ snack = { snack }
47
75
/>
48
76
) }
49
77
</ div >
50
78
51
- const Row = ( { y } ) =>
79
+ const Row = ( { snake , snack , y } ) =>
52
80
< div className = "grid-row" >
53
81
{ GRID . map ( x =>
54
82
< Cell
55
83
x = { x }
56
84
y = { y }
57
85
key = { x }
86
+ snake = { snake }
87
+ snack = { snack }
58
88
/>
59
89
) }
60
90
</ div >
61
91
62
- const Cell = ( { x, y } ) =>
63
- < div className = { getCellCs ( x , y ) } />
92
+ const Cell = ( { snake , snack , x, y } ) =>
93
+ < div className = { getCellCs ( snake , snack , x , y ) } />
64
94
65
95
export default App ;
0 commit comments