use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class RevisedTest method testOverFlyBombersJoiningBattleDie.
@Test
public void testOverFlyBombersJoiningBattleDie() {
// a bomber flies over aa to join a battle, gets hit,
// it should not appear in the battle
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));
final Territory uk = territory("United Kingdom", gameData);
final Territory we = territory("Western Europe", gameData);
final Territory se = territory("Southern Europe", gameData);
final Route route = new Route(uk, territory("7 Sea Zone", gameData), we, se);
move(uk.getUnits().getMatches(Matches.unitIsStrategicBomber()), route);
// the aa gun should have fired and hit
assertTrue(se.getUnits().getMatches(Matches.unitIsStrategicBomber()).isEmpty());
assertTrue(we.getUnits().getMatches(Matches.unitIsStrategicBomber()).isEmpty());
assertTrue(uk.getUnits().getMatches(Matches.unitIsStrategicBomber()).isEmpty());
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class RevisedTest method testAttackSubsAndBattleshipOnDestroyerAndSubs.
@Test
public void testAttackSubsAndBattleshipOnDestroyerAndSubs() {
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(1, british(gameData)));
addTo(from, battleship(gameData).create(1, british(gameData)));
addTo(attacked, submarine(gameData).create(3, 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, 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(british(gameData))).isEmpty());
assertEquals(3, attacked.getUnits().size());
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class WW2V3Year41Test method testAaCasualtiesLowLuckMixedWithRollingMissRadar.
@Test
public void testAaCasualtiesLowLuckMixedWithRollingMissRadar() {
// moved from BattleCalculatorTest because "revised" does not have "radar"
final PlayerID british = GameDataTestUtil.british(gameData);
final ITestDelegateBridge bridge = getDelegateBridge(british);
makeGameLowLuck(gameData);
// setSelectAACasualties(data, false);
givePlayerRadar(germans(gameData));
// 4 bombers and 4 fighters
final Collection<Unit> planes = bomber(gameData).create(4, british(gameData));
planes.addAll(fighter(gameData).create(4, british(gameData)));
final Collection<Unit> defendingAa = territory("Germany", gameData).getUnits().getMatches(Matches.unitIsAaForAnything());
// 1 roll, a miss
// then a dice to select the casualty
final ScriptedRandomSource randomSource = new ScriptedRandomSource(new int[] { 5, 0, 0, 0, ScriptedRandomSource.ERROR });
bridge.setRandomSource(randomSource);
final DiceRoll roll = DiceRoll.rollAa(CollectionUtils.getMatches(planes, Matches.unitIsOfTypes(UnitAttachment.get(defendingAa.iterator().next().getType()).getTargetsAa(gameData))), defendingAa, bridge, territory("Germany", gameData), true);
assertEquals(2, roll.getHits());
// make sure we rolled once
assertEquals(1, randomSource.getTotalRolled());
final Collection<Unit> casualties = BattleCalculator.getAaCasualties(false, planes, planes, defendingAa, defendingAa, roll, bridge, null, null, null, territory("Germany", gameData), null, false, null).getKilled();
assertEquals(2, casualties.size());
assertEquals(4, randomSource.getTotalRolled());
// should be 1 fighter and 2 bombers
assertEquals(1, CollectionUtils.countMatches(casualties, Matches.unitIsStrategicBomber()));
assertEquals(1, CollectionUtils.countMatches(casualties, Matches.unitIsStrategicBomber().negate()));
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class WW2V3Year41Test method testJetPower.
@Test
public void testJetPower() {
// Set up tech
final PlayerID germans = GameDataTestUtil.germans(gameData);
final ITestDelegateBridge delegateBridge = getDelegateBridge(germans(gameData));
TechTracker.addAdvance(germans, delegateBridge, TechAdvance.findAdvance(TechAdvance.TECH_PROPERTY_JET_POWER, gameData, germans));
// Set up the territories
final Territory poland = territory("Poland", gameData);
final Territory eastPoland = territory("East Poland", gameData);
// Set up the unit types
final UnitType fighterType = GameDataTestUtil.fighter(gameData);
delegateBridge.setStepName("germanBattle");
while (!gameData.getSequence().getStep().getName().equals("germanBattle")) {
gameData.getSequence().next();
}
final Collection<TerritoryEffect> territoryEffects = TerritoryEffectHelper.getEffects(eastPoland);
// With JET_POWER attacking fighter hits on 4 (0 base)
final List<Unit> germanFighter = (List<Unit>) poland.getUnits().getUnits(fighterType, 1);
delegateBridge.setRandomSource(new ScriptedRandomSource(new int[] { 3 }));
final DiceRoll roll1 = DiceRoll.rollDice(germanFighter, false, germans, delegateBridge, new MockBattle(eastPoland), "", territoryEffects, null);
assertEquals(1, roll1.getHits());
// With JET_POWER defending fighter misses on 5 (0 base)
delegateBridge.setRandomSource(new ScriptedRandomSource(new int[] { 4 }));
final DiceRoll roll2 = DiceRoll.rollDice(germanFighter, true, germans, delegateBridge, new MockBattle(eastPoland), "", territoryEffects, null);
assertEquals(0, roll2.getHits());
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class WW2V3Year41Test method testLimitBombardtoNumberOfUnloaded.
@Test
public void testLimitBombardtoNumberOfUnloaded() {
final MoveDelegate move = moveDelegate(gameData);
final ITestDelegateBridge bridge = getDelegateBridge(italians(gameData));
when(dummyPlayer.selectShoreBombard(any())).thenReturn(true);
bridge.setRemote(dummyPlayer);
bridge.setStepName("CombatMove");
move.setDelegateBridgeAndPlayer(bridge);
move.start();
final Territory sz14 = territory("14 Sea Zone", gameData);
final Territory sz15 = territory("15 Sea Zone", gameData);
final Territory eg = territory("Egypt", gameData);
final Territory li = territory("Libya", gameData);
final Territory balkans = territory("Balkans", gameData);
// Clear all units from the attacked terr
removeFrom(eg, eg.getUnits().getUnits());
// Add 2 inf
final PlayerID british = GameDataTestUtil.british(gameData);
addTo(eg, infantry(gameData).create(2, british));
// load the transports
load(balkans.getUnits().getMatches(Matches.unitIsLandTransportable()), new Route(balkans, sz14));
// move the fleet
move(sz14.getUnits().getUnits(), new Route(sz14, sz15));
// move troops from Libya
move(li.getUnits().getMatches(Matches.unitOwnedBy(italians(gameData))), new Route(li, eg));
// unload the transports
move(sz15.getUnits().getMatches(Matches.unitIsLand()), new Route(sz15, eg));
move.end();
// start the battle phase, this will ask the user to bombard
battleDelegate(gameData).setDelegateBridgeAndPlayer(bridge);
BattleDelegate.doInitialize(battleDelegate(gameData).getBattleTracker(), bridge);
battleDelegate(gameData).addBombardmentSources();
final MustFightBattle mfb = (MustFightBattle) AbstractMoveDelegate.getBattleTracker(gameData).getPendingBattle(eg, false, null);
// only 2 ships are allowed to bombard (there are 1 battleship and 2 cruisers that COULD bombard, but only 2 ships
// may bombard total)
assertEquals(2, mfb.getBombardingUnits().size());
// Show that bombard casualties can return fire
// Note- the 3 & 2 hits below show default behavior of bombarding at attack strength
// 2= Battleship hitting a 3, 2=Cruiser hitting a 3, 15=British infantry hitting once
bridge.setRandomSource(new ScriptedRandomSource(2, 2, 1, 5, 5, 5, 5, 5));
battleDelegate(gameData).setDelegateBridgeAndPlayer(bridge);
battleDelegate(gameData).start();
// end result should be 2 italian infantry.
assertEquals(3, eg.getUnits().size());
}
Aggregations