minesweeper/src/main/java/org/apidesign/demo/minesweeper/MinesModel.java
author Jaroslav Tulach <jtulach@netbeans.org>
Mon, 10 Feb 2014 16:50:08 +0100
branchminesweeper
changeset 79 03bec9dcc860
parent 76 55b2e1d3ad2b
child 90 eff392cfe687
child 98 8ae7a8464c52
child 107 f8f222243d3c
permissions -rw-r--r--
Play a sound when a square in the minefield is made visible
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@63
    42
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@63
   114
        List<Row> rows = new ArrayList<Row>(height);
jtulach@63
   115
        for (int y = 0; y < height; y++) {
jtulach@63
   116
            Square[] columns = new Square[width];
jtulach@63
   117
            for (int x = 0; x < width; x++) {
jtulach@63
   118
                columns[x] = new Square(SquareType.UNKNOWN, false);
jtulach@63
   119
            }
jtulach@63
   120
            rows.add(new Row(columns));
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@63
   136
        model.getRows().clear();
jtulach@63
   137
        model.getRows().addAll(rows);
jtulach@63
   138
    }
jtulach@63
   139
    
jtulach@64
   140
    @ModelOperation static void computeMines(Mines model) {
jtulach@64
   141
        List<Integer> xBombs = new ArrayList<Integer>();
jtulach@64
   142
        List<Integer> yBombs = new ArrayList<Integer>();
jtulach@64
   143
        final List<Row> rows = model.getRows();
jtulach@66
   144
        boolean emptyHidden = false;
jtulach@69
   145
        SquareType[][] arr = new SquareType[rows.size()][];
jtulach@64
   146
        for (int y = 0; y < rows.size(); y++) {
jtulach@64
   147
            final List<Square> columns = rows.get(y).getColumns();
jtulach@69
   148
            arr[y] = new SquareType[columns.size()];
jtulach@64
   149
            for (int x = 0; x < columns.size(); x++) {
jtulach@64
   150
                Square sq = columns.get(x);
jtulach@64
   151
                if (sq.isMine()) {
jtulach@64
   152
                    xBombs.add(x);
jtulach@64
   153
                    yBombs.add(y);
jtulach@64
   154
                }
jtulach@64
   155
                if (sq.getState().isVisible()) {
jtulach@69
   156
                    arr[y][x] = SquareType.N_0;
jtulach@66
   157
                } else {
jtulach@66
   158
                    if (!sq.isMine()) {
jtulach@66
   159
                        emptyHidden = true;
jtulach@66
   160
                    }
jtulach@64
   161
                }
jtulach@64
   162
            }
jtulach@64
   163
        }
jtulach@64
   164
        for (int i = 0; i < xBombs.size(); i++) {
jtulach@64
   165
            int x = xBombs.get(i);
jtulach@64
   166
            int y = yBombs.get(i);
jtulach@64
   167
            
jtulach@69
   168
            incrementAround(arr, x, y);
jtulach@69
   169
        }
jtulach@69
   170
        for (int y = 0; y < rows.size(); y++) {
jtulach@69
   171
            final List<Square> columns = rows.get(y).getColumns();
jtulach@69
   172
            for (int x = 0; x < columns.size(); x++) {
jtulach@69
   173
                Square sq = columns.get(x);
jtulach@69
   174
                final SquareType newState = arr[y][x];
jtulach@69
   175
                if (newState != null && newState != sq.getState()) {
jtulach@69
   176
                    sq.setState(newState);
jtulach@69
   177
                }
jtulach@69
   178
            }
jtulach@64
   179
        }
jtulach@66
   180
        
jtulach@66
   181
        if (!emptyHidden) {
jtulach@66
   182
            model.setState(GameState.WON);
jtulach@68
   183
            showAllBombs(model, SquareType.DISCOVERED);
jtulach@66
   184
        }
jtulach@64
   185
    }
jtulach@64
   186
    
jtulach@69
   187
    private static void incrementAround(SquareType[][] arr, int x, int y) {
jtulach@69
   188
        incrementAt(arr, x - 1, y - 1);
jtulach@69
   189
        incrementAt(arr, x - 1, y);
jtulach@69
   190
        incrementAt(arr, x - 1, y + 1);
jtulach@64
   191
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, y - 1);
jtulach@69
   197
        incrementAt(arr, x, y + 1);
jtulach@64
   198
    }
jtulach@64
   199
    
jtulach@69
   200
    private static void incrementAt(SquareType[][] arr, int x, int y) {
jtulach@69
   201
        if (y >= 0 && y < arr.length) {
jtulach@69
   202
            SquareType[] r = arr[y];
jtulach@69
   203
            if (x >= 0 && x < r.length) {
jtulach@69
   204
                SquareType sq = r[x];
jtulach@69
   205
                if (sq != null) {
jtulach@69
   206
                    r[x] = sq.moreBombsAround();
jtulach@69
   207
                }
jtulach@64
   208
            }
jtulach@64
   209
        }
jtulach@64
   210
    }
jtulach@64
   211
    
jtulach@68
   212
    static void showAllBombs(Mines model, SquareType state) {
jtulach@63
   213
        for (Row row : model.getRows()) {
jtulach@63
   214
            for (Square square : row.getColumns()) {
jtulach@63
   215
                if (square.isMine()) {
jtulach@68
   216
                    square.setState(state);
jtulach@63
   217
                }
jtulach@63
   218
            }
jtulach@63
   219
        }
jtulach@63
   220
    }
jtulach@63
   221
    
jtulach@79
   222
    private static final AudioClip TOUCH = AudioClip.create("move.mp3");
jtulach@63
   223
    @Function static void click(Mines model, Square data) {
jtulach@63
   224
        if (model.getState() != GameState.IN_PROGRESS) {
jtulach@63
   225
            return;
jtulach@63
   226
        }
jtulach@63
   227
        
jtulach@63
   228
        switch (data.getState()) {
jtulach@63
   229
            case UNKNOWN: 
jtulach@63
   230
                if (data.isMine()) {
jtulach@68
   231
                    showAllBombs(model, SquareType.EXPLOSION);
jtulach@63
   232
                    model.setState(GameState.LOST);
jtulach@63
   233
                } else {
jtulach@79
   234
                    TOUCH.play();
jtulach@68
   235
                    expandKnown(model, data);
jtulach@63
   236
                }
jtulach@63
   237
            break;
jtulach@63
   238
        }
jtulach@63
   239
    }
jtulach@68
   240
    private static void expandKnown(Mines model, Square data) {
jtulach@68
   241
        final List<Row> rows = model.getRows();
jtulach@68
   242
        for (int y = 0; y < rows.size(); y++) {
jtulach@68
   243
            final List<Square> columns = rows.get(y).getColumns();
jtulach@68
   244
            for (int x = 0; x < columns.size(); x++) {
jtulach@68
   245
                Square sq = columns.get(x);
jtulach@68
   246
                if (sq == data) {
jtulach@68
   247
                    expandKnown(model, x, y);
jtulach@68
   248
                    return;
jtulach@68
   249
                }
jtulach@68
   250
            }
jtulach@68
   251
        }
jtulach@68
   252
    }
jtulach@68
   253
    private static void expandKnown(Mines model, int x , int y) {
jtulach@68
   254
        if (y < 0 || y >= model.getRows().size()) {
jtulach@68
   255
            return;
jtulach@68
   256
        }
jtulach@68
   257
        final List<Square> columns = model.getRows().get(y).getColumns();
jtulach@68
   258
        if (x < 0 || x >= columns.size()) {
jtulach@68
   259
            return;
jtulach@68
   260
        }
jtulach@68
   261
        final Square sq = columns.get(x);
jtulach@68
   262
        if (sq.getState() == SquareType.UNKNOWN) {
jtulach@68
   263
            sq.setState(SquareType.N_0);
jtulach@68
   264
            model.computeMines();
jtulach@68
   265
            if (sq.getState() == SquareType.N_0) {
jtulach@68
   266
                expandKnown(model, x - 1, y - 1);
jtulach@68
   267
                expandKnown(model, x - 1, y);
jtulach@68
   268
                expandKnown(model, x - 1, y + 1);
jtulach@68
   269
                expandKnown(model, x , y - 1);
jtulach@68
   270
                expandKnown(model, x, y + 1);
jtulach@68
   271
                expandKnown(model, x + 1, y - 1);
jtulach@68
   272
                expandKnown(model, x + 1, y);
jtulach@68
   273
                expandKnown(model, x + 1, y + 1);
jtulach@68
   274
            }
jtulach@68
   275
        }
jtulach@68
   276
    }
jtulach@63
   277
}