Permalink
Browse files

Pretty much done...

except for a good algorithm to uncover multiple cells at once; this one
makes it too easy.

Almost one hour.
  • Loading branch information...
mishoo committed Dec 18, 2015
1 parent 4e620a7 commit d8854d5c5640e5c4a20b8881e066fc8cdeda0822
Showing with 98 additions and 10 deletions.
  1. +10 −1 minesweeper.css
  2. +72 −8 minesweeper.js
  3. +16 −1 minesweeper.scss
View
@@ -19,10 +19,19 @@
.minesweeper-board .row .cell:last-child {
border-right: 1px solid #888; }
.minesweeper-board .row .cell.uncovered {
background: #fff; }
background: transparent; }
.minesweeper-board .row .cell.bomb {
color: #f00; }
.minesweeper-board .row .cell.bomb:after {
content: "\01F4A3"; }
.minesweeper-board .row .cell.flagged {
color: #00f; }
.minesweeper-board .row .cell.flagged:after {
content: "\2691"; }
.minesweeper-board .row:last-child .cell {
border-bottom: 1px solid #888; }
.minesweeper-board.finished {
background-color: #ffc;
border-color: red; }
/*# sourceMappingURL=minesweeper.css.map */
View
@@ -1,8 +1,13 @@
function Minesweeper(nrows, ncols) {
var boardLength = nrows * ncols;
var board = new Array(boardLength);
var nbombs = 0;
for (var i = boardLength; --i >= 0;) {
board[i] = (Math.random() <= 0.1 ? 1 : 0);
var setBomb = Math.random() <= 0.1;
if (setBomb) {
nbombs++;
}
board[i] = (setBomb ? 1 : 0);
}
function index(row, col) {
@@ -25,10 +30,39 @@ function Minesweeper(nrows, ncols) {
return getField(row, col) & 2;
}
function uncover(row, col) {
function isFlagged(row, col) {
return getField(row, col) & 4;
}
function isSeen(row, col) {
return getField(row, col) & 8;
}
function setUncover(row, col) {
setField(row, col, getField(row, col) | 2);
}
function setFlag(row, col) {
setField(row, col, getField(row, col) | 4);
}
function unsetFlag(row, col) {
setField(row, col, (getField(row, col) | 4) ^ 4);
}
function setSeen(row, col) {
setField(row, col, getField(row, col) | 8);
}
function finished() {
var f = 0, u = 0;
forEach(function(row, col){
if (isFlagged(row, col)) f++;
if (isUncovered(row, col)) u++;
});
return f + u == nrows * ncols;
}
function forEach(f) {
for (var i = 0; i < nrows; ++i) {
for (var j = 0; j < ncols; ++j) {
@@ -66,12 +100,12 @@ function Minesweeper(nrows, ncols) {
if (isBomb(i, j)) {
out += " bomb";
}
} else if (isFlagged(i, j)) {
out += " flagged";
}
out += "' data-row='" + i + "' data-col='" + j + "'>";
if (isUncovered(i, j)) {
if (isBomb(i, j)) {
out += "&#x1F4A3;";
} else {
if (!isBomb(i, j)) {
var count = nearBombs(i, j);
if (count > 0) {
out += count;
@@ -97,19 +131,49 @@ function Minesweeper(nrows, ncols) {
if (isUncovered(row, col)) {
return;
}
uncover(row, col);
setUncover(row, col);
if (isBomb(row, col)) {
forEach(function(row, col){
uncover(row, col);
setUncover(row, col);
});
} else {
var count = nearBombs(row, col);
if (count == 0) {
uncoverArea(row, col);
}
}
refresh(boardEl);
}
function refresh(boardEl) {
boardEl.innerHTML = renderInner();
if (finished()) {
boardEl.classList.add("finished");
}
}
function rightClick(row, col) {
function uncoverArea(row, col) {
if (isSeen(row, col)) {
return;
}
setSeen(row, col);
setUncover(row, col);
if (row > 0 && !isBomb(row - 1, col)) uncoverArea(row - 1, col);
if (row < nrows - 1 && !isBomb(row + 1, col)) uncoverArea(row + 1, col);
if (col > 0 && !isBomb(row, col - 1)) uncoverArea(row, col - 1);
if (col < ncols - 1 && !isBomb(row, col + 1)) uncoverArea(row, col + 1);
}
function rightClick(row, col, boardEl) {
if (isUncovered(row, col)) {
return;
}
if (isFlagged(row, col)) {
unsetFlag(row, col);
} else {
setFlag(row, col);
}
refresh(boardEl);
}
document.addEventListener("mousedown", onMouseDown);
View
@@ -24,11 +24,21 @@
}
&.uncovered {
background: #fff;
background: transparent;
}
&.bomb {
color: #f00;
&:after {
content: "\01F4A3";
}
}
&.flagged {
color: #00f;
&:after {
content: "\2691";
}
}
}
&:last-child {
@@ -37,4 +47,9 @@
}
}
}
&.finished {
background-color: #ffc;
border-color: red;
}
}

0 comments on commit d8854d5

Please sign in to comment.