forked from devinc13/BattlesnakeTester
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrequest_body_builder.js
180 lines (152 loc) · 3.52 KB
/
request_body_builder.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Before using this body, you need to add at least one food, and at least one snake (yourself)
function getEmptyRequestBody(width = 20, height = 20, turn = 0) {
var body = {};
body["game"] = {
"id": 1,
"ruleset": {
"name": "standard",
"version": "v.1.2.3"
},
"timeout": 500
}
body["turn"] = turn;
body["board"] = {
"height": width,
"width": height,
"food": [],
"hazards": [],
"snakes": [],
}
body["you"] = {};
return body;
}
function addFood(body, x, y) {
var food = {
"x": x,
"y": y
};
body["board"]["food"].push(food);
}
// Taken from https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript
function makeRandomString() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 10; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function addSnake(body, snakeCoords, health = 100, name = makeRandomString(), id = makeRandomString()) {
var snakeBody = [];
var head = {};
for (var i = 0; i < snakeCoords.length; i++) {
if (i == 0){
head = {
"x": snakeCoords[i].x,
"y": snakeCoords[i].y };
}
var point = {
"x": snakeCoords[i].x,
"y": snakeCoords[i].y
};
snakeBody.push(point);
};
var snake = {
"body": snakeBody,
"length": snakeCoords.length,
"health": health,
"name": name,
"id": id,
"head": head,
"latency": 10,
"squad": "",
"shout": "",
};
body["board"]["snakes"].push(snake);
}
function addYou(body, snakeCoords, health = 100, name = makeRandomString(), id = makeRandomString()) {
// You need to be in the snakes array also
addSnake(body, snakeCoords, health, name, id);
var yourBody = [];
var head = {};
for (var i = 0; i < snakeCoords.length; i++) {
if (i == 0){
head = {
"x": snakeCoords[i].x,
"y": snakeCoords[i].y };
}
var point = {
"x": snakeCoords[i].x,
"y": snakeCoords[i].y
};
yourBody.push(point);
};
var yourSnake = {
"body": yourBody,
"length": snakeCoords.length,
"health": health,
"name": name,
"id": id,
"head": head,
"latency": 10,
"squad": "",
"shout": "",
};
body["you"] = yourSnake;
}
function printBoard(body) {
// Create board
var board = [];
for (var i = 0; i < body.board.height; i++) {
var row = [];
for (var j = 0; j < body.board.width; j++) {
row.push('-');
}
board.push(row);
}
// Add food
for (var i = 0; i < body.board.food.length; i++) {
var food = body.board.food[i];
board[food.x][food.y] = 'F';
}
// Find your snake id
var yourId = body.you.id;
// Add snakes
for (var i = 0; i < body.board.snakes.length; i++) {
var snake = body.board.snakes[i];
for(var j = 0; j < snake.length; j++) {
var coord = snake.body[j];
// Print your snake differently
if (j == 0) {
if (snake.id == yourId) {
board[coord.x][coord.y] = 'y';
} else {
board[coord.x][coord.y] = 'e';
}
} else {
if (snake.id == yourId) {
board[coord.x][coord.y] = '*';
} else {
board[coord.x][coord.y] = '#';
}
}
}
}
// Print how much health you have
console.log("\n\nYou have " + body["you"]["health"] + " health.");
// Print board
for (var i = 0; i < body.width; i++) {
for (var j = 0; j < body.height; j++) {
// Don't put a newline
process.stdout.write(board[j][i] + ' ');
}
// Just a newline
console.log('');
}
}
module.exports = {
getEmptyRequestBody: getEmptyRequestBody,
addFood: addFood,
addSnake: addSnake,
addYou: addYou,
printBoard: printBoard
}