1.1 --- a/minesweeper/src/main/java/org/apidesign/demo/minesweeper/MinesModel.java Fri Feb 07 17:22:58 2014 +0100
1.2 +++ b/minesweeper/src/main/java/org/apidesign/demo/minesweeper/MinesModel.java Fri Feb 07 20:22:14 2014 +0100
1.3 @@ -77,7 +77,7 @@
1.4 return name().startsWith("N_");
1.5 }
1.6
1.7 - final SquareType moreBombs() {
1.8 + final SquareType moreBombsAround() {
1.9 switch (this) {
1.10 case EXPLOSION:
1.11 case UNKNOWN:
1.12 @@ -121,8 +121,10 @@
1.13 List<Integer> yBombs = new ArrayList<Integer>();
1.14 final List<Row> rows = model.getRows();
1.15 boolean emptyHidden = false;
1.16 + SquareType[][] arr = new SquareType[rows.size()][];
1.17 for (int y = 0; y < rows.size(); y++) {
1.18 final List<Square> columns = rows.get(y).getColumns();
1.19 + arr[y] = new SquareType[columns.size()];
1.20 for (int x = 0; x < columns.size(); x++) {
1.21 Square sq = columns.get(x);
1.22 if (sq.isMine()) {
1.23 @@ -130,7 +132,7 @@
1.24 yBombs.add(y);
1.25 }
1.26 if (sq.getState().isVisible()) {
1.27 - sq.setState(SquareType.N_0);
1.28 + arr[y][x] = SquareType.N_0;
1.29 } else {
1.30 if (!sq.isMine()) {
1.31 emptyHidden = true;
1.32 @@ -142,7 +144,17 @@
1.33 int x = xBombs.get(i);
1.34 int y = yBombs.get(i);
1.35
1.36 - incrementAround(model, x, y);
1.37 + incrementAround(arr, x, y);
1.38 + }
1.39 + for (int y = 0; y < rows.size(); y++) {
1.40 + final List<Square> columns = rows.get(y).getColumns();
1.41 + for (int x = 0; x < columns.size(); x++) {
1.42 + Square sq = columns.get(x);
1.43 + final SquareType newState = arr[y][x];
1.44 + if (newState != null && newState != sq.getState()) {
1.45 + sq.setState(newState);
1.46 + }
1.47 + }
1.48 }
1.49
1.50 if (!emptyHidden) {
1.51 @@ -151,25 +163,27 @@
1.52 }
1.53 }
1.54
1.55 - private static void incrementAround(Mines model, int x, int y) {
1.56 - incrementAt(model, x - 1, y - 1);
1.57 - incrementAt(model, x - 1, y);
1.58 - incrementAt(model, x - 1, y + 1);
1.59 + private static void incrementAround(SquareType[][] arr, int x, int y) {
1.60 + incrementAt(arr, x - 1, y - 1);
1.61 + incrementAt(arr, x - 1, y);
1.62 + incrementAt(arr, x - 1, y + 1);
1.63
1.64 - incrementAt(model, x + 1, y - 1);
1.65 - incrementAt(model, x + 1, y);
1.66 - incrementAt(model, x + 1, y + 1);
1.67 + incrementAt(arr, x + 1, y - 1);
1.68 + incrementAt(arr, x + 1, y);
1.69 + incrementAt(arr, x + 1, y + 1);
1.70
1.71 - incrementAt(model, x, y - 1);
1.72 - incrementAt(model, x, y + 1);
1.73 + incrementAt(arr, x, y - 1);
1.74 + incrementAt(arr, x, y + 1);
1.75 }
1.76
1.77 - private static void incrementAt(Mines model, int x, int y) {
1.78 - if (y >= 0 && y < model.getRows().size()) {
1.79 - Row r = model.getRows().get(y);
1.80 - if (x >= 0 && x < r.getColumns().size()) {
1.81 - Square sq = r.getColumns().get(x);
1.82 - sq.setState(sq.getState().moreBombs());
1.83 + private static void incrementAt(SquareType[][] arr, int x, int y) {
1.84 + if (y >= 0 && y < arr.length) {
1.85 + SquareType[] r = arr[y];
1.86 + if (x >= 0 && x < r.length) {
1.87 + SquareType sq = r[x];
1.88 + if (sq != null) {
1.89 + r[x] = sq.moreBombsAround();
1.90 + }
1.91 }
1.92 }
1.93 }