Skip to content

Commit 73618fe

Browse files
committed
snack respawns random, snack and snake have random cooradinates
1 parent fa927d8 commit 73618fe

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

src/App.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
border-top: 1px solid #000000;
1515
border-left: 1px solid #000000;
16-
width: 10px;
17-
height: 10px;
16+
width: 15px;
17+
height: 15px;
1818
}
1919

2020
.grid-cell-snake {

src/App.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import { some } from 'lodash';
55

66
import './App.css';
77

8+
const GRID_SIZE = 40;
9+
const TICK_RATE = 100;
10+
811
var GRID_ARRAY = [];
912

10-
for (var i = 0; i <= 50; i++) {
13+
for (var i = 0; i <= GRID_SIZE; i++) {
1114
GRID_ARRAY.push(i);
1215
}
1316

@@ -35,8 +38,8 @@ const DIRECTION_TICKS = {
3538
const isPosition = (x, y, diffX, diffY) =>
3639
x === diffX && y === diffY;
3740

38-
const get = (array, property) =>
39-
array.map(v => v[property]);
41+
const getRandomCoordinate = () =>
42+
({ x: Math.floor(Math.random() * GRID_SIZE), y: Math.floor(Math.random() * GRID_SIZE) });
4043

4144
// TODO make own some, use compose
4245
const isSnake = (x, y, snakeCoordinates) => {
@@ -56,20 +59,30 @@ const applySnakePosition = (prevState) => {
5659

5760
// const snakeCoordinatesWithoutLast = prevState.snake.coordinates.slice()
5861

62+
const isSnakeEating = getIsSnakeEating(prevState);
63+
5964
const snakeHead = DIRECTION_TICKS[prevState.controls.direction](
6065
prevState.snake.coordinates[0].x,
6166
prevState.snake.coordinates[0].y,
6267
);
6368

64-
const isSnakeEating = getIsSnakeEating(prevState);
6569
const snakeTail = isSnakeEating
6670
? prevState.snake.coordinates
67-
: prevState.snake.coordinates.slice(0, prevState.snake.coordinates.length - 1)
71+
: prevState.snake.coordinates.slice(0, prevState.snake.coordinates.length - 1);
72+
73+
const snackCoordinate = isSnakeEating
74+
? getRandomCoordinate()
75+
: prevState.snack.coordinate;
76+
77+
console.log(snackCoordinate);
6878

6979
return {
7080
snake: {
7181
coordinates: [snakeHead].concat(snakeTail)
7282
},
83+
snack: {
84+
coordinate: snackCoordinate
85+
}
7386
};
7487
};
7588

@@ -91,23 +104,17 @@ class App extends Component {
91104
direction: CONTROLS.RIGHT,
92105
},
93106
snake: {
94-
coordinates: [{
95-
x: 10,
96-
y: 25,
97-
}],
107+
coordinates: [getRandomCoordinate()],
98108
},
99109
snack: {
100-
coordinate: {
101-
x: 40,
102-
y: 25,
103-
},
110+
coordinate: getRandomCoordinate(),
104111
},
105112
};
106113

107114
}
108115

109116
componentDidMount() {
110-
this.interval = setInterval(this.onTick, 150);
117+
this.interval = setInterval(this.onTick, TICK_RATE);
111118

112119
window.addEventListener('keyup', this.onChangeDirection, false);
113120
}

0 commit comments

Comments
 (0)