-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerGridGameState.java
More file actions
401 lines (326 loc) · 12.9 KB
/
PowerGridGameState.java
File metadata and controls
401 lines (326 loc) · 12.9 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package games.powergrid;
import java.util.HashMap;
import java.util.List;
import core.AbstractGameState;
import core.AbstractParameters;
import core.components.Component;
import core.components.Deck;
import core.components.PartialObservableDeck;
import core.interfaces.IGamePhase;
import games.GameType;
import games.powergrid.components.PowerGridCard;
import games.powergrid.components.PowerGridCity;
import games.powergrid.components.PowerGridGraphBoard;
import games.powergrid.components.PowerGridResourceMarket;
import java.util.*;
import java.util.stream.Collectors;
import static core.CoreConstants.VisibilityMode.*;
/**
* Power Grid Game State (TAG-friendly)
* Keep rules out of here; this is just data + cheap helpers.
*/
import java.util.ArrayList;
import java.util.List;
/**
* Minimal Power Grid Game State for early testing.
* Only includes fields used by your current ForwardModel:
* - gameMap
* - drawPile
* - currentMarket
* - futureMarket
*/
public class PowerGridGameState extends AbstractGameState {
// Keep these public for now to match your ForwardModel's direct field access.
public PowerGridGraphBoard gameMap;
public PowerGridResourceMarket resourceMarket;
public EnumMap<PowerGridParameters.Resource, Integer>[] fuelByPlayer;
public Deck<PowerGridCard> drawPile;
public Deck<PowerGridCard> currentMarket;
public Deck<PowerGridCard> futureMarket;
private List<Integer> turnOrder = new ArrayList<>();
private int turnOrderIndex = 0;
private int[] playerMoney;
private Map<Integer, Bid> currentBids = new HashMap<>();
private int[] cityCountByPlayer;
private int[][] citySlotsById; // [cityId][slot] -> playerId or -1
private PowerGridParameters.Phase currentPhase;
private List<Integer> activeBidders = new ArrayList<>();
private Deck<PowerGridCard>[] ownedPlantsByPlayer;
// Track which plant is currently being auctioned (-1 means none)
private int auctionPlantNumber = -1;
// Track the current highest bid and who holds it
private int currentBid = 0;
private int currentBidder = -1;
public PowerGridGameState(AbstractParameters gameParameters, int nPlayers) {
super(gameParameters, nPlayers);
this.cityCountByPlayer = new int[nPlayers];
this.playerMoney = new int[nPlayers];
}
// ---------- Required TAG overrides ----------
@Override
protected GameType _getGameType() {
// Ensure GameType.PowerGrid exists in your enum; if not, add it or return a placeholder.
return GameType.PowerGrid;
}
@Override
protected List<Component> _getAllComponents() {
ArrayList<Component> all = new ArrayList<>();
if (gameMap != null) all.add(gameMap);
if (resourceMarket != null) all.add(resourceMarket);
if (drawPile != null) all.add(drawPile);
if (currentMarket != null) all.add(currentMarket);
if (futureMarket != null) all.add(futureMarket);
return all;
}
@Override
protected PowerGridGameState _copy(int playerId) {
PowerGridGameState copy = new PowerGridGameState(gameParameters, getNPlayers());
// existing component copies...
copy.gameMap = (this.gameMap == null) ? null : this.gameMap.copy();
copy.drawPile = (this.drawPile == null) ? null : this.drawPile.copy();
copy.currentMarket = (this.currentMarket == null) ? null : this.currentMarket.copy();
copy.futureMarket = (this.futureMarket == null) ? null : this.futureMarket.copy();
copy.resourceMarket = (this.resourceMarket == null) ? null : this.resourceMarket.copy();
// === NEW: copy all scalar/array/list fields you read later ===
// phase
copy.currentPhase = this.currentPhase;
// money & cities
copy.playerMoney = (this.playerMoney == null) ? null : this.playerMoney.clone();
copy.cityCountByPlayer = (this.cityCountByPlayer == null) ? null : this.cityCountByPlayer.clone();
// city slots deep copy
if (this.citySlotsById != null) {
copy.citySlotsById = new int[this.citySlotsById.length][];
for (int i = 0; i < this.citySlotsById.length; i++) {
copy.citySlotsById[i] = (this.citySlotsById[i] == null) ? null : this.citySlotsById[i].clone();
}
}
// turn order
copy.turnOrder = new ArrayList<>(this.turnOrder);
copy.turnOrderIndex = this.turnOrderIndex;
// auction sub-state
copy.auctionPlantNumber = this.auctionPlantNumber;
copy.currentBid = this.currentBid;
copy.currentBidder = this.currentBidder;
copy.activeBidders = new ArrayList<>(this.activeBidders);
// current bids (Bid is immutable enough for shallow copy)
copy.currentBids = new HashMap<>(this.currentBids);
// fuel (you already do this)
if (fuelByPlayer != null) {
@SuppressWarnings("unchecked")
EnumMap<PowerGridParameters.Resource, Integer>[] fbCopy =
new EnumMap[fuelByPlayer.length];
for (int p = 0; p < fuelByPlayer.length; p++) {
fbCopy[p] = new EnumMap<>(PowerGridParameters.Resource.class);
fbCopy[p].putAll(fuelByPlayer[p]);
}
copy.fuelByPlayer = fbCopy;
}
// owned plants by player (deep copy each Deck)
if (this.ownedPlantsByPlayer != null) {
@SuppressWarnings("unchecked")
Deck<PowerGridCard>[] opCopy = (Deck<PowerGridCard>[]) new Deck<?>[this.ownedPlantsByPlayer.length];
for (int p = 0; p < this.ownedPlantsByPlayer.length; p++) {
Deck<PowerGridCard> src = this.ownedPlantsByPlayer[p];
opCopy[p] = (src == null) ? null : src.copy(); // TAG Deck.copy() does a safe component copy
}
copy.ownedPlantsByPlayer = opCopy;
}
return copy;
}
// ---------- (Optional) convenience getters ----------
public PowerGridGraphBoard getGameMap() { return gameMap; }
public Deck<PowerGridCard> getDrawPile() { return drawPile; }
public Deck<PowerGridCard> getCurrentMarket() { return currentMarket; }
public Deck<PowerGridCard> getFutureMarket() { return futureMarket; }
@Override
protected double _getHeuristicScore(int playerId) {
// TODO Auto-generated method stub
return 0;
}
@Override
public double getGameScore(int playerId) {
// TODO Auto-generated method stub
return 0;
}
@Override
protected boolean _equals(Object o) {
// TODO Auto-generated method stub
return false;
}
@SuppressWarnings("unchecked")
public void initFuelStorage() {
int nPlayers = getNPlayers();
fuelByPlayer = new EnumMap[nPlayers];
for (int p = 0; p < nPlayers; p++) {
fuelByPlayer[p] = new EnumMap<>(PowerGridParameters.Resource.class);
for (PowerGridParameters.Resource r : PowerGridParameters.Resource.values())
fuelByPlayer[p].put(r, 0);
}
}
@SuppressWarnings("unchecked")
public void initOwnedPlants() {
int n = getNPlayers();
ownedPlantsByPlayer = (Deck<PowerGridCard>[]) new Deck<?>[n];
for (int p = 0; p < n; p++) {
ownedPlantsByPlayer[p] = new Deck<>("OwnedPlants_P" + p, 0, VISIBLE_TO_ALL);
}
}
// helpers
public int getFuel(int playerId, PowerGridParameters.Resource r) {
return fuelByPlayer[playerId].get(r);
}
public void addFuel(int playerId, PowerGridParameters.Resource r, int amount) {
fuelByPlayer[playerId].merge(r, amount, Integer::sum);
}
public void removeFuel(int playerId, PowerGridParameters.Resource r, int amount) {
int have = getFuel(playerId, r);
if (amount > have) throw new IllegalArgumentException("Player lacks " + r);
fuelByPlayer[playerId].put(r, have - amount);
}
public List<Integer> getTurnOrder() {
return Collections.unmodifiableList(turnOrder);
}
public int getTurnOrderIndex() {
return turnOrderIndex;
}
void setTurnOrder(List<Integer> newOrder) {
turnOrder = new ArrayList<>(newOrder); turnOrderIndex = 0;
}
void advanceTurn() {
turnOrderIndex = (turnOrderIndex + 1) % turnOrder.size();
}
public int getCityCount(int playerId) {
return cityCountByPlayer[playerId];
}
public int [][] getCitygraph() {
return citySlotsById;
}
public void claimCitySlot(int playerId, int cityId, int slotIndex) {
if (citySlotsById[cityId][slotIndex] != -1)
throw new IllegalStateException("Slot already occupied");
citySlotsById[cityId][slotIndex] = playerId;
// Count *cities*, not houses: increment only on first presence in that city
cityCountByPlayer[playerId]++;
}
public void initCityStorageForBoard() {
if (gameMap == null) throw new IllegalStateException("Board not set");
int maxCityId = gameMap.maxCityId();
citySlotsById = new int[maxCityId + 1][3];
for (int id = 0; id <= maxCityId; id++) Arrays.fill(citySlotsById[id], -1);
}
public int getHighestPlantNumber(int playerId) {
// TODO: return the highest-numbered plant owned by playerId.
// If you haven't modeled ownership yet, return 0 as a safe default.
return 0;
}
public String fuelSummary() {
StringBuilder sb = new StringBuilder();
for (int p = 0; p < fuelByPlayer.length; p++) {
sb.append("P").append(p).append(": ");
for (PowerGridParameters.Resource r : PowerGridParameters.Resource.values()) {
sb.append(r).append("=").append(fuelByPlayer[p].get(r)).append(" ");
}
sb.append("\n");
}
return sb.toString();
}
/*Money Helper Methods*/
public void setStartingMoney(int starting_money) {
for (int i = 0; i < nPlayers; i++) {
playerMoney[i] = starting_money;
}
}
public int getPlayersMoney(int playerId) {
return playerMoney[playerId];
}
public int increasePlayerMoney(int playerId, int amount) {
playerMoney[playerId] += amount;
return playerMoney[playerId];
}
public int decreasePlayerMoney(int playerId, int amount) {
playerMoney[playerId] -= amount;
return playerMoney[playerId];
}
// Bid helpers
public static class Bid {
public final int plantNumber;
public final int amount;
public Bid(int plantNumber, int amount) {
this.plantNumber = plantNumber;
this.amount = amount;
}
}
public void recordBid(int playerId, int plantNumber, int amount) {
currentBids.put(playerId, new Bid(plantNumber, amount));
}
public Map<Integer, Bid> getCurrentBids() {
return Collections.unmodifiableMap(currentBids);
}
public void clearBids() {
currentBids.clear();
}
public PowerGridParameters.Phase getPhase() {
return currentPhase;
}
public void setPhase(PowerGridParameters.Phase phase) {
this.currentPhase = phase;
}
//Auction Helpers
public boolean isAuctionLive() {
return auctionPlantNumber != -1;
}
public int getAuctionPlantNumber() { return auctionPlantNumber; }
public void setAuctionPlantNumber(int number) { auctionPlantNumber = number; }
public int getCurrentBid() { return currentBid; }
public void setCurrentBid(int amount, int bidder) {
currentBid = amount;
currentBidder = bidder;
}
public int getCurrentBidder() { return currentBidder; }
public void clearAuction() {
auctionPlantNumber = -1;
currentBid = 0;
currentBidder = -1;
}
public void startAuction(List<Integer> cycle) {
activeBidders.clear();
activeBidders.addAll(cycle);
}
public List<Integer> getActiveBidders() { return activeBidders; }
public void passBid(int playerId) {
activeBidders.remove(playerId);
}
public boolean isStillInAuction(int playerId) {
return activeBidders.contains(playerId);
}
public void passOnAuction(int pid) {
activeBidders.removeIf(p -> p == pid);
}
// Helper for cycling bidders
public int nextActiveBidderAfter(int pid) {
if (activeBidders.isEmpty()) return -1;
int idx = activeBidders.indexOf(pid);
if (idx < 0) {
// If pid isn't in list (e.g., currentBidder is the high bidder, not acting),
// start from the beginning.
return activeBidders.get(0);
}
return activeBidders.get((idx + 1) % activeBidders.size());
}
// --- Ops ---
public Deck<PowerGridCard> getPlayerPlantDeck(int playerId) {
return ownedPlantsByPlayer[playerId];
}
public void addPlantToPlayer(int playerId, PowerGridCard card) {
Deck<PowerGridCard> d = ownedPlantsByPlayer[playerId];
if (d.getSize() >= 3) throw new IllegalStateException("Must replace when already at 3 plants.");
d.add(card);
d.getComponents().sort(Comparator.comparingInt(PowerGridCard::getNumber));
}
public void replacePlant(int playerId, int indexToSell, PowerGridCard newCard) {
Deck<PowerGridCard> d = ownedPlantsByPlayer[playerId];
if (d.getSize() != 3) throw new IllegalStateException("Replace only valid when at 3 plants.");
d.getComponents().set(indexToSell, newCard);
d.getComponents().sort(Comparator.comparingInt(PowerGridCard::getNumber));
}
}