minesweeper/src/main/java/org/apidesign/demo/minesweeper/MinesModel.java
changeset 107 f8f222243d3c
parent 79 03bec9dcc860
child 108 ceebcfdcc742
     1.1 --- a/minesweeper/src/main/java/org/apidesign/demo/minesweeper/MinesModel.java	Mon Feb 10 16:50:08 2014 +0100
     1.2 +++ b/minesweeper/src/main/java/org/apidesign/demo/minesweeper/MinesModel.java	Sun Mar 16 08:30:22 2014 +0100
     1.3 @@ -111,13 +111,23 @@
     1.4      }
     1.5      
     1.6      @ModelOperation static void init(Mines model, int width, int height, int mines) {
     1.7 -        List<Row> rows = new ArrayList<Row>(height);
     1.8 -        for (int y = 0; y < height; y++) {
     1.9 -            Square[] columns = new Square[width];
    1.10 -            for (int x = 0; x < width; x++) {
    1.11 -                columns[x] = new Square(SquareType.UNKNOWN, false);
    1.12 +        List<Row> rows = model.getRows();
    1.13 +        if (rows.size() != height || rows.get(0).getColumns().size() != width) {
    1.14 +            rows = new ArrayList<Row>(height);
    1.15 +            for (int y = 0; y < height; y++) {
    1.16 +                Square[] columns = new Square[width];
    1.17 +                for (int x = 0; x < width; x++) {
    1.18 +                    columns[x] = new Square(SquareType.UNKNOWN, false);
    1.19 +                }
    1.20 +                rows.add(new Row(columns));
    1.21              }
    1.22 -            rows.add(new Row(columns));
    1.23 +        } else {
    1.24 +            for (Row row : rows) {
    1.25 +                for (Square sq : row.getColumns()) {
    1.26 +                    sq.setState(SquareType.UNKNOWN);
    1.27 +                    sq.setMine(false);
    1.28 +                }
    1.29 +            }
    1.30          }
    1.31          
    1.32          Random r = new Random();
    1.33 @@ -133,8 +143,10 @@
    1.34          }
    1.35  
    1.36          model.setState(GameState.IN_PROGRESS);
    1.37 -        model.getRows().clear();
    1.38 -        model.getRows().addAll(rows);
    1.39 +        if (rows != model.getRows()) {
    1.40 +            model.getRows().clear();
    1.41 +            model.getRows().addAll(rows);
    1.42 +        }
    1.43      }
    1.44      
    1.45      @ModelOperation static void computeMines(Mines model) {
    1.46 @@ -233,6 +245,7 @@
    1.47                  } else {
    1.48                      TOUCH.play();
    1.49                      expandKnown(model, data);
    1.50 +                    model.computeMines();
    1.51                  }
    1.52              break;
    1.53          }
    1.54 @@ -260,9 +273,18 @@
    1.55          }
    1.56          final Square sq = columns.get(x);
    1.57          if (sq.getState() == SquareType.UNKNOWN) {
    1.58 -            sq.setState(SquareType.N_0);
    1.59 -            model.computeMines();
    1.60 -            if (sq.getState() == SquareType.N_0) {
    1.61 +            int around = 
    1.62 +                minesAt(model, x - 1, y - 1) +
    1.63 +                minesAt(model, x - 1, y) +
    1.64 +                minesAt(model, x - 1, y + 1) +
    1.65 +                minesAt(model, x , y - 1) +
    1.66 +                minesAt(model, x, y + 1) +
    1.67 +                minesAt(model, x + 1, y - 1) +
    1.68 +                minesAt(model, x + 1, y) +
    1.69 +                minesAt(model, x + 1, y + 1);
    1.70 +            final SquareType t = SquareType.valueOf("N_" + around);
    1.71 +            sq.setState(t);
    1.72 +            if (t == SquareType.N_0) {
    1.73                  expandKnown(model, x - 1, y - 1);
    1.74                  expandKnown(model, x - 1, y);
    1.75                  expandKnown(model, x - 1, y + 1);
    1.76 @@ -274,4 +296,17 @@
    1.77              }
    1.78          }
    1.79      }
    1.80 +    
    1.81 +    private static int minesAt(Mines model, int x, int y) {
    1.82 +        if (y < 0 || y >= model.getRows().size()) {
    1.83 +            return 0;
    1.84 +        }
    1.85 +        final List<Square> columns = model.getRows().get(y).getColumns();
    1.86 +        if (x < 0 || x >= columns.size()) {
    1.87 +            return 0;
    1.88 +        }
    1.89 +        Square sq = columns.get(x);
    1.90 +        return sq.isMine() ? 1 : 0;
    1.91 +    }
    1.92 +    
    1.93  }