minesweeper/src/main/java/org/apidesign/demo/minesweeper/MinesModel.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 07 Feb 2014 15:21:54 +0100
branchminesweeper
changeset 64 3a82f9e6eddd
parent 63 56477205fdb5
child 65 8e31706fc5da
permissions -rw-r--r--
Shows number of bombs in the neibourhood
     1 /**
     2  * The MIT License (MIT)
     3  *
     4  * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     5  *
     6  * Permission is hereby granted, free of charge, to any person obtaining a copy
     7  * of this software and associated documentation files (the "Software"), to deal
     8  * in the Software without restriction, including without limitation the rights
     9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10  * copies of the Software, and to permit persons to whom the Software is
    11  * furnished to do so, subject to the following conditions:
    12  *
    13  * The above copyright notice and this permission notice shall be included in
    14  * all copies or substantial portions of the Software.
    15  *
    16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    22  * THE SOFTWARE.
    23  */
    24 package org.apidesign.demo.minesweeper;
    25 
    26 import java.util.ArrayList;
    27 import java.util.List;
    28 import java.util.Random;
    29 import net.java.html.json.ComputedProperty;
    30 import net.java.html.json.Function;
    31 import net.java.html.json.Model;
    32 import net.java.html.json.ModelOperation;
    33 import net.java.html.json.Property;
    34 
    35 /** Model of the mine field.
    36  */
    37 @Model(className = "Mines", properties = {
    38     @Property(name = "state", type = MinesModel.GameState.class),
    39     @Property(name = "rows", type = Row.class, array = true),
    40 })
    41 final class MinesModel {
    42     enum GameState {
    43         IN_PROGRESS, WON, LOST;
    44     }
    45     
    46     @Model(className = "Row", properties = {
    47         @Property(name = "columns", type = Square.class, array = true)
    48     })
    49     static class RowModel {
    50     }
    51 
    52     @Model(className = "Square", properties = {
    53         @Property(name = "state", type = SquareType.class),
    54         @Property(name = "mine", type = boolean.class)
    55     })
    56     static class SquareModel {
    57         @ComputedProperty static String text(SquareType state) {
    58             if (state == null) return " ";
    59             switch (state) {
    60                 case MINE: return "B";
    61                 case UNKNOWN: return "?";
    62                 case N_0: return " ";
    63             }
    64             return "" + state.ordinal();
    65         }
    66     }
    67     
    68     enum SquareType {
    69         
    70         N_0, N_1, N_2, N_3, N_4, N_5, N_6, N_7, N_8,
    71         UNKNOWN, MINE;
    72         
    73         final boolean isVisible() {
    74             return name().startsWith("N_");
    75         }
    76 
    77         final SquareType moreBombs() {
    78             if (this == MINE || this == UNKNOWN) {
    79                 return this;
    80             }
    81             return values()[ordinal() + 1];
    82         }
    83     }
    84     
    85     @ModelOperation static void init(Mines model, int width, int height, int mines) {
    86         List<Row> rows = new ArrayList<Row>(height);
    87         for (int y = 0; y < height; y++) {
    88             Square[] columns = new Square[width];
    89             for (int x = 0; x < width; x++) {
    90                 columns[x] = new Square(SquareType.UNKNOWN, false);
    91             }
    92             rows.add(new Row(columns));
    93         }
    94         
    95         Random r = new Random();
    96         while (mines > 0) {
    97             int x = r.nextInt(width);
    98             int y = r.nextInt(height);
    99             final Square s = rows.get(y).getColumns().get(x);
   100             if (s.isMine()) {
   101                 continue;
   102             }
   103             s.setMine(true);
   104             mines--;
   105         }
   106 
   107         model.setState(GameState.IN_PROGRESS);
   108         model.getRows().clear();
   109         model.getRows().addAll(rows);
   110     }
   111     
   112     @ModelOperation static void computeMines(Mines model) {
   113         List<Integer> xBombs = new ArrayList<Integer>();
   114         List<Integer> yBombs = new ArrayList<Integer>();
   115         final List<Row> rows = model.getRows();
   116         for (int y = 0; y < rows.size(); y++) {
   117             final List<Square> columns = rows.get(y).getColumns();
   118             for (int x = 0; x < columns.size(); x++) {
   119                 Square sq = columns.get(x);
   120                 if (sq.isMine()) {
   121                     xBombs.add(x);
   122                     yBombs.add(y);
   123                 }
   124                 if (sq.getState().isVisible()) {
   125                     sq.setState(SquareType.N_0);
   126                 }
   127             }
   128         }
   129         for (int i = 0; i < xBombs.size(); i++) {
   130             int x = xBombs.get(i);
   131             int y = yBombs.get(i);
   132             
   133             incrementAround(model, x, y);
   134         }
   135     }
   136     
   137     private static void incrementAround(Mines model, int x, int y) {
   138         incrementAt(model, x - 1, y - 1);
   139         incrementAt(model, x - 1, y);
   140         incrementAt(model, x - 1, y + 1);
   141 
   142         incrementAt(model, x + 1, y - 1);
   143         incrementAt(model, x + 1, y);
   144         incrementAt(model, x + 1, y + 1);
   145         
   146         incrementAt(model, x, y - 1);
   147         incrementAt(model, x, y + 1);
   148     }
   149     
   150     private static void incrementAt(Mines model, int x, int y) {
   151         if (y >= 0 && y < model.getRows().size()) {
   152             Row r = model.getRows().get(y);
   153             if (x >= 0 && x < r.getColumns().size()) {
   154                 Square sq = r.getColumns().get(x);
   155                 sq.setState(sq.getState().moreBombs());
   156             }
   157         }
   158     }
   159     
   160     static void showAllBombs(Mines model) {
   161         for (Row row : model.getRows()) {
   162             for (Square square : row.getColumns()) {
   163                 if (square.isMine()) {
   164                     square.setState(SquareType.MINE);
   165                 }
   166             }
   167         }
   168     }
   169     
   170     @Function static void click(Mines model, Square data) {
   171         if (model.getState() != GameState.IN_PROGRESS) {
   172             return;
   173         }
   174         
   175         switch (data.getState()) {
   176             case UNKNOWN: 
   177                 if (data.isMine()) {
   178                     showAllBombs(model);
   179                     model.setState(GameState.LOST);
   180                 } else {
   181                     data.setState(SquareType.N_0);
   182                     model.computeMines();
   183                 }
   184             break;
   185         }
   186     }
   187 }