/
ComputerPlayer.js
83 lines (67 loc) · 2.14 KB
/
ComputerPlayer.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
var Player = require('./Player.js');
var util = require('./util.js');
var War = require('./War.js');
function ComputerPlayer(options){
// this.name = name;
Player.call(this,options);
}
util.inherits(ComputerPlayer, Player);
ComputerPlayer.prototype.claimUnclaimed = function(board, callBack){
var countriesToAdd = board.unclaimedCountries;
var rIdx = Math.floor(Math.random()*(countriesToAdd.length));
var chosen = countriesToAdd[rIdx];
this.countriesOwned.push(chosen);
chosen.owner = this;
chosen.troops = 1;
board.removeUnclaimed(chosen);
board.update();
callBack();
};
ComputerPlayer.prototype.placeSoldiers = function(num, callBack, totalSoldiers){
var rIdx = Math.floor(Math.random()*(this.countriesOwned.length));
var chosen = this.countriesOwned[rIdx];
chosen.troops += num;
callBack(totalSoldiers);
};
ComputerPlayer.prototype.startWar = function(board, callback){
var rIdx = Math.floor(Math.random()*(this.ableToFight.length));
var chosen = this.ableToFight[rIdx];
var r2idx = Math.floor(Math.random()*(chosen.ableToFight().length));
var defendor = chosen.ableToFight()[r2idx];
var war = new War(chosen, defendor);
this.addOWar(war);
defendor.owner.addDWar(war);
callback(war);
};
ComputerPlayer.prototype.wantToWar = function(callback){
var choice = Math.random();
var ableToWar = false;
this.ableToFight = [];
for (var i = 0; i < this.countriesOwned.length; i++) {
if(this.countriesOwned[i].ableToFight()){
this.ableToFight.push(this.countriesOwned[i]);
ableToWar = true;
}
}
if(!ableToWar){
callback(false);
}else{
if(choice > 0.5 ){
callback(true);
}else {
callback(false);
}
}
};
ComputerPlayer.prototype.moveMen = function(callback){
callback();
};
ComputerPlayer.prototype.getAttackSoldiers = function(war, index, callback){
var choice = Math.floor(Math.random() * (war.aggressor.troops - 1)) + 1;
callback(war, index, choice);
};
ComputerPlayer.prototype.getDefenseSoldiers = function(war, index, callback){
var choice = Math.floor(Math.random() * war.defender.troops) + 1;
callback(war, index, choice);
};
module.exports = ComputerPlayer;