Search in sources :

Example 26 with ScriptedRandomSource

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

the class RevisedTest method testAttackSubsAndDestroyerOnBatleshipAndSubs.

@Test
public void testAttackSubsAndDestroyerOnBatleshipAndSubs() {
    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 BB (two hp) attacks 3 subs and 1 destroyer
    addTo(from, submarine(gameData).create(3, british(gameData)));
    addTo(from, destroyer(gameData).create(1, british(gameData)));
    addTo(attacked, submarine(gameData).create(1, germans(gameData)));
    addTo(attacked, battleship(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, 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);
    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
    // battleship will not get to fire since it is killed by defending sub's sneak attack
    final ScriptedRandomSource randomSource = new ScriptedRandomSource(0, 0, 0, 0, ScriptedRandomSource.ERROR);
    bridge.setRandomSource(randomSource);
    battle.fight(bridge);
    assertEquals(4, randomSource.getTotalRolled());
    assertTrue(attacked.getUnits().getMatches(Matches.unitIsOwnedBy(germans(gameData))).isEmpty());
    assertEquals(3, 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 27 with ScriptedRandomSource

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

the class RevisedTest method testStratBombCasualtiesLowLuckManyBombers.

@Test
public void testStratBombCasualtiesLowLuckManyBombers() {
    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(7, 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);
    // aa guns rolls 0 and hits, next 5 dice are for the bombing raid cost for the
    // surviving bombers
    bridge.setRandomSource(new ScriptedRandomSource(new int[] { 0, 0, 0, 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 - 5, pusAfterRaid);
    // 2 bombers get hit
    assertEquals(5, 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 28 with ScriptedRandomSource

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

the class RevisedTest method testAttackDestroyerAndSubsAgainstSub.

@Test
public void testAttackDestroyerAndSubsAgainstSub() {
    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
    // 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)));
    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, attacker + FIRE, defender + 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 sub hits with sneak attack, but defending sub gets to return fire because it is a sub and this is
    // revised rules
    final ScriptedRandomSource randomSource = new ScriptedRandomSource(0, 0, ScriptedRandomSource.ERROR);
    bridge.setRandomSource(randomSource);
    battle.fight(bridge);
    assertEquals(2, randomSource.getTotalRolled());
    assertTrue(attacked.getUnits().getMatches(Matches.unitIsOwnedBy(germans(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 29 with ScriptedRandomSource

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

the class DiceRollTest method testAaLowLuckDifferentMovement.

@Test
public void testAaLowLuckDifferentMovement() {
    GameDataTestUtil.makeGameLowLuck(gameData);
    final Territory westRussia = gameData.getMap().getTerritory("West Russia");
    final PlayerID russians = GameDataTestUtil.russians(gameData);
    final PlayerID germans = GameDataTestUtil.germans(gameData);
    final UnitType aaGunType = GameDataTestUtil.aaGun(gameData);
    final List<Unit> aaGunList = aaGunType.create(1, germans);
    GameDataTestUtil.addTo(westRussia, aaGunList);
    final UnitType fighterType = GameDataTestUtil.fighter(gameData);
    final List<Unit> fighterList = fighterType.create(6, russians);
    TripleAUnit.get(fighterList.get(0)).setAlreadyMoved(1);
    final ITestDelegateBridge bridge = getDelegateBridge(russians);
    // aa hits at 0 (0 based)
    bridge.setRandomSource(new ScriptedRandomSource(new int[] { ScriptedRandomSource.ERROR }));
    final DiceRoll hit = DiceRoll.rollAa(CollectionUtils.getMatches(fighterList, Matches.unitIsOfTypes(UnitAttachment.get(aaGunList.iterator().next().getType()).getTargetsAa(gameData))), aaGunList, bridge, westRussia, true);
    assertThat(hit.getHits(), is(1));
}
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) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit) Test(org.junit.jupiter.api.Test)

Example 30 with ScriptedRandomSource

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

the class DiceRollTest method testSimple.

@Test
public void testSimple() {
    final Territory westRussia = gameData.getMap().getTerritory("West Russia");
    final MockBattle battle = new MockBattle(westRussia);
    final PlayerID russians = GameDataTestUtil.russians(gameData);
    final ITestDelegateBridge bridge = getDelegateBridge(russians);
    final UnitType infantryType = GameDataTestUtil.infantry(gameData);
    final List<Unit> infantry = infantryType.create(1, russians);
    final Collection<TerritoryEffect> territoryEffects = TerritoryEffectHelper.getEffects(westRussia);
    // infantry defends and hits at 1 (0 based)
    bridge.setRandomSource(new ScriptedRandomSource(new int[] { 1 }));
    final DiceRoll roll = DiceRoll.rollDice(infantry, true, russians, bridge, battle, "", territoryEffects, null);
    assertThat(roll.getHits(), is(1));
    // infantry does not hit at 2 (0 based)
    bridge.setRandomSource(new ScriptedRandomSource(new int[] { 2 }));
    final DiceRoll roll2 = DiceRoll.rollDice(infantry, true, russians, bridge, battle, "", territoryEffects, null);
    assertThat(roll2.getHits(), is(0));
    // infantry attacks and hits at 0 (0 based)
    bridge.setRandomSource(new ScriptedRandomSource(new int[] { 0 }));
    final DiceRoll roll3 = DiceRoll.rollDice(infantry, false, russians, bridge, battle, "", territoryEffects, null);
    assertThat(roll3.getHits(), is(1));
    // infantry attack does not hit at 1 (0 based)
    bridge.setRandomSource(new ScriptedRandomSource(new int[] { 1 }));
    final DiceRoll roll4 = DiceRoll.rollDice(infantry, false, russians, bridge, battle, "", territoryEffects, null);
    assertThat(roll4.getHits(), is(0));
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) TerritoryEffect(games.strategy.engine.data.TerritoryEffect) UnitType(games.strategy.engine.data.UnitType) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit) 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