@@ -13,53 +13,83 @@ for (let i = 0; i <= GRID_SIZE; i++) {
1313const isBorder = ( x , y ) =>
1414 x === 0 || y === 0 || x === GRID_SIZE || y === GRID_SIZE ;
1515
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 ) =>
1720 cs (
1821 'grid-cell' ,
1922 {
2023 '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 ) ,
2126 }
2227 ) ;
2328
2429class App extends Component {
2530 constructor ( props ) {
2631 super ( props ) ;
2732
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+ } ;
2947 }
3048
3149 render ( ) {
50+ const {
51+ snake,
52+ snack,
53+ } = this . state ;
54+
3255 return (
3356 < div className = "app" >
3457 < h1 > Snake!</ h1 >
35- < Grid />
58+ < Grid
59+ snake = { snake }
60+ snack = { snack }
61+ />
3662 </ div >
3763 ) ;
3864 }
3965}
4066
41- const Grid = ( ) =>
67+ const Grid = ( { snake , snack } ) =>
4268 < div >
4369 { GRID . map ( y =>
4470 < Row
4571 y = { y }
4672 key = { y }
73+ snake = { snake }
74+ snack = { snack }
4775 />
4876 ) }
4977 </ div >
5078
51- const Row = ( { y } ) =>
79+ const Row = ( { snake , snack , y } ) =>
5280 < div className = "grid-row" >
5381 { GRID . map ( x =>
5482 < Cell
5583 x = { x }
5684 y = { y }
5785 key = { x }
86+ snake = { snake }
87+ snack = { snack }
5888 />
5989 ) }
6090 </ div >
6191
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 ) } />
6494
6595export default App ;
0 commit comments