public AI(Map map, Map targetMap) { this.map = map; this.targetMap = targetMap; // Wait for its turn BattleSystem.INSTANCE.turn.addObserver(this); // Generate list of available target indexes to shoot targets = new ArrayList<>(); int counter = 0; for (ListIterator i = targetMap.getSpots().listIterator(); i.hasNext(); ) { i.next(); targets.add(new Integer(counter++)); } }
public void placeShips() { // List of available indexes for placing a new ship List<Integer> availableIndexes = new ArrayList<>(); // All available spots: List<Spot> spots = BattleSystem.INSTANCE.enemyMap.getSpots(); // Get all available indexes: int counter = 0; for (ListIterator i = spots.listIterator(); i.hasNext(); ) { i.next(); availableIndexes.add(new Integer(counter++)); } ShipSet shipSet = BattleSystem.INSTANCE.enemyShipSet; while (shipSet.size() > 0) { Ship nextShip = shipSet.pop(); int index = randomIndex(availableIndexes); try { Spot chosen = map.getSpots().get(index); map.placeShip(chosen, nextShip); System.out.println("*** SPOILER ALERT *** | placed ship at " + chosen); // Remove index from availableIndexes: availableIndexes.remove(new Integer(index)); } catch (InvalidShipPositionException ie) { // Remove index from availableIndexes: availableIndexes.remove(new Integer(index)); // Restore ship into ShipSet shipSet.restorePopped(); } catch (Exception e) { break; } } }