use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class DiceRollTest method testSimpleLowLuck.
@Test
public void testSimpleLowLuck() {
GameDataTestUtil.makeGameLowLuck(gameData);
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));
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class LhtrTest method testLhtrBombingRaid.
@Test
public void testLhtrBombingRaid() {
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);
battle.addAttackChange(gameData.getMap().getRoute(uk, germany), uk.getUnits().getMatches(Matches.unitIsStrategicBomber()), null);
addTo(germany, uk.getUnits().getMatches(Matches.unitIsStrategicBomber()));
tracker.getBattleRecords().addBattle(british, battle.getBattleId(), germany, battle.getBattleType());
final ITestDelegateBridge bridge = getDelegateBridge(british);
TechTracker.addAdvance(british, bridge, TechAdvance.findAdvance(TechAdvance.TECH_PROPERTY_HEAVY_BOMBER, gameData, british));
// aa guns rolls 3, misses, bomber rolls 2 dice at 3 and 4
bridge.setRandomSource(new ScriptedRandomSource(new int[] { 2, 2, 3 }));
// if we try to move aa, then the game will ask us if we want to move
// fail if we are called
final ITripleAPlayer player = (ITripleAPlayer) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { ITripleAPlayer.class }, (p, m, a) -> null);
bridge.setRemote(player);
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));
// targets dice is 4, so damage is 1 + 4 = 5
// Changed to match StrategicBombingRaidBattle changes
assertEquals(pusBeforeRaid - 5, pusAfterRaid);
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class LhtrTest method testLhtrBombingRaid2Bombers.
@Test
public void testLhtrBombingRaid2Bombers() {
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);
// add a unit
final Unit bomber = GameDataTestUtil.bomber(gameData).create(british);
final Change change = ChangeFactory.addUnits(uk, Collections.singleton(bomber));
gameData.performChange(change);
final BattleTracker tracker = new BattleTracker();
final StrategicBombingRaidBattle battle = new StrategicBombingRaidBattle(germany, gameData, british, tracker);
battle.addAttackChange(gameData.getMap().getRoute(uk, germany), uk.getUnits().getMatches(Matches.unitIsStrategicBomber()), null);
addTo(germany, uk.getUnits().getMatches(Matches.unitIsStrategicBomber()));
tracker.getBattleRecords().addBattle(british, battle.getBattleId(), germany, battle.getBattleType());
final ITestDelegateBridge bridge = getDelegateBridge(british);
TechTracker.addAdvance(british, bridge, TechAdvance.findAdvance(TechAdvance.TECH_PROPERTY_HEAVY_BOMBER, gameData, british));
// aa guns rolls 3,3 both miss, bomber 1 rolls 2 dice at 3,4 and bomber 2 rolls dice at 1,2
bridge.setRandomSource(new ScriptedRandomSource(new int[] { 3, 3, 2, 3, 0, 1 }));
// if we try to move aa, then the game will ask us if we want to move
// fail if we are called
final ITripleAPlayer player = (ITripleAPlayer) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { ITripleAPlayer.class }, (p, m, a) -> null);
bridge.setRemote(player);
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));
// targets dice is 4, so damage is 1 + 4 = 5
// bomber 2 hits at 2, so damage is 3, for a total of 8
// Changed to match StrategicBombingRaidBattle changes
assertEquals(pusBeforeRaid - 8, pusAfterRaid);
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class MoveDelegateTest method testReloadTransportAfterDyingAmphibious.
@Test
public void testReloadTransportAfterDyingAmphibious() {
bridge = super.getDelegateBridge(british);
bridge.setStepName("britishCombatMove");
Route route = new Route();
route.setStart(northSea);
route.add(balticSeaZone);
IntegerMap<UnitType> map = new IntegerMap<>();
map.put(transport, 1);
map.put(infantry, 2);
// Move from the NorthSea to the BalticSea and validate the move
String results = delegate.move(GameDataTestUtil.getUnits(map, route.getStart()), route);
assertValid(results);
// Unload transports into Finland and validate
route = new Route();
route.setStart(balticSeaZone);
route.add(finlandNorway);
map = new IntegerMap<>();
map.put(infantry, 2);
results = delegate.move(GameDataTestUtil.getUnits(map, route.getStart()), route);
assertValid(results);
// Get the attacking sea units that will retreat
final List<Unit> retreatingSeaUnits = new ArrayList<>();
retreatingSeaUnits.addAll(balticSeaZone.getUnits().getMatches(Matches.enemyUnit(germans, gameData)));
// Get the attacking land units that will retreat and their number
final List<Unit> retreatingLandUnits = new ArrayList<>();
retreatingLandUnits.addAll(finlandNorway.getUnits().getMatches(Matches.enemyUnit(germans, gameData)));
final int retreatingLandSizeInt = retreatingLandUnits.size();
// Get the defending land units that and their number
final List<Unit> defendingLandUnits = new ArrayList<>();
defendingLandUnits.addAll(finlandNorway.getUnits().getMatches(Matches.enemyUnit(british, gameData)));
final int defendingLandSizeInt = defendingLandUnits.size();
// Set up the battles and the dependent battles
final IBattle inFinlandNorway = DelegateFinder.battleDelegate(gameData).getBattleTracker().getPendingBattle(finlandNorway, false, null);
final IBattle inBalticSeaZone = DelegateFinder.battleDelegate(gameData).getBattleTracker().getPendingBattle(balticSeaZone, false, null);
assertNotNull(balticSeaZone);
assertNotNull(finlandNorway);
assertEquals(DelegateFinder.battleDelegate(gameData).getBattleTracker().getDependentOn(inFinlandNorway).iterator().next(), inBalticSeaZone);
// Add some defending units in case there aren't any
final List<Unit> defendList = transport.create(1, germans);
final List<Unit> defendSub = submarine.create(1, germans);
defendList.addAll(defendSub);
// fire the defending transport then the submarine (One hit)
bridge.setRandomSource(new ScriptedRandomSource(new int[] { 0, 2 }));
// Execute the battle and verify no hits
final DiceRoll roll = DiceRoll.rollDice(defendList, true, germans, bridge, new MockBattle(balticSeaZone), "", TerritoryEffectHelper.getEffects(balticSeaZone), null);
assertEquals(1, roll.getHits());
// Get total number of units in Finland before the retreat
final int preCountInt = finlandNorway.getUnits().size();
// Retreat from the Baltic
((MustFightBattle) inBalticSeaZone).externalRetreat(retreatingSeaUnits, northSea, false, bridge);
// Get the total number of units that should be left
final int postCountInt = preCountInt - retreatingLandSizeInt;
// Compare the number of units in Finland to begin with the number after retreating
assertEquals(defendingLandSizeInt, postCountInt);
}
use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.
the class MoveDelegateTest method testReloadTransportAfterDyingAllied.
@Test
public void testReloadTransportAfterDyingAllied() {
bridge = super.getDelegateBridge(british);
bridge.setStepName("britishCombatMove");
Route route = new Route();
route.setStart(northSea);
route.add(balticSeaZone);
IntegerMap<UnitType> map = new IntegerMap<>();
map.put(transport, 1);
map.put(infantry, 2);
// Move from the NorthSea to the BalticSea and validate the move
String results = delegate.move(GameDataTestUtil.getUnits(map, route.getStart()), route);
assertValid(results);
// Unload transports into Finland and validate
route = new Route();
route.setStart(balticSeaZone);
route.add(karelia);
map = new IntegerMap<>();
map.put(infantry, 2);
results = delegate.move(GameDataTestUtil.getUnits(map, route.getStart()), route);
assertValid(results);
// Get the attacking sea units that will retreat
final List<Unit> retreatingSeaUnits = new ArrayList<>();
retreatingSeaUnits.addAll(balticSeaZone.getUnits().getMatches(Matches.enemyUnit(germans, gameData)));
// Get the attacking land units that will retreat and their number
final List<Unit> retreatingLandUnits = new ArrayList<>();
retreatingLandUnits.addAll(karelia.getUnits().getMatches(Matches.isUnitAllied(russians, gameData)));
final int retreatingLandSizeInt = retreatingLandUnits.size();
// Get the defending land units that and their number
final List<Unit> defendingLandUnits = new ArrayList<>();
retreatingLandUnits.addAll(karelia.getUnits().getMatches(Matches.isUnitAllied(british, gameData)));
final int defendingLandSizeInt = defendingLandUnits.size();
// Set up the battles and the dependent battles
final IBattle inBalticSeaZone = DelegateFinder.battleDelegate(gameData).getBattleTracker().getPendingBattle(balticSeaZone, false, null);
assertNotNull(balticSeaZone);
// Add some defending units in case there aren't any
final List<Unit> defendList = transport.create(1, germans);
final List<Unit> defendSub = submarine.create(1, germans);
defendList.addAll(defendSub);
// fire the defending transport then the submarine (One hit)
bridge.setRandomSource(new ScriptedRandomSource(new int[] { 0, 2 }));
// Execute the battle and verify no hits
final DiceRoll roll = DiceRoll.rollDice(defendList, true, germans, bridge, new MockBattle(balticSeaZone), "", TerritoryEffectHelper.getEffects(balticSeaZone), null);
assertEquals(1, roll.getHits());
// Get total number of units in Finland before the retreat
final int preCountInt = karelia.getUnits().size();
// Retreat from the Baltic
((MustFightBattle) inBalticSeaZone).externalRetreat(retreatingSeaUnits, northSea, false, bridge);
// Get the total number of units that should be left
final int postCountInt = preCountInt - retreatingLandSizeInt;
// Compare the number of units in Finland to begin with the number after retreating
assertEquals(defendingLandSizeInt, postCountInt);
}
Aggregations