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