jtulach@63: /** jtulach@63: * The MIT License (MIT) jtulach@63: * jtulach@63: * Copyright (C) 2013 Jaroslav Tulach jtulach@63: * jtulach@63: * Permission is hereby granted, free of charge, to any person obtaining a copy jtulach@63: * of this software and associated documentation files (the "Software"), to deal jtulach@63: * in the Software without restriction, including without limitation the rights jtulach@63: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell jtulach@63: * copies of the Software, and to permit persons to whom the Software is jtulach@63: * furnished to do so, subject to the following conditions: jtulach@63: * jtulach@63: * The above copyright notice and this permission notice shall be included in jtulach@63: * all copies or substantial portions of the Software. jtulach@63: * jtulach@63: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR jtulach@63: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, jtulach@63: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE jtulach@63: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER jtulach@63: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, jtulach@63: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN jtulach@63: * THE SOFTWARE. jtulach@63: */ jtulach@63: package org.apidesign.demo.minesweeper; jtulach@63: jtulach@63: import java.util.ArrayList; jtulach@63: import java.util.List; jtulach@63: import java.util.Random; jtulach@63: import net.java.html.json.ComputedProperty; jtulach@63: import net.java.html.json.Function; jtulach@63: import net.java.html.json.Model; jtulach@63: import net.java.html.json.ModelOperation; jtulach@63: import net.java.html.json.Property; jtulach@63: jtulach@63: /** Model of the mine field. jtulach@63: */ jtulach@63: @Model(className = "Mines", properties = { jtulach@63: @Property(name = "state", type = MinesModel.GameState.class), jtulach@63: @Property(name = "rows", type = Row.class, array = true), jtulach@63: }) jtulach@63: final class MinesModel { jtulach@63: enum GameState { jtulach@63: IN_PROGRESS, WON, LOST; jtulach@63: } jtulach@63: jtulach@63: @Model(className = "Row", properties = { jtulach@63: @Property(name = "columns", type = Square.class, array = true) jtulach@63: }) jtulach@63: static class RowModel { jtulach@63: } jtulach@63: jtulach@63: @Model(className = "Square", properties = { jtulach@63: @Property(name = "state", type = SquareType.class), jtulach@63: @Property(name = "mine", type = boolean.class) jtulach@63: }) jtulach@63: static class SquareModel { jtulach@63: @ComputedProperty static String text(SquareType state) { jtulach@63: if (state == null) return " "; jtulach@63: switch (state) { jtulach@68: case EXPLOSION: return "B"; jtulach@63: case UNKNOWN: return "?"; jtulach@63: case N_0: return " "; jtulach@63: } jtulach@63: return "" + state.ordinal(); jtulach@63: } jtulach@65: jtulach@65: @ComputedProperty static String style(SquareType state) { jtulach@65: return state == null ? null : state.toString(); jtulach@65: } jtulach@63: } jtulach@63: jtulach@63: enum SquareType { jtulach@63: N_0, N_1, N_2, N_3, N_4, N_5, N_6, N_7, N_8, jtulach@68: UNKNOWN, EXPLOSION, DISCOVERED; jtulach@64: jtulach@64: final boolean isVisible() { jtulach@64: return name().startsWith("N_"); jtulach@64: } jtulach@64: jtulach@69: final SquareType moreBombsAround() { jtulach@68: switch (this) { jtulach@68: case EXPLOSION: jtulach@68: case UNKNOWN: jtulach@68: case DISCOVERED: jtulach@68: case N_8: jtulach@68: return this; jtulach@64: } jtulach@64: return values()[ordinal() + 1]; jtulach@64: } jtulach@63: } jtulach@63: jtulach@70: @Function static void smallGame(Mines model) { jtulach@70: model.init(5, 5, 5); jtulach@70: } jtulach@70: @Function static void normalGame(Mines model) { jtulach@70: model.init(10, 10, 10); jtulach@70: } jtulach@70: jtulach@63: @ModelOperation static void init(Mines model, int width, int height, int mines) { jtulach@63: List rows = new ArrayList(height); jtulach@63: for (int y = 0; y < height; y++) { jtulach@63: Square[] columns = new Square[width]; jtulach@63: for (int x = 0; x < width; x++) { jtulach@63: columns[x] = new Square(SquareType.UNKNOWN, false); jtulach@63: } jtulach@63: rows.add(new Row(columns)); jtulach@63: } jtulach@63: jtulach@63: Random r = new Random(); jtulach@63: while (mines > 0) { jtulach@63: int x = r.nextInt(width); jtulach@63: int y = r.nextInt(height); jtulach@63: final Square s = rows.get(y).getColumns().get(x); jtulach@63: if (s.isMine()) { jtulach@63: continue; jtulach@63: } jtulach@63: s.setMine(true); jtulach@63: mines--; jtulach@63: } jtulach@63: jtulach@63: model.setState(GameState.IN_PROGRESS); jtulach@63: model.getRows().clear(); jtulach@63: model.getRows().addAll(rows); jtulach@63: } jtulach@63: jtulach@64: @ModelOperation static void computeMines(Mines model) { jtulach@64: List xBombs = new ArrayList(); jtulach@64: List yBombs = new ArrayList(); jtulach@64: final List rows = model.getRows(); jtulach@66: boolean emptyHidden = false; jtulach@69: SquareType[][] arr = new SquareType[rows.size()][]; jtulach@64: for (int y = 0; y < rows.size(); y++) { jtulach@64: final List columns = rows.get(y).getColumns(); jtulach@69: arr[y] = new SquareType[columns.size()]; jtulach@64: for (int x = 0; x < columns.size(); x++) { jtulach@64: Square sq = columns.get(x); jtulach@64: if (sq.isMine()) { jtulach@64: xBombs.add(x); jtulach@64: yBombs.add(y); jtulach@64: } jtulach@64: if (sq.getState().isVisible()) { jtulach@69: arr[y][x] = SquareType.N_0; jtulach@66: } else { jtulach@66: if (!sq.isMine()) { jtulach@66: emptyHidden = true; jtulach@66: } jtulach@64: } jtulach@64: } jtulach@64: } jtulach@64: for (int i = 0; i < xBombs.size(); i++) { jtulach@64: int x = xBombs.get(i); jtulach@64: int y = yBombs.get(i); jtulach@64: jtulach@69: incrementAround(arr, x, y); jtulach@69: } jtulach@69: for (int y = 0; y < rows.size(); y++) { jtulach@69: final List columns = rows.get(y).getColumns(); jtulach@69: for (int x = 0; x < columns.size(); x++) { jtulach@69: Square sq = columns.get(x); jtulach@69: final SquareType newState = arr[y][x]; jtulach@69: if (newState != null && newState != sq.getState()) { jtulach@69: sq.setState(newState); jtulach@69: } jtulach@69: } jtulach@64: } jtulach@66: jtulach@66: if (!emptyHidden) { jtulach@66: model.setState(GameState.WON); jtulach@68: showAllBombs(model, SquareType.DISCOVERED); jtulach@66: } jtulach@64: } jtulach@64: jtulach@69: private static void incrementAround(SquareType[][] arr, int x, int y) { jtulach@69: incrementAt(arr, x - 1, y - 1); jtulach@69: incrementAt(arr, x - 1, y); jtulach@69: incrementAt(arr, x - 1, y + 1); jtulach@64: jtulach@69: incrementAt(arr, x + 1, y - 1); jtulach@69: incrementAt(arr, x + 1, y); jtulach@69: incrementAt(arr, x + 1, y + 1); jtulach@64: jtulach@69: incrementAt(arr, x, y - 1); jtulach@69: incrementAt(arr, x, y + 1); jtulach@64: } jtulach@64: jtulach@69: private static void incrementAt(SquareType[][] arr, int x, int y) { jtulach@69: if (y >= 0 && y < arr.length) { jtulach@69: SquareType[] r = arr[y]; jtulach@69: if (x >= 0 && x < r.length) { jtulach@69: SquareType sq = r[x]; jtulach@69: if (sq != null) { jtulach@69: r[x] = sq.moreBombsAround(); jtulach@69: } jtulach@64: } jtulach@64: } jtulach@64: } jtulach@64: jtulach@68: static void showAllBombs(Mines model, SquareType state) { jtulach@63: for (Row row : model.getRows()) { jtulach@63: for (Square square : row.getColumns()) { jtulach@63: if (square.isMine()) { jtulach@68: square.setState(state); jtulach@63: } jtulach@63: } jtulach@63: } jtulach@63: } jtulach@63: jtulach@63: @Function static void click(Mines model, Square data) { jtulach@63: if (model.getState() != GameState.IN_PROGRESS) { jtulach@63: return; jtulach@63: } jtulach@63: jtulach@63: switch (data.getState()) { jtulach@63: case UNKNOWN: jtulach@63: if (data.isMine()) { jtulach@68: showAllBombs(model, SquareType.EXPLOSION); jtulach@63: model.setState(GameState.LOST); jtulach@63: } else { jtulach@68: expandKnown(model, data); jtulach@63: } jtulach@63: break; jtulach@63: } jtulach@63: } jtulach@68: private static void expandKnown(Mines model, Square data) { jtulach@68: final List rows = model.getRows(); jtulach@68: for (int y = 0; y < rows.size(); y++) { jtulach@68: final List columns = rows.get(y).getColumns(); jtulach@68: for (int x = 0; x < columns.size(); x++) { jtulach@68: Square sq = columns.get(x); jtulach@68: if (sq == data) { jtulach@68: expandKnown(model, x, y); jtulach@68: return; jtulach@68: } jtulach@68: } jtulach@68: } jtulach@68: } jtulach@68: private static void expandKnown(Mines model, int x , int y) { jtulach@68: if (y < 0 || y >= model.getRows().size()) { jtulach@68: return; jtulach@68: } jtulach@68: final List columns = model.getRows().get(y).getColumns(); jtulach@68: if (x < 0 || x >= columns.size()) { jtulach@68: return; jtulach@68: } jtulach@68: final Square sq = columns.get(x); jtulach@68: if (sq.getState() == SquareType.UNKNOWN) { jtulach@68: sq.setState(SquareType.N_0); jtulach@68: model.computeMines(); jtulach@68: if (sq.getState() == SquareType.N_0) { jtulach@68: expandKnown(model, x - 1, y - 1); jtulach@68: expandKnown(model, x - 1, y); jtulach@68: expandKnown(model, x - 1, y + 1); jtulach@68: expandKnown(model, x , y - 1); jtulach@68: expandKnown(model, x, y + 1); jtulach@68: expandKnown(model, x + 1, y - 1); jtulach@68: expandKnown(model, x + 1, y); jtulach@68: expandKnown(model, x + 1, y + 1); jtulach@68: } jtulach@68: } jtulach@68: } jtulach@63: }