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 text(SquareType state) {
58 if (state == null) return " ";
60 case EXPLOSION: return "B";
61 case UNKNOWN: return "?";
64 return "" + state.ordinal();
67 @ComputedProperty static String style(SquareType state) {
68 return state == null ? null : state.toString();
73 N_0, N_1, N_2, N_3, N_4, N_5, N_6, N_7, N_8,
74 UNKNOWN, EXPLOSION, DISCOVERED;
76 final boolean isVisible() {
77 return name().startsWith("N_");
80 final SquareType moreBombsAround() {
88 return values()[ordinal() + 1];
92 @Function static void smallGame(Mines model) {
95 @Function static void normalGame(Mines model) {
96 model.init(10, 10, 10);
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);
106 rows.add(new Row(columns));
109 Random r = new Random();
111 int x = r.nextInt(width);
112 int y = r.nextInt(height);
113 final Square s = rows.get(y).getColumns().get(x);
121 model.setState(GameState.IN_PROGRESS);
122 model.getRows().clear();
123 model.getRows().addAll(rows);
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);
141 if (sq.getState().isVisible()) {
142 arr[y][x] = SquareType.N_0;
150 for (int i = 0; i < xBombs.size(); i++) {
151 int x = xBombs.get(i);
152 int y = yBombs.get(i);
154 incrementAround(arr, x, y);
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);
168 model.setState(GameState.WON);
169 showAllBombs(model, SquareType.DISCOVERED);
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);
178 incrementAt(arr, x + 1, y - 1);
179 incrementAt(arr, x + 1, y);
180 incrementAt(arr, x + 1, y + 1);
182 incrementAt(arr, x, y - 1);
183 incrementAt(arr, x, y + 1);
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];
192 r[x] = sq.moreBombsAround();
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);
208 @Function static void click(Mines model, Square data) {
209 if (model.getState() != GameState.IN_PROGRESS) {
213 switch (data.getState()) {
216 showAllBombs(model, SquareType.EXPLOSION);
217 model.setState(GameState.LOST);
219 expandKnown(model, data);
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);
231 expandKnown(model, x, y);
237 private static void expandKnown(Mines model, int x , int y) {
238 if (y < 0 || y >= model.getRows().size()) {
241 final List<Square> columns = model.getRows().get(y).getColumns();
242 if (x < 0 || x >= columns.size()) {
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);