minesweeper/src/test/java/org/apidesign/demo/minesweeper/MinesModelTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 07 Feb 2014 16:35:45 +0100
branchminesweeper
changeset 66 9cf895cde4bd
parent 64 3a82f9e6eddd
child 67 2e910e06ed34
permissions -rw-r--r--
Game is won when only hidden places contain bombs
     1 /**
     2  * The MIT License (MIT)
     3  *
     4  * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     5  *
     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:
    12  *
    13  * The above copyright notice and this permission notice shall be included in
    14  * all copies or substantial portions of the Software.
    15  *
    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
    22  * THE SOFTWARE.
    23  */
    24 package org.apidesign.demo.minesweeper;
    25 
    26 import org.apidesign.demo.minesweeper.MinesModel.SquareType;
    27 import static org.testng.Assert.*;
    28 import org.testng.annotations.Test;
    29 
    30 public class MinesModelTest {
    31     @Test public void tenTenTen() {
    32         Mines m = new Mines();
    33         m.init(10, 10, 10);
    34         
    35         assertEquals(m.getRows().size(), 10, "Ten rows");
    36         int cnt = 0;
    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()) {
    41                     cnt++;
    42                 }
    43             }
    44         }
    45         assertEquals(cnt, 10, "Ten mines");
    46     }
    47     
    48     @Test public void bombsSet() {
    49         Mines m = new Mines();
    50         m.init(10, 10, 0);
    51         
    52         set(m, 1, 1, SquareType.UNKNOWN, true);
    53         set(m, 0, 0, SquareType.N_0, false);
    54         set(m, 0, 1, SquareType.N_2, false);
    55         set(m, 0, 2, SquareType.N_3, false);
    56         set(m, 1, 0, SquareType.N_5, false);
    57         set(m, 2, 0, SquareType.N_8, false);
    58         set(m, 3, 0, SquareType.N_2, false);
    59         
    60         m.computeMines();
    61         
    62         assertSquare(m, 0, 0, SquareType.N_1);
    63         assertSquare(m, 1, 0, SquareType.N_1);
    64         assertSquare(m, 2, 0, SquareType.N_1);
    65         assertSquare(m, 3, 0, SquareType.N_0);
    66         assertSquare(m, 0, 1, SquareType.N_1);
    67         assertSquare(m, 0, 2, SquareType.N_1);
    68         assertSquare(m, 2, 2, SquareType.UNKNOWN);
    69     }
    70     
    71     public void gameIsWonIfAllMinesDiscovered() {
    72         Mines m = new Mines();
    73         m.init(2, 1, 0);
    74         set(m, 0, 0, SquareType.UNKNOWN, true);
    75         m.computeMines();
    76         assertEquals(m.getState(), MinesModel.GameState.IN_PROGRESS);
    77         set(m, 1, 0, SquareType.N_0, false);
    78         m.computeMines();
    79         assertEquals(m.getState(), MinesModel.GameState.WON, "All non-bomb squares discovered");
    80     }
    81 
    82     private static void set(Mines m, int x, int y, SquareType squareType, boolean mine) {
    83         Square sq = m.getRows().get(y).getColumns().get(x);
    84         sq.setState(squareType);
    85         sq.setMine(mine);
    86     }
    87     
    88     private static void assertSquare(Mines m, int x, int y, SquareType t) {
    89         Square sq = m.getRows().get(y).getColumns().get(x);
    90         assertEquals(sq.getState(), t, "Expecting at " + x + ":" + y);
    91     }
    92 }