minesweeper/src/main/java/org/apidesign/demo/minesweeper/MinesModel.java
author Jaroslav Tulach <jtulach@netbeans.org>
Mon, 08 Sep 2014 17:15:18 +0200
branchnbrwsr
changeset 208 9073c01ba497
parent 178 87d475e1cf75
permissions -rw-r--r--
When there is HTML/Java project, clicking on 'Develop' should open the project wizard
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@208
    26
import java.awt.event.ActionEvent;
jtulach@208
    27
import java.net.MalformedURLException;
jtulach@208
    28
import java.net.URL;
jtulach@63
    29
import java.util.ArrayList;
jtulach@63
    30
import java.util.List;
jtulach@63
    31
import java.util.Random;
jtulach@208
    32
import java.util.logging.Level;
jtulach@208
    33
import java.util.logging.Logger;
jtulach@208
    34
import javax.swing.Action;
jtulach@63
    35
import net.java.html.json.ComputedProperty;
jtulach@63
    36
import net.java.html.json.Function;
jtulach@63
    37
import net.java.html.json.Model;
jtulach@63
    38
import net.java.html.json.ModelOperation;
jtulach@63
    39
import net.java.html.json.Property;
jtulach@79
    40
import net.java.html.sound.AudioClip;
jtulach@208
    41
import org.openide.awt.HtmlBrowser;
jtulach@208
    42
import org.openide.filesystems.FileObject;
jtulach@208
    43
import org.openide.filesystems.FileUtil;
jtulach@208
    44
import org.openide.util.ContextAwareAction;
jtulach@208
    45
import org.openide.util.Exceptions;
jtulach@208
    46
import org.openide.util.Lookup;
jtulach@208
    47
import org.openide.util.Utilities;
jtulach@63
    48
jtulach@63
    49
/** Model of the mine field.
jtulach@63
    50
 */
jtulach@63
    51
@Model(className = "Mines", properties = {
jtulach@63
    52
    @Property(name = "state", type = MinesModel.GameState.class),
jtulach@63
    53
    @Property(name = "rows", type = Row.class, array = true),
jtulach@63
    54
})
jtulach@90
    55
public final class MinesModel {
jtulach@63
    56
    enum GameState {
jaroslav@164
    57
        IN_PROGRESS, MARKING_MINE, WON, LOST;
jtulach@63
    58
    }
jtulach@63
    59
    
jaroslav@166
    60
    @ComputedProperty static String gameStyle(GameState state) {
jtulach@178
    61
        return state == GameState.MARKING_MINE ? "MARKING" : "PLAYING";
jaroslav@166
    62
    }
jaroslav@166
    63
    
jtulach@63
    64
    @Model(className = "Row", properties = {
jtulach@63
    65
        @Property(name = "columns", type = Square.class, array = true)
jtulach@63
    66
    })
jtulach@63
    67
    static class RowModel {
jtulach@63
    68
    }
jtulach@63
    69
jtulach@63
    70
    @Model(className = "Square", properties = {
jtulach@63
    71
        @Property(name = "state", type = SquareType.class),
jtulach@63
    72
        @Property(name = "mine", type = boolean.class)
jtulach@63
    73
    })
jtulach@63
    74
    static class SquareModel {
jtulach@65
    75
        @ComputedProperty static String style(SquareType state) {
jtulach@65
    76
            return state == null ? null : state.toString();
jtulach@65
    77
        }
jtulach@63
    78
    }
jtulach@63
    79
    
jtulach@63
    80
    enum SquareType {
jtulach@63
    81
        N_0, N_1, N_2, N_3, N_4, N_5, N_6, N_7, N_8,
jaroslav@164
    82
        UNKNOWN, EXPLOSION, DISCOVERED, MARKED;
jtulach@64
    83
        
jtulach@64
    84
        final boolean isVisible() {
jtulach@64
    85
            return name().startsWith("N_");
jtulach@64
    86
        }
jtulach@64
    87
jtulach@69
    88
        final SquareType moreBombsAround() {
jtulach@68
    89
            switch (this) {
jtulach@68
    90
                case EXPLOSION:
jtulach@68
    91
                case UNKNOWN:
jtulach@68
    92
                case DISCOVERED:
jtulach@68
    93
                case N_8:
jtulach@68
    94
                    return this;
jtulach@64
    95
            }
jtulach@64
    96
            return values()[ordinal() + 1];
jtulach@64
    97
        }
jtulach@63
    98
    }
jtulach@63
    99
    
jtulach@76
   100
    @ComputedProperty static boolean fieldShowing(GameState state) {
jtulach@76
   101
        return state != null;
jtulach@76
   102
    }
jtulach@76
   103
    
jaroslav@176
   104
    @ComputedProperty static boolean gameInProgress(GameState state) {
jaroslav@176
   105
        return state == GameState.IN_PROGRESS;
jaroslav@176
   106
    }
jaroslav@176
   107
    
jtulach@76
   108
    @Function static void showHelp(Mines model) {
jtulach@76
   109
        model.setState(null);
jtulach@76
   110
    }
jtulach@76
   111
    
jtulach@70
   112
    @Function static void smallGame(Mines model) {
jtulach@70
   113
        model.init(5, 5, 5);
jtulach@70
   114
    }
jtulach@70
   115
    @Function static void normalGame(Mines model) {
jtulach@70
   116
        model.init(10, 10, 10);
jtulach@70
   117
    }
jtulach@70
   118
    
jtulach@72
   119
    @Function static void giveUp(Mines model) {
jtulach@72
   120
        showAllBombs(model, SquareType.EXPLOSION);
jaroslav@152
   121
        model.setState(GameState.LOST);
jtulach@72
   122
    }
jtulach@72
   123
    
jaroslav@164
   124
    @Function static void markMine(Mines model) {
jaroslav@164
   125
        if (model.getState() == GameState.IN_PROGRESS) {
jaroslav@164
   126
            model.setState(GameState.MARKING_MINE);
jaroslav@164
   127
        }
jaroslav@164
   128
    }
jaroslav@164
   129
    
jtulach@208
   130
    private static final Logger LOG = Logger.getLogger(MinesModel.class.getName());
jtulach@208
   131
    @Function static void develop(Mines model) {
jtulach@208
   132
        Action a = null;
jtulach@208
   133
        try {
jtulach@208
   134
            ClassLoader l = Lookup.getDefault().lookup(ClassLoader.class);
jtulach@208
   135
            if (l == null) {
jtulach@208
   136
                l = Thread.currentThread().getContextClassLoader();
jtulach@208
   137
            }
jtulach@208
   138
            if (l == null) {
jtulach@208
   139
                l = MinesModel.class.getClassLoader();
jtulach@208
   140
            }
jtulach@208
   141
            Class<?> newPrj = Class.forName("org.netbeans.spi.project.ui.support.CommonProjectActions", true, l); // NOI18N
jtulach@208
   142
            a = (Action) newPrj.getMethod("newProjectAction").invoke(null); // NOI18N
jtulach@208
   143
        } catch (Exception ex) {
jtulach@208
   144
            LOG.log(Level.FINE, "Cannot find New project action!", ex);
jtulach@208
   145
        }
jtulach@208
   146
        if (a != null) {
jtulach@208
   147
            FileObject fo = FileUtil.getConfigFile("Templates/Project/ClientSide"); // NOI18N
jtulach@208
   148
            if (fo != null) {
jtulach@208
   149
                a.putValue("PRESELECT_CATEGORY", "ClientSide"); // NOI18N
jtulach@208
   150
                for (FileObject template : fo.getChildren()) {
jtulach@208
   151
                    if (template.getName().contains("htmljava")) { // NOI18N
jtulach@208
   152
                        a.putValue("PRESELECT_TEMPLATE", template.getName()); // NOI18N
jtulach@208
   153
                        a.actionPerformed(new ActionEvent(model, 0, null));
jtulach@208
   154
                        return;
jtulach@208
   155
                    }
jtulach@208
   156
                }
jtulach@208
   157
            }
jtulach@208
   158
        } 
jtulach@208
   159
        try {
jtulach@208
   160
            HtmlBrowser.URLDisplayer.getDefault().showURL(new URL("http://wiki.apidesign.org/wiki/DukeScriptInNetBeans")); // NO18N
jtulach@208
   161
        } catch (MalformedURLException ex) {
jtulach@208
   162
            Exceptions.printStackTrace(ex);
jtulach@208
   163
        }
jtulach@208
   164
    }
jtulach@208
   165
    
jtulach@63
   166
    @ModelOperation static void init(Mines model, int width, int height, int mines) {
jtulach@107
   167
        List<Row> rows = model.getRows();
jtulach@107
   168
        if (rows.size() != height || rows.get(0).getColumns().size() != width) {
jtulach@107
   169
            rows = new ArrayList<Row>(height);
jtulach@107
   170
            for (int y = 0; y < height; y++) {
jtulach@107
   171
                Square[] columns = new Square[width];
jtulach@107
   172
                for (int x = 0; x < width; x++) {
jtulach@107
   173
                    columns[x] = new Square(SquareType.UNKNOWN, false);
jtulach@107
   174
                }
jtulach@107
   175
                rows.add(new Row(columns));
jtulach@63
   176
            }
jtulach@107
   177
        } else {
jtulach@107
   178
            for (Row row : rows) {
jtulach@107
   179
                for (Square sq : row.getColumns()) {
jtulach@107
   180
                    sq.setState(SquareType.UNKNOWN);
jtulach@107
   181
                    sq.setMine(false);
jtulach@107
   182
                }
jtulach@107
   183
            }
jtulach@63
   184
        }
jtulach@63
   185
        
jtulach@63
   186
        Random r = new Random();
jtulach@63
   187
        while (mines > 0) {
jtulach@63
   188
            int x = r.nextInt(width);
jtulach@63
   189
            int y = r.nextInt(height);
jtulach@63
   190
            final Square s = rows.get(y).getColumns().get(x);
jtulach@63
   191
            if (s.isMine()) {
jtulach@63
   192
                continue;
jtulach@63
   193
            }
jtulach@63
   194
            s.setMine(true);
jtulach@63
   195
            mines--;
jtulach@63
   196
        }
jtulach@63
   197
jtulach@63
   198
        model.setState(GameState.IN_PROGRESS);
jtulach@107
   199
        if (rows != model.getRows()) {
jtulach@107
   200
            model.getRows().clear();
jtulach@107
   201
            model.getRows().addAll(rows);
jtulach@107
   202
        }
jtulach@63
   203
    }
jtulach@63
   204
    
jtulach@64
   205
    @ModelOperation static void computeMines(Mines model) {
jtulach@64
   206
        List<Integer> xBombs = new ArrayList<Integer>();
jtulach@64
   207
        List<Integer> yBombs = new ArrayList<Integer>();
jtulach@64
   208
        final List<Row> rows = model.getRows();
jtulach@66
   209
        boolean emptyHidden = false;
jtulach@69
   210
        SquareType[][] arr = new SquareType[rows.size()][];
jtulach@64
   211
        for (int y = 0; y < rows.size(); y++) {
jtulach@64
   212
            final List<Square> columns = rows.get(y).getColumns();
jtulach@69
   213
            arr[y] = new SquareType[columns.size()];
jtulach@64
   214
            for (int x = 0; x < columns.size(); x++) {
jtulach@64
   215
                Square sq = columns.get(x);
jtulach@64
   216
                if (sq.isMine()) {
jtulach@64
   217
                    xBombs.add(x);
jtulach@64
   218
                    yBombs.add(y);
jtulach@64
   219
                }
jtulach@64
   220
                if (sq.getState().isVisible()) {
jtulach@69
   221
                    arr[y][x] = SquareType.N_0;
jtulach@66
   222
                } else {
jtulach@66
   223
                    if (!sq.isMine()) {
jtulach@66
   224
                        emptyHidden = true;
jtulach@66
   225
                    }
jtulach@64
   226
                }
jtulach@64
   227
            }
jtulach@64
   228
        }
jtulach@64
   229
        for (int i = 0; i < xBombs.size(); i++) {
jtulach@64
   230
            int x = xBombs.get(i);
jtulach@64
   231
            int y = yBombs.get(i);
jtulach@64
   232
            
jtulach@69
   233
            incrementAround(arr, x, y);
jtulach@69
   234
        }
jtulach@69
   235
        for (int y = 0; y < rows.size(); y++) {
jtulach@69
   236
            final List<Square> columns = rows.get(y).getColumns();
jtulach@69
   237
            for (int x = 0; x < columns.size(); x++) {
jtulach@69
   238
                Square sq = columns.get(x);
jtulach@69
   239
                final SquareType newState = arr[y][x];
jtulach@69
   240
                if (newState != null && newState != sq.getState()) {
jtulach@69
   241
                    sq.setState(newState);
jtulach@69
   242
                }
jtulach@69
   243
            }
jtulach@64
   244
        }
jtulach@66
   245
        
jtulach@66
   246
        if (!emptyHidden) {
jtulach@66
   247
            model.setState(GameState.WON);
jtulach@68
   248
            showAllBombs(model, SquareType.DISCOVERED);
jtulach@153
   249
            AudioClip applause = AudioClip.create("applause.mp3");
jaroslav@136
   250
            applause.play();
jtulach@66
   251
        }
jtulach@64
   252
    }
jtulach@64
   253
    
jtulach@69
   254
    private static void incrementAround(SquareType[][] arr, int x, int y) {
jtulach@69
   255
        incrementAt(arr, x - 1, y - 1);
jtulach@69
   256
        incrementAt(arr, x - 1, y);
jtulach@69
   257
        incrementAt(arr, x - 1, y + 1);
jtulach@64
   258
jtulach@69
   259
        incrementAt(arr, x + 1, y - 1);
jtulach@69
   260
        incrementAt(arr, x + 1, y);
jtulach@69
   261
        incrementAt(arr, x + 1, y + 1);
jtulach@64
   262
        
jtulach@69
   263
        incrementAt(arr, x, y - 1);
jtulach@69
   264
        incrementAt(arr, x, y + 1);
jtulach@64
   265
    }
jtulach@64
   266
    
jtulach@69
   267
    private static void incrementAt(SquareType[][] arr, int x, int y) {
jtulach@69
   268
        if (y >= 0 && y < arr.length) {
jtulach@69
   269
            SquareType[] r = arr[y];
jtulach@69
   270
            if (x >= 0 && x < r.length) {
jtulach@69
   271
                SquareType sq = r[x];
jtulach@69
   272
                if (sq != null) {
jtulach@69
   273
                    r[x] = sq.moreBombsAround();
jtulach@69
   274
                }
jtulach@64
   275
            }
jtulach@64
   276
        }
jtulach@64
   277
    }
jtulach@64
   278
    
jtulach@68
   279
    static void showAllBombs(Mines model, SquareType state) {
jtulach@63
   280
        for (Row row : model.getRows()) {
jtulach@63
   281
            for (Square square : row.getColumns()) {
jtulach@63
   282
                if (square.isMine()) {
jtulach@68
   283
                    square.setState(state);
jtulach@63
   284
                }
jtulach@63
   285
            }
jtulach@63
   286
        }
jtulach@63
   287
    }
jtulach@63
   288
    
jtulach@63
   289
    @Function static void click(Mines model, Square data) {
jaroslav@164
   290
        if (model.getState() == GameState.MARKING_MINE) {
jaroslav@164
   291
            if (data.getState() == SquareType.UNKNOWN) {
jaroslav@164
   292
                data.setState(SquareType.MARKED);
jaroslav@165
   293
                if (allMarked(model)) {
jaroslav@165
   294
                    model.setState(GameState.WON);
jaroslav@165
   295
                    return;
jaroslav@165
   296
                }
jaroslav@164
   297
            }
jaroslav@164
   298
            model.setState(GameState.IN_PROGRESS);
jaroslav@164
   299
            return;
jaroslav@164
   300
        }
jtulach@63
   301
        if (model.getState() != GameState.IN_PROGRESS) {
jtulach@63
   302
            return;
jtulach@63
   303
        }
jaroslav@164
   304
        if (data.getState() == SquareType.MARKED) {
jaroslav@164
   305
            data.setState(SquareType.UNKNOWN);
jtulach@174
   306
            if (allMarked(model)) {
jtulach@174
   307
                model.setState(GameState.WON);
jtulach@174
   308
            }
jaroslav@164
   309
            return;
jaroslav@164
   310
        }
jaroslav@152
   311
        if (data.getState() != SquareType.UNKNOWN) {
jaroslav@152
   312
            return;
jaroslav@152
   313
        }
jaroslav@152
   314
        if (data.isMine()) {
jaroslav@152
   315
            Square fair = atLeastOnePlaceWhereBombCantBe(model);
jaroslav@152
   316
            if (fair == null) {
jaroslav@152
   317
                if (placeBombElseWhere(model, data)) {
jaroslav@152
   318
                    cleanedUp(model, data);
jaroslav@152
   319
                    return;
jtulach@63
   320
                }
jaroslav@152
   321
            }
jaroslav@152
   322
            explosion(model);
jaroslav@152
   323
        } else {
jaroslav@152
   324
            Square takeFrom = tryStealBomb(model, data);
jaroslav@152
   325
            if (takeFrom != null) {
jaroslav@152
   326
                final Square fair = atLeastOnePlaceWhereBombCantBe(model);
jaroslav@152
   327
                if (fair != null) {
jaroslav@152
   328
                    takeFrom.setMine(false);
jaroslav@152
   329
                    data.setMine(true);
jaroslav@152
   330
                    explosion(model);
jaroslav@152
   331
                    return;
jaroslav@152
   332
                }
jaroslav@152
   333
            }
jaroslav@152
   334
            cleanedUp(model, data);
jtulach@63
   335
        }
jtulach@63
   336
    }
jaroslav@152
   337
jaroslav@152
   338
    private static void cleanedUp(Mines model, Square data) {
jaroslav@152
   339
        AudioClip touch = AudioClip.create("move.mp3");
jaroslav@152
   340
        touch.play();
jaroslav@152
   341
        expandKnown(model, data);
jaroslav@152
   342
        model.computeMines();
jaroslav@152
   343
    }
jaroslav@152
   344
jaroslav@152
   345
    private static void explosion(Mines model) {
jaroslav@152
   346
        showAllBombs(model, SquareType.EXPLOSION);
jaroslav@152
   347
        model.setState(GameState.LOST);
jtulach@153
   348
        AudioClip oops = AudioClip.create("oops.mp3");
jaroslav@152
   349
        oops.play();
jaroslav@152
   350
    }
jaroslav@152
   351
    
jaroslav@152
   352
    private static Square tryStealBomb(Mines model, Square data) {
jaroslav@152
   353
        data.setMine(true);
jaroslav@152
   354
        final List<Row> rows = model.getRows();
jaroslav@152
   355
        for (int y = 0; y < rows.size(); y++) {
jaroslav@152
   356
            final List<Square> columns = rows.get(y).getColumns();
jaroslav@152
   357
            for (int x = 0; x < columns.size(); x++) {
jaroslav@152
   358
                Square sq = columns.get(x);
jaroslav@152
   359
                if (sq == data) {
jaroslav@152
   360
                    continue;
jaroslav@152
   361
                }
jaroslav@152
   362
                if (sq.isMine()) {
jaroslav@152
   363
                    sq.setMine(false);
jaroslav@152
   364
                    final boolean ok = isConsistent(model);
jaroslav@152
   365
                    sq.setMine(true);
jaroslav@152
   366
                    if (ok) {
jaroslav@152
   367
                        data.setMine(false);
jaroslav@152
   368
                        return sq;
jaroslav@152
   369
                    }
jaroslav@152
   370
                }
jaroslav@152
   371
            }
jaroslav@152
   372
        }
jaroslav@152
   373
        data.setMine(false);        
jaroslav@152
   374
        return null;
jaroslav@152
   375
    }
jaroslav@152
   376
    
jaroslav@152
   377
    private static Square atLeastOnePlaceWhereBombCantBe(Mines model) {
jaroslav@152
   378
        final List<Row> rows = model.getRows();
jaroslav@152
   379
        Square cantBe = null;
jaroslav@152
   380
        int discovered = 0;
jaroslav@152
   381
        for (int y = 0; y < rows.size(); y++) {
jaroslav@152
   382
            final List<Square> columns = rows.get(y).getColumns();
jaroslav@152
   383
            for (int x = 0; x < columns.size(); x++) {
jaroslav@152
   384
                Square sq = columns.get(x);
jaroslav@152
   385
                if (sq.getState() == SquareType.UNKNOWN) {
jaroslav@152
   386
                    if (!sq.isMine()) {
jaroslav@152
   387
                        if (tryStealBomb(model, sq) == null) {
jaroslav@152
   388
                            cantBe = sq;
jaroslav@152
   389
                        }
jaroslav@152
   390
                    }
jaroslav@152
   391
                } else {
jaroslav@152
   392
                    discovered++;
jaroslav@152
   393
                }
jaroslav@152
   394
            }
jaroslav@152
   395
        }
jaroslav@152
   396
        
jaroslav@152
   397
        if (discovered > 5) {
jaroslav@152
   398
            return cantBe;
jaroslav@152
   399
        }
jaroslav@152
   400
        
jaroslav@152
   401
        return null;
jaroslav@152
   402
    }
jaroslav@152
   403
    
jaroslav@152
   404
    private static boolean placeBombElseWhere(Mines model, Square moveBomb) {
jaroslav@152
   405
        List<Square> ok = new ArrayList<Square>();
jaroslav@152
   406
        moveBomb.setMine(false);
jaroslav@152
   407
        final List<Row> rows = model.getRows();
jaroslav@152
   408
        for (int y = 0; y < rows.size(); y++) {
jaroslav@152
   409
            final List<Square> columns = rows.get(y).getColumns();
jaroslav@152
   410
            for (int x = 0; x < columns.size(); x++) {
jaroslav@152
   411
                Square sq = columns.get(x);
jaroslav@152
   412
                if (sq == moveBomb || sq.isMine() || sq.getState().isVisible()) {
jaroslav@152
   413
                    continue;
jaroslav@152
   414
                }
jaroslav@152
   415
                sq.setMine(true);
jaroslav@152
   416
                if (isConsistent(model)) {
jaroslav@152
   417
                    ok.add(sq);
jaroslav@152
   418
                }
jaroslav@152
   419
                sq.setMine(false);
jaroslav@152
   420
            }
jaroslav@152
   421
        }
jaroslav@152
   422
        if (ok.isEmpty()) {
jaroslav@152
   423
            moveBomb.setMine(true);
jaroslav@152
   424
            return false;
jaroslav@152
   425
        } else {
jaroslav@152
   426
            int r = new Random().nextInt(ok.size());
jaroslav@152
   427
            ok.get(r).setMine(true);
jaroslav@152
   428
            return true;
jaroslav@152
   429
        }
jaroslav@152
   430
    }
jaroslav@152
   431
    
jtulach@68
   432
    private static void expandKnown(Mines model, Square data) {
jtulach@68
   433
        final List<Row> rows = model.getRows();
jtulach@68
   434
        for (int y = 0; y < rows.size(); y++) {
jtulach@68
   435
            final List<Square> columns = rows.get(y).getColumns();
jtulach@68
   436
            for (int x = 0; x < columns.size(); x++) {
jtulach@68
   437
                Square sq = columns.get(x);
jtulach@68
   438
                if (sq == data) {
jtulach@68
   439
                    expandKnown(model, x, y);
jtulach@68
   440
                    return;
jtulach@68
   441
                }
jtulach@68
   442
            }
jtulach@68
   443
        }
jtulach@68
   444
    }
jtulach@68
   445
    private static void expandKnown(Mines model, int x , int y) {
jtulach@68
   446
        if (y < 0 || y >= model.getRows().size()) {
jtulach@68
   447
            return;
jtulach@68
   448
        }
jtulach@68
   449
        final List<Square> columns = model.getRows().get(y).getColumns();
jtulach@68
   450
        if (x < 0 || x >= columns.size()) {
jtulach@68
   451
            return;
jtulach@68
   452
        }
jtulach@68
   453
        final Square sq = columns.get(x);
jtulach@68
   454
        if (sq.getState() == SquareType.UNKNOWN) {
jaroslav@152
   455
            int around = around(model, x, y);
jtulach@107
   456
            final SquareType t = SquareType.valueOf("N_" + around);
jtulach@107
   457
            sq.setState(t);
jtulach@107
   458
            if (t == SquareType.N_0) {
jtulach@68
   459
                expandKnown(model, x - 1, y - 1);
jtulach@68
   460
                expandKnown(model, x - 1, y);
jtulach@68
   461
                expandKnown(model, x - 1, y + 1);
jtulach@68
   462
                expandKnown(model, x , y - 1);
jtulach@68
   463
                expandKnown(model, x, y + 1);
jtulach@68
   464
                expandKnown(model, x + 1, y - 1);
jtulach@68
   465
                expandKnown(model, x + 1, y);
jtulach@68
   466
                expandKnown(model, x + 1, y + 1);
jtulach@68
   467
            }
jtulach@68
   468
        }
jtulach@68
   469
    }
jaroslav@152
   470
jaroslav@152
   471
    private static int around(Mines model, int x, int y) {
jaroslav@152
   472
        return 
jaroslav@152
   473
            minesAt(model, x - 1, y - 1) +
jaroslav@152
   474
            minesAt(model, x - 1, y) +
jaroslav@152
   475
            minesAt(model, x - 1, y + 1) +
jaroslav@152
   476
            minesAt(model, x , y - 1) +
jaroslav@152
   477
            minesAt(model, x, y + 1) +
jaroslav@152
   478
            minesAt(model, x + 1, y - 1) +
jaroslav@152
   479
            minesAt(model, x + 1, y) +
jaroslav@152
   480
            minesAt(model, x + 1, y + 1);
jaroslav@152
   481
    }
jtulach@90
   482
    
jtulach@107
   483
    private static int minesAt(Mines model, int x, int y) {
jtulach@107
   484
        if (y < 0 || y >= model.getRows().size()) {
jtulach@107
   485
            return 0;
jtulach@107
   486
        }
jtulach@107
   487
        final List<Square> columns = model.getRows().get(y).getColumns();
jtulach@107
   488
        if (x < 0 || x >= columns.size()) {
jtulach@107
   489
            return 0;
jtulach@107
   490
        }
jtulach@107
   491
        Square sq = columns.get(x);
jtulach@107
   492
        return sq.isMine() ? 1 : 0;
jtulach@107
   493
    }
jaroslav@152
   494
    
jaroslav@152
   495
    private static boolean isConsistent(Mines m) {
jaroslav@152
   496
        for (int row = 0; row < m.getRows().size(); row++) {
jaroslav@152
   497
            Row r = m.getRows().get(row);
jaroslav@152
   498
            for (int col = 0; col < r.getColumns().size(); col++) {
jaroslav@152
   499
                Square sq = r.getColumns().get(col);
jaroslav@152
   500
                if (sq.getState().isVisible()) {
jaroslav@152
   501
                    int around = around(m, col, row);
jaroslav@152
   502
                    if (around != sq.getState().ordinal()) {
jaroslav@152
   503
                        return false;
jaroslav@152
   504
                    }
jaroslav@152
   505
                }
jaroslav@152
   506
            }
jaroslav@152
   507
        }
jaroslav@152
   508
        return true;
jaroslav@152
   509
    }
jaroslav@165
   510
    
jaroslav@165
   511
    private static boolean allMarked(Mines m) {
jaroslav@165
   512
        for (Row r : m.getRows()) {
jaroslav@165
   513
            for (Square sq : r.getColumns()) {
jtulach@174
   514
                if (sq.isMine() == (sq.getState() != SquareType.MARKED)) {
jaroslav@165
   515
                    return false;
jaroslav@165
   516
                }
jaroslav@165
   517
            }
jaroslav@165
   518
        }
jaroslav@165
   519
        for (Row r : m.getRows()) {
jaroslav@165
   520
            for (Square sq : r.getColumns()) {
jaroslav@165
   521
                if (sq.isMine()) {
jaroslav@165
   522
                    sq.setState(SquareType.DISCOVERED);
jaroslav@165
   523
                } else {
jaroslav@165
   524
                    sq.setState(SquareType.N_0);
jaroslav@165
   525
                }
jaroslav@165
   526
            }
jaroslav@165
   527
        }
jaroslav@165
   528
        computeMines(m);
jaroslav@165
   529
        return true;
jaroslav@165
   530
    }
jtulach@116
   531
jtulach@90
   532
    /**
jtulach@90
   533
     * Called when page is ready
jtulach@90
   534
     */
jtulach@90
   535
    public static void main(String... args) throws Exception {
jtulach@90
   536
        Mines m = new Mines();
jtulach@90
   537
        m.applyBindings();
jtulach@90
   538
    }
jtulach@63
   539
}