|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.List; |
| 3 | + |
| 4 | +final class FlowerFieldBoard { |
| 5 | + |
| 6 | + private static final char FLOWER_CHAR = '*'; |
| 7 | + |
| 8 | + private static final char SPACE_CHAR = ' '; |
| 9 | + |
| 10 | + private final List<String> rawRepresentation; |
| 11 | + |
| 12 | + private final int numberOfRows; |
| 13 | + |
| 14 | + private final int numberOfColumns; |
| 15 | + |
| 16 | + FlowerFieldBoard(final List<String> rawRepresentation) { |
| 17 | + this.rawRepresentation = rawRepresentation; |
| 18 | + this.numberOfRows = rawRepresentation.size(); |
| 19 | + this.numberOfColumns = rawRepresentation.isEmpty() ? 0 : rawRepresentation.get(0).length(); |
| 20 | + } |
| 21 | + |
| 22 | + List<String> withNumbers() { |
| 23 | + final List<String> result = new ArrayList<>(); |
| 24 | + |
| 25 | + for (int rowNumber = 0; rowNumber < numberOfRows; rowNumber++) { |
| 26 | + result.add(getRowWithNumbers(rowNumber)); |
| 27 | + } |
| 28 | + |
| 29 | + return result; |
| 30 | + } |
| 31 | + |
| 32 | + private String getRowWithNumbers(final int rowNumber) { |
| 33 | + StringBuilder result = new StringBuilder(numberOfColumns); |
| 34 | + |
| 35 | + for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++) { |
| 36 | + result.append(getCellNumber(rowNumber, columnNumber)); |
| 37 | + } |
| 38 | + |
| 39 | + return result.toString(); |
| 40 | + } |
| 41 | + |
| 42 | + private char getCellNumber(final int rowNumber, final int columnNumber) { |
| 43 | + // If (rowNumber, columnNumber) is a flower, we're done. |
| 44 | + if (rawRepresentation.get(rowNumber).charAt(columnNumber) == FLOWER_CHAR) { |
| 45 | + return FLOWER_CHAR; |
| 46 | + } |
| 47 | + |
| 48 | + final int flowerCount = computeFlowerCountAround(rowNumber, columnNumber); |
| 49 | + |
| 50 | + // If computed count is positive, add it to the annotated row. Otherwise, add a blank space. |
| 51 | + return flowerCount > 0 ? Character.forDigit(flowerCount, 10) : SPACE_CHAR; |
| 52 | + } |
| 53 | + |
| 54 | + private int computeFlowerCountAround(final int rowNumber, final int columnNumber) { |
| 55 | + int result = 0; |
| 56 | + |
| 57 | + // Compute row and column ranges to inspect (respecting board edges). |
| 58 | + final int minRowToInspect = Math.max(rowNumber - 1, 0); |
| 59 | + final int maxRowToInspect = Math.min(rowNumber + 1, numberOfRows - 1); |
| 60 | + final int minColToInspect = Math.max(columnNumber - 1, 0); |
| 61 | + final int maxColToInspect = Math.min(columnNumber + 1, numberOfColumns - 1); |
| 62 | + |
| 63 | + // Count flowers in the cells surrounding (row, col). |
| 64 | + for (int rowToInspect = minRowToInspect; rowToInspect <= maxRowToInspect; rowToInspect++) { |
| 65 | + for (int colToInspect = minColToInspect; colToInspect <= maxColToInspect; colToInspect++) { |
| 66 | + if (rawRepresentation.get(rowToInspect).charAt(colToInspect) == FLOWER_CHAR) { |
| 67 | + result += 1; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return result; |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments