-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayerAI.js
74 lines (66 loc) · 1.83 KB
/
playerAI.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
var Player = require('./player.js');
function PlayerAI (options, campus, state) {
Player.call(this, options);
if (this.constructor === PlayerAI) {
throw new Error('AbstractClassException');
}
debugger;
this.is_ai = true;
this.level = 1;
this.name = "AI";
this.campus = campus;
this.state = state;
}
PlayerAI.prototype = Object.create(Player.prototype);
PlayerAI.prototype.constructor = PlayerAI;
/**
* Returns a list of ids of pieces that are connected
* to enemy pieces
*/
PlayerAI.prototype.getPiecesOnBorder = function(team) {
var q = this.getPiecesOwnedBy(team);
var set = {};
for (var i = 0; i < q.length; i++) {
var piece = q[i];
var connected = Object.keys(this.campus.map.pieces[piece]);
for (var j = 0; j < connected.length; j++) {
var border_piece = connected[j];
var owner = this.state[border_piece].team;
if (owner === team) {
set[border_piece] = true;
}
}
}
return Object.keys(set);
};
PlayerAI.prototype.getPiecesOwnedBy = function(team) {
var ret = [];
var keys = Object.keys(this.state);
for (var i = 0; i < keys.length; i++) {
var piece = keys[i];
if (this.state[piece].team === team) {
ret.push(piece);
}
}
return ret;
};
PlayerAI.prototype.getPiecesNoOwner = function() {
return this.getPiecesOwnedBy(-1);
};
PlayerAI.prototype.getEnemyPiecesNear = function(piece) {
var team = this.state[piece].team;
var connected = Object.keys(this.campus.map.pieces[piece]);
var ret = [];
for (var i = 0; i < connected.length; i++) {
var conn_piece = connected[i];
if (this.state[conn_piece].team != team) {
ret.push(conn_piece);
}
}
return ret;
};
PlayerAI.prototype.getRandElem = function (list) {
var r = Math.floor(Math.random() * list.length);
return list[r];
};
module.exports = PlayerAI;