Skip to content

Commit

Permalink
part 02
Browse files Browse the repository at this point in the history
  • Loading branch information
rwieruch committed Sep 2, 2017
1 parent 33db5cd commit 8c63111
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@

.grid-cell-border {
background-color: #000000;
}

.grid-cell-snake {
background-color: #000000;
}

.grid-cell-snack {
background-color: blue;
}
44 changes: 37 additions & 7 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,83 @@ for (let i = 0; i <= GRID_SIZE; i++) {
const isBorder = (x, y) =>
x === 0 || y === 0 || x === GRID_SIZE || y === GRID_SIZE;

const getCellCs = (x, y) =>
const isPosition = (x, y, diffX, diffY) =>
x === diffX && y === diffY;

const getCellCs = (snake, snack, x, y) =>
cs(
'grid-cell',
{
'grid-cell-border': isBorder(x, y),
'grid-cell-snake': isPosition(x, y, snake.coordinate.x, snake.coordinate.y),
'grid-cell-snack': isPosition(x, y, snack.coordinate.x, snack.coordinate.y),
}
);

class App extends Component {
constructor(props) {
super(props);

this.state = {};
this.state = {
snake: {
coordinate: {
x: 20,
y: 10,
},
},
snack: {
coordinate: {
x: 25,
y: 10,
},
}
};
}

render() {
const {
snake,
snack,
} = this.state;

return (
<div className="app">
<h1>Snake!</h1>
<Grid />
<Grid
snake={snake}
snack={snack}
/>
</div>
);
}
}

const Grid = () =>
const Grid = ({ snake, snack }) =>
<div>
{GRID.map(y =>
<Row
y={y}
key={y}
snake={snake}
snack={snack}
/>
)}
</div>

const Row = ({ y }) =>
const Row = ({ snake, snack, y }) =>
<div className="grid-row">
{GRID.map(x =>
<Cell
x={x}
y={y}
key={x}
snake={snake}
snack={snack}
/>
)}
</div>

const Cell = ({ x, y }) =>
<div className={getCellCs(x, y)} />
const Cell = ({ snake, snack, x, y }) =>
<div className={getCellCs(snake, snack, x, y)} />

export default App;

0 comments on commit 8c63111

Please sign in to comment.