1.1 --- a/json-tck/src/main/java/net/java/html/json/tests/MinesTest.java Wed May 21 12:34:09 2014 +0200
1.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/MinesTest.java Mon May 26 14:29:50 2014 +0200
1.3 @@ -100,6 +100,17 @@
1.4 Utils.exposeHTML(MinesTest.class, "");
1.5 }
1.6
1.7 + @KOTest public void countAround() throws Exception {
1.8 + Mines mines = new Mines();
1.9 + mines.init(5, 5, 0);
1.10 + mines.getRows().get(0).getColumns().get(0).setMine(true);
1.11 + mines.getRows().get(1).getColumns().get(0).setMine(true);
1.12 + mines.getRows().get(0).getColumns().get(1).setMine(true);
1.13 +
1.14 + int cnt = around(mines, 1, 1);
1.15 + assert cnt == 3 : "There are three mines around. Was: " + cnt;
1.16 + }
1.17 +
1.18 private static int countChildren(String id) throws Exception {
1.19 return ((Number)Utils.executeScript(
1.20 MinesTest.class,
1.21 @@ -315,8 +326,9 @@
1.22 }
1.23 final Square sq = columns.get(x);
1.24 if (sq.getState() == SquareType.UNKNOWN) {
1.25 - sq.setState(SquareType.N_0);
1.26 - model.computeMines();
1.27 + int around = around(model, x, y);
1.28 + final SquareType t = SquareType.valueOf("N_" + around);
1.29 + sq.setState(t);
1.30 if (sq.getState() == SquareType.N_0) {
1.31 expandKnown(model, x - 1, y - 1);
1.32 expandKnown(model, x - 1, y);
1.33 @@ -329,4 +341,26 @@
1.34 }
1.35 }
1.36 }
1.37 + private static int around(Mines model, int x, int y) {
1.38 + return minesAt(model, x - 1, y - 1)
1.39 + + minesAt(model, x - 1, y)
1.40 + + minesAt(model, x - 1, y + 1)
1.41 + + minesAt(model, x, y - 1)
1.42 + + minesAt(model, x, y + 1)
1.43 + + minesAt(model, x + 1, y - 1)
1.44 + + minesAt(model, x + 1, y)
1.45 + + minesAt(model, x + 1, y + 1);
1.46 + }
1.47 +
1.48 + private static int minesAt(Mines model, int x, int y) {
1.49 + if (y < 0 || y >= model.getRows().size()) {
1.50 + return 0;
1.51 + }
1.52 + final List<Square> columns = model.getRows().get(y).getColumns();
1.53 + if (x < 0 || x >= columns.size()) {
1.54 + return 0;
1.55 + }
1.56 + Square sq = columns.get(x);
1.57 + return sq.isMine() ? 1 : 0;
1.58 + }
1.59 }
2.1 --- a/ko4j/src/main/java/org/netbeans/html/ko4j/Knockout.java Wed May 21 12:34:09 2014 +0200
2.2 +++ b/ko4j/src/main/java/org/netbeans/html/ko4j/Knockout.java Mon May 26 14:29:50 2014 +0200
2.3 @@ -109,6 +109,7 @@
2.4 + " read: function() {\n"
2.5 + " var r = activeGetter();\n"
2.6 + " activeGetter = realGetter;\n"
2.7 + + " if (r) try { r = r.valueOf(); } catch (err) {}\n"
2.8 + " return r;\n"
2.9 + " },\n"
2.10 + " owner: ret\n"