Search in sources :

Example 61 with ScriptedRandomSource

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));
}
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)

Example 62 with ScriptedRandomSource

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);
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) BeforeEach(org.junit.jupiter.api.BeforeEach) Proxy(java.lang.reflect.Proxy) Constants(games.strategy.triplea.Constants) UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) GameDataTestUtil.addTo(games.strategy.triplea.delegate.GameDataTestUtil.addTo) Route(games.strategy.engine.data.Route) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) UnitType(games.strategy.engine.data.UnitType) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TripleAUnit(games.strategy.triplea.TripleAUnit) TestMapGameData(games.strategy.triplea.xml.TestMapGameData) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) Unit(games.strategy.engine.data.Unit) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) Territory(games.strategy.engine.data.Territory) Test(org.junit.jupiter.api.Test) GameData(games.strategy.engine.data.GameData) ChangeFactory(games.strategy.engine.data.changefactory.ChangeFactory) List(java.util.List) PlayerID(games.strategy.engine.data.PlayerID) Change(games.strategy.engine.data.Change) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ITripleAPlayer(games.strategy.triplea.player.ITripleAPlayer) Collections(java.util.Collections) PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) ITripleAPlayer(games.strategy.triplea.player.ITripleAPlayer) Test(org.junit.jupiter.api.Test)

Example 63 with ScriptedRandomSource

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);
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) BeforeEach(org.junit.jupiter.api.BeforeEach) Proxy(java.lang.reflect.Proxy) Constants(games.strategy.triplea.Constants) UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) GameDataTestUtil.addTo(games.strategy.triplea.delegate.GameDataTestUtil.addTo) Route(games.strategy.engine.data.Route) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) UnitType(games.strategy.engine.data.UnitType) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TripleAUnit(games.strategy.triplea.TripleAUnit) TestMapGameData(games.strategy.triplea.xml.TestMapGameData) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) Unit(games.strategy.engine.data.Unit) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) Territory(games.strategy.engine.data.Territory) Test(org.junit.jupiter.api.Test) GameData(games.strategy.engine.data.GameData) ChangeFactory(games.strategy.engine.data.changefactory.ChangeFactory) List(java.util.List) PlayerID(games.strategy.engine.data.PlayerID) Change(games.strategy.engine.data.Change) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ITripleAPlayer(games.strategy.triplea.player.ITripleAPlayer) Collections(java.util.Collections) PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) Change(games.strategy.engine.data.Change) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) ITripleAPlayer(games.strategy.triplea.player.ITripleAPlayer) Test(org.junit.jupiter.api.Test)

Example 64 with ScriptedRandomSource

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);
}
Also used : IntegerMap(games.strategy.util.IntegerMap) ArrayList(java.util.ArrayList) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) Unit(games.strategy.engine.data.Unit) UnitType(games.strategy.engine.data.UnitType) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Example 65 with ScriptedRandomSource

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);
}
Also used : IntegerMap(games.strategy.util.IntegerMap) ArrayList(java.util.ArrayList) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) Unit(games.strategy.engine.data.Unit) UnitType(games.strategy.engine.data.UnitType) Route(games.strategy.engine.data.Route) 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