Skip to content

Commit

Permalink
Added a suspect mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Amyot committed Apr 8, 2015
1 parent 37ee178 commit 190c995
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ public void handlesSafeCell() throws InterruptedException {

minesweeperPage.clickOnCell(1, 2);

minesweeperPage.cellHasClassName(1, 2, "safe");
minesweeperPage.cellContent(1, 2, "2");
minesweeperPage.cellIsSafe(1, 2, "2");
}

@Test
Expand Down Expand Up @@ -88,4 +87,18 @@ public void handlesOpenFieldCell() {
minesweeperPage.cellIsSafe(2, 1, "1");
}

@Test
public void safeModeChallenge() {
minesweeperPage.load(new String[][]{
{"bomb" , "empty", "empty"},
{"empty", "empty", "empty"},
{"empty", "empty", "bomb"}
});
minesweeperPage.activateSuspectMode();

minesweeperPage.clickOnCell(0, 0);

minesweeperPage.cellIsSuspect(0, 0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.openqa.selenium.By.cssSelector;
import static org.openqa.selenium.By.id;

public class MinesweeperPage {
Expand Down Expand Up @@ -67,5 +68,15 @@ public void cellIsSafe(int row, int col, String content) {

public void cellIsTrapped(int row, int col) {
cellHasClassName(row, col, "lost");
cellContent(row, col, "");
}

public void cellIsSuspect(int row, int col) {
cellHasClassName(row, col, "suspect");
cellContent(row, col, "");
}

public void activateSuspectMode() {
element(cssSelector("input#suspect-mode[type=checkbox]")).click();
}
}
69 changes: 39 additions & 30 deletions src/test/js/minesweeper.actions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,56 @@ describe('Actions:', function() {

});

describe('Actions', function() {
it('fails when a cell is trapped', function() {

it('fails when a cell is trapped', function() {
clickOnCell(2, 2);

clickOnCell(2, 2);
expect(document.querySelector('#cell-2x2').className).toEqual('lost');
});

expect(document.querySelector('#cell-2x2').className).toEqual('lost');
});
it('shows bombs around when a cell is safe', function() {

it('shows bombs around when a cell is safe', function() {
clickOnCell(1, 1);

clickOnCell(1, 1);
expect(document.querySelector('#cell-1x1').className).toEqual('safe');
expect(document.querySelector('#cell-1x1').innerHTML).toEqual('1');
});

expect(document.querySelector('#cell-1x1').className).toEqual('safe');
expect(document.querySelector('#cell-1x1').innerHTML).toEqual('1');
});
it('does not display 0 when there is no bomb around', function() {
clickOnCell(4, 4);

it('does not display 0 when there is no bomb around', function() {
clickOnCell(4, 4);
expect(document.querySelector('#cell-4x4').innerHTML).toEqual('');
});

expect(document.querySelector('#cell-4x4').innerHTML).toEqual('');
});
it('reveals open field when a cell has no bomb', function() {
var grid = [
['bomb' , 'empty', 'empty'],
['empty', 'empty', 'empty'],
['empty', 'empty', 'bomb' ]
];

it('reveals open field when a cell has no bomb', function() {
var grid = [
['bomb' , 'empty', 'empty'],
['empty', 'empty', 'empty'],
['empty', 'empty', 'bomb' ]
];
var board = new minesweeper.Board(grid);
board.render(document.querySelector('#minesweeper-grid'));

clickOnCell(3, 1);

var board = new minesweeper.Board(grid);
board.render(document.querySelector('#minesweeper-grid'));
expect(document.querySelector('#cell-2x1').className).toEqual('safe');
expect(document.querySelector('#cell-2x1').innerHTML).toEqual('1');

clickOnCell(3, 1);
expect(document.querySelector('#cell-2x2').className).toEqual('safe');
expect(document.querySelector('#cell-2x2').innerHTML).toEqual('2');

expect(document.querySelector('#cell-2x1').className).toEqual('safe');
expect(document.querySelector('#cell-2x1').innerHTML).toEqual('1');
expect(document.querySelector('#cell-3x2').className).toEqual('safe');
expect(document.querySelector('#cell-3x2').innerHTML).toEqual('1');
});

expect(document.querySelector('#cell-2x2').className).toEqual('safe');
expect(document.querySelector('#cell-2x2').innerHTML).toEqual('2');
it('', function() {
activateSuspectMode();

expect(document.querySelector('#cell-3x2').className).toEqual('safe');
expect(document.querySelector('#cell-3x2').innerHTML).toEqual('1');
});
clickOnCell(1, 1);

expect(document.querySelector('#cell-1x1').className).toEqual('suspect');
expect(document.querySelector('#cell-1x1').innerHTML).toEqual('');
});

var clickOnCell = function(row, col) {
Expand All @@ -75,4 +80,8 @@ describe('Actions:', function() {
document.querySelector('#cell-' + row + 'x' + col).dispatchEvent(click);
};

var activateSuspectMode = function() {
document.querySelector('#suspect-mode').checked = true;
}

});
4 changes: 4 additions & 0 deletions webapp/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ table#minesweeper-grid td.lost {

table#minesweeper-grid td.safe {
background-color: lightgray;
}

table#minesweeper-grid td.suspect {
background-color: yellow;
}
13 changes: 13 additions & 0 deletions webapp/js/minesweeper.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ minesweeper.Cell.prototype = {
},

reveal: function() {
if (this.isSuspectModeActivated()) {
this.suspect();
return;
}

if (this.isTrapped()) {
this.lost();
return;
Expand Down Expand Up @@ -112,6 +117,10 @@ minesweeper.Cell.prototype = {
}
},

suspect: function() {
this.elem.className = 'suspect';
},

isRevealed: function () {
return this.elem.className != '';
},
Expand All @@ -132,6 +141,10 @@ minesweeper.Cell.prototype = {
bombsAround: function () {
return this.neighbours()
.filter(function(cell) { return cell.isTrapped(); }).length;
},

isSuspectModeActivated: function() {
return document.querySelector('#suspect-mode').checked;
}

};
Expand Down
3 changes: 3 additions & 0 deletions webapp/views/minesweeper.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ <h2 id="title">Minesweeper</h2>

<table id="minesweeper-grid">
</table>

<label for="suspect-mode">Suspect Mode</label>
<input type="checkbox" id="suspect-mode" name="suspect-mode" />
</div>

</body>
Expand Down

0 comments on commit 190c995

Please sign in to comment.