Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create board #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
<body>
<div id="message"></div>
<div id="notes"></div>
<div id="diffulty">
<button type="button" id="easy">Easy</button>
<button type="button" id="medium">Medium</button>
<button type="button" id="hard">Hard</button>
</div>
<div class="board"></div>
</body>
</html>
38 changes: 29 additions & 9 deletions minesweeper.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
document.addEventListener('DOMContentLoaded', startGame)

// Define your `board` object here!
var board = {
/*var board = {
cells: [{row: 0, col: 0, isMine: false, hidden: true}, {row: 0, col: 1, isMine: true, hidden: true}, {row: 0, col: 2, isMine: false, hidden: true},
{row: 1, col: 0, isMine: true, hidden: true}, {row: 1, col: 1, isMine: false, hidden: true}, {row: 1, col: 2, isMine: false, hidden: true},
{row: 2, col: 0, isMine: false, hidden: true}, {row: 2, col: 1, isMine: false, hidden: true}, {row: 2, col: 2, isMine: true, hidden: true}],
};
};*/

//var board = {};
var board = {cells:[]};

function createBoard (sideSize) {
//How to input sideSize?
board.cells = [];
for (var i = 0; i < sideSize; i++) {
for (var j = 0; j < sideSize; j++) {
Expand All @@ -21,32 +23,50 @@ function createBoard (sideSize) {
});
};
};
return board;
}

function startGame () {
// Don't remove this function call: it makes the game work!
lib.initBoard()

createBoard(3);

//Tentative 1
//document.getElementById("easy").addEventListener("click", function(){createBoard(3)});
//document.getElementById("medium").addEventListener("click", function(){createBoard(5)});
//document.getElementById("hard").addEventListener("click", function(){createBoard(7)});

//Tentative 2
/*var medium = document.getElementById("medium");
medium.addEventListener("click", function(){
console.log(createBoard(5)
});*/


//loop over each cell.
//Call for countSurroundingMines function.
//add a new surroundingMines property to each cell with the value retrieved above.
//for (var i = 0; i < board.cells.length; i++) {
//board["cells"][i].surroundingMines = countSurroundingMines(board["cells"][i]);
//}
board.cells.forEach(cell => cell.surroundingMines = countSurroundingMines(cell));

lib.initBoard()
// Don't remove this function call: it makes the game work!

document.addEventListener("click", checkForWin);
document.addEventListener("contextmenu", checkForWin);

}

// Define this function to look for a win condition:
//
// 1. Are all of the cells that are NOT mines visible?
// 2. Are all of the mines marked?
function checkForWin () {

for (var i = 0; i < board.cells.length; i++) {
if (board["cells"][i].isMine === true && board["cells"][i].isMarked === false) {
for (var i = 0; i < board.cells.length; i++) {
if (board.cells[i].isMine && !board.cells[i].isMarked) {
return;
} else if (board["cells"][i].isMine === false && board["cells"][i].hidden === true) {
} else if (!board.cells[i].isMine && board.cells[i].hidden) {
return;
}
}
Expand Down