-
Notifications
You must be signed in to change notification settings - Fork 748
/
RandomPlayer.java
408 lines (368 loc) · 14.2 KB
/
RandomPlayer.java
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
402
403
404
405
406
407
408
package org.mage.test.player;
import mage.abilities.*;
import mage.abilities.common.PassAbility;
import mage.abilities.costs.mana.GenericManaCost;
import mage.cards.Card;
import mage.cards.Cards;
import mage.choices.Choice;
import mage.constants.Outcome;
import mage.constants.RangeOfInfluence;
import mage.game.Game;
import mage.game.combat.CombatGroup;
import mage.game.events.GameEvent;
import mage.game.permanent.Permanent;
import mage.game.stack.StackAbility;
import mage.player.ai.ComputerPlayer;
import mage.players.Player;
import mage.target.Target;
import mage.target.TargetAmount;
import mage.target.TargetCard;
import mage.util.RandomUtil;
import java.io.Serializable;
import java.util.*;
/**
* plays randomly
*
* @author BetaSteward_at_googlemail.com
*/
public class RandomPlayer extends ComputerPlayer {
private final boolean isSimulatedPlayer;
private int actionCount = 0;
protected PassAbility pass = new PassAbility();
public RandomPlayer(String name) {
super(name, RangeOfInfluence.ALL);
this.isSimulatedPlayer = true;
}
public RandomPlayer(final RandomPlayer player) {
super(player);
this.isSimulatedPlayer = player.isSimulatedPlayer;
}
@Override
public RandomPlayer copy() {
return new RandomPlayer(this);
}
public boolean isSimulatedPlayer() {
return this.isSimulatedPlayer;
}
public int getActionCount() {
return actionCount;
}
@Override
public boolean priority(Game game) {
boolean didSomething = false;
Ability ability = getAction(game);
if (!(ability instanceof PassAbility)) {
didSomething = true;
}
activateAbility((ActivatedAbility) ability, game);
actionCount++;
return didSomething;
}
private Ability getAction(Game game) {
List<ActivatedAbility> playables = getPlayableAbilities(game);
Ability ability;
while (true) {
if (playables.size() == 1) {
ability = playables.get(0);
} else {
ability = playables.get(RandomUtil.nextInt(playables.size()));
}
List<Ability> options = getPlayableOptions(ability, game);
if (!options.isEmpty()) {
if (options.size() == 1) {
ability = options.get(0);
} else {
ability = options.get(RandomUtil.nextInt(options.size()));
}
}
if (!ability.getManaCosts().getVariableCosts().isEmpty()) {
int amount = getAvailableManaProducers(game).size() - ability.getManaCosts().manaValue();
if (amount > 0) {
ability = ability.copy();
ability.getManaCostsToPay().add(new GenericManaCost(RandomUtil.nextInt(amount)));
}
}
// check if ability kills player, if not then it's ok to play
// if (ability.isUsesStack()) {
// Game testSim = game.copy();
// activateAbility((ActivatedAbility) ability, testSim);
// StackObject testAbility = testSim.getStack().pop();
// testAbility.resolve(testSim);
// testSim.applyEffects();
// testSim.checkStateAndTriggered();
// if (!testSim.getPlayer(playerId).hasLost()) {
// break;
// }
// }
// else {
break;
// }
}
return ability;
}
protected List<ActivatedAbility> getPlayableAbilities(Game game) {
List<ActivatedAbility> playables = getPlayable(game, true);
playables.add(pass);
return playables;
}
@Override
public boolean triggerAbility(TriggeredAbility source, Game game) {
if (source != null && source.canChooseTarget(game, playerId)) {
Ability ability;
List<Ability> options = getPlayableOptions(source, game);
if (options.isEmpty()) {
ability = source;
} else {
if (options.size() == 1) {
ability = options.get(0);
} else {
ability = options.get(RandomUtil.nextInt(options.size()));
}
}
if (ability.isUsesStack()) {
game.getStack().push(new StackAbility(ability, playerId));
if (ability.activate(game, false)) {
game.fireEvent(new GameEvent(GameEvent.EventType.TRIGGERED_ABILITY, ability.getId(), ability, ability.getControllerId()));
actionCount++;
return true;
}
} else {
if (ability.activate(game, false)) {
ability.resolve(game);
actionCount++;
return true;
}
}
}
return false;
}
@Override
public void selectAttackers(Game game, UUID attackingPlayerId) {
//useful only for two player games - will only attack first opponent
UUID defenderId = game.getOpponents(playerId).iterator().next();
List<Permanent> attackersList = super.getAvailableAttackers(defenderId, game);
//use binary digits to calculate powerset of attackers
int powerElements = (int) Math.pow(2, attackersList.size());
int value = RandomUtil.nextInt(powerElements);
StringBuilder binary = new StringBuilder();
binary.append(Integer.toBinaryString(value));
while (binary.length() < attackersList.size()) {
binary.insert(0, '0'); //pad with zeros
}
for (int i = 0; i < attackersList.size(); i++) {
if (binary.charAt(i) == '1') {
setStoredBookmark(game.bookmarkState()); // makes it possible to UNDO a declared attacker with costs from e.g. Propaganda
if (!game.getCombat().declareAttacker(attackersList.get(i).getId(), defenderId, playerId, game)) {
game.undo(playerId);
}
}
}
actionCount++;
}
@Override
public void selectBlockers(Ability source, Game game, UUID defendingPlayerId) {
int numGroups = game.getCombat().getGroups().size();
if (numGroups == 0) {
return;
}
List<Permanent> blockers = getAvailableBlockers(game);
for (Permanent blocker : blockers) {
int check = RandomUtil.nextInt(numGroups + 1);
if (check < numGroups) {
CombatGroup group = game.getCombat().getGroups().get(check);
if (!group.getAttackers().isEmpty()) {
this.declareBlocker(this.getId(), blocker.getId(), group.getAttackers().get(0), game);
}
}
}
actionCount++;
}
@Override
public void abort() {
abort = true;
}
protected boolean chooseRandom(Target target, Game game) {
Set<UUID> possibleTargets = target.possibleTargets(playerId, game);
if (possibleTargets.isEmpty()) {
return false;
}
if (possibleTargets.size() == 1) {
target.add(possibleTargets.iterator().next(), game);
return true;
}
Iterator<UUID> it = possibleTargets.iterator();
int targetNum = RandomUtil.nextInt(possibleTargets.size());
UUID targetId = it.next();
for (int i = 0; i < targetNum; i++) {
targetId = it.next();
}
target.add(targetId, game);
return true;
}
protected boolean chooseRandomTarget(Target target, Ability source, Game game) {
Set<UUID> possibleTargets = target.possibleTargets(playerId, source, game);
if (possibleTargets.isEmpty()) {
return false;
}
if (!target.isRequired(source)) {
if (RandomUtil.nextInt(possibleTargets.size() + 1) == 0) {
return false;
}
}
if (possibleTargets.size() == 1) {
target.addTarget(possibleTargets.iterator().next(), source, game); // todo: addtryaddtarget or return type (see computerPlayer)
return true;
}
Iterator<UUID> it = possibleTargets.iterator();
int targetNum = RandomUtil.nextInt(possibleTargets.size());
UUID targetId = it.next();
for (int i = 0; i < targetNum; i++) {
targetId = it.next();
}
target.addTarget(targetId, source, game); // todo: addtryaddtarget or return type (see computerPlayer)
return true;
}
@Override
public boolean choose(Outcome outcome, Target target, Ability source, Game game) {
return chooseRandom(target, game);
}
@Override
public boolean choose(Outcome outcome, Target target, Ability source, Game game, Map<String, Serializable> options) {
return chooseRandom(target, game);
}
@Override
public boolean choose(Outcome outcome, Cards cards, TargetCard target, Game game) {
if (cards.isEmpty()) {
return false;
}
Set<UUID> possibleTargets = target.possibleTargets(playerId, cards, game);
if (possibleTargets.isEmpty()) {
return true;
}
Iterator<UUID> it = possibleTargets.iterator();
int targetNum = RandomUtil.nextInt(possibleTargets.size());
UUID targetId = it.next();
for (int i = 0; i < targetNum; i++) {
targetId = it.next();
}
target.add(targetId, game);
return true;
}
@Override
public boolean chooseTarget(Outcome outcome, Target target, Ability source, Game game) {
return chooseRandomTarget(target, source, game);
}
@Override
public boolean chooseTarget(Outcome outcome, Cards cards, TargetCard target, Ability source, Game game) {
if (cards.isEmpty()) {
return !target.isRequired(source);
}
Card card = cards.getRandom(game);
if (card != null) {
target.addTarget(card.getId(), source, game); // todo: addtryaddtarget or return type (see computerPlayer)
return true;
}
return false;
}
@Override
public boolean chooseTargetAmount(Outcome outcome, TargetAmount target, Ability source, Game game) {
Set<UUID> possibleTargets = target.possibleTargets(playerId, source, game);
if (possibleTargets.isEmpty()) {
return !target.isRequired(source);
}
if (!target.isRequired(source)) {
if (RandomUtil.nextInt(possibleTargets.size() + 1) == 0) {
return false;
}
}
if (possibleTargets.size() == 1) {
target.addTarget(possibleTargets.iterator().next(), target.getAmountRemaining(), source, game); // todo: addtryaddtarget or return type (see computerPlayer)
return true;
}
Iterator<UUID> it = possibleTargets.iterator();
int targetNum = RandomUtil.nextInt(possibleTargets.size());
UUID targetId = it.next();
for (int i = 0; i < targetNum; i++) {
targetId = it.next();
}
target.addTarget(targetId, RandomUtil.nextInt(target.getAmountRemaining()) + 1, source, game); // todo: addtryaddtarget or return type (see computerPlayer)
return true;
}
@Override
public boolean chooseMulligan(Game game) {
return RandomUtil.nextBoolean();
}
@Override
public boolean chooseUse(Outcome outcome, String message, Ability source, Game game) {
return RandomUtil.nextBoolean();
}
@Override
public boolean choosePile(Outcome outcome, String message, List<? extends Card> pile1, List<? extends Card> pile2, Game game) {
return RandomUtil.nextBoolean();
}
@Override
public boolean choose(Outcome outcome, Choice choice, Game game) {
choice.setRandomChoice();
return true;
}
@Override
public int chooseReplacementEffect(Map<String, String> rEffects, Game game) {
return RandomUtil.nextInt(rEffects.size());
}
@Override
public TriggeredAbility chooseTriggeredAbility(List<TriggeredAbility> abilities, Game game) {
return abilities.get(RandomUtil.nextInt(abilities.size()));
}
@Override
public Mode chooseMode(Modes modes, Ability source, Game game) {
Iterator<Mode> it = modes.getAvailableModes(source, game).iterator();
Mode mode = it.next();
if (modes.size() == 1) {
return mode;
}
int modeNum = RandomUtil.nextInt(modes.getAvailableModes(source, game).size());
for (int i = 0; i < modeNum; i++) {
mode = it.next();
}
return mode;
}
@Override
public UUID chooseAttackerOrder(List<Permanent> attackers, Game game) {
return attackers.get(RandomUtil.nextInt(attackers.size())).getId();
}
@Override
public UUID chooseBlockerOrder(List<Permanent> blockers, CombatGroup combatGroup, List<UUID> blockerOrder, Game game) {
return blockers.get(RandomUtil.nextInt(blockers.size())).getId();
}
@Override
public void assignDamage(int damage, List<UUID> targets, String singleTargetName, UUID attackerId, Ability source, Game game) {
int remainingDamage = damage;
UUID targetId;
int amount;
while (remainingDamage > 0) {
if (targets.size() == 1) {
targetId = targets.get(0);
amount = remainingDamage;
} else {
targetId = targets.get(RandomUtil.nextInt(targets.size()));
amount = RandomUtil.nextInt(damage + 1);
}
Permanent permanent = game.getPermanent(targetId);
if (permanent != null) {
permanent.damage(amount, attackerId, source, game, false, true);
remainingDamage -= amount;
} else {
Player player = game.getPlayer(targetId);
if (player != null) {
player.damage(amount, attackerId, source, game);
remainingDamage -= amount;
}
}
targets.remove(targetId);
}
}
@Override
public int getAmount(int min, int max, String message, Game game) {
return RandomUtil.nextInt(max - min) + min;
}
}