minesweeper/src/main/java/org/apidesign/demo/minesweeper/MinesModel.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 27 Jun 2014 15:05:03 +0200
changeset 163 823630089369
parent 153 6d2eb47e966b
child 164 b56bc5060fac
permissions -rw-r--r--
Using CSS:after instead of computed HTML fragment
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
 */
jtulach@63
    38
@Model(className = "Mines", 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 {
jtulach@63
    44
        IN_PROGRESS, WON, LOST;
jtulach@63
    45
    }
jtulach@63
    46
    
jtulach@63
    47
    @Model(className = "Row", properties = {
jtulach@63
    48
        @Property(name = "columns", type = Square.class, array = true)
jtulach@63
    49
    })
jtulach@63
    50
    static class RowModel {
jtulach@63
    51
    }
jtulach@63
    52
jtulach@63
    53
    @Model(className = "Square", properties = {
jtulach@63
    54
        @Property(name = "state", type = SquareType.class),
jtulach@63
    55
        @Property(name = "mine", type = boolean.class)
jtulach@63
    56
    })
jtulach@63
    57
    static class SquareModel {
jtulach@65
    58
        @ComputedProperty static String style(SquareType state) {
jtulach@65
    59
            return state == null ? null : state.toString();
jtulach@65
    60
        }
jtulach@63
    61
    }
jtulach@63
    62
    
jtulach@63
    63
    enum SquareType {
jtulach@63
    64
        N_0, N_1, N_2, N_3, N_4, N_5, N_6, N_7, N_8,
jtulach@68
    65
        UNKNOWN, EXPLOSION, DISCOVERED;
jtulach@64
    66
        
jtulach@64
    67
        final boolean isVisible() {
jtulach@64
    68
            return name().startsWith("N_");
jtulach@64
    69
        }
jtulach@64
    70
jtulach@69
    71
        final SquareType moreBombsAround() {
jtulach@68
    72
            switch (this) {
jtulach@68
    73
                case EXPLOSION:
jtulach@68
    74
                case UNKNOWN:
jtulach@68
    75
                case DISCOVERED:
jtulach@68
    76
                case N_8:
jtulach@68
    77
                    return this;
jtulach@64
    78
            }
jtulach@64
    79
            return values()[ordinal() + 1];
jtulach@64
    80
        }
jtulach@63
    81
    }
jtulach@63
    82
    
jtulach@76
    83
    @ComputedProperty static boolean fieldShowing(GameState state) {
jtulach@76
    84
        return state != null;
jtulach@76
    85
    }
jtulach@76
    86
    
jtulach@76
    87
    @Function static void showHelp(Mines model) {
jtulach@76
    88
        model.setState(null);
jtulach@76
    89
    }
jtulach@76
    90
    
jtulach@70
    91
    @Function static void smallGame(Mines model) {
jtulach@70
    92
        model.init(5, 5, 5);
jtulach@70
    93
    }
jtulach@70
    94
    @Function static void normalGame(Mines model) {
jtulach@70
    95
        model.init(10, 10, 10);
jtulach@70
    96
    }
jtulach@70
    97
    
jtulach@72
    98
    @Function static void giveUp(Mines model) {
jtulach@72
    99
        showAllBombs(model, SquareType.EXPLOSION);
jaroslav@152
   100
        model.setState(GameState.LOST);
jtulach@72
   101
    }
jtulach@72
   102
    
jtulach@63
   103
    @ModelOperation static void init(Mines model, int width, int height, int mines) {
jtulach@107
   104
        List<Row> rows = model.getRows();
jtulach@107
   105
        if (rows.size() != height || rows.get(0).getColumns().size() != width) {
jtulach@107
   106
            rows = new ArrayList<Row>(height);
jtulach@107
   107
            for (int y = 0; y < height; y++) {
jtulach@107
   108
                Square[] columns = new Square[width];
jtulach@107
   109
                for (int x = 0; x < width; x++) {
jtulach@107
   110
                    columns[x] = new Square(SquareType.UNKNOWN, false);
jtulach@107
   111
                }
jtulach@107
   112
                rows.add(new Row(columns));
jtulach@63
   113
            }
jtulach@107
   114
        } else {
jtulach@107
   115
            for (Row row : rows) {
jtulach@107
   116
                for (Square sq : row.getColumns()) {
jtulach@107
   117
                    sq.setState(SquareType.UNKNOWN);
jtulach@107
   118
                    sq.setMine(false);
jtulach@107
   119
                }
jtulach@107
   120
            }
jtulach@63
   121
        }
jtulach@63
   122
        
jtulach@63
   123
        Random r = new Random();
jtulach@63
   124
        while (mines > 0) {
jtulach@63
   125
            int x = r.nextInt(width);
jtulach@63
   126
            int y = r.nextInt(height);
jtulach@63
   127
            final Square s = rows.get(y).getColumns().get(x);
jtulach@63
   128
            if (s.isMine()) {
jtulach@63
   129
                continue;
jtulach@63
   130
            }
jtulach@63
   131
            s.setMine(true);
jtulach@63
   132
            mines--;
jtulach@63
   133
        }
jtulach@63
   134
jtulach@63
   135
        model.setState(GameState.IN_PROGRESS);
jtulach@107
   136
        if (rows != model.getRows()) {
jtulach@107
   137
            model.getRows().clear();
jtulach@107
   138
            model.getRows().addAll(rows);
jtulach@107
   139
        }
jtulach@63
   140
    }
jtulach@63
   141
    
jtulach@64
   142
    @ModelOperation static void computeMines(Mines model) {
jtulach@64
   143
        List<Integer> xBombs = new ArrayList<Integer>();
jtulach@64
   144
        List<Integer> yBombs = new ArrayList<Integer>();
jtulach@64
   145
        final List<Row> rows = model.getRows();
jtulach@66
   146
        boolean emptyHidden = false;
jtulach@69
   147
        SquareType[][] arr = new SquareType[rows.size()][];
jtulach@64
   148
        for (int y = 0; y < rows.size(); y++) {
jtulach@64
   149
            final List<Square> columns = rows.get(y).getColumns();
jtulach@69
   150
            arr[y] = new SquareType[columns.size()];
jtulach@64
   151
            for (int x = 0; x < columns.size(); x++) {
jtulach@64
   152
                Square sq = columns.get(x);
jtulach@64
   153
                if (sq.isMine()) {
jtulach@64
   154
                    xBombs.add(x);
jtulach@64
   155
                    yBombs.add(y);
jtulach@64
   156
                }
jtulach@64
   157
                if (sq.getState().isVisible()) {
jtulach@69
   158
                    arr[y][x] = SquareType.N_0;
jtulach@66
   159
                } else {
jtulach@66
   160
                    if (!sq.isMine()) {
jtulach@66
   161
                        emptyHidden = true;
jtulach@66
   162
                    }
jtulach@64
   163
                }
jtulach@64
   164
            }
jtulach@64
   165
        }
jtulach@64
   166
        for (int i = 0; i < xBombs.size(); i++) {
jtulach@64
   167
            int x = xBombs.get(i);
jtulach@64
   168
            int y = yBombs.get(i);
jtulach@64
   169
            
jtulach@69
   170
            incrementAround(arr, x, y);
jtulach@69
   171
        }
jtulach@69
   172
        for (int y = 0; y < rows.size(); y++) {
jtulach@69
   173
            final List<Square> columns = rows.get(y).getColumns();
jtulach@69
   174
            for (int x = 0; x < columns.size(); x++) {
jtulach@69
   175
                Square sq = columns.get(x);
jtulach@69
   176
                final SquareType newState = arr[y][x];
jtulach@69
   177
                if (newState != null && newState != sq.getState()) {
jtulach@69
   178
                    sq.setState(newState);
jtulach@69
   179
                }
jtulach@69
   180
            }
jtulach@64
   181
        }
jtulach@66
   182
        
jtulach@66
   183
        if (!emptyHidden) {
jtulach@66
   184
            model.setState(GameState.WON);
jtulach@68
   185
            showAllBombs(model, SquareType.DISCOVERED);
jtulach@153
   186
            AudioClip applause = AudioClip.create("applause.mp3");
jaroslav@136
   187
            applause.play();
jtulach@66
   188
        }
jtulach@64
   189
    }
jtulach@64
   190
    
jtulach@69
   191
    private static void incrementAround(SquareType[][] arr, int x, int y) {
jtulach@69
   192
        incrementAt(arr, x - 1, y - 1);
jtulach@69
   193
        incrementAt(arr, x - 1, y);
jtulach@69
   194
        incrementAt(arr, x - 1, y + 1);
jtulach@64
   195
jtulach@69
   196
        incrementAt(arr, x + 1, y - 1);
jtulach@69
   197
        incrementAt(arr, x + 1, y);
jtulach@69
   198
        incrementAt(arr, x + 1, y + 1);
jtulach@64
   199
        
jtulach@69
   200
        incrementAt(arr, x, y - 1);
jtulach@69
   201
        incrementAt(arr, x, y + 1);
jtulach@64
   202
    }
jtulach@64
   203
    
jtulach@69
   204
    private static void incrementAt(SquareType[][] arr, int x, int y) {
jtulach@69
   205
        if (y >= 0 && y < arr.length) {
jtulach@69
   206
            SquareType[] r = arr[y];
jtulach@69
   207
            if (x >= 0 && x < r.length) {
jtulach@69
   208
                SquareType sq = r[x];
jtulach@69
   209
                if (sq != null) {
jtulach@69
   210
                    r[x] = sq.moreBombsAround();
jtulach@69
   211
                }
jtulach@64
   212
            }
jtulach@64
   213
        }
jtulach@64
   214
    }
jtulach@64
   215
    
jtulach@68
   216
    static void showAllBombs(Mines model, SquareType state) {
jtulach@63
   217
        for (Row row : model.getRows()) {
jtulach@63
   218
            for (Square square : row.getColumns()) {
jtulach@63
   219
                if (square.isMine()) {
jtulach@68
   220
                    square.setState(state);
jtulach@63
   221
                }
jtulach@63
   222
            }
jtulach@63
   223
        }
jtulach@63
   224
    }
jtulach@63
   225
    
jtulach@63
   226
    @Function static void click(Mines model, Square data) {
jtulach@63
   227
        if (model.getState() != GameState.IN_PROGRESS) {
jtulach@63
   228
            return;
jtulach@63
   229
        }
jaroslav@152
   230
        if (data.getState() != SquareType.UNKNOWN) {
jaroslav@152
   231
            return;
jaroslav@152
   232
        }
jaroslav@152
   233
        if (data.isMine()) {
jaroslav@152
   234
            Square fair = atLeastOnePlaceWhereBombCantBe(model);
jaroslav@152
   235
            if (fair == null) {
jaroslav@152
   236
                if (placeBombElseWhere(model, data)) {
jaroslav@152
   237
                    cleanedUp(model, data);
jaroslav@152
   238
                    return;
jtulach@63
   239
                }
jaroslav@152
   240
            }
jaroslav@152
   241
            explosion(model);
jaroslav@152
   242
        } else {
jaroslav@152
   243
            Square takeFrom = tryStealBomb(model, data);
jaroslav@152
   244
            if (takeFrom != null) {
jaroslav@152
   245
                final Square fair = atLeastOnePlaceWhereBombCantBe(model);
jaroslav@152
   246
                if (fair != null) {
jaroslav@152
   247
                    takeFrom.setMine(false);
jaroslav@152
   248
                    data.setMine(true);
jaroslav@152
   249
                    explosion(model);
jaroslav@152
   250
                    return;
jaroslav@152
   251
                }
jaroslav@152
   252
            }
jaroslav@152
   253
            cleanedUp(model, data);
jtulach@63
   254
        }
jtulach@63
   255
    }
jaroslav@152
   256
jaroslav@152
   257
    private static void cleanedUp(Mines model, Square data) {
jaroslav@152
   258
        AudioClip touch = AudioClip.create("move.mp3");
jaroslav@152
   259
        touch.play();
jaroslav@152
   260
        expandKnown(model, data);
jaroslav@152
   261
        model.computeMines();
jaroslav@152
   262
    }
jaroslav@152
   263
jaroslav@152
   264
    private static void explosion(Mines model) {
jaroslav@152
   265
        showAllBombs(model, SquareType.EXPLOSION);
jaroslav@152
   266
        model.setState(GameState.LOST);
jtulach@153
   267
        AudioClip oops = AudioClip.create("oops.mp3");
jaroslav@152
   268
        oops.play();
jaroslav@152
   269
    }
jaroslav@152
   270
    
jaroslav@152
   271
    private static Square tryStealBomb(Mines model, Square data) {
jaroslav@152
   272
        data.setMine(true);
jaroslav@152
   273
        final List<Row> rows = model.getRows();
jaroslav@152
   274
        for (int y = 0; y < rows.size(); y++) {
jaroslav@152
   275
            final List<Square> columns = rows.get(y).getColumns();
jaroslav@152
   276
            for (int x = 0; x < columns.size(); x++) {
jaroslav@152
   277
                Square sq = columns.get(x);
jaroslav@152
   278
                if (sq == data) {
jaroslav@152
   279
                    continue;
jaroslav@152
   280
                }
jaroslav@152
   281
                if (sq.isMine()) {
jaroslav@152
   282
                    sq.setMine(false);
jaroslav@152
   283
                    final boolean ok = isConsistent(model);
jaroslav@152
   284
                    sq.setMine(true);
jaroslav@152
   285
                    if (ok) {
jaroslav@152
   286
                        data.setMine(false);
jaroslav@152
   287
                        return sq;
jaroslav@152
   288
                    }
jaroslav@152
   289
                }
jaroslav@152
   290
            }
jaroslav@152
   291
        }
jaroslav@152
   292
        data.setMine(false);        
jaroslav@152
   293
        return null;
jaroslav@152
   294
    }
jaroslav@152
   295
    
jaroslav@152
   296
    private static Square atLeastOnePlaceWhereBombCantBe(Mines model) {
jaroslav@152
   297
        final List<Row> rows = model.getRows();
jaroslav@152
   298
        Square cantBe = null;
jaroslav@152
   299
        int discovered = 0;
jaroslav@152
   300
        for (int y = 0; y < rows.size(); y++) {
jaroslav@152
   301
            final List<Square> columns = rows.get(y).getColumns();
jaroslav@152
   302
            for (int x = 0; x < columns.size(); x++) {
jaroslav@152
   303
                Square sq = columns.get(x);
jaroslav@152
   304
                if (sq.getState() == SquareType.UNKNOWN) {
jaroslav@152
   305
                    if (!sq.isMine()) {
jaroslav@152
   306
                        if (tryStealBomb(model, sq) == null) {
jaroslav@152
   307
                            cantBe = sq;
jaroslav@152
   308
                        }
jaroslav@152
   309
                    }
jaroslav@152
   310
                } else {
jaroslav@152
   311
                    discovered++;
jaroslav@152
   312
                }
jaroslav@152
   313
            }
jaroslav@152
   314
        }
jaroslav@152
   315
        
jaroslav@152
   316
        if (discovered > 5) {
jaroslav@152
   317
            return cantBe;
jaroslav@152
   318
        }
jaroslav@152
   319
        
jaroslav@152
   320
        return null;
jaroslav@152
   321
    }
jaroslav@152
   322
    
jaroslav@152
   323
    private static boolean placeBombElseWhere(Mines model, Square moveBomb) {
jaroslav@152
   324
        List<Square> ok = new ArrayList<Square>();
jaroslav@152
   325
        moveBomb.setMine(false);
jaroslav@152
   326
        final List<Row> rows = model.getRows();
jaroslav@152
   327
        for (int y = 0; y < rows.size(); y++) {
jaroslav@152
   328
            final List<Square> columns = rows.get(y).getColumns();
jaroslav@152
   329
            for (int x = 0; x < columns.size(); x++) {
jaroslav@152
   330
                Square sq = columns.get(x);
jaroslav@152
   331
                if (sq == moveBomb || sq.isMine() || sq.getState().isVisible()) {
jaroslav@152
   332
                    continue;
jaroslav@152
   333
                }
jaroslav@152
   334
                sq.setMine(true);
jaroslav@152
   335
                if (isConsistent(model)) {
jaroslav@152
   336
                    ok.add(sq);
jaroslav@152
   337
                }
jaroslav@152
   338
                sq.setMine(false);
jaroslav@152
   339
            }
jaroslav@152
   340
        }
jaroslav@152
   341
        if (ok.isEmpty()) {
jaroslav@152
   342
            moveBomb.setMine(true);
jaroslav@152
   343
            return false;
jaroslav@152
   344
        } else {
jaroslav@152
   345
            int r = new Random().nextInt(ok.size());
jaroslav@152
   346
            ok.get(r).setMine(true);
jaroslav@152
   347
            return true;
jaroslav@152
   348
        }
jaroslav@152
   349
    }
jaroslav@152
   350
    
jtulach@68
   351
    private static void expandKnown(Mines model, Square data) {
jtulach@68
   352
        final List<Row> rows = model.getRows();
jtulach@68
   353
        for (int y = 0; y < rows.size(); y++) {
jtulach@68
   354
            final List<Square> columns = rows.get(y).getColumns();
jtulach@68
   355
            for (int x = 0; x < columns.size(); x++) {
jtulach@68
   356
                Square sq = columns.get(x);
jtulach@68
   357
                if (sq == data) {
jtulach@68
   358
                    expandKnown(model, x, y);
jtulach@68
   359
                    return;
jtulach@68
   360
                }
jtulach@68
   361
            }
jtulach@68
   362
        }
jtulach@68
   363
    }
jtulach@68
   364
    private static void expandKnown(Mines model, int x , int y) {
jtulach@68
   365
        if (y < 0 || y >= model.getRows().size()) {
jtulach@68
   366
            return;
jtulach@68
   367
        }
jtulach@68
   368
        final List<Square> columns = model.getRows().get(y).getColumns();
jtulach@68
   369
        if (x < 0 || x >= columns.size()) {
jtulach@68
   370
            return;
jtulach@68
   371
        }
jtulach@68
   372
        final Square sq = columns.get(x);
jtulach@68
   373
        if (sq.getState() == SquareType.UNKNOWN) {
jaroslav@152
   374
            int around = around(model, x, y);
jtulach@107
   375
            final SquareType t = SquareType.valueOf("N_" + around);
jtulach@107
   376
            sq.setState(t);
jtulach@107
   377
            if (t == SquareType.N_0) {
jtulach@68
   378
                expandKnown(model, x - 1, y - 1);
jtulach@68
   379
                expandKnown(model, x - 1, y);
jtulach@68
   380
                expandKnown(model, x - 1, y + 1);
jtulach@68
   381
                expandKnown(model, x , y - 1);
jtulach@68
   382
                expandKnown(model, x, y + 1);
jtulach@68
   383
                expandKnown(model, x + 1, y - 1);
jtulach@68
   384
                expandKnown(model, x + 1, y);
jtulach@68
   385
                expandKnown(model, x + 1, y + 1);
jtulach@68
   386
            }
jtulach@68
   387
        }
jtulach@68
   388
    }
jaroslav@152
   389
jaroslav@152
   390
    private static int around(Mines model, int x, int y) {
jaroslav@152
   391
        return 
jaroslav@152
   392
            minesAt(model, x - 1, y - 1) +
jaroslav@152
   393
            minesAt(model, x - 1, y) +
jaroslav@152
   394
            minesAt(model, x - 1, y + 1) +
jaroslav@152
   395
            minesAt(model, x , y - 1) +
jaroslav@152
   396
            minesAt(model, x, y + 1) +
jaroslav@152
   397
            minesAt(model, x + 1, y - 1) +
jaroslav@152
   398
            minesAt(model, x + 1, y) +
jaroslav@152
   399
            minesAt(model, x + 1, y + 1);
jaroslav@152
   400
    }
jtulach@90
   401
    
jtulach@107
   402
    private static int minesAt(Mines model, int x, int y) {
jtulach@107
   403
        if (y < 0 || y >= model.getRows().size()) {
jtulach@107
   404
            return 0;
jtulach@107
   405
        }
jtulach@107
   406
        final List<Square> columns = model.getRows().get(y).getColumns();
jtulach@107
   407
        if (x < 0 || x >= columns.size()) {
jtulach@107
   408
            return 0;
jtulach@107
   409
        }
jtulach@107
   410
        Square sq = columns.get(x);
jtulach@107
   411
        return sq.isMine() ? 1 : 0;
jtulach@107
   412
    }
jaroslav@152
   413
    
jaroslav@152
   414
    private static boolean isConsistent(Mines m) {
jaroslav@152
   415
        for (int row = 0; row < m.getRows().size(); row++) {
jaroslav@152
   416
            Row r = m.getRows().get(row);
jaroslav@152
   417
            for (int col = 0; col < r.getColumns().size(); col++) {
jaroslav@152
   418
                Square sq = r.getColumns().get(col);
jaroslav@152
   419
                if (sq.getState().isVisible()) {
jaroslav@152
   420
                    int around = around(m, col, row);
jaroslav@152
   421
                    if (around != sq.getState().ordinal()) {
jaroslav@152
   422
                        return false;
jaroslav@152
   423
                    }
jaroslav@152
   424
                }
jaroslav@152
   425
            }
jaroslav@152
   426
        }
jaroslav@152
   427
        return true;
jaroslav@152
   428
    }
jtulach@116
   429
jtulach@90
   430
    /**
jtulach@90
   431
     * Called when page is ready
jtulach@90
   432
     */
jtulach@90
   433
    public static void main(String... args) throws Exception {
jtulach@90
   434
        Mines m = new Mines();
jtulach@90
   435
        m.applyBindings();
jtulach@90
   436
    }
jtulach@63
   437
}