minesweeper/src/test/java/org/apidesign/demo/minesweeper/MinesModelTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 17 Jul 2014 08:23:25 +0200
changeset 174 a57b2414b855
parent 165 276db4d4d795
permissions -rw-r--r--
One should not be able to win the game by marking all the squares
     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 clickRemovesMarkedSign() {
    49         Mines m = new Mines();
    50         m.init(10, 10, 10);
    51         
    52         final Square sq = m.getRows().get(5).getColumns().get(5);
    53         MinesModel.markMine(m);
    54         MinesModel.click(m, sq);
    55         
    56         assertEquals(sq.getState(), SquareType.MARKED, "Changed to marked");
    57         
    58         MinesModel.click(m, sq);
    59         
    60         assertEquals(sq.getState(), SquareType.UNKNOWN, "Changed back to unknown");
    61     }
    62 
    63     @Test public void gameWonWhenAllMarked() {
    64         Mines m = new Mines();
    65         m.init(10, 10, 10);
    66         
    67         for (Row row : m.getRows()) {
    68             for (Square sq : row.getColumns()) {
    69                 if (sq.isMine()) {
    70                     MinesModel.markMine(m);
    71                     MinesModel.click(m, sq);
    72                 }
    73             }
    74         }
    75         
    76         assertEquals(m.getState(), MinesModel.GameState.WON, "All mines found. You have won!");
    77     }
    78     
    79     @Test public void gameNotWonWhenTooMuchIsMarked() {
    80         Mines m = new Mines();
    81         m.init(10, 10, 10);
    82         
    83         Square additional = null;
    84         for (Row row : m.getRows()) {
    85             for (Square sq : row.getColumns()) {
    86                 if (sq.isMine()) {
    87                     MinesModel.markMine(m);
    88                     MinesModel.click(m, sq);
    89                 } else if (additional == null) {
    90                     MinesModel.markMine(m);
    91                     MinesModel.click(m, additional = sq);
    92                 }
    93             } 
    94         }
    95         
    96         assertEquals(m.getState(), MinesModel.GameState.IN_PROGRESS, "One additional mine is marked!");
    97         
    98         // remove the mark
    99         MinesModel.click(m, additional);
   100         
   101         assertEquals(m.getState(), MinesModel.GameState.WON, "All mines found. You have won!");
   102         
   103         
   104     }
   105     
   106     @Test public void bombsSet() {
   107         Mines m = new Mines();
   108         m.init(10, 10, 0);
   109         
   110         set(m, 1, 1, SquareType.UNKNOWN, true);
   111         set(m, 0, 0, SquareType.N_0, false);
   112         set(m, 0, 1, SquareType.N_2, false);
   113         set(m, 0, 2, SquareType.N_3, false);
   114         set(m, 1, 0, SquareType.N_5, false);
   115         set(m, 2, 0, SquareType.N_8, false);
   116         set(m, 3, 0, SquareType.N_2, false);
   117         
   118         m.computeMines();
   119         
   120         assertSquare(m, 0, 0, SquareType.N_1);
   121         assertSquare(m, 1, 0, SquareType.N_1);
   122         assertSquare(m, 2, 0, SquareType.N_1);
   123         assertSquare(m, 3, 0, SquareType.N_0);
   124         assertSquare(m, 0, 1, SquareType.N_1);
   125         assertSquare(m, 0, 2, SquareType.N_1);
   126         assertSquare(m, 2, 2, SquareType.UNKNOWN);
   127     }
   128     
   129     @Test public void gameIsWonIfAllMinesDiscovered() {
   130         Mines m = new Mines();
   131         m.init(2, 1, 0);
   132         set(m, 0, 0, SquareType.UNKNOWN, true);
   133         m.computeMines();
   134         assertEquals(m.getState(), MinesModel.GameState.IN_PROGRESS);
   135         set(m, 1, 0, SquareType.N_0, false);
   136         m.computeMines();
   137         assertEquals(m.getState(), MinesModel.GameState.WON, "All non-bomb squares discovered");
   138     }
   139 
   140     @Test public void unhideNeibourghsOfEmptyPieces() {
   141         Mines m = new Mines();
   142         m.init(3, 3, 0);
   143         set(m, 0, 0, SquareType.UNKNOWN, true);
   144         MinesModel.click(m, m.getRows().get(2).getColumns().get(2));
   145 
   146         assertSquare(m, 0, 0, SquareType.DISCOVERED);
   147         assertSquare(m, 0, 1, SquareType.N_1);
   148         assertSquare(m, 1, 1, SquareType.N_1);
   149         assertSquare(m, 1, 0, SquareType.N_1);
   150         assertSquare(m, 2, 0, SquareType.N_0);
   151         assertSquare(m, 2, 1, SquareType.N_0);
   152         assertSquare(m, 2, 2, SquareType.N_0);
   153         assertSquare(m, 1, 2, SquareType.N_0);
   154     }
   155     
   156     private static void set(Mines m, int x, int y, SquareType squareType, boolean mine) {
   157         Square sq = m.getRows().get(y).getColumns().get(x);
   158         sq.setState(squareType);
   159         sq.setMine(mine);
   160     }
   161     
   162     private static void assertSquare(Mines m, int x, int y, SquareType t) {
   163         Square sq = m.getRows().get(y).getColumns().get(x);
   164         assertEquals(sq.getState(), t, "Expecting at " + x + ":" + y);
   165     }
   166 }