// Sweeper.js 120525 const numofrows = 10; const numofcols = 10; const mines = 10; const game=document.getElementById("maingame"); newquestiontext="To get a New Question click the

Get a Question

button"; gameover=0; clicks=0; // to catch 1st time loser let board = []; // preload images and sounds imgbgname=['mine.png','newcup.png','explosion.jpg','map1.jpg','slashsink.jpg','tick.gif','goldbar.jpg']; imgbgarray=[]; //declare empty array for (i=0;i= 0 && ni = 0 &&nj Map Access Denied

You Must Correctly Answer
a Question before You can
Search for Mines!

"+newquestiontext); //document.getElementById('questionbutton').style.visibility='visible'; document.getElementById('questionbutton').style.display='block'; } else {clicks=clicks+1; // keep track of number of clicks to catch 1st click revealCell(row, col)} } function revealCell(row, col) { // out of range or already clicked, ignore if (row < 0 ||row >= numofrows ||col < 0 ||col >= numofcols || board[row][col].revealed) {return;} board[row][col].revealed = true; // show current cell if (board[row][col].isMine) // stepped on a mine, game over {lost(); return;} else if (board[row][col].count === 0) { // no mines around cell, show adjacent cells for (let deltax = -1;deltax <= 1;deltax++) { for (let deltay = -1;deltay <= 1;deltay++) {revealCell(row + deltax,col + deltay);} } } displaycells(); won(); // check for win ? } function displaycells(){ // rebuild all cells each time game.innerHTML = ""; for (let i = 0; i < numofrows; i++) { for (let j = 0;j < numofcols;j++) { const cell =document.createElement("div"); cell.className = "mapcell"; if (board[i][j].revealed) // if class property is true { cell.classList.add("revealed"); if (board[i][j].isMine) { cell.classList.add("mine"); cell.innerHTML =""; } else if (board[i][j].count >0) {cell.textContent = board[i][j].count; } } cell.addEventListener("click",() => allowclick(i, j)); game.appendChild(cell); } game.appendChild(document.createElement("br")); } // stop clicks on map till question answered correctly mapcellclick=0; // reset to needing a question answered //document.getElementById('questionbutton').style.visibility='visible'; document.getElementById('questionbutton').style.display='block'; } function won(){ totalcells=0; // if all good cells only revealed game won for (let i = 0; i < numofrows; i++) {for (let j = 0;j < numofcols;j++) { if(board[i][j].revealed) // count non mine cells {totalcells=totalcells+1; goodcells=numofrows*numofcols-mines; if(totalcells==goodcells) {soundplayer('win'); revealall(); // show all cells alertboxer("

Game Over!

Congratulations


You Located all the Mines and
Won the Game."); gameover=1; } } } } } function lost(){ soundplayer('boom'); mapcellclick=0; revealall(); // show all cells and contents // hide question button document.getElementById('questionbutton').style.display='none'; // show special message for 1st time loser if (clicks==1){losetext=' Your 1st Attempt was Unlucky
'} else {losetext=''} alertboxer("

Game Over!


"+losetext+"You Hit a Mine!"); gameover=1; } function revealall(){ // once game over, show all cells revealed proprty to true then display for (let i = 0; i < numofrows; i++) {for (let j = 0;j < numofcols;j++) {board[i][j].revealed = true;} } displaycells() // document.getElementById('questionbutton').style.visibility='hidden'; document.getElementById('questionbutton').style.display='none'; document.getElementById('replay').style.display='block'; } function displayinstructions(){ if(document.getElementById('instructions').style.display=='block') {//document.getElementById('instructions').style.display='none'; $("#instructions").slideUp("normal");} else{//document.getElementById('instructions').style.display='block'; $("#instructions").slideDown("normal");} } function correct(choice){ if (qloaded==1) {qloaded=0 // only allow question to be answered once only if (choice==answer) //answer correct {mapcellclick=1; //allow map to be clicked alertboxer("

Correct


Click a Cell on the Map

") $("#qdiv").slideUp("normal"); // hide question } else { alertboxer("

Incorrect

"+answer+"
was the Correct Answer

Try again.
"+newquestiontext); // let player try a new question document.getElementById('questionbutton').style.display='block'; } } else {alertboxer("

Information

Question Already answered.
"+newquestiontext+"

")} } function soundplayer(x){ // use new html audio tag to play mp3s document.getElementById("sndplayer").src=x+".mp3"; document.getElementById("sndplayer").play(); } function replay(){ // 060525 gameover=0; aboxtitle='Minesweeper Quiz'; alertsetup(); splashscreen=""; alertboxer(splashscreen); if(typeof title!='string'){title=''} // 220411 - use game title if available document.getElementById('title').innerHTML=gametitle+" Minesweeper Quiz"; initializegame(); displaycells(); displayinstructions(); mapcellclick=0; // allow click on map when question answered clicks=0; // to catch 1st time loser }