Search in sources :

Example 76 with ITestDelegateBridge

use of games.strategy.engine.data.ITestDelegateBridge in project triplea by triplea-game.

the class RevisedTest method testCanalMovePass.

@Test
public void testCanalMovePass() {
    final Territory sz15 = territory("15 Sea Zone", gameData);
    final Territory sz34 = territory("34 Sea Zone", gameData);
    final ITestDelegateBridge bridge = getDelegateBridge(british(gameData));
    bridge.setStepName("CombatMove");
    final MoveDelegate moveDelegate = moveDelegate(gameData);
    moveDelegate.setDelegateBridgeAndPlayer(bridge);
    moveDelegate.start();
    final String error = moveDelegate.move(sz15.getUnits().getUnits(), new Route(sz15, sz34));
    assertValid(error);
}
Also used : Territory(games.strategy.engine.data.Territory) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Example 77 with ITestDelegateBridge

use of games.strategy.engine.data.ITestDelegateBridge in project triplea by triplea-game.

the class RevisedTest method testAaOwnership.

@Test
public void testAaOwnership() {
    // Set up players
    // PlayerID british = GameDataTestUtil.british(gameData);
    final PlayerID japanese = GameDataTestUtil.japanese(gameData);
    // PlayerID americans = GameDataTestUtil.americans(gameData);
    // Set up the territories
    final Territory india = territory("India", gameData);
    final Territory fic = territory("French Indochina", gameData);
    final Territory china = territory("China", gameData);
    final Territory kwang = territory("Kwantung", gameData);
    // Preset units in FIC
    final UnitType infType = infantry(gameData);
    // UnitType aaType = GameDataTestUtil.aaGun(gameData);
    removeFrom(fic, fic.getUnits().getUnits());
    addTo(fic, aaGun(gameData).create(1, japanese));
    addTo(fic, infantry(gameData).create(1, japanese));
    assertEquals(2, fic.getUnits().getUnitCount());
    // Get attacking units
    final Collection<Unit> britishUnits = india.getUnits().getUnits(infType, 1);
    final Collection<Unit> japaneseUnits = kwang.getUnits().getUnits(infType, 1);
    final Collection<Unit> americanUnits = china.getUnits().getUnits(infType, 1);
    // Get Owner prior to battle
    assertTrue(fic.getUnits().allMatch(Matches.unitIsOwnedBy(japanese(gameData))));
    final String preOwner = fic.getOwner().getName();
    assertEquals(Constants.PLAYER_NAME_JAPANESE, preOwner);
    // Set up the move delegate
    ITestDelegateBridge delegateBridge = getDelegateBridge(british(gameData));
    final MoveDelegate moveDelegate = moveDelegate(gameData);
    delegateBridge.setStepName("CombatMove");
    moveDelegate.setDelegateBridgeAndPlayer(delegateBridge);
    moveDelegate.start();
    /*
     * add a VALID BRITISH attack
     */
    String validResults = moveDelegate.move(britishUnits, new Route(india, fic));
    assertValid(validResults);
    moveDelegate(gameData).end();
    // Set up battle
    MustFightBattle battle = (MustFightBattle) AbstractMoveDelegate.getBattleTracker(gameData).getPendingBattle(fic, false, null);
    delegateBridge.setRemote(dummyPlayer);
    // fight
    ScriptedRandomSource randomSource = new ScriptedRandomSource(0, 5);
    delegateBridge.setRandomSource(randomSource);
    battle.fight(delegateBridge);
    // Get Owner after to battle
    assertTrue(fic.getUnits().allMatch(Matches.unitIsOwnedBy(british(gameData))));
    final String postOwner = fic.getOwner().getName();
    assertEquals(Constants.PLAYER_NAME_BRITISH, postOwner);
    /*
     * add a VALID JAPANESE attack
     */
    // Set up battle
    delegateBridge = getDelegateBridge(japanese(gameData));
    delegateBridge.setStepName("CombatMove");
    moveDelegate.setDelegateBridgeAndPlayer(delegateBridge);
    moveDelegate.start();
    // Move to battle
    validResults = moveDelegate.move(japaneseUnits, new Route(kwang, fic));
    assertValid(validResults);
    moveDelegate(gameData).end();
    battle = (MustFightBattle) AbstractMoveDelegate.getBattleTracker(gameData).getPendingBattle(fic, false, null);
    delegateBridge.setRemote(dummyPlayer);
    // fight
    randomSource = new ScriptedRandomSource(0, 5);
    delegateBridge.setRandomSource(randomSource);
    battle.fight(delegateBridge);
    // Get Owner after to battle
    assertTrue(fic.getUnits().allMatch(Matches.unitIsOwnedBy(japanese(gameData))));
    final String midOwner = fic.getOwner().getName();
    assertEquals(Constants.PLAYER_NAME_JAPANESE, midOwner);
    /*
     * add a VALID AMERICAN attack
     */
    // Set up battle
    delegateBridge = getDelegateBridge(americans(gameData));
    delegateBridge.setStepName("CombatMove");
    moveDelegate.setDelegateBridgeAndPlayer(delegateBridge);
    moveDelegate.start();
    // Move to battle
    validResults = moveDelegate.move(americanUnits, new Route(china, fic));
    assertValid(validResults);
    moveDelegate(gameData).end();
    battle = (MustFightBattle) AbstractMoveDelegate.getBattleTracker(gameData).getPendingBattle(fic, false, null);
    delegateBridge.setRemote(dummyPlayer);
    // fight
    randomSource = new ScriptedRandomSource(0, 5);
    delegateBridge.setRandomSource(randomSource);
    battle.fight(delegateBridge);
    // Get Owner after to battle
    assertTrue(fic.getUnits().allMatch(Matches.unitIsOwnedBy(americans(gameData))));
    final String endOwner = fic.getOwner().getName();
    assertEquals(Constants.PLAYER_NAME_AMERICANS, endOwner);
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) UnitType(games.strategy.engine.data.UnitType) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Example 78 with ITestDelegateBridge

use of games.strategy.engine.data.ITestDelegateBridge in project triplea by triplea-game.

the class RevisedTest method testAttackSubsOnSubs.

@Test
public void testAttackSubsOnSubs() {
    final String defender = "Germans";
    final String attacker = "British";
    final Territory attacked = territory("31 Sea Zone", gameData);
    final Territory from = territory("32 Sea Zone", gameData);
    // 1 sub attacks 1 sub
    addTo(from, submarine(gameData).create(1, british(gameData)));
    addTo(attacked, submarine(gameData).create(1, germans(gameData)));
    final ITestDelegateBridge bridge = getDelegateBridge(british(gameData));
    bridge.setStepName("CombatMove");
    moveDelegate(gameData).setDelegateBridgeAndPlayer(bridge);
    moveDelegate(gameData).start();
    move(from.getUnits().getUnits(), new Route(from, attacked));
    moveDelegate(gameData).end();
    final MustFightBattle battle = (MustFightBattle) AbstractMoveDelegate.getBattleTracker(gameData).getPendingBattle(attacked, false, null);
    final List<String> steps = battle.determineStepStrings(true);
    assertEquals(Arrays.asList(attacker + SUBS_FIRE, defender + SELECT_SUB_CASUALTIES, defender + SUBS_FIRE, attacker + SELECT_SUB_CASUALTIES, REMOVE_SNEAK_ATTACK_CASUALTIES, REMOVE_CASUALTIES, attacker + SUBS_SUBMERGE, defender + SUBS_SUBMERGE, attacker + ATTACKER_WITHDRAW).toString(), steps.toString());
    final List<IExecutable> execs = battle.getBattleExecutables(false);
    final int attackSubs = getIndex(execs, MustFightBattle.AttackSubs.class);
    final int defendSubs = getIndex(execs, MustFightBattle.DefendSubs.class);
    assertTrue(attackSubs < defendSubs);
    bridge.setRemote(dummyPlayer);
    // fight, each sub should fire
    // and hit
    final ScriptedRandomSource randomSource = new ScriptedRandomSource(0, 0, ScriptedRandomSource.ERROR);
    bridge.setRandomSource(randomSource);
    battle.fight(bridge);
    assertEquals(2, randomSource.getTotalRolled());
    assertTrue(attacked.getUnits().isEmpty());
}
Also used : Territory(games.strategy.engine.data.Territory) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Example 79 with ITestDelegateBridge

use of games.strategy.engine.data.ITestDelegateBridge in project triplea by triplea-game.

the class RevisedTest method testRetreatBug.

@Test
public void testRetreatBug() {
    final PlayerID russians = GameDataTestUtil.russians(gameData);
    final PlayerID americans = GameDataTestUtil.americans(gameData);
    final ITestDelegateBridge bridge = getDelegateBridge(russians);
    // we need to initialize the original owner
    final InitializationDelegate initDel = (InitializationDelegate) gameData.getDelegateList().getDelegate("initDelegate");
    initDel.setDelegateBridgeAndPlayer(bridge);
    initDel.start();
    initDel.end();
    // make sinkian japanese owned, put one infantry in it
    final Territory sinkiang = gameData.getMap().getTerritory("Sinkiang");
    gameData.performChange(ChangeFactory.removeUnits(sinkiang, sinkiang.getUnits().getUnits()));
    final PlayerID japanese = GameDataTestUtil.japanese(gameData);
    sinkiang.setOwner(japanese);
    final UnitType infantryType = infantry(gameData);
    gameData.performChange(ChangeFactory.addUnits(sinkiang, infantryType.create(1, japanese)));
    // now move to attack it
    final MoveDelegate moveDelegate = (MoveDelegate) gameData.getDelegateList().getDelegate("move");
    bridge.setStepName("CombatMove");
    moveDelegate.setDelegateBridgeAndPlayer(bridge);
    moveDelegate.start();
    final Territory novo = gameData.getMap().getTerritory("Novosibirsk");
    moveDelegate.move(novo.getUnits().getUnits(), gameData.getMap().getRoute(novo, sinkiang));
    moveDelegate.end();
    final BattleDelegate battle = (BattleDelegate) gameData.getDelegateList().getDelegate("battle");
    battle.setDelegateBridgeAndPlayer(bridge);
    bridge.setRandomSource(new ScriptedRandomSource(new int[] { 0, 0, 0 }));
    bridge.setRemote(dummyPlayer);
    // fights battle
    battle.start();
    battle.end();
    assertEquals(sinkiang.getOwner(), americans);
    assertTrue(battle.getBattleTracker().wasConquered(sinkiang));
    bridge.setStepName("NonCombatMove");
    moveDelegate.setDelegateBridgeAndPlayer(bridge);
    moveDelegate.start();
    final Territory russia = gameData.getMap().getTerritory("Russia");
    // move two tanks from russia, then undo
    final Route r = new Route();
    r.setStart(russia);
    r.add(novo);
    r.add(sinkiang);
    assertNull(moveDelegate.move(russia.getUnits().getMatches(Matches.unitCanBlitz()), r));
    moveDelegate.undoMove(0);
    assertTrue(battle.getBattleTracker().wasConquered(sinkiang));
    // now move the planes into the territory
    assertNull(moveDelegate.move(russia.getUnits().getMatches(Matches.unitIsAir()), r));
    // make sure they can't land, they can't because the territory was conquered
    assertEquals(1, moveDelegate.getTerritoriesWhereAirCantLand().size());
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) UnitType(games.strategy.engine.data.UnitType) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Example 80 with ITestDelegateBridge

use of games.strategy.engine.data.ITestDelegateBridge in project triplea by triplea-game.

the class RevisedTest method testContinuedBattles.

@Test
public void testContinuedBattles() {
    final PlayerID russians = GameDataTestUtil.russians(gameData);
    final PlayerID germans = germans(gameData);
    final ITestDelegateBridge bridge = getDelegateBridge(germans);
    final MoveDelegate moveDelegate = (MoveDelegate) gameData.getDelegateList().getDelegate("move");
    bridge.setStepName("CombatMove");
    moveDelegate.setDelegateBridgeAndPlayer(bridge);
    moveDelegate.start();
    // set up battle
    final Territory germany = gameData.getMap().getTerritory("Germany");
    final Territory karelia = gameData.getMap().getTerritory("Karelia S.S.R.");
    final Territory sz5 = gameData.getMap().getTerritory("5 Sea Zone");
    gameData.performChange(ChangeFactory.removeUnits(sz5, sz5.getUnits().getUnits()));
    final UnitType infantryType = infantry(gameData);
    final UnitType subType = submarine(gameData);
    final UnitType trnType = transport(gameData);
    gameData.performChange(ChangeFactory.addUnits(sz5, subType.create(1, germans)));
    gameData.performChange(ChangeFactory.addUnits(sz5, trnType.create(1, germans)));
    gameData.performChange(ChangeFactory.addUnits(sz5, subType.create(1, russians)));
    // submerge the russian sub
    final TripleAUnit sub = (TripleAUnit) CollectionUtils.getMatches(sz5.getUnits().getUnits(), Matches.unitIsOwnedBy(russians)).iterator().next();
    sub.setSubmerged(true);
    // now move an infantry through the sz
    String results = moveDelegate.move(CollectionUtils.getNMatches(germany.getUnits().getUnits(), 1, Matches.unitIsOfType(infantryType)), gameData.getMap().getRoute(germany, sz5), CollectionUtils.getMatches(sz5.getUnits().getUnits(), Matches.unitIsOfType(trnType)));
    assertNull(results);
    results = moveDelegate.move(CollectionUtils.getNMatches(sz5.getUnits().getUnits(), 1, Matches.unitIsOfType(infantryType)), gameData.getMap().getRoute(sz5, karelia));
    assertNull(results);
    moveDelegate.end();
    final BattleDelegate battle = (BattleDelegate) gameData.getDelegateList().getDelegate("battle");
    battle.setDelegateBridgeAndPlayer(bridge);
    battle.start();
    final BattleTracker tracker = AbstractMoveDelegate.getBattleTracker(gameData);
    // The battle should NOT be empty
    assertTrue(tracker.hasPendingBattle(sz5, false));
    assertFalse(tracker.getPendingBattle(sz5, false, null).isEmpty());
    battle.end();
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) UnitType(games.strategy.engine.data.UnitType) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) TripleAUnit(games.strategy.triplea.TripleAUnit) Test(org.junit.jupiter.api.Test)

Aggregations

ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)128 Test (org.junit.jupiter.api.Test)127 Territory (games.strategy.engine.data.Territory)119 PlayerID (games.strategy.engine.data.PlayerID)94 Route (games.strategy.engine.data.Route)80 TripleAUnit (games.strategy.triplea.TripleAUnit)60 Unit (games.strategy.engine.data.Unit)58 ScriptedRandomSource (games.strategy.engine.random.ScriptedRandomSource)56 UnitType (games.strategy.engine.data.UnitType)40 Change (games.strategy.engine.data.Change)12 ArrayList (java.util.ArrayList)11 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)8 GameData (games.strategy.engine.data.GameData)5 GameDataTestUtil.bidPlaceDelegate (games.strategy.triplea.delegate.GameDataTestUtil.bidPlaceDelegate)5 ITripleAPlayer (games.strategy.triplea.player.ITripleAPlayer)5 TestMapGameData (games.strategy.triplea.xml.TestMapGameData)5 IntegerMap (games.strategy.util.IntegerMap)5 List (java.util.List)5 TerritoryEffect (games.strategy.engine.data.TerritoryEffect)4 ChangeFactory (games.strategy.engine.data.changefactory.ChangeFactory)4