Skip to content

Commit 870600f

Browse files
committed
refactor
1 parent 73618fe commit 870600f

File tree

1 file changed

+15
-35
lines changed

1 file changed

+15
-35
lines changed

src/App.js

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import React, { Component } from 'react';
22
import cs from 'classnames';
33

4-
import { some } from 'lodash';
5-
64
import './App.css';
75

86
const GRID_SIZE = 40;
97
const TICK_RATE = 100;
10-
11-
var GRID_ARRAY = [];
8+
const GRID_ARRAY = [];
129

1310
for (var i = 0; i <= GRID_SIZE; i++) {
1411
GRID_ARRAY.push(i);
@@ -39,7 +36,10 @@ const isPosition = (x, y, diffX, diffY) =>
3936
x === diffX && y === diffY;
4037

4138
const getRandomCoordinate = () =>
42-
({ x: Math.floor(Math.random() * GRID_SIZE), y: Math.floor(Math.random() * GRID_SIZE) });
39+
({
40+
x: Math.floor(Math.random() * GRID_SIZE),
41+
y: Math.floor(Math.random() * GRID_SIZE),
42+
});
4343

4444
// TODO make own some, use compose
4545
const isSnake = (x, y, snakeCoordinates) => {
@@ -49,23 +49,20 @@ const isSnake = (x, y, snakeCoordinates) => {
4949
}
5050
}
5151
return false;
52-
}
52+
};
5353

5454
// TODO compose instead: direction ticks
5555
// TODO make last a previous compose step
5656
const applySnakePosition = (prevState) => {
57-
// TODO babel stage
58-
// const [...snakeCoordinatesWithoutLast, lastCoordinate] = prevState.snake.coordinates;
59-
60-
// const snakeCoordinatesWithoutLast = prevState.snake.coordinates.slice()
61-
6257
const isSnakeEating = getIsSnakeEating(prevState);
6358

6459
const snakeHead = DIRECTION_TICKS[prevState.controls.direction](
6560
prevState.snake.coordinates[0].x,
6661
prevState.snake.coordinates[0].y,
6762
);
6863

64+
// TODO babel stage
65+
// const [...snakeCoordinatesWithoutLast, lastCoordinate] = prevState.snake.coordinates;
6966
const snakeTail = isSnakeEating
7067
? prevState.snake.coordinates
7168
: prevState.snake.coordinates.slice(0, prevState.snake.coordinates.length - 1);
@@ -74,15 +71,13 @@ const applySnakePosition = (prevState) => {
7471
? getRandomCoordinate()
7572
: prevState.snack.coordinate;
7673

77-
console.log(snackCoordinate);
78-
7974
return {
8075
snake: {
81-
coordinates: [snakeHead].concat(snakeTail)
76+
coordinates: [snakeHead].concat(snakeTail), // babel
8277
},
8378
snack: {
84-
coordinate: snackCoordinate
85-
}
79+
coordinate: snackCoordinate,
80+
},
8681
};
8782
};
8883

@@ -93,7 +88,7 @@ const doChangeDirection = (direction) => () => ({
9388
});
9489

9590
const getIsSnakeEating = ({ snake, snack }) =>
96-
isPosition(snake.coordinates[0].x, snake.coordinates[0].y, snack.coordinate.x, snack.coordinate.y)
91+
isPosition(snake.coordinates[0].x, snake.coordinates[0].y, snack.coordinate.x, snack.coordinate.y);
9792

9893
class App extends Component {
9994
constructor(props) {
@@ -110,7 +105,6 @@ class App extends Component {
110105
coordinate: getRandomCoordinate(),
111106
},
112107
};
113-
114108
}
115109

116110
componentDidMount() {
@@ -136,7 +130,7 @@ class App extends Component {
136130
}
137131

138132
render() {
139-
const { snake, snack } = this.state
133+
const { snake, snack } = this.state;
140134
return (
141135
<div>
142136
<Grid
@@ -169,21 +163,7 @@ const Row = ({ snake, snack, y }) =>
169163
/>)}
170164
</div>
171165

172-
const Cell = ({
173-
snake,
174-
snack,
175-
x,
176-
y,
177-
}) => {
178-
const cellCs = cs(
179-
"grid-cell",
180-
{
181-
"grid-cell-snake": isSnake(x, y, snake.coordinates),
182-
"grid-cell-snack": isPosition(x, y, snack.coordinate.x, snack.coordinate.y),
183-
}
184-
);
185-
186-
return <div className={cellCs} />;
187-
}
166+
const Cell = ({ snake, snack, x, y }) =>
167+
<div className={getCellCs(sname, snack, x, y)} />;
188168

189169
export default App;

0 commit comments

Comments
 (0)