SpaceBattleSimulation { protected static boolean battle(List<Ship> friendly, List<Ship> enemy){ Ship friendlyShip = friendly.remove(0); Ship enemyShip = enemy.remove(0); int friendlyFleetSize = friendly.size() + 1; int enemyFleetSize = enemy.size() + 1; while(friendlyFleetSize > 0 && enemyFleetSize > 0){ engagement(enemyShip, friendlyShip); if(friendlyShip.isDestroyed){ if(friendly.size() > 0){ friendlyShip = friendly.remove(0); } --friendlyFleetSize; } if(enemyShip.isDestroyed){ if(enemy.size() > 0){ enemyShip = enemy.remove(0); } --enemyFleetSize; } } return friendlyFleetSize > 0 && enemyFleetSize == 0; }  static void main(String[] args); static void engagement(Ship enemy, Ship friend);  }
@Test public void battleTestEnemyWins() { SpaceBattleSimulation sim = new SpaceBattleSimulation(); List<SpaceBattleSimulation.Ship> friendly = new ArrayList<SpaceBattleSimulation.Ship>(); List<SpaceBattleSimulation.Ship> enemy = new ArrayList<SpaceBattleSimulation.Ship>(); friendly.add(sim.new Dreadnaught()); friendly.add(sim.new PatrolVessel()); enemy.add(sim.new Bomber()); enemy.add(sim.new Bomber()); enemy.add(sim.new Destroyer()); boolean isVictory = SpaceBattleSimulation.battle(friendly, enemy); if(isVictory){ System.out.println("Friendly wins!"); } else{ System.out.println("Enemy Wins!"); } }
@Test public void battleTestEnemyLoses() { SpaceBattleSimulation sim = new SpaceBattleSimulation(); List<SpaceBattleSimulation.Ship> friendly = new ArrayList<SpaceBattleSimulation.Ship>(); List<SpaceBattleSimulation.Ship> enemy = new ArrayList<SpaceBattleSimulation.Ship>(); friendly.add(sim.new Dreadnaught()); friendly.add(sim.new PatrolVessel()); enemy.add(sim.new Destroyer()); enemy.add(sim.new Bomber()); enemy.add(sim.new Bomber()); boolean isVictory = SpaceBattleSimulation.battle(friendly, enemy); if(isVictory){ System.out.println("Friendly wins!"); } else{ System.out.println("Enemy Wins!"); } }