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 moreBombs() {
88 return values()[ordinal() + 1];
92 @ModelOperation static void init(Mines model, int width, int height, int mines) {
93 List<Row> rows = new ArrayList<Row>(height);
94 for (int y = 0; y < height; y++) {
95 Square[] columns = new Square[width];
96 for (int x = 0; x < width; x++) {
97 columns[x] = new Square(SquareType.UNKNOWN, false);
99 rows.add(new Row(columns));
102 Random r = new Random();
104 int x = r.nextInt(width);
105 int y = r.nextInt(height);
106 final Square s = rows.get(y).getColumns().get(x);
114 model.setState(GameState.IN_PROGRESS);
115 model.getRows().clear();
116 model.getRows().addAll(rows);
119 @ModelOperation static void computeMines(Mines model) {
120 List<Integer> xBombs = new ArrayList<Integer>();
121 List<Integer> yBombs = new ArrayList<Integer>();
122 final List<Row> rows = model.getRows();
123 boolean emptyHidden = false;
124 for (int y = 0; y < rows.size(); y++) {
125 final List<Square> columns = rows.get(y).getColumns();
126 for (int x = 0; x < columns.size(); x++) {
127 Square sq = columns.get(x);
132 if (sq.getState().isVisible()) {
133 sq.setState(SquareType.N_0);
141 for (int i = 0; i < xBombs.size(); i++) {
142 int x = xBombs.get(i);
143 int y = yBombs.get(i);
145 incrementAround(model, x, y);
149 model.setState(GameState.WON);
150 showAllBombs(model, SquareType.DISCOVERED);
154 private static void incrementAround(Mines model, int x, int y) {
155 incrementAt(model, x - 1, y - 1);
156 incrementAt(model, x - 1, y);
157 incrementAt(model, x - 1, y + 1);
159 incrementAt(model, x + 1, y - 1);
160 incrementAt(model, x + 1, y);
161 incrementAt(model, x + 1, y + 1);
163 incrementAt(model, x, y - 1);
164 incrementAt(model, x, y + 1);
167 private static void incrementAt(Mines model, int x, int y) {
168 if (y >= 0 && y < model.getRows().size()) {
169 Row r = model.getRows().get(y);
170 if (x >= 0 && x < r.getColumns().size()) {
171 Square sq = r.getColumns().get(x);
172 sq.setState(sq.getState().moreBombs());
177 static void showAllBombs(Mines model, SquareType state) {
178 for (Row row : model.getRows()) {
179 for (Square square : row.getColumns()) {
180 if (square.isMine()) {
181 square.setState(state);
187 @Function static void click(Mines model, Square data) {
188 if (model.getState() != GameState.IN_PROGRESS) {
192 switch (data.getState()) {
195 showAllBombs(model, SquareType.EXPLOSION);
196 model.setState(GameState.LOST);
198 expandKnown(model, data);
203 private static void expandKnown(Mines model, Square data) {
204 final List<Row> rows = model.getRows();
205 for (int y = 0; y < rows.size(); y++) {
206 final List<Square> columns = rows.get(y).getColumns();
207 for (int x = 0; x < columns.size(); x++) {
208 Square sq = columns.get(x);
210 expandKnown(model, x, y);
216 private static void expandKnown(Mines model, int x , int y) {
217 if (y < 0 || y >= model.getRows().size()) {
220 final List<Square> columns = model.getRows().get(y).getColumns();
221 if (x < 0 || x >= columns.size()) {
224 final Square sq = columns.get(x);
225 if (sq.getState() == SquareType.UNKNOWN) {
226 sq.setState(SquareType.N_0);
227 model.computeMines();
228 if (sq.getState() == SquareType.N_0) {
229 expandKnown(model, x - 1, y - 1);
230 expandKnown(model, x - 1, y);
231 expandKnown(model, x - 1, y + 1);
232 expandKnown(model, x , y - 1);
233 expandKnown(model, x, y + 1);
234 expandKnown(model, x + 1, y - 1);
235 expandKnown(model, x + 1, y);
236 expandKnown(model, x + 1, y + 1);