You are viewing a single comment's thread. Return to all comments →
public static List<String> bomberMan(int n, List<String> grid) { // Write your code here if(n == 1){ return grid; } if(n % 2 == 0){ List<String> ans = new ArrayList<>(); for(int i = 0;i<grid.size();i++){ StringBuilder sb = new StringBuilder(); for(int j = 0;j<grid.get(0).length();j++){ sb.append('O'); } ans.add(sb.toString()); } return ans; } if(n % 4 == 1){ return bombMap(bombMap(grid)); } // n % 4 == 3 return bombMap(grid); } private static List<String> bombMap(List<String> grid){ List<String> ans = new ArrayList<>(); for(int i = 0;i<grid.size();i++){ String row = grid.get(i); StringBuilder sb = new StringBuilder(); for(int j = 0;j<row.length();j++){ if(row.charAt(j) == 'O'){ sb.append('.'); continue; } if(checkBomb(grid,i,j)){ sb.append('.'); }else{ sb.append('O'); } } ans.add(sb.toString()); } return ans; } private static boolean checkBomb(List<String> grid,int i,int j){ if(i - 1 >= 0 && grid.get(i-1).charAt(j) == 'O'){ return true; } if(i + 1 < grid.size() && grid.get(i+1).charAt(j) == 'O'){ return true; } if(j - 1 >= 0 && grid.get(i).charAt(j-1) == 'O'){ return true; } int len = grid.get(0).length(); if(j + 1 < len && grid.get(i).charAt(j+1) == 'O'){ return true; } return false; }
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 →