use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class WW2V3Year41Test method testBombardStrengthVariable.
@Test
public void testBombardStrengthVariable() {
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 balkans = territory("Balkans", gameData);
// Clear all units
removeFrom(eg, eg.getUnits().getUnits());
removeFrom(sz14, sz14.getUnits().getUnits());
// Add 2 inf to the attacked terr
final PlayerID british = GameDataTestUtil.british(gameData);
addTo(eg, infantry(gameData).create(2, british));
// create/load the destroyers and transports
final PlayerID italians = GameDataTestUtil.italians(gameData);
addTo(sz14, transport(gameData).create(1, italians));
addTo(sz14, destroyer(gameData).create(2, italians));
// load the transports
load(balkans.getUnits().getMatches(Matches.unitIsLandTransportable()), new Route(balkans, sz14));
// move the fleet
move(sz14.getUnits().getUnits(), new Route(sz14, sz15));
// unload the transports
move(sz15.getUnits().getMatches(Matches.unitIsLand()), new Route(sz15, eg));
move.end();
// Set the tech for DDs bombard
// ww2v3 doesn't have this tech, so this does nothing...
// TechAttachment.get(italians).setDestroyerBombard("true");
UnitAttachment.get(destroyer(gameData)).setCanBombard("true");
// Set the bombard strength for the DDs
final Collection<Unit> dds = CollectionUtils.getMatches(sz15.getUnits().getUnits(), Matches.unitIsDestroyer());
final Iterator<Unit> ddIter = dds.iterator();
while (ddIter.hasNext()) {
final Unit unit = ddIter.next();
final UnitAttachment ua = UnitAttachment.get(unit.getType());
ua.setBombard(3);
}
// 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);
assertNotNull(mfb);
// Show that bombard casualties can return fire
// destroyer bombard hit/miss on rolls of 4 & 3
// landing inf miss
// defending inf hit
bridge.setRandomSource(new ScriptedRandomSource(3, 2, 6, 6, 1, 1));
battleDelegate(gameData).setDelegateBridgeAndPlayer(bridge);
BattleDelegate.doInitialize(battleDelegate(gameData).getBattleTracker(), bridge);
battleDelegate(gameData).addBombardmentSources();
fight(battleDelegate(gameData), eg);
// 1 defending inf remaining
assertEquals(1, eg.getUnits().size());
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class WW2V3Year41Test 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_SUBMERGE, attacker + SUBS_FIRE, defender + SELECT_SUB_CASUALTIES, REMOVE_SNEAK_ATTACK_CASUALTIES, attacker + FIRE, defender + SELECT_CASUALTIES, defender + SUBS_FIRE, attacker + SELECT_SUB_CASUALTIES, REMOVE_CASUALTIES, attacker + ATTACKER_WITHDRAW).toString(), steps.toString());
bridge.setRemote(dummyPlayer);
// attacking subs sneak attack and hit
// no chance to return fire
final ScriptedRandomSource randomSource = new ScriptedRandomSource(0, ScriptedRandomSource.ERROR);
bridge.setRandomSource(randomSource);
battle.fight(bridge);
assertEquals(1, randomSource.getTotalRolled());
assertTrue(attacked.getUnits().getMatches(Matches.unitIsOwnedBy(germans(gameData))).isEmpty());
assertEquals(2, attacked.getUnits().size());
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class WW2V3Year41Test method testAaCasualtiesLowLuckMixedRadar.
@Test
public void testAaCasualtiesLowLuckMixedRadar() {
// 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));
// 3 bombers and 3 fighters
final Collection<Unit> planes = bomber(gameData).create(3, british(gameData));
planes.addAll(fighter(gameData).create(3, british(gameData)));
final Collection<Unit> defendingAa = territory("Germany", gameData).getUnits().getMatches(Matches.unitIsAaForAnything());
// don't allow rolling, 6 of each is deterministic
bridge.setRandomSource(new ScriptedRandomSource(new int[] { ScriptedRandomSource.ERROR }));
final DiceRoll roll = DiceRoll.rollAa(CollectionUtils.getMatches(planes, Matches.unitIsOfTypes(UnitAttachment.get(defendingAa.iterator().next().getType()).getTargetsAa(gameData))), defendingAa, bridge, territory("Germany", gameData), true);
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());
// should be 1 fighter and 1 bomber
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 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_SUBMERGE, defender + SUBS_SUBMERGE, attacker + SUBS_FIRE, defender + SELECT_SUB_CASUALTIES, defender + SUBS_FIRE, attacker + SELECT_SUB_CASUALTIES, REMOVE_SNEAK_ATTACK_CASUALTIES, REMOVE_CASUALTIES, attacker + ATTACKER_WITHDRAW).toString(), steps.toString());
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());
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class WW2V3Year41Test method testAaCasualtiesLowLuckMixedWithRollingRadar.
@Test
public void testAaCasualtiesLowLuckMixedWithRollingRadar() {
// 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 hit
// then a dice to select the casualty
final ScriptedRandomSource randomSource = new ScriptedRandomSource(new int[] { 0, 1 });
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);
// 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(3, casualties.size());
// should be 1 fighter and 2 bombers
assertEquals(2, CollectionUtils.countMatches(casualties, Matches.unitIsStrategicBomber()));
assertEquals(1, CollectionUtils.countMatches(casualties, Matches.unitIsStrategicBomber().negate()));
}
Aggregations