forked from willcrichton/sevenwonders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoring.php
283 lines (249 loc) · 9.01 KB
/
scoring.php
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
<?php
class Resource {
const STONE = 0;
const WOOD = 1;
const ORE = 2;
const CLAY = 3;
const LINEN = 4;
const GLASS = 5;
const PAPER = 6;
protected $amts = array(self::STONE => 0, self::WOOD => 0, self::ORE => 0,
self::CLAY => 0, self::LINEN => 0, self::GLASS => 0,
self::PAPER => 0);
protected $only_one = false;
protected $_buyable = false;
public function __construct($only_one, $buyable) {
$this->only_one = $only_one;
$this->_buyable = $buyable;
}
public function buyable() {
return $this->_buyable;
}
public function only_one(){
return $this->only_one;
}
public function getAmts(){
return $this->amts;
}
public function add($resource) {
$this->amts[$resource]++;
}
public function json() {
// Not enough information to rebuild this object, but we just care about
// the name for display purposes (for now?)
foreach ($this->amts as $type => $amt) {
if ($amt == 0)
continue;
switch ($type) {
case self::STONE: return 'stone';
case self::WOOD: return 'wood';
case self::ORE: return 'ore';
case self::CLAY: return 'clay';
case self::LINEN: return 'linen';
case self::GLASS: return 'glass';
case self::PAPER: return 'paper';
}
}
return '<unknown>';
}
public function discount($discounts) {
// This isn't exactly optimal, but what we're doing here is that if a
// discounted resource appears at least once in our resource, then our
// entire resource is discounted. This turns out to always be true
// because discounts apply to WOCS or LGP, and no card has both of those
// types of resources (thankfully)
foreach ($discounts as $resource)
foreach ($resource->amts as $type => $amt)
if ($amt > 0 && $this->amts[$type] > 0)
return 1;
return 2;
}
public static function satisfy($want, $have, $maxcost) {
$total = array(self::STONE => 0, self::WOOD => 0, self::ORE => 0,
self::CLAY => 0, self::LINEN => 0, self::GLASS => 0,
self::PAPER => 0);
foreach ($want as $resource) {
foreach ($resource->amts as $res => $amt) {
$total[$res] += $amt;
}
}
// Try to consume low cost resources first so we can prune out all of
// the very costly resource combinations early on (if possible)
usort($have, function($a, $b) {
// First off, prefer cheaper resources to find the cheapest solution
// first.
if ($a->cost != $b->cost)
return $a->cost - $b->cost;
// Next, prefer not only_one resources because they may force us to
// make some form of decision multiple times.
if ($a->resource->only_one() != $b->resource->only_one()) {
if ($a->resource->only_one())
return 1; // prefer $b
return -1; // prefer $a
}
// Finally, prefer simpler resources first
return array_sum($a->resource->getAmts()) -
array_sum($b->resource->getAmts());
});
$ret = array();
self::tryuse(array('left' => 0, 'right' => 0, 'self' => 0),
$have, $total, $ret, $maxcost, 100);
return $ret;
}
private static function tryuse($costs, $available, &$want, &$ret,
$money, $minsofar) {
// If we ran out of money, then we're done
if ($money < 0)
return 100;
// If someone got this resource for free, then there's no use to go any
// farther
if ($minsofar == 0)
return 100;
$allZero = true;
foreach ($want as $amount) {
if ($amount > 0) {
$allZero = false;
break;
}
}
if ($allZero) {
$sum = array_sum($costs);
// Don't add ridiculous combinations where an absurd amount of money
// is spent when very little could be spent
if ($sum < $minsofar + 2) {
// Make sure we don't add duplicates to the array
if (array_search($costs, $ret) === false)
$ret[] = $costs;
}
return min($minsofar, $sum);
}
// If we ran out of resources, then we're done
if (count($available) == 0)
return 100;
$option = array_shift($available);
$resource = $option->resource;
$costs[$option->direction] += $option->cost;
$money -= $option->cost;
$used = false;
if ($resource->only_one) {
// If we can only use one of these resources, try each one
// individually and see if we can satisfy
foreach ($resource->amts as $type => $amt) {
if ($want[$type] <= 0 || $amt == 0)
continue;
$used = true;
$want[$type] -= $amt;
$rec = self::tryuse($costs, $available, $want, $ret, $money,
$minsofar);
$want[$type] += $amt;
$minsofar = min($rec, $minsofar);
}
} else {
// If we can use this multi-resource, then use as much of it as
// possible and then move on to using another resource.
foreach ($resource->amts as $type => $amt) {
if ($want[$type] > 0 && $amt > 0)
$used = true;
$want[$type] -= $amt;
}
if ($used) {
$rec = self::tryuse($costs, $available, $want, $ret, $money,
$minsofar);
$minsofar = min($rec, $minsofar);
}
foreach ($resource->amts as $type => $amt)
$want[$type] += $amt;
}
$costs[$option->direction] -= $option->cost;
$money += $option->cost;
// Try not using this resource if we didn't already.
if (!$used || $option->cost > 0) {
$rec = self::tryuse($costs, $available, $want, $ret, $money,
$minsofar);
return min($rec, $minsofar);
}
return $minsofar;
}
}
class ResourceOption {
public $resource;
public $direction;
public $cost;
public static function left(Resource $resource, $cost = 1) {
$ret = new ResourceOption();
$ret->cost = $cost;
$ret->direction = 'left';
$ret->resource = $resource;
return $ret;
}
public static function right(Resource $resource, $cost = 1) {
$ret = new ResourceOption();
$ret->cost = $cost;
$ret->direction = 'right';
$ret->resource = $resource;
return $ret;
}
public static function me(Resource $resource) {
$ret = new ResourceOption();
$ret->cost = 0;
$ret->direction = 'self';
$ret->resource = $resource;
return $ret;
}
}
class Science {
const ANY = 0;
const GEAR = 1;
const COMPASS = 2;
const TABLET = 3;
private $amts = array(self::ANY => 0, self::GEAR => 0, self::COMPASS => 0,
self::TABLET => 0);
public function add($science) {
$this->amts[$science]++;
}
public function points() {
// Maximize point value of wildcards
if ($this->amts[self::ANY] > 0) {
$max = 0;
$this->amts[self::ANY]--;
foreach (array(self::GEAR, self::COMPASS, self::TABLET) as $s) {
$this->amts[$s]++;
$max = max($max, $this->points());
$this->amts[$s]--;
}
$this->amts[self::ANY]++;
return $max;
}
$min = 1000;
$points = 0;
foreach ($this->amts as $typ => $amt) {
if ($typ == self::ANY) continue;
$points += $amt * $amt;
$min = min($min, $amt);
}
return $points + $min * 7;
}
}
class Military {
private $_size = 0;
private $amt = array(-1 => 0, 1 => 0, 3 => 0, 5 => 0);
// Player receives appropriate military tokens at end of age
public function fight($other, $age) {
if ($other->_size > $this->_size) {
$this->amt[-1]++;
} else if ($other->_size < $this->_size) {
$this->amt[2 * $age - 1]++;
}
}
public function points() {
$sum = 0;
foreach ($this->amt as $mult => $amt) {
$sum += $mult * $amt;
}
return $sum;
}
public function add($amt) { $this->_size += $amt; }
public function json() { return $this->amt; }
public function size() { return $this->_size; }
public function losses() { return $this->amt[-1]; }
}