package inf101.v18.connectfour.events.random; import inf101.v18.connectfour.board.IBoard; import inf101.v18.connectfour.board.IVerticalBoard; import inf101.v18.connectfour.objects.Empty; import inf101.v18.connectfour.objects.IPlayerObject; import java.util.ArrayList; /** * An event which removes objects from the board. */ public class Bomb extends RandomEvent { void act(IVerticalBoard b) { int x = random.nextInt(b.getWidth()); int y = b.getTopObject(x, IPlayerObject.class); for (ArrayList list : generateSquare(b, x, y, random.nextInt(2) + 1)) { b.set(list.get(0), list.get(1), new Empty()); } } /** * Gets all locations in a square a range from a point. * * @param b A board * @param x The x index of the starting point * @param y The y index of the starting point * @param range The range of the square * @return A list of lists of integers */ private ArrayList> generateSquare(IBoard b, int x, int y, int range) { ArrayList> locations = new ArrayList<>(); for (int i = x - range; i <= x + range; i++) { for (int j = y - range; j <= y + range; j++) { if (i >= 0 && i < b.getWidth() && j >= 0 && j < b.getHeight()) { ArrayList coords = new ArrayList<>(); coords.add(i); coords.add(j); locations.add(coords); } } } return locations; } @Override public String announcement() { return "A bomb just exploded!"; } }