Search in sources :

Example 21 with ScriptedRandomSource

use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.

the class RevisedTest method testStratBombCasualties.

@Test
public void testStratBombCasualties() {
    final Territory germany = gameData.getMap().getTerritory("Germany");
    final Territory uk = gameData.getMap().getTerritory("United Kingdom");
    final PlayerID germans = GameDataTestUtil.germans(gameData);
    final PlayerID british = GameDataTestUtil.british(gameData);
    final BattleTracker tracker = new BattleTracker();
    final StrategicBombingRaidBattle battle = new StrategicBombingRaidBattle(germany, gameData, british, tracker);
    final List<Unit> bombers = uk.getUnits().getMatches(Matches.unitIsStrategicBomber());
    addTo(germany, bombers);
    battle.addAttackChange(gameData.getMap().getRoute(uk, germany), bombers, null);
    tracker.getBattleRecords().addBattle(british, battle.getBattleId(), germany, battle.getBattleType());
    final ITestDelegateBridge bridge = getDelegateBridge(british);
    bridge.setRemote(dummyPlayer);
    // aa guns rolls 0 and hits
    bridge.setRandomSource(new ScriptedRandomSource(new int[] { 0, ScriptedRandomSource.ERROR }));
    final int pusBeforeRaid = germans.getResources().getQuantity(gameData.getResourceList().getResource(Constants.PUS));
    battle.fight(bridge);
    final int pusAfterRaid = germans.getResources().getQuantity(gameData.getResourceList().getResource(Constants.PUS));
    assertEquals(pusBeforeRaid, pusAfterRaid);
    assertEquals(0, germany.getUnits().getMatches(Matches.unitIsOwnedBy(british)).size());
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) Test(org.junit.jupiter.api.Test)

Example 22 with ScriptedRandomSource

use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.

the class RevisedTest method testAttackDestroyerAndSubsAgainstSubAndDestroyer.

@Test
public void testAttackDestroyerAndSubsAgainstSubAndDestroyer() {
    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 and 1 destroyer attack 1 sub and 1 destroyer
    // defender sneak attacks, not attacker
    addTo(from, submarine(gameData).create(1, british(gameData)));
    addTo(from, destroyer(gameData).create(1, british(gameData)));
    addTo(attacked, submarine(gameData).create(1, germans(gameData)));
    addTo(attacked, destroyer(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, attacker + FIRE, defender + SELECT_CASUALTIES, defender + FIRE, attacker + SELECT_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);
    when(dummyPlayer.selectCasualties(any(), any(), anyInt(), any(), any(), any(), any(), any(), any(), anyBoolean(), any(), any(), any(), any(), anyBoolean())).thenAnswer(invocation -> {
        final Collection<Unit> selectFrom = invocation.getArgument(0);
        return new CasualtyDetails(Collections.singletonList(selectFrom.iterator().next()), new ArrayList<>(), false);
    });
    bridge.setRemote(dummyPlayer);
    final ScriptedRandomSource randomSource = new ScriptedRandomSource(0, 0, 0, 0, ScriptedRandomSource.ERROR);
    bridge.setRandomSource(randomSource);
    battle.fight(bridge);
    assertEquals(4, randomSource.getTotalRolled());
    assertEquals(0, attacked.getUnits().size());
}
Also used : Territory(games.strategy.engine.data.Territory) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) CasualtyDetails(games.strategy.triplea.delegate.dataObjects.CasualtyDetails) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Example 23 with ScriptedRandomSource

use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.

the class RevisedTest method testStratBombCasualtiesLowLuck.

@Test
public void testStratBombCasualtiesLowLuck() {
    makeGameLowLuck(gameData);
    final Territory germany = gameData.getMap().getTerritory("Germany");
    final Territory uk = gameData.getMap().getTerritory("United Kingdom");
    final PlayerID germans = GameDataTestUtil.germans(gameData);
    final PlayerID british = GameDataTestUtil.british(gameData);
    final BattleTracker tracker = new BattleTracker();
    final StrategicBombingRaidBattle battle = new StrategicBombingRaidBattle(germany, gameData, british, tracker);
    final List<Unit> bombers = bomber(gameData).create(2, british);
    addTo(germany, bombers);
    battle.addAttackChange(gameData.getMap().getRoute(uk, germany), bombers, null);
    tracker.getBattleRecords().addBattle(british, battle.getBattleId(), germany, battle.getBattleType());
    final ITestDelegateBridge bridge = getDelegateBridge(british);
    bridge.setRemote(dummyPlayer);
    // should be exactly 3 rolls total. would be exactly 2 rolls if the number of units being shot at = max dice side of
    // the AA gun, because
    // the casualty selection roll would not happen in LL
    // first 0 is the AA gun rolling 1@2 and getting a 1, which is a hit
    // second 0 is the LL AA casualty selection randomly picking the first unit to die
    // third 0 is the single remaining bomber dealing 1 damage to the enemy's PUs
    bridge.setRandomSource(new ScriptedRandomSource(new int[] { 0, 0, 0, ScriptedRandomSource.ERROR }));
    final int pusBeforeRaid = germans.getResources().getQuantity(gameData.getResourceList().getResource(Constants.PUS));
    battle.fight(bridge);
    final int pusAfterRaid = germans.getResources().getQuantity(gameData.getResourceList().getResource(Constants.PUS));
    assertEquals(pusBeforeRaid - 1, pusAfterRaid);
    assertEquals(1, germany.getUnits().getMatches(Matches.unitIsOwnedBy(british)).size());
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) Test(org.junit.jupiter.api.Test)

Example 24 with ScriptedRandomSource

use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.

the class RevisedTest method testAttackSubsOnDestroyer.

@Test
public void testAttackSubsOnDestroyer() {
    final String defender = "Germans";
    final String attacker = "British";
    final Territory attacked = territory("31 Sea Zone", gameData);
    final Territory from = territory("32 Sea Zone", gameData);
    // 2 sub attacks 1 sub and 1 destroyer
    addTo(from, submarine(gameData).create(2, british(gameData)));
    addTo(attacked, submarine(gameData).create(1, germans(gameData)));
    addTo(attacked, destroyer(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);
    /*
     * Here are the exact errata clarifications on how REVISED rules subs work:
     * Every sub, regardless of whether it is on the attacking or defending side, fires in the Opening Fire step of
     * combat. That is the only
     * time a sub ever fires.
     * Losses caused by attacking or defending subs are removed at the end of the Opening Fire step, before normal
     * attack and defense rolls,
     * unless the enemy has a destroyer present.
     * If the enemy (attacker or defender) has a destroyer, then hits caused by your subs are not removed until the
     * Remove Casualties step
     * (step 6) of combat.
     * In other words, subs work exactly the same for the attacker and the defender. Nothing, not even a destroyer, ever
     * stops a sub from
     * rolling its die (attack or defense) in the Opening Fire step.
     * What a destroyer does do is let you keep your units that were sunk by enemy subs on the battle board until step
     * 6, allowing them to
     * fire back before going to the scrap heap.
     */
    assertEquals(Arrays.asList(attacker + SUBS_FIRE, defender + SELECT_SUB_CASUALTIES, defender + SUBS_FIRE, attacker + SELECT_SUB_CASUALTIES, REMOVE_SNEAK_ATTACK_CASUALTIES, defender + FIRE, attacker + SELECT_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);
    // attacking subs fires, defending destroyer and sub still gets to fire
    // attacking subs still gets to fire even if defending sub hits
    final ScriptedRandomSource randomSource = new ScriptedRandomSource(0, 2, 0, 0, ScriptedRandomSource.ERROR);
    bridge.setRandomSource(randomSource);
    battle.fight(bridge);
    assertEquals(4, randomSource.getTotalRolled());
    assertTrue(attacked.getUnits().getMatches(Matches.unitIsOwnedBy(british(gameData))).isEmpty());
    assertEquals(1, attacked.getUnits().size());
}
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 25 with ScriptedRandomSource

use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.

the class RevisedTest method testMultipleOverFlyBombersDies.

@Test
public void testMultipleOverFlyBombersDies() {
    final PlayerID british = british(gameData);
    final MoveDelegate moveDelegate = (MoveDelegate) gameData.getDelegateList().getDelegate("move");
    final ITestDelegateBridge bridge = getDelegateBridge(british);
    bridge.setStepName("CombatMove");
    moveDelegate.setDelegateBridgeAndPlayer(bridge);
    moveDelegate.start();
    when(dummyPlayer.confirmMoveInFaceOfAa(any())).thenReturn(true);
    bridge.setRemote(dummyPlayer);
    bridge.setRandomSource(new ScriptedRandomSource(0, 4));
    final Territory uk = territory("United Kingdom", gameData);
    final Territory sz7 = territory("7 Sea Zone", gameData);
    final Territory we = territory("Western Europe", gameData);
    final Territory se = territory("Southern Europe", gameData);
    final Territory balk = territory("Balkans", gameData);
    addTo(uk, bomber(gameData).create(1, british));
    final Route route = new Route(uk, sz7, we, se, balk);
    move(uk.getUnits().getMatches(Matches.unitIsStrategicBomber()), route);
    // the aa gun should have fired (one hit, one miss in each territory overflown). the bombers no longer exists
    assertTrue(uk.getUnits().getMatches(Matches.unitIsStrategicBomber()).isEmpty());
    assertTrue(we.getUnits().getMatches(Matches.unitIsStrategicBomber()).isEmpty());
    assertTrue(se.getUnits().getMatches(Matches.unitIsStrategicBomber()).isEmpty());
    assertTrue(balk.getUnits().getMatches(Matches.unitIsStrategicBomber()).isEmpty());
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) 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)

Aggregations

ScriptedRandomSource (games.strategy.engine.random.ScriptedRandomSource)72 Test (org.junit.jupiter.api.Test)71 ITestDelegateBridge (games.strategy.engine.data.ITestDelegateBridge)55 Territory (games.strategy.engine.data.Territory)50 Unit (games.strategy.engine.data.Unit)48 PlayerID (games.strategy.engine.data.PlayerID)45 TripleAUnit (games.strategy.triplea.TripleAUnit)41 Route (games.strategy.engine.data.Route)28 UnitType (games.strategy.engine.data.UnitType)26 GameData (games.strategy.engine.data.GameData)13 TestMapGameData (games.strategy.triplea.xml.TestMapGameData)13 ArrayList (java.util.ArrayList)9 List (java.util.List)6 TerritoryEffect (games.strategy.engine.data.TerritoryEffect)5 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)5 CasualtyDetails (games.strategy.triplea.delegate.dataObjects.CasualtyDetails)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 IntegerMap (games.strategy.util.IntegerMap)4 Collection (java.util.Collection)4 Change (games.strategy.engine.data.Change)3