-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathValueEstimator.java
More file actions
361 lines (303 loc) · 10 KB
/
ValueEstimator.java
File metadata and controls
361 lines (303 loc) · 10 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package monopoly;
import java.util.LinkedList;
import java.util.Queue;
class ValueEstimator {
private final int NUM_PLAYERS;
private final int DECK_SIZE = 16;
private final Queue<Player> players; //contains all player positions
private final Board board; //stores a board
private final Cards chance; //stores a deck of chance cards
private double[] doubleProb;
//constructs ValueEstimator object
public ValueEstimator(Board board, Queue<Player> queue, ProbDice probDice, Cards chance) {
players = queue;
//iterate through queue of players, enqueue position and enum
this.board = board;
this.chance = chance;
if (probDice.numDice() == 2)
setDouble();
this.NUM_PLAYERS = players.size();
}
public static void main(String[] args) {
Deck chanceDeck = new RandomDeck();
Deck commDeck = new RandomDeck();
Board board = new Board(chanceDeck, commDeck, false);
ProbDice probDice = new ProbDice();
Queue<Player> queue = new LinkedList<>();
Cards chance = (Cards) board.square(7);
Player player1 = new HumanPlayer("John");
Player player2 = new HumanPlayer("Jim");
Player player3 = new HumanPlayer("James");
Player player4 = new HumanPlayer("Jacob");
queue.add(player1);
queue.add(player2);
queue.add(player3);
queue.add(player4);
ValueEstimator value = new ValueEstimator(board, queue, probDice, chance);
double[] probs = value.probLanding(0);
for (double prob : probs)
System.out.println(prob);
double totProb = 0;
for (int i = 0; i < 41; i++) {
double prob = value.getProb(i, 0);
System.out.println(i + ") " + prob * 100);
totProb += prob;
}
System.out.println("Tot prob: " + totProb);
}
public double expectedValue(int squarePos, int val) {
double[] probs = probLanding(squarePos);
double tot = 0;
for (int i = 0; i < probs.length; i++)
tot += probs[i] * val * i;
return tot;
}
//return the probability of a given number of players landing
//on a given property
private double[] probLanding(int squarePos) {
//first array dimension is the square position
//second array dimension is the number of players landing on square
double[] probs = new double[NUM_PLAYERS + 1];
double[] totProbs = new double[NUM_PLAYERS + 1];
int num = 0;
for (Player p : players)
probs[num++] = getProb(squarePos, p.position());
totProbs[0] = 1;
for (double prob : probs)
totProbs[0] *= (1 - prob);
for (int i = 0; i < probs.length; i++) {
double prod = probs[i];
for (int j = 0; j < probs.length; j++) {
if (i != j)
prod *= (1 - probs[j]);
}
totProbs[1] += prod;
}
if (NUM_PLAYERS == 1)
return totProbs;
for (int i = 0; i < probs.length; i++) {
for (int j = i + 1; j < probs.length; j++) {
double prod = probs[i] * probs[j];
for (int k = 0; k < probs.length; k++) {
if (i != k && j != k)
prod *= (1 - probs[k]);
}
totProbs[2] += prod;
}
}
if (NUM_PLAYERS == 2)
return totProbs;
//assume that p[4+] is negligible.
totProbs[3] = 1 - totProbs[0] - totProbs[1] - totProbs[2];
return totProbs;
}
//probability of getting from playerPos to squarePos
private double getProb(int squarePos, int playerPos)
{
Square square = board.square(squarePos);
if (squarePos < playerPos)
squarePos += board.size(); //accounts for passing go
int dist = squarePos - playerPos;
double prob = 0;
if (square instanceof Cards)
return cardProb(dist, square);
else if (squarePos < 40)
prob = directProb(dist);
prob += indirectProb(squarePos, playerPos, square);
return prob;
}
private double cardProb(int dist, Square square) {
Cards cards = (Cards) square;
if (cards.type() == Card.CardType.CHANCE)
return directProb(dist) * (6.0 / 16);
else
return directProb(dist) * (14.0 / 16);
}
//probability of landing directly on property by probDice roll
private double directProb(int dist)
{
return doubleProb(dist);
//return singleProb(dist);
}
//probability of landing on property by chance card
private double indirectProb(int squarePos, int playerPos, Square square)
{
return chanceProb(squarePos, playerPos, square) + communityProb(squarePos, playerPos);
}
private double communityProb(int squarePos, int playerPos) {
int comPosA = 2;
int comPosB = 17;
int comPosC = 33;
if (squarePos != 0 && squarePos != 40)
return 0;
if (comPosA < playerPos)
comPosA += board.size(); //accounts for passing go
if (comPosB < playerPos)
comPosB += board.size(); //accounts for passing go
if (comPosC < playerPos)
comPosC += board.size(); //accounts for passing go
int distA = comPosA - playerPos;
int distB = comPosB - playerPos;
int distC = comPosC - playerPos;
double probChanceA = directProb(distA);
double probChanceB = directProb(distB);
double probChanceC = directProb(distC);
double probChance = probChanceA + probChanceB + probChanceC;
return probChance / DECK_SIZE;
}
private double chanceProb(int squarePos, int playerPos, Square square) {
//TODO Account for rolls remaining after chance if doubles
double prob = 0.0;
int chancePosA = 7;
int chancePosB = 22;
int chancePosC = 36;
if (chancePosA < playerPos)
chancePosA += board.size(); //accounts for passing go
if (chancePosB < playerPos)
chancePosB += board.size(); //accounts for passing go
if (chancePosC < playerPos)
chancePosC += board.size(); //accounts for passing go
int distA = chancePosA - playerPos;
int distB = chancePosB - playerPos;
int distC = chancePosC - playerPos;
double probChanceA = directProb(distA);
double probChanceB = directProb(distB);
double probChanceC = directProb(distC);
double probChance = probChanceA + probChanceB + probChanceC;
int SIZE = DECK_SIZE;
for (Card card : chance.cards()) {
if (card.travelTo() == squarePos)
prob += probChance / SIZE;
else if (card.travel() == squarePos - chancePosA)
prob += probChanceA / SIZE;
else if (card.travel() == squarePos - chancePosB)
prob += probChanceB / SIZE;
else if (card.travel() == squarePos - chancePosC)
prob += probChanceC / SIZE;
prob += moveNearest(card, square, probChanceA, probChanceB, probChanceC) / SIZE;
}
return prob;
}
private double moveNearest(Card card, Square square, double probA, double probB, double probC) {
if (card.action() != Card.CardAction.MOVE_NEAREST)
return 0;
if (square instanceof Railroad) {
if (!card.travelRail())
return 0;
switch (square.position()) {
case 5:
return probC;
case 15:
return probA;
case 25:
return probB;
case 35:
return 0;
default:
throw new RuntimeException("Impossible railroad position.");
}
}
if (square instanceof Utility) {
if (card.travelRail())
return 0;
switch (square.position()) {
case 12:
return probA + probC;
case 28:
return probB;
default:
throw new RuntimeException("Impossible utility position.");
}
}
return 0;
}
private void setDouble()
{
final double D = 36.0;
final double T = 1296.0;
final int MAX = 36;
int[] ways = new int[MAX];
int[] doubleWays = new int[MAX];
int[] tripleWays = new int[MAX];
doubleProb = new double[MAX];
// double[] singleProb = new double[13];
ways[2] = 1;
ways[3] = 2;
ways[4] = 3;
ways[5] = 4;
ways[6] = 5;
ways[7] = 6;
ways[8] = 5;
ways[9] = 4;
ways[10] = 3;
ways[11] = 2;
ways[12] = 1;
doubleWays[4] = 1;
doubleWays[5] = 2;
doubleWays[6] = 4;
doubleWays[7] = 6;
doubleWays[8] = 9;
doubleWays[9] = 12;
doubleWays[10] = 14;
doubleWays[11] = 16;
doubleWays[12] = 17;
doubleWays[13] = 18;
doubleWays[14] = 18;
doubleWays[15] = 18;
doubleWays[16] = 17;
doubleWays[17] = 16;
doubleWays[18] = 14;
doubleWays[19] = 12;
doubleWays[20] = 9;
doubleWays[21] = 6;
doubleWays[22] = 4;
doubleWays[23] = 2;
doubleWays[24] = 1;
tripleWays[0] = 216;
tripleWays[7] = 2;
tripleWays[8] = 2;
tripleWays[9] = 8;
tripleWays[10] = 8;
tripleWays[11] = 20;
tripleWays[12] = 18;
tripleWays[13] = 36;
tripleWays[14] = 30;
tripleWays[15] = 54;
tripleWays[16] = 42;
tripleWays[17] = 72;
tripleWays[18] = 54;
tripleWays[19] = 86;
tripleWays[20] = 62;
tripleWays[21] = 92;
tripleWays[22] = 62;
tripleWays[23] = 86;
tripleWays[24] = 54;
tripleWays[25] = 72;
tripleWays[26] = 42;
tripleWays[27] = 54;
tripleWays[28] = 30;
tripleWays[29] = 36;
tripleWays[30] = 18;
tripleWays[31] = 20;
tripleWays[32] = 8;
tripleWays[33] = 8;
tripleWays[34] = 2;
tripleWays[35] = 2;
for (int i = 0; i < MAX; i++)
doubleProb[i] = (ways[i] + doubleWays[i] / D + tripleWays[i] / T) / MAX;
// for (int i = 0; i < 13; i++)
// singleProb[i] = ways[i] / 36.0;
}
// --Commented out by Inspection START (6/21/2015 9:22 AM):
// public double singleProb(int roll) {
// if (roll < 2 || roll > 12)
// return 0.0;
// return singleProb[roll];
// }
// --Commented out by Inspection STOP (6/21/2015 9:22 AM)
private double doubleProb(int roll) {
if (roll < 2 || roll > 35)
return 0.0;
return doubleProb[roll];
}
}