Search in sources :

Example 41 with ScriptedRandomSource

use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.

the class RevisedTest method testStratBombRaidWithHeavyBombers.

@Test
public void testStratBombRaidWithHeavyBombers() {
    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
    bridge.setRandomSource(new ScriptedRandomSource(new int[] { 3, 2, 2 }));
    // if we try to move aa, then the game will ask us if we want to move
    // fail if we are called
    final InvocationHandler handler = (proxy, method, args) -> null;
    final ITripleAPlayer player = (ITripleAPlayer) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { ITripleAPlayer.class }, handler);
    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));
    assertEquals(pusBeforeRaid - 6, pusAfterRaid);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) GameDataTestUtil.move(games.strategy.triplea.delegate.GameDataTestUtil.move) Arrays(java.util.Arrays) GameDataTestUtil.bomber(games.strategy.triplea.delegate.GameDataTestUtil.bomber) GameDataTestUtil.japanese(games.strategy.triplea.delegate.GameDataTestUtil.japanese) Constants(games.strategy.triplea.Constants) UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) GameDataTestUtil.addTo(games.strategy.triplea.delegate.GameDataTestUtil.addTo) TechAttachment(games.strategy.triplea.attachments.TechAttachment) GameDataTestUtil.aaGun(games.strategy.triplea.delegate.GameDataTestUtil.aaGun) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) GameDataTestUtil.moveDelegate(games.strategy.triplea.delegate.GameDataTestUtil.moveDelegate) GameDataTestUtil.makeGameLowLuck(games.strategy.triplea.delegate.GameDataTestUtil.makeGameLowLuck) GameDataTestUtil.americans(games.strategy.triplea.delegate.GameDataTestUtil.americans) GameDataTestUtil.getIndex(games.strategy.triplea.delegate.GameDataTestUtil.getIndex) TestMapGameData(games.strategy.triplea.xml.TestMapGameData) GameDataTestUtil.techDelegate(games.strategy.triplea.delegate.GameDataTestUtil.techDelegate) GameDataTestUtil.removeFrom(games.strategy.triplea.delegate.GameDataTestUtil.removeFrom) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) GameDataTestUtil.assertValid(games.strategy.triplea.delegate.GameDataTestUtil.assertValid) Collection(java.util.Collection) GameDataTestUtil.load(games.strategy.triplea.delegate.GameDataTestUtil.load) Set(java.util.Set) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) GameDataTestUtil.territory(games.strategy.triplea.delegate.GameDataTestUtil.territory) SUBS_FIRE(games.strategy.triplea.delegate.BattleStepStrings.SUBS_FIRE) GameDataTestUtil.armour(games.strategy.triplea.delegate.GameDataTestUtil.armour) Test(org.junit.jupiter.api.Test) SUBS_SUBMERGE(games.strategy.triplea.delegate.BattleStepStrings.SUBS_SUBMERGE) GameData(games.strategy.engine.data.GameData) List(java.util.List) PlayerID(games.strategy.engine.data.PlayerID) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ITripleAPlayer(games.strategy.triplea.player.ITripleAPlayer) REMOVE_CASUALTIES(games.strategy.triplea.delegate.BattleStepStrings.REMOVE_CASUALTIES) GameDataTestUtil.destroyer(games.strategy.triplea.delegate.GameDataTestUtil.destroyer) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) GameDataTestUtil.infantry(games.strategy.triplea.delegate.GameDataTestUtil.infantry) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Proxy(java.lang.reflect.Proxy) GameDataTestUtil.battleship(games.strategy.triplea.delegate.GameDataTestUtil.battleship) SELECT_SUB_CASUALTIES(games.strategy.triplea.delegate.BattleStepStrings.SELECT_SUB_CASUALTIES) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) GameDataTestUtil.placeDelegate(games.strategy.triplea.delegate.GameDataTestUtil.placeDelegate) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) ArrayList(java.util.ArrayList) PlaceableUnits(games.strategy.triplea.delegate.dataObjects.PlaceableUnits) Answer(org.mockito.stubbing.Answer) Route(games.strategy.engine.data.Route) GameDataTestUtil.assertError(games.strategy.triplea.delegate.GameDataTestUtil.assertError) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SELECT_CASUALTIES(games.strategy.triplea.delegate.BattleStepStrings.SELECT_CASUALTIES) CasualtyDetails(games.strategy.triplea.delegate.dataObjects.CasualtyDetails) GameDataTestUtil.bidPlaceDelegate(games.strategy.triplea.delegate.GameDataTestUtil.bidPlaceDelegate) CasualtyList(games.strategy.triplea.delegate.dataObjects.CasualtyList) UnitType(games.strategy.engine.data.UnitType) FIRE(games.strategy.triplea.delegate.BattleStepStrings.FIRE) GameDataTestUtil.germans(games.strategy.triplea.delegate.GameDataTestUtil.germans) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TripleAUnit(games.strategy.triplea.TripleAUnit) TechResults(games.strategy.triplea.delegate.dataObjects.TechResults) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) TechnologyFrontier(games.strategy.engine.data.TechnologyFrontier) CollectionUtils(games.strategy.util.CollectionUtils) Unit(games.strategy.engine.data.Unit) GameDataTestUtil.submarine(games.strategy.triplea.delegate.GameDataTestUtil.submarine) GameDataTestUtil.transport(games.strategy.triplea.delegate.GameDataTestUtil.transport) Territory(games.strategy.engine.data.Territory) Mockito.when(org.mockito.Mockito.when) GameDataTestUtil.british(games.strategy.triplea.delegate.GameDataTestUtil.british) ChangeFactory(games.strategy.engine.data.changefactory.ChangeFactory) REMOVE_SNEAK_ATTACK_CASUALTIES(games.strategy.triplea.delegate.BattleStepStrings.REMOVE_SNEAK_ATTACK_CASUALTIES) Change(games.strategy.engine.data.Change) ATTACKER_WITHDRAW(games.strategy.triplea.delegate.BattleStepStrings.ATTACKER_WITHDRAW) GameDataTestUtil.assertMoveError(games.strategy.triplea.delegate.GameDataTestUtil.assertMoveError) InvocationHandler(java.lang.reflect.InvocationHandler) 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) InvocationHandler(java.lang.reflect.InvocationHandler) ITripleAPlayer(games.strategy.triplea.player.ITripleAPlayer) Test(org.junit.jupiter.api.Test)

Example 42 with ScriptedRandomSource

use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.

the class RevisedTest method testAaOwnership.

@Test
public void testAaOwnership() {
    // Set up players
    // PlayerID british = GameDataTestUtil.british(gameData);
    final PlayerID japanese = GameDataTestUtil.japanese(gameData);
    // PlayerID americans = GameDataTestUtil.americans(gameData);
    // Set up the territories
    final Territory india = territory("India", gameData);
    final Territory fic = territory("French Indochina", gameData);
    final Territory china = territory("China", gameData);
    final Territory kwang = territory("Kwantung", gameData);
    // Preset units in FIC
    final UnitType infType = infantry(gameData);
    // UnitType aaType = GameDataTestUtil.aaGun(gameData);
    removeFrom(fic, fic.getUnits().getUnits());
    addTo(fic, aaGun(gameData).create(1, japanese));
    addTo(fic, infantry(gameData).create(1, japanese));
    assertEquals(2, fic.getUnits().getUnitCount());
    // Get attacking units
    final Collection<Unit> britishUnits = india.getUnits().getUnits(infType, 1);
    final Collection<Unit> japaneseUnits = kwang.getUnits().getUnits(infType, 1);
    final Collection<Unit> americanUnits = china.getUnits().getUnits(infType, 1);
    // Get Owner prior to battle
    assertTrue(fic.getUnits().allMatch(Matches.unitIsOwnedBy(japanese(gameData))));
    final String preOwner = fic.getOwner().getName();
    assertEquals(Constants.PLAYER_NAME_JAPANESE, preOwner);
    // Set up the move delegate
    ITestDelegateBridge delegateBridge = getDelegateBridge(british(gameData));
    final MoveDelegate moveDelegate = moveDelegate(gameData);
    delegateBridge.setStepName("CombatMove");
    moveDelegate.setDelegateBridgeAndPlayer(delegateBridge);
    moveDelegate.start();
    /*
     * add a VALID BRITISH attack
     */
    String validResults = moveDelegate.move(britishUnits, new Route(india, fic));
    assertValid(validResults);
    moveDelegate(gameData).end();
    // Set up battle
    MustFightBattle battle = (MustFightBattle) AbstractMoveDelegate.getBattleTracker(gameData).getPendingBattle(fic, false, null);
    delegateBridge.setRemote(dummyPlayer);
    // fight
    ScriptedRandomSource randomSource = new ScriptedRandomSource(0, 5);
    delegateBridge.setRandomSource(randomSource);
    battle.fight(delegateBridge);
    // Get Owner after to battle
    assertTrue(fic.getUnits().allMatch(Matches.unitIsOwnedBy(british(gameData))));
    final String postOwner = fic.getOwner().getName();
    assertEquals(Constants.PLAYER_NAME_BRITISH, postOwner);
    /*
     * add a VALID JAPANESE attack
     */
    // Set up battle
    delegateBridge = getDelegateBridge(japanese(gameData));
    delegateBridge.setStepName("CombatMove");
    moveDelegate.setDelegateBridgeAndPlayer(delegateBridge);
    moveDelegate.start();
    // Move to battle
    validResults = moveDelegate.move(japaneseUnits, new Route(kwang, fic));
    assertValid(validResults);
    moveDelegate(gameData).end();
    battle = (MustFightBattle) AbstractMoveDelegate.getBattleTracker(gameData).getPendingBattle(fic, false, null);
    delegateBridge.setRemote(dummyPlayer);
    // fight
    randomSource = new ScriptedRandomSource(0, 5);
    delegateBridge.setRandomSource(randomSource);
    battle.fight(delegateBridge);
    // Get Owner after to battle
    assertTrue(fic.getUnits().allMatch(Matches.unitIsOwnedBy(japanese(gameData))));
    final String midOwner = fic.getOwner().getName();
    assertEquals(Constants.PLAYER_NAME_JAPANESE, midOwner);
    /*
     * add a VALID AMERICAN attack
     */
    // Set up battle
    delegateBridge = getDelegateBridge(americans(gameData));
    delegateBridge.setStepName("CombatMove");
    moveDelegate.setDelegateBridgeAndPlayer(delegateBridge);
    moveDelegate.start();
    // Move to battle
    validResults = moveDelegate.move(americanUnits, new Route(china, fic));
    assertValid(validResults);
    moveDelegate(gameData).end();
    battle = (MustFightBattle) AbstractMoveDelegate.getBattleTracker(gameData).getPendingBattle(fic, false, null);
    delegateBridge.setRemote(dummyPlayer);
    // fight
    randomSource = new ScriptedRandomSource(0, 5);
    delegateBridge.setRandomSource(randomSource);
    battle.fight(delegateBridge);
    // Get Owner after to battle
    assertTrue(fic.getUnits().allMatch(Matches.unitIsOwnedBy(americans(gameData))));
    final String endOwner = fic.getOwner().getName();
    assertEquals(Constants.PLAYER_NAME_AMERICANS, endOwner);
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) UnitType(games.strategy.engine.data.UnitType) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Example 43 with ScriptedRandomSource

use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.

the class RevisedTest 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_FIRE, defender + SELECT_SUB_CASUALTIES, defender + SUBS_FIRE, attacker + SELECT_SUB_CASUALTIES, REMOVE_SNEAK_ATTACK_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);
    // 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());
}
Also used : Territory(games.strategy.engine.data.Territory) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Example 44 with ScriptedRandomSource

use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.

the class RevisedTest method testRetreatBug.

@Test
public void testRetreatBug() {
    final PlayerID russians = GameDataTestUtil.russians(gameData);
    final PlayerID americans = GameDataTestUtil.americans(gameData);
    final ITestDelegateBridge bridge = getDelegateBridge(russians);
    // we need to initialize the original owner
    final InitializationDelegate initDel = (InitializationDelegate) gameData.getDelegateList().getDelegate("initDelegate");
    initDel.setDelegateBridgeAndPlayer(bridge);
    initDel.start();
    initDel.end();
    // make sinkian japanese owned, put one infantry in it
    final Territory sinkiang = gameData.getMap().getTerritory("Sinkiang");
    gameData.performChange(ChangeFactory.removeUnits(sinkiang, sinkiang.getUnits().getUnits()));
    final PlayerID japanese = GameDataTestUtil.japanese(gameData);
    sinkiang.setOwner(japanese);
    final UnitType infantryType = infantry(gameData);
    gameData.performChange(ChangeFactory.addUnits(sinkiang, infantryType.create(1, japanese)));
    // now move to attack it
    final MoveDelegate moveDelegate = (MoveDelegate) gameData.getDelegateList().getDelegate("move");
    bridge.setStepName("CombatMove");
    moveDelegate.setDelegateBridgeAndPlayer(bridge);
    moveDelegate.start();
    final Territory novo = gameData.getMap().getTerritory("Novosibirsk");
    moveDelegate.move(novo.getUnits().getUnits(), gameData.getMap().getRoute(novo, sinkiang));
    moveDelegate.end();
    final BattleDelegate battle = (BattleDelegate) gameData.getDelegateList().getDelegate("battle");
    battle.setDelegateBridgeAndPlayer(bridge);
    bridge.setRandomSource(new ScriptedRandomSource(new int[] { 0, 0, 0 }));
    bridge.setRemote(dummyPlayer);
    // fights battle
    battle.start();
    battle.end();
    assertEquals(sinkiang.getOwner(), americans);
    assertTrue(battle.getBattleTracker().wasConquered(sinkiang));
    bridge.setStepName("NonCombatMove");
    moveDelegate.setDelegateBridgeAndPlayer(bridge);
    moveDelegate.start();
    final Territory russia = gameData.getMap().getTerritory("Russia");
    // move two tanks from russia, then undo
    final Route r = new Route();
    r.setStart(russia);
    r.add(novo);
    r.add(sinkiang);
    assertNull(moveDelegate.move(russia.getUnits().getMatches(Matches.unitCanBlitz()), r));
    moveDelegate.undoMove(0);
    assertTrue(battle.getBattleTracker().wasConquered(sinkiang));
    // now move the planes into the territory
    assertNull(moveDelegate.move(russia.getUnits().getMatches(Matches.unitIsAir()), r));
    // make sure they can't land, they can't because the territory was conquered
    assertEquals(1, moveDelegate.getTerritoriesWhereAirCantLand().size());
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) UnitType(games.strategy.engine.data.UnitType) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Example 45 with ScriptedRandomSource

use of games.strategy.engine.random.ScriptedRandomSource in project triplea by triplea-game.

the class RevisedTest method testOverFlyBombersDies.

@Test
public void testOverFlyBombersDies() {
    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. the bomber no longer exists
    assertTrue(se.getUnits().getMatches(Matches.unitIsStrategicBomber()).isEmpty());
    assertTrue(we.getUnits().getMatches(Matches.unitIsStrategicBomber()).isEmpty());
    assertTrue(uk.getUnits().getMatches(Matches.unitIsStrategicBomber()).isEmpty());
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) 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