1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/minesweeper/src/main/java/org/apidesign/demo/minesweeper/MinesModel.java Fri Feb 07 14:47:07 2014 +0100
1.3 @@ -0,0 +1,126 @@
1.4 +/**
1.5 + * The MIT License (MIT)
1.6 + *
1.7 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.8 + *
1.9 + * Permission is hereby granted, free of charge, to any person obtaining a copy
1.10 + * of this software and associated documentation files (the "Software"), to deal
1.11 + * in the Software without restriction, including without limitation the rights
1.12 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1.13 + * copies of the Software, and to permit persons to whom the Software is
1.14 + * furnished to do so, subject to the following conditions:
1.15 + *
1.16 + * The above copyright notice and this permission notice shall be included in
1.17 + * all copies or substantial portions of the Software.
1.18 + *
1.19 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1.20 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1.21 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1.22 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1.23 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1.24 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1.25 + * THE SOFTWARE.
1.26 + */
1.27 +package org.apidesign.demo.minesweeper;
1.28 +
1.29 +import java.util.ArrayList;
1.30 +import java.util.List;
1.31 +import java.util.Random;
1.32 +import net.java.html.json.ComputedProperty;
1.33 +import net.java.html.json.Function;
1.34 +import net.java.html.json.Model;
1.35 +import net.java.html.json.ModelOperation;
1.36 +import net.java.html.json.Property;
1.37 +
1.38 +/** Model of the mine field.
1.39 + */
1.40 +@Model(className = "Mines", properties = {
1.41 + @Property(name = "state", type = MinesModel.GameState.class),
1.42 + @Property(name = "rows", type = Row.class, array = true),
1.43 +})
1.44 +final class MinesModel {
1.45 + enum GameState {
1.46 + IN_PROGRESS, WON, LOST;
1.47 + }
1.48 +
1.49 + @Model(className = "Row", properties = {
1.50 + @Property(name = "columns", type = Square.class, array = true)
1.51 + })
1.52 + static class RowModel {
1.53 + }
1.54 +
1.55 + @Model(className = "Square", properties = {
1.56 + @Property(name = "state", type = SquareType.class),
1.57 + @Property(name = "mine", type = boolean.class)
1.58 + })
1.59 + static class SquareModel {
1.60 + @ComputedProperty static String text(SquareType state) {
1.61 + if (state == null) return " ";
1.62 + switch (state) {
1.63 + case MINE: return "B";
1.64 + case UNKNOWN: return "?";
1.65 + case N_0: return " ";
1.66 + }
1.67 + return "" + state.ordinal();
1.68 + }
1.69 + }
1.70 +
1.71 + enum SquareType {
1.72 + N_0, N_1, N_2, N_3, N_4, N_5, N_6, N_7, N_8,
1.73 + UNKNOWN, MINE
1.74 + }
1.75 +
1.76 + @ModelOperation static void init(Mines model, int width, int height, int mines) {
1.77 + List<Row> rows = new ArrayList<Row>(height);
1.78 + for (int y = 0; y < height; y++) {
1.79 + Square[] columns = new Square[width];
1.80 + for (int x = 0; x < width; x++) {
1.81 + columns[x] = new Square(SquareType.UNKNOWN, false);
1.82 + }
1.83 + rows.add(new Row(columns));
1.84 + }
1.85 +
1.86 + Random r = new Random();
1.87 + while (mines > 0) {
1.88 + int x = r.nextInt(width);
1.89 + int y = r.nextInt(height);
1.90 + final Square s = rows.get(y).getColumns().get(x);
1.91 + if (s.isMine()) {
1.92 + continue;
1.93 + }
1.94 + s.setMine(true);
1.95 + mines--;
1.96 + }
1.97 +
1.98 + model.setState(GameState.IN_PROGRESS);
1.99 + model.getRows().clear();
1.100 + model.getRows().addAll(rows);
1.101 + }
1.102 +
1.103 + static void showAllBombs(Mines model) {
1.104 + for (Row row : model.getRows()) {
1.105 + for (Square square : row.getColumns()) {
1.106 + if (square.isMine()) {
1.107 + square.setState(SquareType.MINE);
1.108 + }
1.109 + }
1.110 + }
1.111 + }
1.112 +
1.113 + @Function static void click(Mines model, Square data) {
1.114 + if (model.getState() != GameState.IN_PROGRESS) {
1.115 + return;
1.116 + }
1.117 +
1.118 + switch (data.getState()) {
1.119 + case UNKNOWN:
1.120 + if (data.isMine()) {
1.121 + showAllBombs(model);
1.122 + model.setState(GameState.LOST);
1.123 + } else {
1.124 + data.setState(SquareType.N_0);
1.125 + }
1.126 + break;
1.127 + }
1.128 + }
1.129 +}