Skip to content

Commit 8c63111

Browse files
committed
part 02
1 parent 33db5cd commit 8c63111

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

src/App.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,12 @@
2626

2727
.grid-cell-border {
2828
background-color: #000000;
29+
}
30+
31+
.grid-cell-snake {
32+
background-color: #000000;
33+
}
34+
35+
.grid-cell-snack {
36+
background-color: blue;
2937
}

src/App.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,83 @@ for (let i = 0; i <= GRID_SIZE; i++) {
1313
const 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

2429
class 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

6595
export default App;

0 commit comments

Comments
 (0)