minesweeper/src/main/java/org/apidesign/demo/minesweeper/MinesModel.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 08 Feb 2014 06:23:40 +0100
branchminesweeper
changeset 71 3e372ded7adc
parent 70 5f851f669a15
child 72 9dacd7d46405
permissions -rw-r--r--
A bit of responsive design and using OK sign for discovered bombs
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@63
    34
jtulach@63
    35
/** Model of the mine field.
jtulach@63
    36
 */
jtulach@63
    37
@Model(className = "Mines", properties = {
jtulach@63
    38
    @Property(name = "state", type = MinesModel.GameState.class),
jtulach@63
    39
    @Property(name = "rows", type = Row.class, array = true),
jtulach@63
    40
})
jtulach@63
    41
final class MinesModel {
jtulach@63
    42
    enum GameState {
jtulach@63
    43
        IN_PROGRESS, WON, LOST;
jtulach@63
    44
    }
jtulach@63
    45
    
jtulach@63
    46
    @Model(className = "Row", properties = {
jtulach@63
    47
        @Property(name = "columns", type = Square.class, array = true)
jtulach@63
    48
    })
jtulach@63
    49
    static class RowModel {
jtulach@63
    50
    }
jtulach@63
    51
jtulach@63
    52
    @Model(className = "Square", properties = {
jtulach@63
    53
        @Property(name = "state", type = SquareType.class),
jtulach@63
    54
        @Property(name = "mine", type = boolean.class)
jtulach@63
    55
    })
jtulach@63
    56
    static class SquareModel {
jtulach@71
    57
        @ComputedProperty static String html(SquareType state) {
jtulach@63
    58
            if (state == null) return " ";
jtulach@63
    59
            switch (state) {
jtulach@68
    60
                case EXPLOSION: return "B";
jtulach@63
    61
                case UNKNOWN: return "?";
jtulach@71
    62
                case DISCOVERED: return "&#x2714;";  
jtulach@71
    63
                case N_0: return "&nbsp;";
jtulach@63
    64
            }
jtulach@63
    65
            return "" + state.ordinal();
jtulach@63
    66
        }
jtulach@65
    67
        
jtulach@65
    68
        @ComputedProperty static String style(SquareType state) {
jtulach@65
    69
            return state == null ? null : state.toString();
jtulach@65
    70
        }
jtulach@63
    71
    }
jtulach@63
    72
    
jtulach@63
    73
    enum SquareType {
jtulach@63
    74
        N_0, N_1, N_2, N_3, N_4, N_5, N_6, N_7, N_8,
jtulach@68
    75
        UNKNOWN, EXPLOSION, DISCOVERED;
jtulach@64
    76
        
jtulach@64
    77
        final boolean isVisible() {
jtulach@64
    78
            return name().startsWith("N_");
jtulach@64
    79
        }
jtulach@64
    80
jtulach@69
    81
        final SquareType moreBombsAround() {
jtulach@68
    82
            switch (this) {
jtulach@68
    83
                case EXPLOSION:
jtulach@68
    84
                case UNKNOWN:
jtulach@68
    85
                case DISCOVERED:
jtulach@68
    86
                case N_8:
jtulach@68
    87
                    return this;
jtulach@64
    88
            }
jtulach@64
    89
            return values()[ordinal() + 1];
jtulach@64
    90
        }
jtulach@63
    91
    }
jtulach@63
    92
    
jtulach@70
    93
    @Function static void smallGame(Mines model) {
jtulach@70
    94
        model.init(5, 5, 5);
jtulach@70
    95
    }
jtulach@70
    96
    @Function static void normalGame(Mines model) {
jtulach@70
    97
        model.init(10, 10, 10);
jtulach@70
    98
    }
jtulach@70
    99
    
jtulach@63
   100
    @ModelOperation static void init(Mines model, int width, int height, int mines) {
jtulach@63
   101
        List<Row> rows = new ArrayList<Row>(height);
jtulach@63
   102
        for (int y = 0; y < height; y++) {
jtulach@63
   103
            Square[] columns = new Square[width];
jtulach@63
   104
            for (int x = 0; x < width; x++) {
jtulach@63
   105
                columns[x] = new Square(SquareType.UNKNOWN, false);
jtulach@63
   106
            }
jtulach@63
   107
            rows.add(new Row(columns));
jtulach@63
   108
        }
jtulach@63
   109
        
jtulach@63
   110
        Random r = new Random();
jtulach@63
   111
        while (mines > 0) {
jtulach@63
   112
            int x = r.nextInt(width);
jtulach@63
   113
            int y = r.nextInt(height);
jtulach@63
   114
            final Square s = rows.get(y).getColumns().get(x);
jtulach@63
   115
            if (s.isMine()) {
jtulach@63
   116
                continue;
jtulach@63
   117
            }
jtulach@63
   118
            s.setMine(true);
jtulach@63
   119
            mines--;
jtulach@63
   120
        }
jtulach@63
   121
jtulach@63
   122
        model.setState(GameState.IN_PROGRESS);
jtulach@63
   123
        model.getRows().clear();
jtulach@63
   124
        model.getRows().addAll(rows);
jtulach@63
   125
    }
jtulach@63
   126
    
jtulach@64
   127
    @ModelOperation static void computeMines(Mines model) {
jtulach@64
   128
        List<Integer> xBombs = new ArrayList<Integer>();
jtulach@64
   129
        List<Integer> yBombs = new ArrayList<Integer>();
jtulach@64
   130
        final List<Row> rows = model.getRows();
jtulach@66
   131
        boolean emptyHidden = false;
jtulach@69
   132
        SquareType[][] arr = new SquareType[rows.size()][];
jtulach@64
   133
        for (int y = 0; y < rows.size(); y++) {
jtulach@64
   134
            final List<Square> columns = rows.get(y).getColumns();
jtulach@69
   135
            arr[y] = new SquareType[columns.size()];
jtulach@64
   136
            for (int x = 0; x < columns.size(); x++) {
jtulach@64
   137
                Square sq = columns.get(x);
jtulach@64
   138
                if (sq.isMine()) {
jtulach@64
   139
                    xBombs.add(x);
jtulach@64
   140
                    yBombs.add(y);
jtulach@64
   141
                }
jtulach@64
   142
                if (sq.getState().isVisible()) {
jtulach@69
   143
                    arr[y][x] = SquareType.N_0;
jtulach@66
   144
                } else {
jtulach@66
   145
                    if (!sq.isMine()) {
jtulach@66
   146
                        emptyHidden = true;
jtulach@66
   147
                    }
jtulach@64
   148
                }
jtulach@64
   149
            }
jtulach@64
   150
        }
jtulach@64
   151
        for (int i = 0; i < xBombs.size(); i++) {
jtulach@64
   152
            int x = xBombs.get(i);
jtulach@64
   153
            int y = yBombs.get(i);
jtulach@64
   154
            
jtulach@69
   155
            incrementAround(arr, x, y);
jtulach@69
   156
        }
jtulach@69
   157
        for (int y = 0; y < rows.size(); y++) {
jtulach@69
   158
            final List<Square> columns = rows.get(y).getColumns();
jtulach@69
   159
            for (int x = 0; x < columns.size(); x++) {
jtulach@69
   160
                Square sq = columns.get(x);
jtulach@69
   161
                final SquareType newState = arr[y][x];
jtulach@69
   162
                if (newState != null && newState != sq.getState()) {
jtulach@69
   163
                    sq.setState(newState);
jtulach@69
   164
                }
jtulach@69
   165
            }
jtulach@64
   166
        }
jtulach@66
   167
        
jtulach@66
   168
        if (!emptyHidden) {
jtulach@66
   169
            model.setState(GameState.WON);
jtulach@68
   170
            showAllBombs(model, SquareType.DISCOVERED);
jtulach@66
   171
        }
jtulach@64
   172
    }
jtulach@64
   173
    
jtulach@69
   174
    private static void incrementAround(SquareType[][] arr, int x, int y) {
jtulach@69
   175
        incrementAt(arr, x - 1, y - 1);
jtulach@69
   176
        incrementAt(arr, x - 1, y);
jtulach@69
   177
        incrementAt(arr, x - 1, y + 1);
jtulach@64
   178
jtulach@69
   179
        incrementAt(arr, x + 1, y - 1);
jtulach@69
   180
        incrementAt(arr, x + 1, y);
jtulach@69
   181
        incrementAt(arr, x + 1, y + 1);
jtulach@64
   182
        
jtulach@69
   183
        incrementAt(arr, x, y - 1);
jtulach@69
   184
        incrementAt(arr, x, y + 1);
jtulach@64
   185
    }
jtulach@64
   186
    
jtulach@69
   187
    private static void incrementAt(SquareType[][] arr, int x, int y) {
jtulach@69
   188
        if (y >= 0 && y < arr.length) {
jtulach@69
   189
            SquareType[] r = arr[y];
jtulach@69
   190
            if (x >= 0 && x < r.length) {
jtulach@69
   191
                SquareType sq = r[x];
jtulach@69
   192
                if (sq != null) {
jtulach@69
   193
                    r[x] = sq.moreBombsAround();
jtulach@69
   194
                }
jtulach@64
   195
            }
jtulach@64
   196
        }
jtulach@64
   197
    }
jtulach@64
   198
    
jtulach@68
   199
    static void showAllBombs(Mines model, SquareType state) {
jtulach@63
   200
        for (Row row : model.getRows()) {
jtulach@63
   201
            for (Square square : row.getColumns()) {
jtulach@63
   202
                if (square.isMine()) {
jtulach@68
   203
                    square.setState(state);
jtulach@63
   204
                }
jtulach@63
   205
            }
jtulach@63
   206
        }
jtulach@63
   207
    }
jtulach@63
   208
    
jtulach@63
   209
    @Function static void click(Mines model, Square data) {
jtulach@63
   210
        if (model.getState() != GameState.IN_PROGRESS) {
jtulach@63
   211
            return;
jtulach@63
   212
        }
jtulach@63
   213
        
jtulach@63
   214
        switch (data.getState()) {
jtulach@63
   215
            case UNKNOWN: 
jtulach@63
   216
                if (data.isMine()) {
jtulach@68
   217
                    showAllBombs(model, SquareType.EXPLOSION);
jtulach@63
   218
                    model.setState(GameState.LOST);
jtulach@63
   219
                } else {
jtulach@68
   220
                    expandKnown(model, data);
jtulach@63
   221
                }
jtulach@63
   222
            break;
jtulach@63
   223
        }
jtulach@63
   224
    }
jtulach@68
   225
    private static void expandKnown(Mines model, Square data) {
jtulach@68
   226
        final List<Row> rows = model.getRows();
jtulach@68
   227
        for (int y = 0; y < rows.size(); y++) {
jtulach@68
   228
            final List<Square> columns = rows.get(y).getColumns();
jtulach@68
   229
            for (int x = 0; x < columns.size(); x++) {
jtulach@68
   230
                Square sq = columns.get(x);
jtulach@68
   231
                if (sq == data) {
jtulach@68
   232
                    expandKnown(model, x, y);
jtulach@68
   233
                    return;
jtulach@68
   234
                }
jtulach@68
   235
            }
jtulach@68
   236
        }
jtulach@68
   237
    }
jtulach@68
   238
    private static void expandKnown(Mines model, int x , int y) {
jtulach@68
   239
        if (y < 0 || y >= model.getRows().size()) {
jtulach@68
   240
            return;
jtulach@68
   241
        }
jtulach@68
   242
        final List<Square> columns = model.getRows().get(y).getColumns();
jtulach@68
   243
        if (x < 0 || x >= columns.size()) {
jtulach@68
   244
            return;
jtulach@68
   245
        }
jtulach@68
   246
        final Square sq = columns.get(x);
jtulach@68
   247
        if (sq.getState() == SquareType.UNKNOWN) {
jtulach@68
   248
            sq.setState(SquareType.N_0);
jtulach@68
   249
            model.computeMines();
jtulach@68
   250
            if (sq.getState() == SquareType.N_0) {
jtulach@68
   251
                expandKnown(model, x - 1, y - 1);
jtulach@68
   252
                expandKnown(model, x - 1, y);
jtulach@68
   253
                expandKnown(model, x - 1, y + 1);
jtulach@68
   254
                expandKnown(model, x , y - 1);
jtulach@68
   255
                expandKnown(model, x, y + 1);
jtulach@68
   256
                expandKnown(model, x + 1, y - 1);
jtulach@68
   257
                expandKnown(model, x + 1, y);
jtulach@68
   258
                expandKnown(model, x + 1, y + 1);
jtulach@68
   259
            }
jtulach@68
   260
        }
jtulach@68
   261
    }
jtulach@63
   262
}