minesweeper/src/main/java/org/apidesign/demo/minesweeper/MinesModel.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 20 Mar 2014 11:25:28 +0100
changeset 116 4dce5ea7e13a
parent 108 ceebcfdcc742
parent 90 eff392cfe687
child 124 533c2be1747c
child 136 175cbb03dc5f
permissions -rw-r--r--
Merging teavm branch into default line, now when teavm 0.1 is out
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@71
    58
        @ComputedProperty static String html(SquareType state) {
jtulach@75
    59
            if (state == null) return "&nbsp;";
jtulach@63
    60
            switch (state) {
jtulach@75
    61
                case EXPLOSION: return "&#x2717;";
jtulach@75
    62
                case UNKNOWN: return "&nbsp;";
jtulach@71
    63
                case DISCOVERED: return "&#x2714;";  
jtulach@71
    64
                case N_0: return "&nbsp;";
jtulach@63
    65
            }
jtulach@75
    66
            return "&#x278" + (state.ordinal() - 1);
jtulach@63
    67
        }
jtulach@65
    68
        
jtulach@65
    69
        @ComputedProperty static String style(SquareType state) {
jtulach@65
    70
            return state == null ? null : state.toString();
jtulach@65
    71
        }
jtulach@63
    72
    }
jtulach@63
    73
    
jtulach@63
    74
    enum SquareType {
jtulach@63
    75
        N_0, N_1, N_2, N_3, N_4, N_5, N_6, N_7, N_8,
jtulach@68
    76
        UNKNOWN, EXPLOSION, DISCOVERED;
jtulach@64
    77
        
jtulach@64
    78
        final boolean isVisible() {
jtulach@64
    79
            return name().startsWith("N_");
jtulach@64
    80
        }
jtulach@64
    81
jtulach@69
    82
        final SquareType moreBombsAround() {
jtulach@68
    83
            switch (this) {
jtulach@68
    84
                case EXPLOSION:
jtulach@68
    85
                case UNKNOWN:
jtulach@68
    86
                case DISCOVERED:
jtulach@68
    87
                case N_8:
jtulach@68
    88
                    return this;
jtulach@64
    89
            }
jtulach@64
    90
            return values()[ordinal() + 1];
jtulach@64
    91
        }
jtulach@63
    92
    }
jtulach@63
    93
    
jtulach@76
    94
    @ComputedProperty static boolean fieldShowing(GameState state) {
jtulach@76
    95
        return state != null;
jtulach@76
    96
    }
jtulach@76
    97
    
jtulach@76
    98
    @Function static void showHelp(Mines model) {
jtulach@76
    99
        model.setState(null);
jtulach@76
   100
    }
jtulach@76
   101
    
jtulach@70
   102
    @Function static void smallGame(Mines model) {
jtulach@70
   103
        model.init(5, 5, 5);
jtulach@70
   104
    }
jtulach@70
   105
    @Function static void normalGame(Mines model) {
jtulach@70
   106
        model.init(10, 10, 10);
jtulach@70
   107
    }
jtulach@70
   108
    
jtulach@72
   109
    @Function static void giveUp(Mines model) {
jtulach@72
   110
        showAllBombs(model, SquareType.EXPLOSION);
jtulach@72
   111
    }
jtulach@72
   112
    
jtulach@63
   113
    @ModelOperation static void init(Mines model, int width, int height, int mines) {
jtulach@107
   114
        List<Row> rows = model.getRows();
jtulach@107
   115
        if (rows.size() != height || rows.get(0).getColumns().size() != width) {
jtulach@107
   116
            rows = new ArrayList<Row>(height);
jtulach@107
   117
            for (int y = 0; y < height; y++) {
jtulach@107
   118
                Square[] columns = new Square[width];
jtulach@107
   119
                for (int x = 0; x < width; x++) {
jtulach@107
   120
                    columns[x] = new Square(SquareType.UNKNOWN, false);
jtulach@107
   121
                }
jtulach@107
   122
                rows.add(new Row(columns));
jtulach@63
   123
            }
jtulach@107
   124
        } else {
jtulach@107
   125
            for (Row row : rows) {
jtulach@107
   126
                for (Square sq : row.getColumns()) {
jtulach@107
   127
                    sq.setState(SquareType.UNKNOWN);
jtulach@107
   128
                    sq.setMine(false);
jtulach@107
   129
                }
jtulach@107
   130
            }
jtulach@63
   131
        }
jtulach@63
   132
        
jtulach@63
   133
        Random r = new Random();
jtulach@63
   134
        while (mines > 0) {
jtulach@63
   135
            int x = r.nextInt(width);
jtulach@63
   136
            int y = r.nextInt(height);
jtulach@63
   137
            final Square s = rows.get(y).getColumns().get(x);
jtulach@63
   138
            if (s.isMine()) {
jtulach@63
   139
                continue;
jtulach@63
   140
            }
jtulach@63
   141
            s.setMine(true);
jtulach@63
   142
            mines--;
jtulach@63
   143
        }
jtulach@63
   144
jtulach@63
   145
        model.setState(GameState.IN_PROGRESS);
jtulach@107
   146
        if (rows != model.getRows()) {
jtulach@107
   147
            model.getRows().clear();
jtulach@107
   148
            model.getRows().addAll(rows);
jtulach@107
   149
        }
jtulach@63
   150
    }
jtulach@63
   151
    
jtulach@64
   152
    @ModelOperation static void computeMines(Mines model) {
jtulach@64
   153
        List<Integer> xBombs = new ArrayList<Integer>();
jtulach@64
   154
        List<Integer> yBombs = new ArrayList<Integer>();
jtulach@64
   155
        final List<Row> rows = model.getRows();
jtulach@66
   156
        boolean emptyHidden = false;
jtulach@69
   157
        SquareType[][] arr = new SquareType[rows.size()][];
jtulach@64
   158
        for (int y = 0; y < rows.size(); y++) {
jtulach@64
   159
            final List<Square> columns = rows.get(y).getColumns();
jtulach@69
   160
            arr[y] = new SquareType[columns.size()];
jtulach@64
   161
            for (int x = 0; x < columns.size(); x++) {
jtulach@64
   162
                Square sq = columns.get(x);
jtulach@64
   163
                if (sq.isMine()) {
jtulach@64
   164
                    xBombs.add(x);
jtulach@64
   165
                    yBombs.add(y);
jtulach@64
   166
                }
jtulach@64
   167
                if (sq.getState().isVisible()) {
jtulach@69
   168
                    arr[y][x] = SquareType.N_0;
jtulach@66
   169
                } else {
jtulach@66
   170
                    if (!sq.isMine()) {
jtulach@66
   171
                        emptyHidden = true;
jtulach@66
   172
                    }
jtulach@64
   173
                }
jtulach@64
   174
            }
jtulach@64
   175
        }
jtulach@64
   176
        for (int i = 0; i < xBombs.size(); i++) {
jtulach@64
   177
            int x = xBombs.get(i);
jtulach@64
   178
            int y = yBombs.get(i);
jtulach@64
   179
            
jtulach@69
   180
            incrementAround(arr, x, y);
jtulach@69
   181
        }
jtulach@69
   182
        for (int y = 0; y < rows.size(); y++) {
jtulach@69
   183
            final List<Square> columns = rows.get(y).getColumns();
jtulach@69
   184
            for (int x = 0; x < columns.size(); x++) {
jtulach@69
   185
                Square sq = columns.get(x);
jtulach@69
   186
                final SquareType newState = arr[y][x];
jtulach@69
   187
                if (newState != null && newState != sq.getState()) {
jtulach@69
   188
                    sq.setState(newState);
jtulach@69
   189
                }
jtulach@69
   190
            }
jtulach@64
   191
        }
jtulach@66
   192
        
jtulach@66
   193
        if (!emptyHidden) {
jtulach@66
   194
            model.setState(GameState.WON);
jtulach@68
   195
            showAllBombs(model, SquareType.DISCOVERED);
jtulach@66
   196
        }
jtulach@64
   197
    }
jtulach@64
   198
    
jtulach@69
   199
    private static void incrementAround(SquareType[][] arr, int x, int y) {
jtulach@69
   200
        incrementAt(arr, x - 1, y - 1);
jtulach@69
   201
        incrementAt(arr, x - 1, y);
jtulach@69
   202
        incrementAt(arr, x - 1, y + 1);
jtulach@64
   203
jtulach@69
   204
        incrementAt(arr, x + 1, y - 1);
jtulach@69
   205
        incrementAt(arr, x + 1, y);
jtulach@69
   206
        incrementAt(arr, x + 1, y + 1);
jtulach@64
   207
        
jtulach@69
   208
        incrementAt(arr, x, y - 1);
jtulach@69
   209
        incrementAt(arr, x, y + 1);
jtulach@64
   210
    }
jtulach@64
   211
    
jtulach@69
   212
    private static void incrementAt(SquareType[][] arr, int x, int y) {
jtulach@69
   213
        if (y >= 0 && y < arr.length) {
jtulach@69
   214
            SquareType[] r = arr[y];
jtulach@69
   215
            if (x >= 0 && x < r.length) {
jtulach@69
   216
                SquareType sq = r[x];
jtulach@69
   217
                if (sq != null) {
jtulach@69
   218
                    r[x] = sq.moreBombsAround();
jtulach@69
   219
                }
jtulach@64
   220
            }
jtulach@64
   221
        }
jtulach@64
   222
    }
jtulach@64
   223
    
jtulach@68
   224
    static void showAllBombs(Mines model, SquareType state) {
jtulach@63
   225
        for (Row row : model.getRows()) {
jtulach@63
   226
            for (Square square : row.getColumns()) {
jtulach@63
   227
                if (square.isMine()) {
jtulach@68
   228
                    square.setState(state);
jtulach@63
   229
                }
jtulach@63
   230
            }
jtulach@63
   231
        }
jtulach@63
   232
    }
jtulach@63
   233
    
jtulach@108
   234
    private static AudioClip TOUCH;
jtulach@63
   235
    @Function static void click(Mines model, Square data) {
jtulach@63
   236
        if (model.getState() != GameState.IN_PROGRESS) {
jtulach@63
   237
            return;
jtulach@63
   238
        }
jtulach@63
   239
        
jtulach@63
   240
        switch (data.getState()) {
jtulach@63
   241
            case UNKNOWN: 
jtulach@63
   242
                if (data.isMine()) {
jtulach@68
   243
                    showAllBombs(model, SquareType.EXPLOSION);
jtulach@63
   244
                    model.setState(GameState.LOST);
jtulach@63
   245
                } else {
jtulach@108
   246
                    if (TOUCH == null) {
jtulach@108
   247
                        TOUCH = AudioClip.create("move.mp3");
jtulach@108
   248
                    }
jtulach@79
   249
                    TOUCH.play();
jtulach@68
   250
                    expandKnown(model, data);
jtulach@107
   251
                    model.computeMines();
jtulach@63
   252
                }
jtulach@63
   253
            break;
jtulach@63
   254
        }
jtulach@63
   255
    }
jtulach@68
   256
    private static void expandKnown(Mines model, Square data) {
jtulach@68
   257
        final List<Row> rows = model.getRows();
jtulach@68
   258
        for (int y = 0; y < rows.size(); y++) {
jtulach@68
   259
            final List<Square> columns = rows.get(y).getColumns();
jtulach@68
   260
            for (int x = 0; x < columns.size(); x++) {
jtulach@68
   261
                Square sq = columns.get(x);
jtulach@68
   262
                if (sq == data) {
jtulach@68
   263
                    expandKnown(model, x, y);
jtulach@68
   264
                    return;
jtulach@68
   265
                }
jtulach@68
   266
            }
jtulach@68
   267
        }
jtulach@68
   268
    }
jtulach@68
   269
    private static void expandKnown(Mines model, int x , int y) {
jtulach@68
   270
        if (y < 0 || y >= model.getRows().size()) {
jtulach@68
   271
            return;
jtulach@68
   272
        }
jtulach@68
   273
        final List<Square> columns = model.getRows().get(y).getColumns();
jtulach@68
   274
        if (x < 0 || x >= columns.size()) {
jtulach@68
   275
            return;
jtulach@68
   276
        }
jtulach@68
   277
        final Square sq = columns.get(x);
jtulach@68
   278
        if (sq.getState() == SquareType.UNKNOWN) {
jtulach@107
   279
            int around = 
jtulach@107
   280
                minesAt(model, x - 1, y - 1) +
jtulach@107
   281
                minesAt(model, x - 1, y) +
jtulach@107
   282
                minesAt(model, x - 1, y + 1) +
jtulach@107
   283
                minesAt(model, x , y - 1) +
jtulach@107
   284
                minesAt(model, x, y + 1) +
jtulach@107
   285
                minesAt(model, x + 1, y - 1) +
jtulach@107
   286
                minesAt(model, x + 1, y) +
jtulach@107
   287
                minesAt(model, x + 1, y + 1);
jtulach@107
   288
            final SquareType t = SquareType.valueOf("N_" + around);
jtulach@107
   289
            sq.setState(t);
jtulach@107
   290
            if (t == SquareType.N_0) {
jtulach@68
   291
                expandKnown(model, x - 1, y - 1);
jtulach@68
   292
                expandKnown(model, x - 1, y);
jtulach@68
   293
                expandKnown(model, x - 1, y + 1);
jtulach@68
   294
                expandKnown(model, x , y - 1);
jtulach@68
   295
                expandKnown(model, x, y + 1);
jtulach@68
   296
                expandKnown(model, x + 1, y - 1);
jtulach@68
   297
                expandKnown(model, x + 1, y);
jtulach@68
   298
                expandKnown(model, x + 1, y + 1);
jtulach@68
   299
            }
jtulach@68
   300
        }
jtulach@68
   301
    }
jtulach@90
   302
    
jtulach@107
   303
    private static int minesAt(Mines model, int x, int y) {
jtulach@107
   304
        if (y < 0 || y >= model.getRows().size()) {
jtulach@107
   305
            return 0;
jtulach@107
   306
        }
jtulach@107
   307
        final List<Square> columns = model.getRows().get(y).getColumns();
jtulach@107
   308
        if (x < 0 || x >= columns.size()) {
jtulach@107
   309
            return 0;
jtulach@107
   310
        }
jtulach@107
   311
        Square sq = columns.get(x);
jtulach@107
   312
        return sq.isMine() ? 1 : 0;
jtulach@107
   313
    }
jtulach@116
   314
jtulach@90
   315
    /**
jtulach@90
   316
     * Called when page is ready
jtulach@90
   317
     */
jtulach@90
   318
    public static void main(String... args) throws Exception {
jtulach@90
   319
        Mines m = new Mines();
jtulach@90
   320
        m.applyBindings();
jtulach@90
   321
    }
jtulach@63
   322
}