use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class WW2V3Year41Test 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
// no sneak attacks
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, attacker + FIRE, defender + SELECT_CASUALTIES, defender + SUBS_FIRE, attacker + SELECT_SUB_CASUALTIES, defender + FIRE, attacker + SELECT_CASUALTIES, REMOVE_CASUALTIES, attacker + ATTACKER_WITHDRAW).toString(), steps.toString());
when(dummyPlayer.selectCasualties(any(), any(), anyInt(), any(), any(), any(), any(), any(), any(), anyBoolean(), any(), any(), any(), any(), anyBoolean())).thenAnswer(new Answer<CasualtyDetails>() {
@Override
public CasualtyDetails answer(final InvocationOnMock invocation) {
final Collection<Unit> selectFrom = invocation.getArgument(0);
return new CasualtyDetails(Arrays.asList(selectFrom.iterator().next()), new ArrayList<>(), false);
}
});
bridge.setRemote(dummyPlayer);
// attacking subs sneak attack and hit
// no chance to return fire
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());
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class WW2V3Year41Test method testTechTokens.
@Test
public void testTechTokens() {
// Set up the test
final PlayerID germans = GameDataTestUtil.germans(gameData);
final ITestDelegateBridge delegateBridge = getDelegateBridge(germans);
delegateBridge.setStepName("germanTech");
final TechnologyDelegate techDelegate = techDelegate(gameData);
techDelegate.setDelegateBridgeAndPlayer(delegateBridge);
techDelegate.start();
final TechnologyFrontier mech = new TechnologyFrontier("", gameData);
mech.addAdvance(TechAdvance.findAdvance(TechAdvance.TECH_PROPERTY_MECHANIZED_INFANTRY, gameData, null));
// Add tech token
gameData.performChange(ChangeFactory.changeResourcesChange(germans, gameData.getResourceList().getResource(Constants.TECH_TOKENS), 1));
// Check to make sure it was successful
final int initTokens = germans.getResources().getQuantity("techTokens");
assertEquals(1, initTokens);
// Fail the roll
delegateBridge.setRandomSource(new ScriptedRandomSource(new int[] { 3 }));
final TechResults roll = techDelegate.rollTech(1, mech, 0, null);
// Check to make sure it failed
assertEquals(0, roll.getHits());
final int midTokens = germans.getResources().getQuantity("techTokens");
assertEquals(1, midTokens);
// Make a Successful roll
delegateBridge.setRandomSource(new ScriptedRandomSource(new int[] { 5 }));
final TechResults roll2 = techDelegate.rollTech(1, mech, 0, null);
// Check to make sure it succeeded and all tokens were removed
assertEquals(1, roll2.getHits());
final int finalTokens = germans.getResources().getQuantity("techTokens");
assertEquals(0, finalTokens);
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class MustFightBattleTest method testFightWithIsSuicideOnHit.
@Test
public void testFightWithIsSuicideOnHit() throws Exception {
final GameData twwGameData = TestMapGameData.TWW.getGameData();
// Create battle with 1 cruiser attacking 1 mine
final PlayerID usa = GameDataTestUtil.usa(twwGameData);
final PlayerID germany = GameDataTestUtil.germany(twwGameData);
final Territory sz33 = territory("33 Sea Zone", twwGameData);
addTo(sz33, GameDataTestUtil.americanCruiser(twwGameData).create(1, usa));
final Territory sz40 = territory("40 Sea Zone", twwGameData);
addTo(sz40, GameDataTestUtil.germanMine(twwGameData).create(1, germany));
final ITestDelegateBridge bridge = GameDataTestUtil.getDelegateBridge(usa, twwGameData);
bridge.setStepName("CombatMove");
moveDelegate(twwGameData).setDelegateBridgeAndPlayer(bridge);
moveDelegate(twwGameData).start();
move(sz33.getUnits().getUnits(), new Route(sz33, sz40));
moveDelegate(twwGameData).end();
final MustFightBattle battle = (MustFightBattle) AbstractMoveDelegate.getBattleTracker(twwGameData).getPendingBattle(sz40, false, null);
bridge.setRemote(dummyPlayer);
// Set first roll to hit (mine AA) and check that both units are killed
final ScriptedRandomSource randomSource = new ScriptedRandomSource(0, ScriptedRandomSource.ERROR);
bridge.setRandomSource(randomSource);
battle.fight(bridge);
assertEquals(1, randomSource.getTotalRolled());
assertEquals(0, sz40.getUnits().size());
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class PacificTest method testJapanAttackFirstRound.
@Test
public void testJapanAttackFirstRound() {
bridge.setStepName("japaneseBattle");
while (!gameData.getSequence().getStep().getName().equals("japaneseBattle")) {
gameData.getSequence().next();
}
// >>> After patch normal to-hits will miss <<<
// Defending US infantry miss on a 2 (0 base)
final List<Unit> infantryUs = infantry.create(1, americans);
final Collection<TerritoryEffect> territoryEffects = TerritoryEffectHelper.getEffects(queensland);
bridge.setRandomSource(new ScriptedRandomSource(new int[] { 1 }));
DiceRoll roll = DiceRoll.rollDice(infantryUs, true, americans, bridge, new MockBattle(queensland), "", territoryEffects, null);
assertEquals(0, roll.getHits());
// Defending US marines miss on a 2 (0 base)
final List<Unit> marineUs = marine.create(1, americans);
bridge.setRandomSource(new ScriptedRandomSource(new int[] { 1 }));
roll = DiceRoll.rollDice(marineUs, true, americans, bridge, new MockBattle(queensland), "", territoryEffects, null);
assertEquals(0, roll.getHits());
// Chinese units
// Defending Chinese infantry still hit on a 2 (0 base)
final List<Unit> infantryChina = infantry.create(1, chinese);
bridge.setRandomSource(new ScriptedRandomSource(new int[] { 1 }));
roll = DiceRoll.rollDice(infantryChina, true, chinese, bridge, new MockBattle(queensland), "", territoryEffects, null);
assertEquals(1, roll.getHits());
// Defending US infantry hit on a 1 (0 base)
bridge.setRandomSource(new ScriptedRandomSource(new int[] { 0 }));
roll = DiceRoll.rollDice(infantryUs, true, americans, bridge, new MockBattle(queensland), "", territoryEffects, null);
assertEquals(1, roll.getHits());
// Defending US marines hit on a 1 (0 base)
bridge.setRandomSource(new ScriptedRandomSource(new int[] { 0 }));
roll = DiceRoll.rollDice(marineUs, true, americans, bridge, new MockBattle(queensland), "", territoryEffects, null);
assertEquals(1, roll.getHits());
// Chinese units
// Defending Chinese infantry still hit on a 2 (0 base)
bridge.setRandomSource(new ScriptedRandomSource(new int[] { 1 }));
roll = DiceRoll.rollDice(infantryChina, true, chinese, bridge, new MockBattle(queensland), "", territoryEffects, null);
assertEquals(1, roll.getHits());
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class RevisedTest method testTransportsUnloadingToMultipleTerritoriesDie.
@Test
public void testTransportsUnloadingToMultipleTerritoriesDie() {
// two transports enter a battle, but drop off
// their units to two allied territories before
// the begin the battle
// the units they drop off should die with the transports
final PlayerID germans = germans(gameData);
final PlayerID british = british(gameData);
final Territory sz6 = territory("6 Sea Zone", gameData);
final Territory sz5 = territory("5 Sea Zone", gameData);
final Territory germany = territory("Germany", gameData);
final Territory norway = territory("Norway", gameData);
final Territory we = territory("Western Europe", gameData);
final Territory uk = territory("United Kingdom", gameData);
addTo(sz6, destroyer(gameData).create(2, british));
addTo(sz5, transport(gameData).create(3, germans));
addTo(germany, armour(gameData).create(3, germans));
final ITestDelegateBridge bridge = getDelegateBridge(germans(gameData));
bridge.setStepName("CombatMove");
bridge.setRemote(dummyPlayer);
moveDelegate(gameData).setDelegateBridgeAndPlayer(bridge);
moveDelegate(gameData).start();
// load two transports, 1 tank each
load(germany.getUnits().getMatches(Matches.unitCanBlitz()).subList(0, 1), new Route(germany, sz5));
load(germany.getUnits().getMatches(Matches.unitCanBlitz()).subList(0, 1), new Route(germany, sz5));
load(germany.getUnits().getMatches(Matches.unitCanBlitz()).subList(0, 1), new Route(germany, sz5));
// attack sz 6
move(sz5.getUnits().getMatches(Matches.unitCanBlitz().or(Matches.unitIsTransport())), new Route(sz5, sz6));
// unload transports, 1 each to a different country
// this move is illegal now
assertMoveError(sz6.getUnits().getMatches(Matches.unitCanBlitz()).subList(0, 1), new Route(sz6, norway));
// this move is illegal now
assertMoveError(sz6.getUnits().getMatches(Matches.unitCanBlitz()).subList(0, 1), new Route(sz6, we));
move(sz6.getUnits().getMatches(Matches.unitCanBlitz()).subList(0, 1), new Route(sz6, uk));
// fight the battle
moveDelegate(gameData).end();
final MustFightBattle battle = (MustFightBattle) AbstractMoveDelegate.getBattleTracker(gameData).getPendingBattle(sz6, false, null);
// everything hits, this will kill both transports
bridge.setRandomSource(new ScriptedRandomSource(0));
battle.fight(bridge);
// the armour should have died
assertEquals(0, norway.getUnits().countMatches(Matches.unitCanBlitz()));
assertEquals(2, we.getUnits().countMatches(Matches.unitCanBlitz()));
assertEquals(0, uk.getUnits().countMatches(Matches.unitIsOwnedBy(germans)));
}
Aggregations