minesweeper/src/main/java/org/apidesign/demo/minesweeper/MinesModel.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 07 Feb 2014 16:35:45 +0100
branchminesweeper
changeset 66 9cf895cde4bd
parent 65 8e31706fc5da
child 68 d41833895448
permissions -rw-r--r--
Game is won when only hidden places contain 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@63
    57
        @ComputedProperty static String text(SquareType state) {
jtulach@63
    58
            if (state == null) return " ";
jtulach@63
    59
            switch (state) {
jtulach@63
    60
                case MINE: return "B";
jtulach@63
    61
                case UNKNOWN: return "?";
jtulach@63
    62
                case N_0: return " ";
jtulach@63
    63
            }
jtulach@63
    64
            return "" + state.ordinal();
jtulach@63
    65
        }
jtulach@65
    66
        
jtulach@65
    67
        @ComputedProperty static String style(SquareType state) {
jtulach@65
    68
            return state == null ? null : state.toString();
jtulach@65
    69
        }
jtulach@63
    70
    }
jtulach@63
    71
    
jtulach@63
    72
    enum SquareType {
jtulach@64
    73
        
jtulach@63
    74
        N_0, N_1, N_2, N_3, N_4, N_5, N_6, N_7, N_8,
jtulach@64
    75
        UNKNOWN, MINE;
jtulach@64
    76
        
jtulach@64
    77
        final boolean isVisible() {
jtulach@64
    78
            return name().startsWith("N_");
jtulach@64
    79
        }
jtulach@64
    80
jtulach@64
    81
        final SquareType moreBombs() {
jtulach@64
    82
            if (this == MINE || this == UNKNOWN) {
jtulach@64
    83
                return this;
jtulach@64
    84
            }
jtulach@64
    85
            return values()[ordinal() + 1];
jtulach@64
    86
        }
jtulach@63
    87
    }
jtulach@63
    88
    
jtulach@63
    89
    @ModelOperation static void init(Mines model, int width, int height, int mines) {
jtulach@63
    90
        List<Row> rows = new ArrayList<Row>(height);
jtulach@63
    91
        for (int y = 0; y < height; y++) {
jtulach@63
    92
            Square[] columns = new Square[width];
jtulach@63
    93
            for (int x = 0; x < width; x++) {
jtulach@63
    94
                columns[x] = new Square(SquareType.UNKNOWN, false);
jtulach@63
    95
            }
jtulach@63
    96
            rows.add(new Row(columns));
jtulach@63
    97
        }
jtulach@63
    98
        
jtulach@63
    99
        Random r = new Random();
jtulach@63
   100
        while (mines > 0) {
jtulach@63
   101
            int x = r.nextInt(width);
jtulach@63
   102
            int y = r.nextInt(height);
jtulach@63
   103
            final Square s = rows.get(y).getColumns().get(x);
jtulach@63
   104
            if (s.isMine()) {
jtulach@63
   105
                continue;
jtulach@63
   106
            }
jtulach@63
   107
            s.setMine(true);
jtulach@63
   108
            mines--;
jtulach@63
   109
        }
jtulach@63
   110
jtulach@63
   111
        model.setState(GameState.IN_PROGRESS);
jtulach@63
   112
        model.getRows().clear();
jtulach@63
   113
        model.getRows().addAll(rows);
jtulach@63
   114
    }
jtulach@63
   115
    
jtulach@64
   116
    @ModelOperation static void computeMines(Mines model) {
jtulach@64
   117
        List<Integer> xBombs = new ArrayList<Integer>();
jtulach@64
   118
        List<Integer> yBombs = new ArrayList<Integer>();
jtulach@64
   119
        final List<Row> rows = model.getRows();
jtulach@66
   120
        boolean emptyHidden = false;
jtulach@64
   121
        for (int y = 0; y < rows.size(); y++) {
jtulach@64
   122
            final List<Square> columns = rows.get(y).getColumns();
jtulach@64
   123
            for (int x = 0; x < columns.size(); x++) {
jtulach@64
   124
                Square sq = columns.get(x);
jtulach@64
   125
                if (sq.isMine()) {
jtulach@64
   126
                    xBombs.add(x);
jtulach@64
   127
                    yBombs.add(y);
jtulach@64
   128
                }
jtulach@64
   129
                if (sq.getState().isVisible()) {
jtulach@64
   130
                    sq.setState(SquareType.N_0);
jtulach@66
   131
                } else {
jtulach@66
   132
                    if (!sq.isMine()) {
jtulach@66
   133
                        emptyHidden = true;
jtulach@66
   134
                    }
jtulach@64
   135
                }
jtulach@64
   136
            }
jtulach@64
   137
        }
jtulach@64
   138
        for (int i = 0; i < xBombs.size(); i++) {
jtulach@64
   139
            int x = xBombs.get(i);
jtulach@64
   140
            int y = yBombs.get(i);
jtulach@64
   141
            
jtulach@64
   142
            incrementAround(model, x, y);
jtulach@64
   143
        }
jtulach@66
   144
        
jtulach@66
   145
        if (!emptyHidden) {
jtulach@66
   146
            model.setState(GameState.WON);
jtulach@66
   147
        }
jtulach@64
   148
    }
jtulach@64
   149
    
jtulach@64
   150
    private static void incrementAround(Mines model, int x, int y) {
jtulach@64
   151
        incrementAt(model, x - 1, y - 1);
jtulach@64
   152
        incrementAt(model, x - 1, y);
jtulach@64
   153
        incrementAt(model, x - 1, y + 1);
jtulach@64
   154
jtulach@64
   155
        incrementAt(model, x + 1, y - 1);
jtulach@64
   156
        incrementAt(model, x + 1, y);
jtulach@64
   157
        incrementAt(model, x + 1, y + 1);
jtulach@64
   158
        
jtulach@64
   159
        incrementAt(model, x, y - 1);
jtulach@64
   160
        incrementAt(model, x, y + 1);
jtulach@64
   161
    }
jtulach@64
   162
    
jtulach@64
   163
    private static void incrementAt(Mines model, int x, int y) {
jtulach@64
   164
        if (y >= 0 && y < model.getRows().size()) {
jtulach@64
   165
            Row r = model.getRows().get(y);
jtulach@64
   166
            if (x >= 0 && x < r.getColumns().size()) {
jtulach@64
   167
                Square sq = r.getColumns().get(x);
jtulach@64
   168
                sq.setState(sq.getState().moreBombs());
jtulach@64
   169
            }
jtulach@64
   170
        }
jtulach@64
   171
    }
jtulach@64
   172
    
jtulach@63
   173
    static void showAllBombs(Mines model) {
jtulach@63
   174
        for (Row row : model.getRows()) {
jtulach@63
   175
            for (Square square : row.getColumns()) {
jtulach@63
   176
                if (square.isMine()) {
jtulach@63
   177
                    square.setState(SquareType.MINE);
jtulach@63
   178
                }
jtulach@63
   179
            }
jtulach@63
   180
        }
jtulach@63
   181
    }
jtulach@63
   182
    
jtulach@63
   183
    @Function static void click(Mines model, Square data) {
jtulach@63
   184
        if (model.getState() != GameState.IN_PROGRESS) {
jtulach@63
   185
            return;
jtulach@63
   186
        }
jtulach@63
   187
        
jtulach@63
   188
        switch (data.getState()) {
jtulach@63
   189
            case UNKNOWN: 
jtulach@63
   190
                if (data.isMine()) {
jtulach@63
   191
                    showAllBombs(model);
jtulach@63
   192
                    model.setState(GameState.LOST);
jtulach@63
   193
                } else {
jtulach@63
   194
                    data.setState(SquareType.N_0);
jtulach@64
   195
                    model.computeMines();
jtulach@63
   196
                }
jtulach@63
   197
            break;
jtulach@63
   198
        }
jtulach@63
   199
    }
jtulach@63
   200
}