You are viewing a single comment's thread. Return to all comments →
private static final Map<Integer, List<Integer>> bomb = new HashMap<>(); public static List<String> bomberMan(int n, List<String> grid) { List<List<String>> collect = grid.stream() .map(l -> Arrays.stream(l.split("")).collect(Collectors.toList())) .collect(Collectors.toList()); int loop = n / 2; if (n == 1) return grid; if (n % 2 == 0) { fill(collect); return collect.stream().map(l -> String.join("", l)).collect(Collectors.toList()); } else { fill(collect); boom(collect); bomb.clear(); } if (loop % 2 == 0) { fill(collect); boom(collect); } return collect.stream().map(l -> l.stream().collect(Collectors.joining())).collect(Collectors.toList()); } private static void boom(List<List<String>> list) { for (Map.Entry<Integer, List<Integer>> entry : bomb.entrySet()) { int key = entry.getKey(); List<Integer> value = entry.getValue(); if (key == 0) { boomLine(value, list.get(key), true); if (list.size() - 1 > key) boomLine(value, list.get(key + 1), false); } if (key > 0 && key < list.size() - 1) { boomLine(value, list.get(key), true); boomLine(value, list.get(key + 1), false); boomLine(value, list.get(key - 1), false); } if (key == list.size() - 1) { boomLine(value, list.get(key), true); if (key != 0) boomLine(value, list.get(key - 1), false); } } } private static void boomLine(List<Integer> bomb, List<String> list, boolean fillAround) { int size = list.size() - 1; for (int v : bomb) { list.set(v, "."); if (fillAround) { fillAround(list, size, v); } } } private static void fillAround(List<String> list, int size, int bomb) { if (bomb - 1 >= 0) { list.set(bomb - 1, "."); } if (size >= bomb + 1) { list.set(bomb + 1, "."); } } private static void fill(List<List<String>> list) { for (int i = 0; i < list.size(); i++) { List<String> strings = list.get(i); for (int j = 0; j < strings.size(); j++) { AtomicInteger atomicInteger = new AtomicInteger(); atomicInteger.set(j); if (strings.get(j).equals("O")) { bomb.compute(i, (a, b) -> { if (b == null) { List<Integer> integers = new ArrayList<>(); integers.add(atomicInteger.get()); return integers; } b.add(atomicInteger.get()); return b; }); } strings.set(j, "O"); } } }
Seems like cookies are disabled on this browser, please enable them to open this website
The Bomberman Game
You are viewing a single comment's thread. Return to all comments →