2 * The MIT License (MIT)
4 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 package org.apidesign.demo.minesweeper;
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;
35 /** Model of the mine field.
37 @Model(className = "Mines", properties = {
38 @Property(name = "state", type = MinesModel.GameState.class),
39 @Property(name = "rows", type = Row.class, array = true),
41 final class MinesModel {
43 IN_PROGRESS, WON, LOST;
46 @Model(className = "Row", properties = {
47 @Property(name = "columns", type = Square.class, array = true)
49 static class RowModel {
52 @Model(className = "Square", properties = {
53 @Property(name = "state", type = SquareType.class),
54 @Property(name = "mine", type = boolean.class)
56 static class SquareModel {
57 @ComputedProperty static String html(SquareType state) {
58 if (state == null) return " ";
60 case EXPLOSION: return "B";
61 case UNKNOWN: return "?";
62 case DISCOVERED: return "✔";
63 case N_0: return " ";
65 return "" + state.ordinal();
68 @ComputedProperty static String style(SquareType state) {
69 return state == null ? null : state.toString();
74 N_0, N_1, N_2, N_3, N_4, N_5, N_6, N_7, N_8,
75 UNKNOWN, EXPLOSION, DISCOVERED;
77 final boolean isVisible() {
78 return name().startsWith("N_");
81 final SquareType moreBombsAround() {
89 return values()[ordinal() + 1];
93 @Function static void smallGame(Mines model) {
96 @Function static void normalGame(Mines model) {
97 model.init(10, 10, 10);
100 @ModelOperation static void init(Mines model, int width, int height, int mines) {
101 List<Row> rows = new ArrayList<Row>(height);
102 for (int y = 0; y < height; y++) {
103 Square[] columns = new Square[width];
104 for (int x = 0; x < width; x++) {
105 columns[x] = new Square(SquareType.UNKNOWN, false);
107 rows.add(new Row(columns));
110 Random r = new Random();
112 int x = r.nextInt(width);
113 int y = r.nextInt(height);
114 final Square s = rows.get(y).getColumns().get(x);
122 model.setState(GameState.IN_PROGRESS);
123 model.getRows().clear();
124 model.getRows().addAll(rows);
127 @ModelOperation static void computeMines(Mines model) {
128 List<Integer> xBombs = new ArrayList<Integer>();
129 List<Integer> yBombs = new ArrayList<Integer>();
130 final List<Row> rows = model.getRows();
131 boolean emptyHidden = false;
132 SquareType[][] arr = new SquareType[rows.size()][];
133 for (int y = 0; y < rows.size(); y++) {
134 final List<Square> columns = rows.get(y).getColumns();
135 arr[y] = new SquareType[columns.size()];
136 for (int x = 0; x < columns.size(); x++) {
137 Square sq = columns.get(x);
142 if (sq.getState().isVisible()) {
143 arr[y][x] = SquareType.N_0;
151 for (int i = 0; i < xBombs.size(); i++) {
152 int x = xBombs.get(i);
153 int y = yBombs.get(i);
155 incrementAround(arr, x, y);
157 for (int y = 0; y < rows.size(); y++) {
158 final List<Square> columns = rows.get(y).getColumns();
159 for (int x = 0; x < columns.size(); x++) {
160 Square sq = columns.get(x);
161 final SquareType newState = arr[y][x];
162 if (newState != null && newState != sq.getState()) {
163 sq.setState(newState);
169 model.setState(GameState.WON);
170 showAllBombs(model, SquareType.DISCOVERED);
174 private static void incrementAround(SquareType[][] arr, int x, int y) {
175 incrementAt(arr, x - 1, y - 1);
176 incrementAt(arr, x - 1, y);
177 incrementAt(arr, x - 1, y + 1);
179 incrementAt(arr, x + 1, y - 1);
180 incrementAt(arr, x + 1, y);
181 incrementAt(arr, x + 1, y + 1);
183 incrementAt(arr, x, y - 1);
184 incrementAt(arr, x, y + 1);
187 private static void incrementAt(SquareType[][] arr, int x, int y) {
188 if (y >= 0 && y < arr.length) {
189 SquareType[] r = arr[y];
190 if (x >= 0 && x < r.length) {
191 SquareType sq = r[x];
193 r[x] = sq.moreBombsAround();
199 static void showAllBombs(Mines model, SquareType state) {
200 for (Row row : model.getRows()) {
201 for (Square square : row.getColumns()) {
202 if (square.isMine()) {
203 square.setState(state);
209 @Function static void click(Mines model, Square data) {
210 if (model.getState() != GameState.IN_PROGRESS) {
214 switch (data.getState()) {
217 showAllBombs(model, SquareType.EXPLOSION);
218 model.setState(GameState.LOST);
220 expandKnown(model, data);
225 private static void expandKnown(Mines model, Square data) {
226 final List<Row> rows = model.getRows();
227 for (int y = 0; y < rows.size(); y++) {
228 final List<Square> columns = rows.get(y).getColumns();
229 for (int x = 0; x < columns.size(); x++) {
230 Square sq = columns.get(x);
232 expandKnown(model, x, y);
238 private static void expandKnown(Mines model, int x , int y) {
239 if (y < 0 || y >= model.getRows().size()) {
242 final List<Square> columns = model.getRows().get(y).getColumns();
243 if (x < 0 || x >= columns.size()) {
246 final Square sq = columns.get(x);
247 if (sq.getState() == SquareType.UNKNOWN) {
248 sq.setState(SquareType.N_0);
249 model.computeMines();
250 if (sq.getState() == SquareType.N_0) {
251 expandKnown(model, x - 1, y - 1);
252 expandKnown(model, x - 1, y);
253 expandKnown(model, x - 1, y + 1);
254 expandKnown(model, x , y - 1);
255 expandKnown(model, x, y + 1);
256 expandKnown(model, x + 1, y - 1);
257 expandKnown(model, x + 1, y);
258 expandKnown(model, x + 1, y + 1);