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