Skip to content

Commit 1ef644d

Browse files
committed
snake has an array of coordinates instead of one
1 parent 9ae2e1f commit 1ef644d

File tree

1 file changed

+53
-14
lines changed

1 file changed

+53
-14
lines changed

src/App.js

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

4+
import { some } from 'lodash';
5+
46
import './App.css';
57

68
var GRID_ARRAY = [];
@@ -30,12 +32,46 @@ const DIRECTION_TICKS = {
3032
LEFT: (x, y) => ({ x: x - 1, y }),
3133
};
3234

33-
// TODO compose instead
34-
const applySnakePosition = (prevState) => ({
35-
snake: {
36-
position: {
37-
...DIRECTION_TICKS[prevState.controls.direction](prevState.snake.position.x, prevState.snake.position.y),
35+
const isPosition = (x, y, diffX, diffY) =>
36+
x === diffX && y === diffY;
37+
38+
const get = (array, property) =>
39+
array.map(v => v[property]);
40+
41+
// TODO make own some, use compose
42+
const isSnake = (x, y, snakeCoordinates) => {
43+
for (var i = 0; i < snakeCoordinates.length; i++) {
44+
if (isPosition(snakeCoordinates[i].x, snakeCoordinates[i].y, x, y)) {
45+
return true;
46+
}
47+
}
48+
return false;
49+
}
50+
51+
// TODO compose instead: direction ticks
52+
// TODO make last a previous compose step
53+
const applySnakePosition = (prevState) => {
54+
// TODO babel stage
55+
// const [...snakeCoordinatesWithoutLast, lastCoordinate] = prevState.snake.coordinates;
56+
57+
// const snakeCoordinatesWithoutLast = prevState.snake.coordinates.slice()
58+
59+
return {
60+
snake: {
61+
coordinates: [
62+
DIRECTION_TICKS[prevState.controls.direction](
63+
prevState.snake.coordinates[0].x,
64+
prevState.snake.coordinates[0].y,
65+
),
66+
// ...prevState.snake.coordinates,
67+
],
3868
},
69+
};
70+
};
71+
72+
const applySnakeAte = (prevState) => ({
73+
snake: {
74+
3975
},
4076
});
4177

@@ -45,6 +81,9 @@ const doChangeDirection = (direction) => () => ({
4581
},
4682
});
4783

84+
const isSnakeEating = ({ snake, snack }) =>
85+
isPosition(snake.coordinates[0].x, snack.coordinate.x) && isPosition(snake.coordinates[0].y, snack.coordinate.y);
86+
4887
class App extends Component {
4988
constructor(props) {
5089
super(props);
@@ -54,13 +93,13 @@ class App extends Component {
5493
direction: CONTROLS.RIGHT,
5594
},
5695
snake: {
57-
position: {
96+
coordinates: [{
5897
x: 10,
5998
y: 25,
60-
},
99+
}],
61100
},
62101
snack: {
63-
position: {
102+
coordinate: {
64103
x: 40,
65104
y: 25,
66105
},
@@ -82,6 +121,10 @@ class App extends Component {
82121
}
83122

84123
onTick = () => {
124+
// if (isSnakeEating(this.state)) {
125+
// this.setState(applySnakeAte);
126+
// }
127+
85128
this.setState(applySnakePosition);
86129
}
87130

@@ -93,7 +136,6 @@ class App extends Component {
93136

94137
render() {
95138
const { snake, snack } = this.state
96-
console.log(this.state.controls.direction);;
97139
return (
98140
<div>
99141
<Grid
@@ -126,9 +168,6 @@ const Row = ({ snake, snack, y }) =>
126168
/>)}
127169
</div>
128170

129-
const isPosition = (x, y, diffX, diffY) =>
130-
x === diffX && y === diffY;
131-
132171
const Cell = ({
133172
snake,
134173
snack,
@@ -138,8 +177,8 @@ const Cell = ({
138177
const cellCs = cs(
139178
"grid-cell",
140179
{
141-
"grid-cell-snake": isPosition(x, y, snake.position.x, snake.position.y),
142-
"grid-cell-snack": isPosition(x, y, snack.position.x, snack.position.y),
180+
"grid-cell-snake": isSnake(x, y, snake.coordinates),
181+
"grid-cell-snack": isPosition(x, y, snack.coordinate.x, snack.coordinate.y),
143182
}
144183
);
145184

0 commit comments

Comments
 (0)