-
Notifications
You must be signed in to change notification settings - Fork 138
/
lib.js
163 lines (136 loc) · 3.07 KB
/
lib.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'use strict';
var config = require('./config');
function getOurHeadLocation(snakes) {
var i, snake;
for (i = 0; i < snakes.length; i++) {
snake = snakes[i];
if (snake.name === config.snake.name) {
return {
x: snake.coords[0][0],
y: snake.coords[0][1]
};
}
}
throw new Error("We are not in the map.");
}
exports.getOurHeadLocation = getOurHeadLocation;
function distance(x1, y1, x2, y2) {
return Math.sqrt(
Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2)
);
}
function findClosestFood(ourLocation, food) {
var closest = {
distance: Number.MAX_VALUE,
coords: {
x: 0,
y: 0
}
};
var i, d;
for (i = 0; i < food.length; i++) {
d = distance(ourLocation.x, ourLocation.y, food[i][0], food[i][1]);
if (d <= closest.distance) {
closest.distance = d;
closest.coords = {
x: food[i][0],
y: food[i][1]
};
}
}
return {
x: closest.coords.x,
y: closest.coords.y
};
}
exports.findClosestFood = findClosestFood;
function oppositeDirection(direction) {
switch (direction) {
case 'up':
return 'down';
case 'down':
return 'up';
case 'left':
return 'right';
case 'right':
return 'left';
default:
throw new Error("Unknown direction: " + direction);
}
}
exports.oppositeDirection = oppositeDirection;
function isLocationSafe(pos, board) {
var tile = board[pos.x][pos.y];
if (tile === undefined) { // Outside of board.
return false;
}
switch (tile.state) {
case 'food':
case 'empty':
return true;
default:
return false;
}
}
exports.nextMove = function nextMove(ourLocation, foodLocation, snakes, board, lastDirection) {
var move;
var deltaX = foodLocation.x - ourLocation.x;
var deltaY = foodLocation.y - ourLocation.y;
var newCoords = {
x: ourLocation.x,
y: ourLocation.y
};
if (deltaX > 0) {
move = 'right';
newCoords.x++;
} else if (deltaX < 0) {
move = 'left';
newCoords.x--;
} else if (deltaY > 0) {
move = 'down';
newCoords.y++;
} else if (deltaY < 0) {
move = 'up';
newCoords.y--;
} else {
console.log('Both deltaX and deltaY are zero.');
}
var allMoves = ['up', 'down', 'left', 'right'];
while (true) {
if (isLocationSafe(newCoords, board)) {
break;
}
var index = allMoves.indexOf(move);
allMoves.splice(index, 1);
newCoords.x = ourLocation.x;
newCoords.y = ourLocation.y;
if (allMoves.length === 0) {
// No possible moves.
if (lastDirection === null) {
return 'right'; // Why not.
} else {
return oppositeDirection(lastDirection);
}
}
move = allMoves[0];
switch (move) {
case 'up':
newCoords.y--;
break;
case 'down':
newCoords.y++;
break;
case 'left':
newCoords.x--;
break;
case 'right':
newCoords.x++;
break;
}
}
return move;
};
exports.generateStartTaunt = function generateStartTaunt(gameId) {
var idParts = gameId.split('-');
return "Time for a " + idParts[idParts.length - 1] + "-nado!";
};