minesweeper/src/main/java/org/apidesign/demo/minesweeper/MinesModel.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 28 Jun 2015 06:27:44 +0200
changeset 234 29c53842c02a
parent 227 fd26342cf23d
permissions -rw-r--r--
Remembering reference to the model
jtulach@63
     1
/**
jtulach@63
     2
 * The MIT License (MIT)
jtulach@63
     3
 *
jtulach@63
     4
 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@63
     5
 *
jtulach@63
     6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
jtulach@63
     7
 * of this software and associated documentation files (the "Software"), to deal
jtulach@63
     8
 * in the Software without restriction, including without limitation the rights
jtulach@63
     9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jtulach@63
    10
 * copies of the Software, and to permit persons to whom the Software is
jtulach@63
    11
 * furnished to do so, subject to the following conditions:
jtulach@63
    12
 *
jtulach@63
    13
 * The above copyright notice and this permission notice shall be included in
jtulach@63
    14
 * all copies or substantial portions of the Software.
jtulach@63
    15
 *
jtulach@63
    16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jtulach@63
    17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jtulach@63
    18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jtulach@63
    19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jtulach@63
    20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jtulach@63
    21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jtulach@63
    22
 * THE SOFTWARE.
jtulach@63
    23
 */
jtulach@63
    24
package org.apidesign.demo.minesweeper;
jtulach@63
    25
jtulach@63
    26
import java.util.ArrayList;
jtulach@63
    27
import java.util.List;
jtulach@63
    28
import java.util.Random;
jtulach@63
    29
import net.java.html.json.ComputedProperty;
jtulach@63
    30
import net.java.html.json.Function;
jtulach@63
    31
import net.java.html.json.Model;
jtulach@63
    32
import net.java.html.json.ModelOperation;
jtulach@63
    33
import net.java.html.json.Property;
jtulach@79
    34
import net.java.html.sound.AudioClip;
jtulach@63
    35
jtulach@63
    36
/** Model of the mine field.
jtulach@63
    37
 */
jaroslav@227
    38
@Model(className = "Mines", targetId = "", properties = {
jtulach@63
    39
    @Property(name = "state", type = MinesModel.GameState.class),
jtulach@63
    40
    @Property(name = "rows", type = Row.class, array = true),
jtulach@63
    41
})
jtulach@90
    42
public final class MinesModel {
jtulach@63
    43
    enum GameState {
jaroslav@164
    44
        IN_PROGRESS, MARKING_MINE, WON, LOST;
jtulach@63
    45
    }
jtulach@63
    46
    
jaroslav@166
    47
    @ComputedProperty static String gameStyle(GameState state) {
jtulach@178
    48
        return state == GameState.MARKING_MINE ? "MARKING" : "PLAYING";
jaroslav@166
    49
    }
jaroslav@166
    50
    
jtulach@63
    51
    @Model(className = "Row", properties = {
jtulach@63
    52
        @Property(name = "columns", type = Square.class, array = true)
jtulach@63
    53
    })
jtulach@63
    54
    static class RowModel {
jtulach@63
    55
    }
jtulach@63
    56
jtulach@63
    57
    @Model(className = "Square", properties = {
jtulach@63
    58
        @Property(name = "state", type = SquareType.class),
jtulach@63
    59
        @Property(name = "mine", type = boolean.class)
jtulach@63
    60
    })
jtulach@63
    61
    static class SquareModel {
jtulach@65
    62
        @ComputedProperty static String style(SquareType state) {
jtulach@65
    63
            return state == null ? null : state.toString();
jtulach@65
    64
        }
jtulach@63
    65
    }
jtulach@63
    66
    
jtulach@63
    67
    enum SquareType {
jtulach@63
    68
        N_0, N_1, N_2, N_3, N_4, N_5, N_6, N_7, N_8,
jaroslav@164
    69
        UNKNOWN, EXPLOSION, DISCOVERED, MARKED;
jtulach@64
    70
        
jtulach@64
    71
        final boolean isVisible() {
jtulach@64
    72
            return name().startsWith("N_");
jtulach@64
    73
        }
jtulach@64
    74
jtulach@69
    75
        final SquareType moreBombsAround() {
jtulach@68
    76
            switch (this) {
jtulach@68
    77
                case EXPLOSION:
jtulach@68
    78
                case UNKNOWN:
jtulach@68
    79
                case DISCOVERED:
jtulach@68
    80
                case N_8:
jtulach@68
    81
                    return this;
jtulach@64
    82
            }
jtulach@64
    83
            return values()[ordinal() + 1];
jtulach@64
    84
        }
jtulach@63
    85
    }
jtulach@63
    86
    
jtulach@76
    87
    @ComputedProperty static boolean fieldShowing(GameState state) {
jtulach@76
    88
        return state != null;
jtulach@76
    89
    }
jtulach@76
    90
    
jaroslav@176
    91
    @ComputedProperty static boolean gameInProgress(GameState state) {
jaroslav@176
    92
        return state == GameState.IN_PROGRESS;
jaroslav@176
    93
    }
jaroslav@176
    94
    
jtulach@76
    95
    @Function static void showHelp(Mines model) {
jtulach@76
    96
        model.setState(null);
jtulach@76
    97
    }
jtulach@76
    98
    
jtulach@70
    99
    @Function static void smallGame(Mines model) {
jtulach@70
   100
        model.init(5, 5, 5);
jtulach@70
   101
    }
jtulach@70
   102
    @Function static void normalGame(Mines model) {
jtulach@70
   103
        model.init(10, 10, 10);
jtulach@70
   104
    }
jtulach@70
   105
    
jtulach@72
   106
    @Function static void giveUp(Mines model) {
jtulach@72
   107
        showAllBombs(model, SquareType.EXPLOSION);
jaroslav@152
   108
        model.setState(GameState.LOST);
jtulach@72
   109
    }
jtulach@72
   110
    
jaroslav@164
   111
    @Function static void markMine(Mines model) {
jaroslav@164
   112
        if (model.getState() == GameState.IN_PROGRESS) {
jaroslav@164
   113
            model.setState(GameState.MARKING_MINE);
jaroslav@164
   114
        }
jaroslav@164
   115
    }
jaroslav@164
   116
    
jtulach@63
   117
    @ModelOperation static void init(Mines model, int width, int height, int mines) {
jtulach@107
   118
        List<Row> rows = model.getRows();
jtulach@107
   119
        if (rows.size() != height || rows.get(0).getColumns().size() != width) {
jtulach@107
   120
            rows = new ArrayList<Row>(height);
jtulach@107
   121
            for (int y = 0; y < height; y++) {
jtulach@107
   122
                Square[] columns = new Square[width];
jtulach@107
   123
                for (int x = 0; x < width; x++) {
jtulach@107
   124
                    columns[x] = new Square(SquareType.UNKNOWN, false);
jtulach@107
   125
                }
jtulach@107
   126
                rows.add(new Row(columns));
jtulach@63
   127
            }
jtulach@107
   128
        } else {
jtulach@107
   129
            for (Row row : rows) {
jtulach@107
   130
                for (Square sq : row.getColumns()) {
jtulach@107
   131
                    sq.setState(SquareType.UNKNOWN);
jtulach@107
   132
                    sq.setMine(false);
jtulach@107
   133
                }
jtulach@107
   134
            }
jtulach@63
   135
        }
jtulach@63
   136
        
jtulach@63
   137
        Random r = new Random();
jtulach@63
   138
        while (mines > 0) {
jtulach@63
   139
            int x = r.nextInt(width);
jtulach@63
   140
            int y = r.nextInt(height);
jtulach@63
   141
            final Square s = rows.get(y).getColumns().get(x);
jtulach@63
   142
            if (s.isMine()) {
jtulach@63
   143
                continue;
jtulach@63
   144
            }
jtulach@63
   145
            s.setMine(true);
jtulach@63
   146
            mines--;
jtulach@63
   147
        }
jtulach@63
   148
jtulach@63
   149
        model.setState(GameState.IN_PROGRESS);
jtulach@107
   150
        if (rows != model.getRows()) {
jtulach@107
   151
            model.getRows().clear();
jtulach@107
   152
            model.getRows().addAll(rows);
jtulach@107
   153
        }
jtulach@63
   154
    }
jtulach@63
   155
    
jtulach@64
   156
    @ModelOperation static void computeMines(Mines model) {
jtulach@64
   157
        List<Integer> xBombs = new ArrayList<Integer>();
jtulach@64
   158
        List<Integer> yBombs = new ArrayList<Integer>();
jtulach@64
   159
        final List<Row> rows = model.getRows();
jtulach@66
   160
        boolean emptyHidden = false;
jtulach@69
   161
        SquareType[][] arr = new SquareType[rows.size()][];
jtulach@64
   162
        for (int y = 0; y < rows.size(); y++) {
jtulach@64
   163
            final List<Square> columns = rows.get(y).getColumns();
jtulach@69
   164
            arr[y] = new SquareType[columns.size()];
jtulach@64
   165
            for (int x = 0; x < columns.size(); x++) {
jtulach@64
   166
                Square sq = columns.get(x);
jtulach@64
   167
                if (sq.isMine()) {
jtulach@64
   168
                    xBombs.add(x);
jtulach@64
   169
                    yBombs.add(y);
jtulach@64
   170
                }
jtulach@64
   171
                if (sq.getState().isVisible()) {
jtulach@69
   172
                    arr[y][x] = SquareType.N_0;
jtulach@66
   173
                } else {
jtulach@66
   174
                    if (!sq.isMine()) {
jtulach@66
   175
                        emptyHidden = true;
jtulach@66
   176
                    }
jtulach@64
   177
                }
jtulach@64
   178
            }
jtulach@64
   179
        }
jtulach@64
   180
        for (int i = 0; i < xBombs.size(); i++) {
jtulach@64
   181
            int x = xBombs.get(i);
jtulach@64
   182
            int y = yBombs.get(i);
jtulach@64
   183
            
jtulach@69
   184
            incrementAround(arr, x, y);
jtulach@69
   185
        }
jtulach@69
   186
        for (int y = 0; y < rows.size(); y++) {
jtulach@69
   187
            final List<Square> columns = rows.get(y).getColumns();
jtulach@69
   188
            for (int x = 0; x < columns.size(); x++) {
jtulach@69
   189
                Square sq = columns.get(x);
jtulach@69
   190
                final SquareType newState = arr[y][x];
jtulach@69
   191
                if (newState != null && newState != sq.getState()) {
jtulach@69
   192
                    sq.setState(newState);
jtulach@69
   193
                }
jtulach@69
   194
            }
jtulach@64
   195
        }
jtulach@66
   196
        
jtulach@66
   197
        if (!emptyHidden) {
jtulach@66
   198
            model.setState(GameState.WON);
jtulach@68
   199
            showAllBombs(model, SquareType.DISCOVERED);
jtulach@153
   200
            AudioClip applause = AudioClip.create("applause.mp3");
jaroslav@136
   201
            applause.play();
jtulach@66
   202
        }
jtulach@64
   203
    }
jtulach@64
   204
    
jtulach@69
   205
    private static void incrementAround(SquareType[][] arr, int x, int y) {
jtulach@69
   206
        incrementAt(arr, x - 1, y - 1);
jtulach@69
   207
        incrementAt(arr, x - 1, y);
jtulach@69
   208
        incrementAt(arr, x - 1, y + 1);
jtulach@64
   209
jtulach@69
   210
        incrementAt(arr, x + 1, y - 1);
jtulach@69
   211
        incrementAt(arr, x + 1, y);
jtulach@69
   212
        incrementAt(arr, x + 1, y + 1);
jtulach@64
   213
        
jtulach@69
   214
        incrementAt(arr, x, y - 1);
jtulach@69
   215
        incrementAt(arr, x, y + 1);
jtulach@64
   216
    }
jtulach@64
   217
    
jtulach@69
   218
    private static void incrementAt(SquareType[][] arr, int x, int y) {
jtulach@69
   219
        if (y >= 0 && y < arr.length) {
jtulach@69
   220
            SquareType[] r = arr[y];
jtulach@69
   221
            if (x >= 0 && x < r.length) {
jtulach@69
   222
                SquareType sq = r[x];
jtulach@69
   223
                if (sq != null) {
jtulach@69
   224
                    r[x] = sq.moreBombsAround();
jtulach@69
   225
                }
jtulach@64
   226
            }
jtulach@64
   227
        }
jtulach@64
   228
    }
jtulach@64
   229
    
jtulach@68
   230
    static void showAllBombs(Mines model, SquareType state) {
jtulach@63
   231
        for (Row row : model.getRows()) {
jtulach@63
   232
            for (Square square : row.getColumns()) {
jtulach@63
   233
                if (square.isMine()) {
jtulach@68
   234
                    square.setState(state);
jtulach@63
   235
                }
jtulach@63
   236
            }
jtulach@63
   237
        }
jtulach@63
   238
    }
jtulach@63
   239
    
jtulach@63
   240
    @Function static void click(Mines model, Square data) {
jaroslav@164
   241
        if (model.getState() == GameState.MARKING_MINE) {
jaroslav@164
   242
            if (data.getState() == SquareType.UNKNOWN) {
jaroslav@164
   243
                data.setState(SquareType.MARKED);
jaroslav@165
   244
                if (allMarked(model)) {
jaroslav@165
   245
                    model.setState(GameState.WON);
jaroslav@165
   246
                    return;
jaroslav@165
   247
                }
jaroslav@164
   248
            }
jaroslav@164
   249
            model.setState(GameState.IN_PROGRESS);
jaroslav@164
   250
            return;
jaroslav@164
   251
        }
jtulach@63
   252
        if (model.getState() != GameState.IN_PROGRESS) {
jtulach@63
   253
            return;
jtulach@63
   254
        }
jaroslav@164
   255
        if (data.getState() == SquareType.MARKED) {
jaroslav@164
   256
            data.setState(SquareType.UNKNOWN);
jtulach@174
   257
            if (allMarked(model)) {
jtulach@174
   258
                model.setState(GameState.WON);
jtulach@174
   259
            }
jaroslav@164
   260
            return;
jaroslav@164
   261
        }
jaroslav@152
   262
        if (data.getState() != SquareType.UNKNOWN) {
jaroslav@152
   263
            return;
jaroslav@152
   264
        }
jaroslav@152
   265
        if (data.isMine()) {
jaroslav@152
   266
            Square fair = atLeastOnePlaceWhereBombCantBe(model);
jaroslav@152
   267
            if (fair == null) {
jaroslav@152
   268
                if (placeBombElseWhere(model, data)) {
jaroslav@152
   269
                    cleanedUp(model, data);
jaroslav@152
   270
                    return;
jtulach@63
   271
                }
jaroslav@152
   272
            }
jaroslav@152
   273
            explosion(model);
jaroslav@152
   274
        } else {
jaroslav@152
   275
            Square takeFrom = tryStealBomb(model, data);
jaroslav@152
   276
            if (takeFrom != null) {
jaroslav@152
   277
                final Square fair = atLeastOnePlaceWhereBombCantBe(model);
jaroslav@152
   278
                if (fair != null) {
jaroslav@152
   279
                    takeFrom.setMine(false);
jaroslav@152
   280
                    data.setMine(true);
jaroslav@152
   281
                    explosion(model);
jaroslav@152
   282
                    return;
jaroslav@152
   283
                }
jaroslav@152
   284
            }
jaroslav@152
   285
            cleanedUp(model, data);
jtulach@63
   286
        }
jtulach@63
   287
    }
jaroslav@152
   288
jaroslav@152
   289
    private static void cleanedUp(Mines model, Square data) {
jaroslav@152
   290
        AudioClip touch = AudioClip.create("move.mp3");
jaroslav@152
   291
        touch.play();
jaroslav@152
   292
        expandKnown(model, data);
jaroslav@152
   293
        model.computeMines();
jaroslav@152
   294
    }
jaroslav@152
   295
jaroslav@152
   296
    private static void explosion(Mines model) {
jaroslav@152
   297
        showAllBombs(model, SquareType.EXPLOSION);
jaroslav@152
   298
        model.setState(GameState.LOST);
jtulach@153
   299
        AudioClip oops = AudioClip.create("oops.mp3");
jaroslav@152
   300
        oops.play();
jaroslav@152
   301
    }
jaroslav@152
   302
    
jaroslav@152
   303
    private static Square tryStealBomb(Mines model, Square data) {
jaroslav@152
   304
        data.setMine(true);
jaroslav@152
   305
        final List<Row> rows = model.getRows();
jaroslav@152
   306
        for (int y = 0; y < rows.size(); y++) {
jaroslav@152
   307
            final List<Square> columns = rows.get(y).getColumns();
jaroslav@152
   308
            for (int x = 0; x < columns.size(); x++) {
jaroslav@152
   309
                Square sq = columns.get(x);
jaroslav@152
   310
                if (sq == data) {
jaroslav@152
   311
                    continue;
jaroslav@152
   312
                }
jaroslav@152
   313
                if (sq.isMine()) {
jaroslav@152
   314
                    sq.setMine(false);
jaroslav@152
   315
                    final boolean ok = isConsistent(model);
jaroslav@152
   316
                    sq.setMine(true);
jaroslav@152
   317
                    if (ok) {
jaroslav@152
   318
                        data.setMine(false);
jaroslav@152
   319
                        return sq;
jaroslav@152
   320
                    }
jaroslav@152
   321
                }
jaroslav@152
   322
            }
jaroslav@152
   323
        }
jaroslav@152
   324
        data.setMine(false);        
jaroslav@152
   325
        return null;
jaroslav@152
   326
    }
jaroslav@152
   327
    
jaroslav@152
   328
    private static Square atLeastOnePlaceWhereBombCantBe(Mines model) {
jaroslav@152
   329
        final List<Row> rows = model.getRows();
jaroslav@152
   330
        Square cantBe = null;
jaroslav@152
   331
        int discovered = 0;
jaroslav@152
   332
        for (int y = 0; y < rows.size(); y++) {
jaroslav@152
   333
            final List<Square> columns = rows.get(y).getColumns();
jaroslav@152
   334
            for (int x = 0; x < columns.size(); x++) {
jaroslav@152
   335
                Square sq = columns.get(x);
jaroslav@152
   336
                if (sq.getState() == SquareType.UNKNOWN) {
jaroslav@152
   337
                    if (!sq.isMine()) {
jaroslav@152
   338
                        if (tryStealBomb(model, sq) == null) {
jaroslav@152
   339
                            cantBe = sq;
jaroslav@152
   340
                        }
jaroslav@152
   341
                    }
jaroslav@152
   342
                } else {
jaroslav@152
   343
                    discovered++;
jaroslav@152
   344
                }
jaroslav@152
   345
            }
jaroslav@152
   346
        }
jaroslav@152
   347
        
jaroslav@152
   348
        if (discovered > 5) {
jaroslav@152
   349
            return cantBe;
jaroslav@152
   350
        }
jaroslav@152
   351
        
jaroslav@152
   352
        return null;
jaroslav@152
   353
    }
jaroslav@152
   354
    
jaroslav@152
   355
    private static boolean placeBombElseWhere(Mines model, Square moveBomb) {
jaroslav@152
   356
        List<Square> ok = new ArrayList<Square>();
jaroslav@152
   357
        moveBomb.setMine(false);
jaroslav@152
   358
        final List<Row> rows = model.getRows();
jaroslav@152
   359
        for (int y = 0; y < rows.size(); y++) {
jaroslav@152
   360
            final List<Square> columns = rows.get(y).getColumns();
jaroslav@152
   361
            for (int x = 0; x < columns.size(); x++) {
jaroslav@152
   362
                Square sq = columns.get(x);
jaroslav@152
   363
                if (sq == moveBomb || sq.isMine() || sq.getState().isVisible()) {
jaroslav@152
   364
                    continue;
jaroslav@152
   365
                }
jaroslav@152
   366
                sq.setMine(true);
jaroslav@152
   367
                if (isConsistent(model)) {
jaroslav@152
   368
                    ok.add(sq);
jaroslav@152
   369
                }
jaroslav@152
   370
                sq.setMine(false);
jaroslav@152
   371
            }
jaroslav@152
   372
        }
jaroslav@152
   373
        if (ok.isEmpty()) {
jaroslav@152
   374
            moveBomb.setMine(true);
jaroslav@152
   375
            return false;
jaroslav@152
   376
        } else {
jaroslav@152
   377
            int r = new Random().nextInt(ok.size());
jaroslav@152
   378
            ok.get(r).setMine(true);
jaroslav@152
   379
            return true;
jaroslav@152
   380
        }
jaroslav@152
   381
    }
jaroslav@152
   382
    
jtulach@68
   383
    private static void expandKnown(Mines model, Square data) {
jtulach@68
   384
        final List<Row> rows = model.getRows();
jtulach@68
   385
        for (int y = 0; y < rows.size(); y++) {
jtulach@68
   386
            final List<Square> columns = rows.get(y).getColumns();
jtulach@68
   387
            for (int x = 0; x < columns.size(); x++) {
jtulach@68
   388
                Square sq = columns.get(x);
jtulach@68
   389
                if (sq == data) {
jtulach@68
   390
                    expandKnown(model, x, y);
jtulach@68
   391
                    return;
jtulach@68
   392
                }
jtulach@68
   393
            }
jtulach@68
   394
        }
jtulach@68
   395
    }
jtulach@68
   396
    private static void expandKnown(Mines model, int x , int y) {
jtulach@68
   397
        if (y < 0 || y >= model.getRows().size()) {
jtulach@68
   398
            return;
jtulach@68
   399
        }
jtulach@68
   400
        final List<Square> columns = model.getRows().get(y).getColumns();
jtulach@68
   401
        if (x < 0 || x >= columns.size()) {
jtulach@68
   402
            return;
jtulach@68
   403
        }
jtulach@68
   404
        final Square sq = columns.get(x);
jtulach@68
   405
        if (sq.getState() == SquareType.UNKNOWN) {
jaroslav@152
   406
            int around = around(model, x, y);
jtulach@107
   407
            final SquareType t = SquareType.valueOf("N_" + around);
jtulach@107
   408
            sq.setState(t);
jtulach@107
   409
            if (t == SquareType.N_0) {
jtulach@68
   410
                expandKnown(model, x - 1, y - 1);
jtulach@68
   411
                expandKnown(model, x - 1, y);
jtulach@68
   412
                expandKnown(model, x - 1, y + 1);
jtulach@68
   413
                expandKnown(model, x , y - 1);
jtulach@68
   414
                expandKnown(model, x, y + 1);
jtulach@68
   415
                expandKnown(model, x + 1, y - 1);
jtulach@68
   416
                expandKnown(model, x + 1, y);
jtulach@68
   417
                expandKnown(model, x + 1, y + 1);
jtulach@68
   418
            }
jtulach@68
   419
        }
jtulach@68
   420
    }
jaroslav@152
   421
jaroslav@152
   422
    private static int around(Mines model, int x, int y) {
jaroslav@152
   423
        return 
jaroslav@152
   424
            minesAt(model, x - 1, y - 1) +
jaroslav@152
   425
            minesAt(model, x - 1, y) +
jaroslav@152
   426
            minesAt(model, x - 1, y + 1) +
jaroslav@152
   427
            minesAt(model, x , y - 1) +
jaroslav@152
   428
            minesAt(model, x, y + 1) +
jaroslav@152
   429
            minesAt(model, x + 1, y - 1) +
jaroslav@152
   430
            minesAt(model, x + 1, y) +
jaroslav@152
   431
            minesAt(model, x + 1, y + 1);
jaroslav@152
   432
    }
jtulach@90
   433
    
jtulach@107
   434
    private static int minesAt(Mines model, int x, int y) {
jtulach@107
   435
        if (y < 0 || y >= model.getRows().size()) {
jtulach@107
   436
            return 0;
jtulach@107
   437
        }
jtulach@107
   438
        final List<Square> columns = model.getRows().get(y).getColumns();
jtulach@107
   439
        if (x < 0 || x >= columns.size()) {
jtulach@107
   440
            return 0;
jtulach@107
   441
        }
jtulach@107
   442
        Square sq = columns.get(x);
jtulach@107
   443
        return sq.isMine() ? 1 : 0;
jtulach@107
   444
    }
jaroslav@152
   445
    
jaroslav@152
   446
    private static boolean isConsistent(Mines m) {
jaroslav@152
   447
        for (int row = 0; row < m.getRows().size(); row++) {
jaroslav@152
   448
            Row r = m.getRows().get(row);
jaroslav@152
   449
            for (int col = 0; col < r.getColumns().size(); col++) {
jaroslav@152
   450
                Square sq = r.getColumns().get(col);
jaroslav@152
   451
                if (sq.getState().isVisible()) {
jaroslav@152
   452
                    int around = around(m, col, row);
jaroslav@152
   453
                    if (around != sq.getState().ordinal()) {
jaroslav@152
   454
                        return false;
jaroslav@152
   455
                    }
jaroslav@152
   456
                }
jaroslav@152
   457
            }
jaroslav@152
   458
        }
jaroslav@152
   459
        return true;
jaroslav@152
   460
    }
jaroslav@165
   461
    
jaroslav@165
   462
    private static boolean allMarked(Mines m) {
jaroslav@165
   463
        for (Row r : m.getRows()) {
jaroslav@165
   464
            for (Square sq : r.getColumns()) {
jtulach@174
   465
                if (sq.isMine() == (sq.getState() != SquareType.MARKED)) {
jaroslav@165
   466
                    return false;
jaroslav@165
   467
                }
jaroslav@165
   468
            }
jaroslav@165
   469
        }
jaroslav@165
   470
        for (Row r : m.getRows()) {
jaroslav@165
   471
            for (Square sq : r.getColumns()) {
jaroslav@165
   472
                if (sq.isMine()) {
jaroslav@165
   473
                    sq.setState(SquareType.DISCOVERED);
jaroslav@165
   474
                } else {
jaroslav@165
   475
                    sq.setState(SquareType.N_0);
jaroslav@165
   476
                }
jaroslav@165
   477
            }
jaroslav@165
   478
        }
jaroslav@165
   479
        computeMines(m);
jaroslav@165
   480
        return true;
jaroslav@165
   481
    }
jtulach@116
   482
jaroslav@234
   483
    private static Mines ui;
jtulach@90
   484
    public static void main(String... args) throws Exception {
jaroslav@234
   485
        ui = new Mines();
jaroslav@234
   486
        ui.applyBindings();
jtulach@90
   487
    }
jtulach@63
   488
}