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