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 org.apidesign.demo.minesweeper.MinesModel.SquareType;
27 import static org.testng.Assert.*;
28 import org.testng.annotations.Test;
30 public class MinesModelTest {
31 @Test public void tenTenTen() {
32 Mines m = new Mines();
35 assertEquals(m.getRows().size(), 10, "Ten rows");
37 for (Row row : m.getRows()) {
38 assertEquals(row.getColumns().size(), 10, "Ten columns in each row");
39 for (Square square : row.getColumns()) {
40 if (square.isMine()) {
45 assertEquals(cnt, 10, "Ten mines");
48 @Test public void clickRemovesMarkedSign() {
49 Mines m = new Mines();
52 final Square sq = m.getRows().get(5).getColumns().get(5);
53 MinesModel.markMine(m);
54 MinesModel.click(m, sq);
56 assertEquals(sq.getState(), SquareType.MARKED, "Changed to marked");
58 MinesModel.click(m, sq);
60 assertEquals(sq.getState(), SquareType.UNKNOWN, "Changed back to unknown");
63 @Test public void bombsSet() {
64 Mines m = new Mines();
67 set(m, 1, 1, SquareType.UNKNOWN, true);
68 set(m, 0, 0, SquareType.N_0, false);
69 set(m, 0, 1, SquareType.N_2, false);
70 set(m, 0, 2, SquareType.N_3, false);
71 set(m, 1, 0, SquareType.N_5, false);
72 set(m, 2, 0, SquareType.N_8, false);
73 set(m, 3, 0, SquareType.N_2, false);
77 assertSquare(m, 0, 0, SquareType.N_1);
78 assertSquare(m, 1, 0, SquareType.N_1);
79 assertSquare(m, 2, 0, SquareType.N_1);
80 assertSquare(m, 3, 0, SquareType.N_0);
81 assertSquare(m, 0, 1, SquareType.N_1);
82 assertSquare(m, 0, 2, SquareType.N_1);
83 assertSquare(m, 2, 2, SquareType.UNKNOWN);
86 @Test public void gameIsWonIfAllMinesDiscovered() {
87 Mines m = new Mines();
89 set(m, 0, 0, SquareType.UNKNOWN, true);
91 assertEquals(m.getState(), MinesModel.GameState.IN_PROGRESS);
92 set(m, 1, 0, SquareType.N_0, false);
94 assertEquals(m.getState(), MinesModel.GameState.WON, "All non-bomb squares discovered");
97 @Test public void unhideNeibourghsOfEmptyPieces() {
98 Mines m = new Mines();
100 set(m, 0, 0, SquareType.UNKNOWN, true);
101 MinesModel.click(m, m.getRows().get(2).getColumns().get(2));
103 assertSquare(m, 0, 0, SquareType.DISCOVERED);
104 assertSquare(m, 0, 1, SquareType.N_1);
105 assertSquare(m, 1, 1, SquareType.N_1);
106 assertSquare(m, 1, 0, SquareType.N_1);
107 assertSquare(m, 2, 0, SquareType.N_0);
108 assertSquare(m, 2, 1, SquareType.N_0);
109 assertSquare(m, 2, 2, SquareType.N_0);
110 assertSquare(m, 1, 2, SquareType.N_0);
113 private static void set(Mines m, int x, int y, SquareType squareType, boolean mine) {
114 Square sq = m.getRows().get(y).getColumns().get(x);
115 sq.setState(squareType);
119 private static void assertSquare(Mines m, int x, int y, SquareType t) {
120 Square sq = m.getRows().get(y).getColumns().get(x);
121 assertEquals(sq.getState(), t, "Expecting at " + x + ":" + y);