Skip to content

Commit

Permalink
Fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tehdiplomat committed Mar 23, 2024
1 parent 628e87b commit 34709f5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 39 deletions.
25 changes: 16 additions & 9 deletions forge-ai/src/main/java/forge/ai/simulation/GameCopier.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public Game makeCopy(PhaseType advanceToPhase, Player aiPlayer) {

for (int i = 0; i < origGame.getPlayers().size(); i++) {
Player origPlayer = origGame.getPlayers().get(i);
Player newPlayer = newGame.getPlayers().get(i);
Player newPlayer = newGame.getPlayer(origPlayer.getId());
newPlayer.setLife(origPlayer.getLife(), null);
newPlayer.setLifeLostLastTurn(origPlayer.getLifeLostLastTurn());
newPlayer.setLifeLostThisTurn(origPlayer.getLifeLostThisTurn());
Expand Down Expand Up @@ -235,6 +235,7 @@ private RegisteredPlayer clonePlayer(RegisteredPlayer p) {
}

private void copyGameState(Game newGame, Player aiPlayer) {
newGame.EXPERIMENTAL_RESTORE_SNAPSHOT = origGame.EXPERIMENTAL_RESTORE_SNAPSHOT;
newGame.setAge(origGame.getAge());

// TODO countersAddedThisTurn
Expand Down Expand Up @@ -490,16 +491,22 @@ public GameObject find(GameObject o) {
return snapshot.find(o);
}

GameObject result = cardMap.get(o);
if (result != null)
return result;
// TODO: Have only one GameObject map?
result = playerMap.get(o);
if (result != null)
return result;
GameObject result = null;
if (o instanceof Card) {
result = cardMap.get(o);
if (result != null) {
return result;
} else {
System.out.println("Couldn't map " + o + "/" + System.identityHashCode(o));
}
} else if (o instanceof Player) {
result = playerMap.get(o);
if (result != null)
return result;
}
if (o != null)
throw new RuntimeException("Couldn't map " + o + "/" + System.identityHashCode(o));
return null;
return result;
}
public GameObject reverseFind(GameObject o) {
if (origGame.EXPERIMENTAL_RESTORE_SNAPSHOT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,7 @@ public void testActivateAbilityTriggers() {
AssertJUnit.assertEquals(1, heraldCopy.getToughnessBonusFromCounters());
AssertJUnit.assertEquals(1, heraldCopy.getPowerBonusFromCounters());

// I Don't think the trigger has resolved at this point?

Card warriorToken = findCardWithName(simGame, "Warrior Token");
AssertJUnit.assertNull(warriorToken);


warriorToken = findCardWithName(simGame, "Warrior Token");
AssertJUnit.assertNotNull(warriorToken);
AssertJUnit.assertTrue(warriorToken.isSick());
AssertJUnit.assertEquals(1, warriorToken.getCurrentPower());
Expand Down Expand Up @@ -160,6 +154,7 @@ public void testEnchantedAbilities() {
@Test
public void testEtbTriggers() {
Game game = initAndCreateGame();
Player p0 = game.getPlayers().get(0);
Player p = game.getPlayers().get(1);
addCard("Black Knight", p);
addCards("Swamp", 5, p);
Expand All @@ -177,13 +172,17 @@ public void testEtbTriggers() {
int score = sim.simulateSpellAbility(playMerchantSa).value;
AssertJUnit.assertTrue(String.format("score=%d vs. origScore=%d", score, origScore), score > origScore);
Game simGame = sim.getSimulatedGameState();
AssertJUnit.assertEquals(24, simGame.getPlayers().get(1).getLife());
AssertJUnit.assertEquals(16, simGame.getPlayers().get(0).getLife());

Player simP0 = simGame.getPlayer(p0.getId());
Player simP = simGame.getPlayer(p.getId());
AssertJUnit.assertEquals(24, simP.getLife());
AssertJUnit.assertEquals(16, simP0.getLife());
}

@Test
public void testSimulateUnmorph() {
Game game = initAndCreateGame();
Player p0 = game.getPlayers().get(0);
Player p = game.getPlayers().get(1);
Card ripper = createCard("Ruthless Ripper", p);
ripper.turnFaceDownNoUpdate();
Expand All @@ -200,7 +199,9 @@ public void testSimulateUnmorph() {
SpellAbility unmorphSA = findSAWithPrefix(ripper, "Morph — Reveal a black card");
AssertJUnit.assertNotNull(unmorphSA);
sim.simulateSpellAbility(unmorphSA);
AssertJUnit.assertEquals(18, simGame.getPlayers().get(0).getLife());

Player simP0 = simGame.getPlayer(p0.getId());
AssertJUnit.assertEquals(18, simP0.getLife());
}

@Test
Expand All @@ -222,8 +223,10 @@ public void testFindingOwnCard() {
AssertJUnit.assertNotNull(fractureSa);
fractureSa.getTargets().add(p0);
sim.simulateSpellAbility(fractureSa);
AssertJUnit.assertEquals(1, simGame.getPlayers().get(0).getCardsIn(ZoneType.Hand).size());
AssertJUnit.assertEquals(0, simGame.getPlayers().get(1).getCardsIn(ZoneType.Hand).size());
Player simP0 = simGame.getPlayer(p0.getId());
Player simP1 = simGame.getPlayer(p1.getId());
AssertJUnit.assertEquals(1, simP0.getCardsIn(ZoneType.Hand).size());
AssertJUnit.assertEquals(0, simP1.getCardsIn(ZoneType.Hand).size());
}

@Test
Expand All @@ -249,7 +252,7 @@ public void testPlaneswalkerAbilities() {
Card vampireToken = findCardWithName(simGame, "Vampire Token");
AssertJUnit.assertNotNull(vampireToken);

Player simP = simGame.getPlayers().get(1);
Player simP = simGame.getPlayer(p.getId());
cards = ComputerUtilAbility.getAvailableCards(simGame, simP);
abilities = ComputerUtilAbility.getSpellAbilities(cards, simP);
SpellAbility minusTwoSim = findSAWithPrefix(abilities, "-2: Create a 2/2 black Vampire");
Expand All @@ -260,7 +263,7 @@ public void testPlaneswalkerAbilities() {

GameCopier copier = new GameCopier(simGame);
Game copy = copier.makeCopy();
Player copyP = copy.getPlayers().get(1);
Player copyP = copy.getPlayer(p.getId());
cards = ComputerUtilAbility.getAvailableCards(copy, copyP);
abilities = ComputerUtilAbility.getSpellAbilities(cards, copyP);
SpellAbility minusTwoCopy = findSAWithPrefix(abilities, "-2: Create a 2/2 black Vampire");
Expand Down Expand Up @@ -1037,44 +1040,46 @@ public void testEnergy() {
GameSimulator sim = createSimulator(game, p);
sim.simulateSpellAbility(playTurtle);
Game simGame = sim.getSimulatedGameState();
Player simP = simGame.getPlayers().get(1);
Player simP = simGame.getPlayer(p.getId());
AssertJUnit.assertEquals(2, simP.getCounters(CounterEnumType.ENERGY));

GameCopier copier = new GameCopier(simGame);
Game copy = copier.makeCopy();
Player copyP = copy.getPlayers().get(1);
Player copyP = copy.getPlayer(p.getId());
AssertJUnit.assertEquals(2, copyP.getCounters(CounterEnumType.ENERGY));
}

@Test
public void testFloatingMana() {
Game game = initAndCreateGame();
Player p = game.getPlayers().get(1);
addCard("Swamp", p);
Card darkRitualCard = addCardToZone("Dark Ritual", p, ZoneType.Hand);
Card darkConfidantCard = addCardToZone("Dark Confidant", p, ZoneType.Hand);
Card deathriteCard = addCardToZone("Deathrite Shaman", p, ZoneType.Hand);
Player p0 = game.getPlayers().get(0);
Player p1 = game.getPlayers().get(1);
addCard("Swamp", p1);
Card darkRitualCard = addCardToZone("Dark Ritual", p1, ZoneType.Hand);
Card darkConfidantCard = addCardToZone("Dark Confidant", p1, ZoneType.Hand);
Card deathriteCard = addCardToZone("Deathrite Shaman", p1, ZoneType.Hand);

game.getPhaseHandler().devModeSet(PhaseType.MAIN2, p);
game.getPhaseHandler().devModeSet(PhaseType.MAIN2, p1);
game.getAction().checkStateEffects(true);
AssertJUnit.assertTrue(p.getManaPool().isEmpty());
AssertJUnit.assertTrue(p1.getManaPool().isEmpty());

SpellAbility playRitual = darkRitualCard.getSpellAbilities().get(0);
GameSimulator sim = createSimulator(game, p);
GameSimulator sim = createSimulator(game, p1);
sim.simulateSpellAbility(playRitual);
Game simGame = sim.getSimulatedGameState();
Player simP = simGame.getPlayers().get(1);
AssertJUnit.assertEquals(3, simP.getManaPool().totalMana());
AssertJUnit.assertEquals(3, simP.getManaPool().getAmountOfColor(MagicColor.BLACK));

Player simP1 = simGame.getPlayer(p1.getId());
AssertJUnit.assertEquals(3, simP1.getManaPool().totalMana());
AssertJUnit.assertEquals(3, simP1.getManaPool().getAmountOfColor(MagicColor.BLACK));

Card darkConfidantCard2 = (Card) sim.getGameCopier().find(darkConfidantCard);
SpellAbility playDarkConfidant2 = darkConfidantCard2.getSpellAbilities().get(0);
Card deathriteCard2 = (Card) sim.getGameCopier().find(deathriteCard);

GameSimulator sim2 = createSimulator(simGame, simP);
GameSimulator sim2 = createSimulator(simGame, simP1);
sim2.simulateSpellAbility(playDarkConfidant2);
Game sim2Game = sim2.getSimulatedGameState();
Player sim2P = sim2Game.getPlayers().get(1);
Player sim2P = sim2Game.getPlayer(simP1.getId());
AssertJUnit.assertEquals(1, sim2P.getManaPool().totalMana());
AssertJUnit.assertEquals(1, sim2P.getManaPool().getAmountOfColor(MagicColor.BLACK));

Expand All @@ -1084,7 +1089,7 @@ public void testFloatingMana() {
GameSimulator sim3 = createSimulator(sim2Game, sim2P);
sim3.simulateSpellAbility(playDeathriteCard3);
Game sim3Game = sim3.getSimulatedGameState();
Player sim3P = sim3Game.getPlayers().get(1);
Player sim3P = sim3Game.getPlayer(sim2P.getId());
AssertJUnit.assertEquals(0, sim3P.getManaPool().totalMana());
AssertJUnit.assertEquals(0, sim3P.getManaPool().getAmountOfColor(MagicColor.BLACK));
}
Expand Down Expand Up @@ -2302,7 +2307,8 @@ public void testAlphaBrawl() {

// 2 times 7 damage with life gain = 14 + 20 = 34 (damage to Stormwild Capridor
// is prevented)
AssertJUnit.assertEquals(34, simGame.getPlayers().get(0).getLife());
Player simplayer1 = simGame.getPlayer(p1.getId());
AssertJUnit.assertEquals(34, simplayer1.getLife());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ public void testLandSearchForCombo() {
// Next, expected to use Thespian's Stage to copy Dark Depths.
Plan.Decision d2 = picker.getPlan().getDecisions().get(1);
String expected = "{2}, {T}: Thespian's Stage becomes a copy of target land, except it has this ability.";

AssertJUnit.assertNotNull(d2.saRef);
AssertJUnit.assertEquals(expected, d2.saRef.toString());
AssertJUnit.assertTrue(d2.targets.toString().contains("Dark Depths"));
}
Expand Down Expand Up @@ -623,6 +625,12 @@ public void testPlayingRemovalBeforeBlocks() {

SpellAbilityPicker picker = new SpellAbilityPicker(game, p);
SpellAbility sa = picker.chooseSpellAbilityToPlay(null);
AssertJUnit.assertNull(sa);
// Decided to wait til after declare blockers to play removal

game.getPhaseHandler().devModeSet(PhaseType.COMBAT_DECLARE_BLOCKERS, p);

sa = picker.chooseSpellAbilityToPlay(null);
AssertJUnit.assertNotNull(sa);
AssertJUnit.assertEquals("Destroy target nonblack creature.", sa.toString());
AssertJUnit.assertEquals(blocker, sa.getTargetCard());
Expand Down

0 comments on commit 34709f5

Please sign in to comment.