forked from KJPS/kjps2017
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
106 lines (90 loc) · 2.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
/**
* Nolasīt spēles tāvokli no stdin.
*/
function readGameState()
{
var fs = require('fs');
var jsonString = fs.readFileSync('/dev/stdin').toString();
return JSON.parse(jsonString);
}
/**
* Iegūt spēlētājus no spēles stāvokļa.
*/
function getPlayers(gameState)
{
var players = [];
for (var i = 0; i < gameState.participants.length; ++i) {
var participant = gameState.participants[i];
if (participant.discr == 'player') {
players.push(participant);
}
}
return players;
}
/**
* Iegūt spokus no spēles stāvokļa.
*/
function getGhosts(gameState)
{
var ghosts = [];
for (var i = 0; i < gameState.participants.length; ++i) {
var participant = gameState.participants[i];
if (participant.discr == 'ghost') {
ghosts.push(participant);
}
}
return ghosts;
}
/**
* Iegūt spēles punktu tabulu.
*/
function getPointTable(gameState)
{
return gameState.points;
}
/**
* Iegūt sava spēlētāja datus.
*/
function getMyPlayer(gameState)
{
return gameState.my_player;
}
/**
* Pārbaudīt, vai var pārvietoties no (x1, y1) uz (x2, y2).
*/
function canMoveTo(gameState, x1, y1, x2, y2)
{
if (x1 < 0 || x1 >= gameState.width
|| y1 < 0 || y1 >= gameState.height
|| x2 < 0 || x2 >= gameState.width
|| y2 < 0 || y2 >= gameState.height
) {
return false;
}
var movements = [
[0, -1],
[1, 0],
[0, 1],
[-1, 0]
];
var way = null;
for (var i = 0; i < movements.length; ++i) {
var m = movements[i];
if (x1 + m[0] == x2 && y1 + m[1] == y2) {
way = i;
break;
}
}
if (way === null) {
return false;
}
return !(gameState.maze[y1][x1] & (1 << way));
}
/**
* Pārbaudīt, vai savs spēlētājs var pārvietoties uz x, y koordinātēm.
*/
function canIMoveTo(gameState, x, y)
{
var myPlayer = gameState.my_player;
return canMoveTo(gameState, myPlayer.x, myPlayer.y, x, y);
}