forked from step-batch-7/snake-trinangkur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.js
75 lines (62 loc) · 1.83 KB
/
snake.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const areCellsEqual = (cellOne, cellTwo) =>
cellOne.every((ordinate, index) => ordinate === cellTwo[index]);
class Snake {
constructor(positions, direction, type) {
this.positions = positions.slice();
this.direction = direction;
this.type = type;
this.previousTail = [0, 0];
}
get head() {
const head = this.positions[this.positions.length - 1];
return head.slice();
}
getStat() {
return {
location: this.positions,
species: this.type,
previousTail: this.previousTail
};
}
turnLeft() {
this.direction.turnLeft();
}
turnRight() {
this.direction.turnRight();
}
move() {
const [headX, headY] = this.positions[this.positions.length - 1];
this.previousTail = this.positions.shift();
const [deltaX, deltaY] = this.direction.delta;
this.positions.push([headX + deltaX, headY + deltaY]);
}
wrap(boundary) {
this.move();
const maxColNum = boundary.colNum + 1;
const maxRowNum = boundary.rowNum + 1;
let [headX, headY] = this.head;
headX = (maxColNum + headX) % maxColNum;
headY = (headY + maxRowNum) % maxRowNum;
this.positions[this.positions.length - 1] = [headX, headY];
}
eatFood() {
this.positions.unshift(this.previousTail);
}
hasReached(position) {
return areCellsEqual(this.head, position);
}
hasEatenItself() {
const restBody = this.positions.slice(0, this.positions.length - 1);
return restBody.some(part => areCellsEqual(part, this.head));
}
hasTouched(boundary) {
const touchedVertically =
this.head[0] < 0 || this.head[0] > boundary.colNum;
const touchedHorizontally =
this.head[1] < 0 || this.head[1] > boundary.rowNum;
return touchedHorizontally || touchedVertically;
}
hasTouchedSnake(positions) {
return positions.some(part => this.hasReached(part));
}
}