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