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