mirror of
https://github.com/magefree/mage.git
synced 2026-01-26 21:29:17 -08:00
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:
parent
00c7b3753c
commit
2906f86324
22 changed files with 106 additions and 47 deletions
|
|
@ -86,8 +86,14 @@ public class TestPlayer implements Player {
|
|||
|
||||
private int maxCallsWithoutAction = 400;
|
||||
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 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
|
||||
|
|
@ -125,7 +131,7 @@ public class TestPlayer implements Player {
|
|||
|
||||
public TestPlayer(final TestPlayer testPlayer) {
|
||||
this.AIPlayer = testPlayer.AIPlayer;
|
||||
this.AICanChooseInStrictMode = testPlayer.AICanChooseInStrictMode;
|
||||
this.AIRealGameSimulation = testPlayer.AIRealGameSimulation;
|
||||
this.foundNoAction = testPlayer.foundNoAction;
|
||||
this.actions.addAll(testPlayer.actions);
|
||||
this.choices.addAll(testPlayer.choices);
|
||||
|
|
@ -720,7 +726,7 @@ public class TestPlayer implements Player {
|
|||
|
||||
// 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);
|
||||
actions.remove(action);
|
||||
return true;
|
||||
|
|
@ -728,7 +734,7 @@ public class TestPlayer implements Player {
|
|||
|
||||
// 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());
|
||||
computerPlayer.priority(game);
|
||||
return true;
|
||||
|
|
@ -1897,7 +1903,7 @@ public class TestPlayer implements Player {
|
|||
}
|
||||
|
||||
private void chooseStrictModeFailed(String choiceType, Game game, String reason, boolean printAbilities) {
|
||||
if (strictChooseMode && !AICanChooseInStrictMode) {
|
||||
if (strictChooseMode && !AIRealGameSimulation) {
|
||||
if (printAbilities) {
|
||||
printStart("Available mana for " + computerPlayer.getName());
|
||||
printMana(game, computerPlayer.getManaAvailable(game));
|
||||
|
|
@ -3060,7 +3066,18 @@ public class TestPlayer implements Player {
|
|||
|
||||
@Override
|
||||
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
|
||||
|
|
@ -3449,8 +3466,8 @@ public class TestPlayer implements Player {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean isTestMode() {
|
||||
return computerPlayer.isTestMode();
|
||||
public boolean isTestsMode() {
|
||||
return computerPlayer.isTestsMode();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -4193,8 +4210,8 @@ public class TestPlayer implements Player {
|
|||
return computerPlayer;
|
||||
}
|
||||
|
||||
public void setAICanChooseInStrictMode(boolean AICanChooseInStrictMode) {
|
||||
this.AICanChooseInStrictMode = AICanChooseInStrictMode;
|
||||
public void setAIRealGameSimulation(boolean AIRealGameSimulation) {
|
||||
this.AIRealGameSimulation = AIRealGameSimulation;
|
||||
}
|
||||
|
||||
public Map<Integer, HashMap<UUID, ArrayList<org.mage.test.player.PlayerAction>>> getRollbackActions() {
|
||||
|
|
|
|||
|
|
@ -13,6 +13,10 @@ import org.mage.test.serverside.base.impl.CardTestPlayerAPIImpl;
|
|||
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
|
||||
*/
|
||||
public abstract class CardTestPlayerBaseAI extends CardTestPlayerAPIImpl {
|
||||
|
|
@ -33,6 +37,7 @@ public abstract class CardTestPlayerBaseAI extends CardTestPlayerAPIImpl {
|
|||
if (name.equals("PlayerA")) {
|
||||
TestPlayer testPlayer = new TestPlayer(new TestComputerPlayer7("PlayerA", RangeOfInfluence.ONE, skill));
|
||||
testPlayer.setAIPlayer(true);
|
||||
testPlayer.setAIRealGameSimulation(true); // enable AI logic simulation for all turns by default
|
||||
return testPlayer;
|
||||
}
|
||||
return super.createPlayer(name, rangeOfInfluence);
|
||||
|
|
|
|||
|
|
@ -1571,9 +1571,9 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement
|
|||
}
|
||||
|
||||
/**
|
||||
* AI play one PRIORITY with multi game simulations (calcs and play ONE best
|
||||
* action, can be called with stack) All choices must be made by AI
|
||||
* (e.g.strict mode possible)
|
||||
* AI play one PRIORITY with multi game simulations like real game
|
||||
* (calcs and play ONE best action, can be called with stack)
|
||||
* All choices must be made by AI (e.g.strict mode possible)
|
||||
*
|
||||
* @param turnNum
|
||||
* @param step
|
||||
|
|
@ -1595,11 +1595,11 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement
|
|||
}
|
||||
|
||||
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) {
|
||||
@Override
|
||||
public void onActionRemovedLater(Game game, TestPlayer player) {
|
||||
player.setAICanChooseInStrictMode(false);
|
||||
player.setAIRealGameSimulation(false);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -477,7 +477,7 @@ public class PlayerStub implements Player {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean isTestMode() {
|
||||
public boolean isTestsMode() {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue