Test framework: simplified AI logic and tests, added usage comments. Devs recommendations:

* in card's code use player.isComputer instead player.isHuman (it help to split Human/AI logic and test both by unit tests);
* usage example: AI hint to skip or auto-calculate choices instead call of real choose dialogs;
* unit tests for Human logic: call normal commands;
* unit tests for AI logic: call aiXXX commands;
This commit is contained in:
Oleg Agafonov 2021-03-07 22:51:58 +04:00
parent 00c7b3753c
commit 2906f86324
22 changed files with 106 additions and 47 deletions

View file

@ -109,8 +109,9 @@ public class ComputerPlayer extends PlayerImpl implements Player {
public boolean chooseMulligan(Game game) { public boolean chooseMulligan(Game game) {
log.debug("chooseMulligan"); log.debug("chooseMulligan");
if (hand.size() < 6 if (hand.size() < 6
|| isTestMode() || isTestsMode() // ignore mulligan in tests
|| game.getClass().getName().contains("Momir")) { || game.getClass().getName().contains("Momir") // ignore mulligan in Momir games
) {
return false; return false;
} }
Set<Card> lands = hand.getCards(new FilterLandCard(), game); Set<Card> lands = hand.getCards(new FilterLandCard(), game);
@ -2880,9 +2881,10 @@ public class ComputerPlayer extends PlayerImpl implements Player {
@Override @Override
public boolean isHuman() { public boolean isHuman() {
if (human) { if (human) {
log.error("computer must be not human", new Throwable()); throw new IllegalStateException("Computer player can't be Human");
} else {
return false;
} }
return human;
} }
@Override @Override

View file

@ -73,7 +73,7 @@ class AdNauseamEffect extends OneShotEffect {
controller.revealCards(sourceCard.getIdName() + " put into hand", new CardsImpl(card), game); controller.revealCards(sourceCard.getIdName() + " put into hand", new CardsImpl(card), game);
// AI workaround to stop infinite choose (only one card allows) // AI workaround to stop infinite choose (only one card allows)
if (!controller.isHuman() && !controller.isTestMode()) { if (controller.isComputer()) {
break; break;
} }
} }

View file

@ -66,7 +66,7 @@ class ChoiceOfDamnationsEffect extends OneShotEffect {
// AI hint // AI hint
int amount; int amount;
if (!targetPlayer.isHuman() && !targetPlayer.isTestMode()) { if (targetPlayer.isComputer()) {
// AI as defender // AI as defender
int safeLifeToLost = Math.max(0, targetPlayer.getLife() / 2); int safeLifeToLost = Math.max(0, targetPlayer.getLife() / 2);
amount = Math.min(numberPermanents, safeLifeToLost); amount = Math.min(numberPermanents, safeLifeToLost);
@ -80,7 +80,7 @@ class ChoiceOfDamnationsEffect extends OneShotEffect {
// AI hint // AI hint
boolean chooseLoseLife; boolean chooseLoseLife;
if (!targetPlayer.isHuman() && !targetPlayer.isTestMode()) { if (targetPlayer.isComputer()) {
// AI as attacker // AI as attacker
chooseLoseLife = (numberPermanents == 0 || amount <= numberPermanents || targetPlayer.getLife() < amount); chooseLoseLife = (numberPermanents == 0 || amount <= numberPermanents || targetPlayer.getLife() < amount);
} else { } else {

View file

@ -69,7 +69,7 @@ class CrazedFirecatEffect extends OneShotEffect {
flipsWon++; flipsWon++;
// AI workaround to stop on good condition // AI workaround to stop on good condition
if (!controller.isHuman() && !controller.isTestMode() && flipsWon >= 2) { if (controller.isComputer() && flipsWon >= 2) {
break; break;
} }
} }

View file

@ -72,7 +72,7 @@ class FieryGambitEffect extends OneShotEffect {
} }
// AI workaround to stop flips on good result // AI workaround to stop flips on good result
if (!controller.isHuman() && !controller.isTestMode() && flipsWon >= 3) { if (controller.isComputer() && flipsWon >= 3) {
controllerStopped = true; controllerStopped = true;
break; break;
} }

View file

@ -69,7 +69,7 @@ class FluctuatorEffect extends CostModificationEffectImpl {
} }
if (reduceMax > 0) { if (reduceMax > 0) {
int reduce; int reduce;
if (game.inCheckPlayableState() || !controller.isHuman()) { if (game.inCheckPlayableState() || controller.isComputer()) {
reduce = reduceMax; reduce = reduceMax;
} else { } else {
ChoiceImpl choice = new ChoiceImpl(true); ChoiceImpl choice = new ChoiceImpl(true);

View file

@ -78,7 +78,9 @@ class IllicitAuctionEffect extends GainControlTargetEffect {
if (currentPlayer.canRespond() if (currentPlayer.canRespond()
&& currentPlayer.chooseUse(Outcome.GainControl, text, source, game)) { && currentPlayer.chooseUse(Outcome.GainControl, text, source, game)) {
int newBid = 0; int newBid = 0;
if (!currentPlayer.isHuman()) {//AI will evaluate the creature and bid if (currentPlayer.isComputer()) {
// AI hint
// AI will evaluate the creature and bid
CreatureEvaluator eval = new CreatureEvaluator(); CreatureEvaluator eval = new CreatureEvaluator();
int computerLife = currentPlayer.getLife(); int computerLife = currentPlayer.getLife();
int creatureValue = eval.evaluate(targetCreature, game); int creatureValue = eval.evaluate(targetCreature, game);

View file

@ -70,7 +70,7 @@ class InvasionPlansEffect extends ContinuousRuleModifyingEffectImpl {
Player blockController = game.getPlayer(game.getCombat().getAttackingPlayerId()); Player blockController = game.getPlayer(game.getCombat().getAttackingPlayerId());
if (blockController != null) { if (blockController != null) {
// temporary workaround for AI bugging out while choosing blockers // temporary workaround for AI bugging out while choosing blockers
if (blockController.isHuman()) { if (!blockController.isComputer()) {
game.getCombat().selectBlockers(blockController, source, game); game.getCombat().selectBlockers(blockController, source, game);
return event.getPlayerId().equals(game.getCombat().getAttackingPlayerId()); return event.getPlayerId().equals(game.getCombat().getAttackingPlayerId());
} }

View file

@ -73,7 +73,11 @@ class LimDulsVaultEffect extends OneShotEffect {
player.shuffleLibrary(source, game); player.shuffleLibrary(source, game);
player.putCardsOnTopOfLibrary(cards, game, source, true); player.putCardsOnTopOfLibrary(cards, game, source, true);
} }
} while (doAgain && player.isHuman()); // AI must stop using it as infinite // AI must stop using it as infinite
if (player.isComputer()) {
break;
}
} while (doAgain);
return true; return true;
} }

View file

@ -70,13 +70,15 @@ class MagesContestEffect extends OneShotEffect {
do { do {
if (currentPlayer.canRespond()) { if (currentPlayer.canRespond()) {
int newBid = 0; int newBid = 0;
if (!currentPlayer.isHuman()) { if (currentPlayer.isComputer()) {
// AI hint
// make AI evaluate value of the spell to decide on bidding, should be reworked // make AI evaluate value of the spell to decide on bidding, should be reworked
int maxBid = Math.min(RandomUtil.nextInt(Math.max(currentPlayer.getLife(), 1)) + RandomUtil.nextInt(Math.max(spell.getConvertedManaCost(), 1)), currentPlayer.getLife()); int maxBid = Math.min(RandomUtil.nextInt(Math.max(currentPlayer.getLife(), 1)) + RandomUtil.nextInt(Math.max(spell.getConvertedManaCost(), 1)), currentPlayer.getLife());
if (highBid + 1 < maxBid) { if (highBid + 1 < maxBid) {
newBid = highBid + 1; newBid = highBid + 1;
} }
} else if (currentPlayer.chooseUse(Outcome.Benefit, winner.getLogName() + " has bet " + highBid + " life. Top the bid?", source, game)) { } else if (currentPlayer.chooseUse(Outcome.Benefit, winner.getLogName() + " has bet " + highBid + " life. Top the bid?", source, game)) {
// Human choose
newBid = currentPlayer.getAmount(highBid + 1, Integer.MAX_VALUE, "Choose bid", game); newBid = currentPlayer.getAmount(highBid + 1, Integer.MAX_VALUE, "Choose bid", game);
} }
if (newBid > highBid) { if (newBid > highBid) {

View file

@ -92,7 +92,7 @@ class PainsRewardEffect extends OneShotEffect {
private int chooseLifeAmountToBid(Player player, int currentBig, Game game) { private int chooseLifeAmountToBid(Player player, int currentBig, Game game) {
int newBid; int newBid;
if (!player.isHuman() && !player.isTestMode()) { if (player.isComputer()) {
// AI choose // AI choose
newBid = currentBig + 1; newBid = currentBig + 1;
} else { } else {

View file

@ -92,14 +92,16 @@ class SlaughterTheStrongEffect extends OneShotEffect {
// human can de-select targets, but AI must choose only one time // human can de-select targets, but AI must choose only one time
Target target; Target target;
if (player.isHuman()) { if (player.isComputer()) {
target = new TargetPermanent(0, 1, currentFilter, true); // AI settings
} else {
FilterControlledCreaturePermanent strictFilter = currentFilter.copy(); FilterControlledCreaturePermanent strictFilter = currentFilter.copy();
selectedCreatures.stream().forEach(id -> { selectedCreatures.stream().forEach(id -> {
strictFilter.add(Predicates.not(new PermanentIdPredicate(id))); strictFilter.add(Predicates.not(new PermanentIdPredicate(id)));
}); });
target = new TargetPermanent(0, 1, strictFilter, true); target = new TargetPermanent(0, 1, strictFilter, true);
} else {
// Human settings
target = new TargetPermanent(0, 1, currentFilter, true);
} }
player.chooseTarget(Outcome.BoostCreature, target, source, game); player.chooseTarget(Outcome.BoostCreature, target, source, game);
@ -110,7 +112,11 @@ class SlaughterTheStrongEffect extends OneShotEffect {
selectedCreatures.add(target.getFirstTarget()); selectedCreatures.add(target.getFirstTarget());
} }
} else { } else {
if (player.isHuman()) { if (player.isComputer()) {
// AI stops
selectionDone = true;
} else {
// Human can continue
String selected = "Selected: "; String selected = "Selected: ";
for (UUID creatureId : selectedCreatures) { for (UUID creatureId : selectedCreatures) {
Permanent creature = game.getPermanent(creatureId); Permanent creature = game.getPermanent(creatureId);
@ -123,8 +129,6 @@ class SlaughterTheStrongEffect extends OneShotEffect {
selected, selected,
"End the selection", "End the selection",
"Continue the selection", source, game); "Continue the selection", source, game);
} else {
selectionDone = true;
} }
} }
} }

View file

@ -72,7 +72,7 @@ class VolcanoHellionEffect extends OneShotEffect {
Permanent permanent = game.getPermanent(source.getFirstTarget()); Permanent permanent = game.getPermanent(source.getFirstTarget());
if (controller != null) { if (controller != null) {
int amount; int amount;
if (!controller.isHuman() && !controller.isTestMode()) { if (controller.isComputer()) {
// AI hint: have much life and can destroy target permanent // AI hint: have much life and can destroy target permanent
int safeLifeToLost = Math.min(6, controller.getLife() / 2); int safeLifeToLost = Math.min(6, controller.getLife() / 2);
if (permanent != null && permanent.getToughness().getValue() <= safeLifeToLost) { if (permanent != null && permanent.getToughness().getValue() <= safeLifeToLost) {

View file

@ -86,8 +86,14 @@ public class TestPlayer implements Player {
private int maxCallsWithoutAction = 400; private int maxCallsWithoutAction = 400;
private int foundNoAction = 0; private int foundNoAction = 0;
private boolean AIPlayer; // full playable AI
private boolean AICanChooseInStrictMode = false; // AI can choose in custom aiXXX commands (e.g. on one priority or step) // full playable AI, TODO: can be deleted?
private boolean AIPlayer;
// AI simulates a real game, e.g. ignores strict mode and play command/priority, see aiXXX commands
// true - unit tests uses real AI logic (e.g. AI hints and AI workarounds in cards)
// false - unit tests uses Human logic and dialogs
private boolean AIRealGameSimulation = false;
private final List<PlayerAction> actions = new ArrayList<>(); private final List<PlayerAction> actions = new ArrayList<>();
private final Map<PlayerAction, PhaseStep> actionsToRemoveLater = new HashMap<>(); // remove actions later, on next step (e.g. for AI commands) private final Map<PlayerAction, PhaseStep> actionsToRemoveLater = new HashMap<>(); // remove actions later, on next step (e.g. for AI commands)
private final Map<Integer, HashMap<UUID, ArrayList<PlayerAction>>> rollbackActions = new HashMap<>(); // actions to add after a executed rollback private final Map<Integer, HashMap<UUID, ArrayList<PlayerAction>>> rollbackActions = new HashMap<>(); // actions to add after a executed rollback
@ -125,7 +131,7 @@ public class TestPlayer implements Player {
public TestPlayer(final TestPlayer testPlayer) { public TestPlayer(final TestPlayer testPlayer) {
this.AIPlayer = testPlayer.AIPlayer; this.AIPlayer = testPlayer.AIPlayer;
this.AICanChooseInStrictMode = testPlayer.AICanChooseInStrictMode; this.AIRealGameSimulation = testPlayer.AIRealGameSimulation;
this.foundNoAction = testPlayer.foundNoAction; this.foundNoAction = testPlayer.foundNoAction;
this.actions.addAll(testPlayer.actions); this.actions.addAll(testPlayer.actions);
this.choices.addAll(testPlayer.choices); this.choices.addAll(testPlayer.choices);
@ -720,7 +726,7 @@ public class TestPlayer implements Player {
// play priority // play priority
if (command.equals(AI_COMMAND_PLAY_PRIORITY)) { if (command.equals(AI_COMMAND_PLAY_PRIORITY)) {
AICanChooseInStrictMode = true; // disable on action's remove AIRealGameSimulation = true; // disable on action's remove
computerPlayer.priority(game); computerPlayer.priority(game);
actions.remove(action); actions.remove(action);
return true; return true;
@ -728,7 +734,7 @@ public class TestPlayer implements Player {
// play step // play step
if (command.equals(AI_COMMAND_PLAY_STEP)) { if (command.equals(AI_COMMAND_PLAY_STEP)) {
AICanChooseInStrictMode = true; // disable on action's remove AIRealGameSimulation = true; // disable on action's remove
actionsToRemoveLater.put(action, game.getStep().getType()); actionsToRemoveLater.put(action, game.getStep().getType());
computerPlayer.priority(game); computerPlayer.priority(game);
return true; return true;
@ -1897,7 +1903,7 @@ public class TestPlayer implements Player {
} }
private void chooseStrictModeFailed(String choiceType, Game game, String reason, boolean printAbilities) { private void chooseStrictModeFailed(String choiceType, Game game, String reason, boolean printAbilities) {
if (strictChooseMode && !AICanChooseInStrictMode) { if (strictChooseMode && !AIRealGameSimulation) {
if (printAbilities) { if (printAbilities) {
printStart("Available mana for " + computerPlayer.getName()); printStart("Available mana for " + computerPlayer.getName());
printMana(game, computerPlayer.getManaAvailable(game)); printMana(game, computerPlayer.getManaAvailable(game));
@ -3060,7 +3066,18 @@ public class TestPlayer implements Player {
@Override @Override
public boolean isHuman() { public boolean isHuman() {
return computerPlayer.isHuman(); return false;
}
@Override
public boolean isComputer() {
// all players in unit tests are computers, so you must use AIRealGameSimulation to test different logic (Human vs AI)
if (isTestsMode()) {
return AIRealGameSimulation;
} else {
throw new IllegalStateException("Can't use test player outside of unit tests");
//return !isHuman();
}
} }
@Override @Override
@ -3449,8 +3466,8 @@ public class TestPlayer implements Player {
} }
@Override @Override
public boolean isTestMode() { public boolean isTestsMode() {
return computerPlayer.isTestMode(); return computerPlayer.isTestsMode();
} }
@Override @Override
@ -4193,8 +4210,8 @@ public class TestPlayer implements Player {
return computerPlayer; return computerPlayer;
} }
public void setAICanChooseInStrictMode(boolean AICanChooseInStrictMode) { public void setAIRealGameSimulation(boolean AIRealGameSimulation) {
this.AICanChooseInStrictMode = AICanChooseInStrictMode; this.AIRealGameSimulation = AIRealGameSimulation;
} }
public Map<Integer, HashMap<UUID, ArrayList<org.mage.test.player.PlayerAction>>> getRollbackActions() { public Map<Integer, HashMap<UUID, ArrayList<org.mage.test.player.PlayerAction>>> getRollbackActions() {

View file

@ -13,6 +13,10 @@ import org.mage.test.serverside.base.impl.CardTestPlayerAPIImpl;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
/** /**
* PlayerA is full AI player and process all actions as AI logic. You don't need aiXXX commands in that tests.
*
* If you need custom AI tests then use CardTestPlayerBaseWithAIHelps with aiXXX commands
*
* @author LevelX2 * @author LevelX2
*/ */
public abstract class CardTestPlayerBaseAI extends CardTestPlayerAPIImpl { public abstract class CardTestPlayerBaseAI extends CardTestPlayerAPIImpl {
@ -33,6 +37,7 @@ public abstract class CardTestPlayerBaseAI extends CardTestPlayerAPIImpl {
if (name.equals("PlayerA")) { if (name.equals("PlayerA")) {
TestPlayer testPlayer = new TestPlayer(new TestComputerPlayer7("PlayerA", RangeOfInfluence.ONE, skill)); TestPlayer testPlayer = new TestPlayer(new TestComputerPlayer7("PlayerA", RangeOfInfluence.ONE, skill));
testPlayer.setAIPlayer(true); testPlayer.setAIPlayer(true);
testPlayer.setAIRealGameSimulation(true); // enable AI logic simulation for all turns by default
return testPlayer; return testPlayer;
} }
return super.createPlayer(name, rangeOfInfluence); return super.createPlayer(name, rangeOfInfluence);

View file

@ -1571,9 +1571,9 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement
} }
/** /**
* AI play one PRIORITY with multi game simulations (calcs and play ONE best * AI play one PRIORITY with multi game simulations like real game
* action, can be called with stack) All choices must be made by AI * (calcs and play ONE best action, can be called with stack)
* (e.g.strict mode possible) * All choices must be made by AI (e.g.strict mode possible)
* *
* @param turnNum * @param turnNum
* @param step * @param step
@ -1595,11 +1595,11 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement
} }
public PlayerAction createAIPlayerAction(int turnNum, PhaseStep step, String aiCommand) { public PlayerAction createAIPlayerAction(int turnNum, PhaseStep step, String aiCommand) {
// AI actions must disable and enable strict mode // AI commands must disable and enable real game simulation and strict mode
return new PlayerAction("", turnNum, step, AI_PREFIX + aiCommand) { return new PlayerAction("", turnNum, step, AI_PREFIX + aiCommand) {
@Override @Override
public void onActionRemovedLater(Game game, TestPlayer player) { public void onActionRemovedLater(Game game, TestPlayer player) {
player.setAICanChooseInStrictMode(false); player.setAIRealGameSimulation(false);
} }
}; };
} }

View file

@ -477,7 +477,7 @@ public class PlayerStub implements Player {
} }
@Override @Override
public boolean isTestMode() { public boolean isTestsMode() {
return false; return false;
} }

View file

@ -319,7 +319,7 @@ public abstract class AbilityImpl implements Ability {
// unit tests only: it allows to add targets/choices by two ways: // unit tests only: it allows to add targets/choices by two ways:
// 1. From cast/activate command params (process it here) // 1. From cast/activate command params (process it here)
// 2. From single addTarget/setChoice, it's a preffered method for tests (process it in normal choose dialogs like human player) // 2. From single addTarget/setChoice, it's a preffered method for tests (process it in normal choose dialogs like human player)
if (controller.isTestMode()) { if (controller.isTestsMode()) {
if (!controller.addTargets(this, game)) { if (!controller.addTargets(this, game)) {
return false; return false;
} }

View file

@ -94,8 +94,9 @@ public class AssistAbility extends SimpleStaticAbility implements AlternateManaP
} }
// AI can't use assist (can't ask another player to help), maybe in teammode it can be enabled, but tests must works all the time // AI can't use assist (can't ask another player to help), maybe in teammode it can be enabled, but tests must works all the time
// Outcome.AIDontUseIt
Player controller = game.getPlayer(source.getControllerId()); Player controller = game.getPlayer(source.getControllerId());
if (controller != null && !controller.isTestMode() && !controller.isHuman()) { if (controller != null && controller.isComputer()) {
return options; return options;
} }
@ -170,7 +171,7 @@ class AssistEffect extends OneShotEffect {
if (controller != null && spell != null && targetPlayer != null) { if (controller != null && spell != null && targetPlayer != null) {
// AI can't assist other players, maybe for teammates only (but tests must work as normal) // AI can't assist other players, maybe for teammates only (but tests must work as normal)
int amountToPay = 0; int amountToPay = 0;
if (targetPlayer.isHuman() || targetPlayer.isTestMode()) { if (!targetPlayer.isComputer()) {
amountToPay = targetPlayer.announceXMana(0, unpaid.getMana().getGeneric(), amountToPay = targetPlayer.announceXMana(0, unpaid.getMana().getGeneric(),
"How much mana to pay as assist for " + controller.getName() + "?", game, source); "How much mana to pay as assist for " + controller.getName() + "?", game, source);
} }

View file

@ -1418,7 +1418,7 @@ public abstract class GameImpl implements Game, Serializable {
+ ex.getMessage()); + ex.getMessage());
} }
Player activePlayer = this.getPlayer(getActivePlayerId()); Player activePlayer = this.getPlayer(getActivePlayerId());
if (activePlayer != null && !activePlayer.isTestMode()) { if (activePlayer != null && !activePlayer.isTestsMode()) {
errorContinueCounter++; errorContinueCounter++;
continue; continue;
} else { } else {

View file

@ -48,8 +48,32 @@ import java.util.*;
*/ */
public interface Player extends MageItem, Copyable<Player> { public interface Player extends MageItem, Copyable<Player> {
/**
* Current player is real life player (human). Try to use in GUI and network engine only.
*
* WARNING, you must use isComputer instead isHuman in card's code (for good Human/AI logic testing in unit tests)
* TODO: check combat code and other and replace isHuman to isComputer usage if possible (if AI support that actions)
* @return
*/
boolean isHuman(); boolean isHuman();
boolean isTestsMode();
/**
* Current player is AI. Use it in card's code and all other places.
*
* It help to split Human/AI logic and test both by unit tests.
*
* Usage example: AI hint to skip or auto-calculate choices instead call of real choose dialogs
* - unit tests for Human logic: call normal commands
* - unit tests for AI logic: call aiXXX commands
*
* @return
*/
default boolean isComputer() {
return !isHuman();
}
String getName(); String getName();
String getLogName(); String getLogName();
@ -314,8 +338,6 @@ public interface Player extends MageItem, Copyable<Player> {
void setGameUnderYourControl(boolean value, boolean fullRestore); void setGameUnderYourControl(boolean value, boolean fullRestore);
boolean isTestMode();
void setTestMode(boolean value); void setTestMode(boolean value);
void addAction(String action); void addAction(String action);

View file

@ -2715,7 +2715,7 @@ public abstract class PlayerImpl implements Player, Serializable {
} }
// only humans can use it // only humans can use it
if (!targetPlayer.isHuman() && !targetPlayer.isTestMode()) { if (targetPlayer.isComputer()) {
return false; return false;
} }
@ -3866,7 +3866,7 @@ public abstract class PlayerImpl implements Player, Serializable {
} }
@Override @Override
public boolean isTestMode() { public boolean isTestsMode() {
return isTestMode; return isTestMode;
} }