Skip to content

Commit 47fc1c6

Browse files
committed
grid, snake, snack initial
1 parent da655a8 commit 47fc1c6

File tree

3 files changed

+97
-31
lines changed

3 files changed

+97
-31
lines changed

src/App.css

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1-
.App {
2-
text-align: center;
1+
.grid {
2+
33
}
44

5-
.App-logo {
6-
animation: App-logo-spin infinite 20s linear;
7-
height: 80px;
5+
.grid-row {
6+
display: flex;
7+
flex-direction: row;
88
}
99

10-
.App-header {
11-
background-color: #222;
12-
height: 150px;
13-
padding: 20px;
14-
color: white;
10+
.grid-cell {
11+
display: flex;
12+
flex-direction: column;
13+
14+
border-top: 1px solid #000000;
15+
border-left: 1px solid #000000;
16+
width: 10px;
17+
height: 10px;
1518
}
1619

17-
.App-intro {
18-
font-size: large;
20+
.grid-cell-snake {
21+
background-color: #000000;
1922
}
2023

21-
@keyframes App-logo-spin {
22-
from { transform: rotate(0deg); }
23-
to { transform: rotate(360deg); }
24+
.grid-cell-snack {
25+
background-color: #008000;
2426
}
27+
28+
.grid-cell:last-child {
29+
border-right: 1px solid #000000;
30+
}

src/App.js

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,88 @@
11
import React, { Component } from 'react';
2-
import logo from './logo.svg';
2+
import { connect } from 'react-redux';
3+
import cs from 'classnames';
4+
35
import './App.css';
46

7+
var GRID_ARRAY = [];
8+
9+
for (var i = 0; i <= 50; i++) {
10+
GRID_ARRAY.push(i);
11+
}
12+
513
class App extends Component {
14+
constructor(props) {
15+
super(props);
16+
17+
this.state = {
18+
snake: {
19+
position: {
20+
x: 10,
21+
y: 25,
22+
},
23+
},
24+
snack: {
25+
position: {
26+
x: 40,
27+
y: 25,
28+
},
29+
},
30+
}
31+
}
32+
633
render() {
34+
const { snake, snack } = this.state;
35+
736
return (
8-
<div className="App">
9-
<div className="App-header">
10-
<img src={logo} className="App-logo" alt="logo" />
11-
<h2>Welcome to React</h2>
12-
</div>
13-
<p className="App-intro">
14-
To get started, edit <code>src/App.js</code> and save to reload.
15-
</p>
37+
<div>
38+
<Grid
39+
snake={snake}
40+
snack={snack}
41+
/>
1642
</div>
1743
);
1844
}
1945
}
2046

47+
const Grid = ({ snake, snack }) =>
48+
<div className="grid">
49+
{GRID_ARRAY.map(y => <Row
50+
y={y}
51+
key={y}
52+
snake={snake}
53+
snack={snack}
54+
/>)}
55+
</div>
56+
57+
const Row = ({ snake, snack, y }) =>
58+
<div className="grid-row">
59+
{GRID_ARRAY.map(x => <Cell
60+
x={x}
61+
y={y}
62+
key={x}
63+
snake={snake}
64+
snack={snack}
65+
/>)}
66+
</div>
67+
68+
const isPosition = (x, y, diffX, diffY) =>
69+
x === diffX && y === diffY;
70+
71+
const Cell = ({
72+
snake,
73+
snack,
74+
x,
75+
y,
76+
}) => {
77+
const cellCs = cs(
78+
"grid-cell",
79+
{
80+
"grid-cell-snake": isPosition(x, y, snake.position.x, snake.position.y),
81+
"grid-cell-snack": isPosition(x, y, snack.position.x, snack.position.y),
82+
}
83+
);
84+
85+
return <div className={cellCs} />;
86+
}
87+
2188
export default App;

src/logo.svg

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)