forked from intrepidcoder/monopoly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.js
209 lines (162 loc) · 5.53 KB
/
ai.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// The purpose of this AI is not to be a relistic opponant, but to give an example of a vaild AI player.
function AITest(p) {
this.alertList = "";
// This variable is static, it is not related to each instance.
this.constructor.count++;
p.name = "AI Test " + this.constructor.count;
// Decide whether to buy a property the AI landed on.
// Return: boolean (true to buy).
// Arguments:
// index: the property's index (0-39).
this.buyProperty = function(index) {
console.log("buyProperty");
var s = square[index];
if (p.money > s.price + 50) {
return true;
} else {
return false;
}
}
// Determine the response to an offered trade.
// Return: boolean/instanceof Trade: a valid Trade object to counter offer (with the AI as the recipient); false to decline; true to accept.
// Arguments:
// tradeObj: the proposed trade, an instanceof Trade, has the AI as the recipient.
this.acceptTrade = function(tradeObj) {
console.log("acceptTrade");
var tradeValue = 0;
var money = tradeObj.getMoney();
var initiator = tradeObj.getInitiator();
var recipient = tradeObj.getRecipient();
var property = [];
tradeValue += 10 * tradeObj.getCommunityChestJailCard();
tradeValue += 10 * tradeObj.getChanceJailCard();
tradeValue += money;
for (var i = 0; i < 40; i++) {
property[i] = tradeObj.getProperty(i);
tradeValue += tradeObj.getProperty(i) * square[i].price * (square[i].mortgage ? 0.5 : 1);
}
console.log(tradeValue);
var proposedMoney = 25 - tradeValue + money;
if (tradeValue > 25) {
return true;
} else if (tradeValue >= -50 && initiator.money > proposedMoney) {
return new Trade(initiator, recipient, proposedMoney, property, tradeObj.getCommunityChestJailCard(), tradeObj.getChanceJailCard());
}
return false;
}
// This function is called at the beginning of the AI's turn, before any dice are rolled. The purpose is to allow the AI to manage property and/or initiate trades.
// Return: boolean: Must return true if and only if the AI proposed a trade.
this.beforeTurn = function() {
console.log("beforeTurn");
var s;
var allGroupOwned;
var max;
var leastHouseProperty;
var leastHouseNumber;
// Buy houses.
for (var i = 0; i < 40; i++) {
s = square[i];
if (s.owner === p.index && s.groupNumber >= 3) {
max = s.group.length;
allGroupOwned = true;
leastHouseNumber = 6; // No property will ever have 6 houses.
for (var j = max - 1; j >= 0; j--) {
if (square[s.group[j]].owner !== p.index) {
allGroupOwned = false;
break;
}
if (square[s.group[j]].house < leastHouseNumber) {
leastHouseProperty = square[s.group[j]];
leastHouseNumber = leastHouseProperty.house;
}
}
if (!allGroupOwned) {
continue;
}
if (p.money > leastHouseProperty.houseprice + 100) {
buyHouse(leastHouseProperty.index);
}
}
}
// Unmortgage property
for (var i = 39; i >= 0; i--) {
s = square[i];
if (s.owner === p.index && s.mortgage && p.money > s.price) {
unmortgage(i);
}
}
return false;
}
var utilityForRailroadFlag = true; // Don't offer this trade more than once.
// This function is called every time the AI lands on a square. The purpose is to allow the AI to manage property and/or initiate trades.
// Return: boolean: Must return true if and only if the AI proposed a trade.
this.onLand = function() {
console.log("onLand");
var proposedTrade;
var property = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var railroadIndexes = [5, 15, 25, 35];
var requestedRailroad;
var offeredUtility;
var s;
// If AI owns exactly one utility, try to trade it for a railroad.
for (var i = 0; i < 4; i++) {
s = square[railroadIndexes[i]];
if (s.owner !== 0 && s.owner !== p.index) {
requestedRailroad = s.index;
break;
}
}
if (square[12].owner === p.index && square[28].owner !== p.index) {
offeredUtility = 12;
} else if (square[28].owner === p.index && square[12].owner !== p.index) {
offeredUtility = 28;
}
if (utilityForRailroadFlag && game.getDie(1) !== game.getDie(2) && requestedRailroad && offeredUtility) {
utilityForRailroadFlag = false;
property[requestedRailroad] = -1;
property[offeredUtility] = 1;
proposedTrade = new Trade(p, player[square[requestedRailroad].owner], 0, property, 0, 0)
game.trade(proposedTrade);
return true;
}
return false;
}
// Determine whether to post bail/use get out of jail free card (if in possession).
// Return: boolean: true to post bail/use card.
this.postBail = function() {
console.log("postBail");
// p.jailroll === 2 on third turn in jail.
if ((p.communityChestJailCard || p.chanceJailCard) && p.jailroll === 2) {
return true;
} else {
return false;
}
}
// Mortgage enough properties to pay debt.
// Return: void: don't return anything, just call the functions mortgage()/sellhouse()
this.payDebt = function() {
console.log("payDebt");
for (var i = 39; i >= 0; i--) {
s = square[i];
if (s.owner === p.index && !s.mortgage && s.house === 0) {
mortgage(i);
console.log(s.name);
}
if (p.money >= 0) {
return;
}
}
}
// Determine what to bid during an auction.
// Return: integer: -1 for exit auction, 0 for pass, a positive value for the bid.
this.bid = function(property, currentBid) {
console.log("bid");
var bid;
bid = currentBid + Math.round(Math.random() * 20 + 10);
if (p.money < bid + 50 || bid > square[property].price * 1.5) {
return -1;
} else {
return bid;
}
}
}