Author Topic: JQuery help - Coding Minesweeper  (Read 1011 times)

Code: [Select]
<DOCTYPE HTML>
<html>
<head>
<style>
input[type="number"] {
width: 50px;
padding: 1px;
background-color: grey;
color: #fff;
font-weight: bold;
font-size: 16px;
font-family: monospace;
}
#Demineur {
width: 252px;
height: 252px;
background: #999;
padding: 0px;
border: 2px solid #000;
}
input[type="button"] {
letter-spacing: normal;
word-spacing: normal;
line-height: normal;
text-transform: none;
text-indent: 0px;
text-shadow: none;
}
input[type='button'].square {
margin: 0px;
padding: 0px;
width: 21px;
height: 21px;
font-weight: bold;
font-size: 14px;
background-color: #BFBFBF;
display: inline-block;
float: left;
border-top: 3px solid white;
border-left: 3px solid white;
border-right: 3px solid #707070;
border-bottom: 3px solid #707070;
}
input[type='button'].square:active, input[type='button'].square.active {
background-color: #BFBFBF;
border-top: 3px solid #BFBFBF;
border-left: 3px solid #BFBFBF;
border-right: 3px solid #BFBFBF;
border-bottom: 3px solid #BFBFBF;
}
#scoreboard {
padding: 10px;
width: auto;
height: 26px;
background-color: #BFBFBF;
text-align: left;
border-top: 3px solid #707070;
border-left: 3px solid #707070;
border-right: 3px solid white;
border-bottom: 3px solid white;
}
#scoreboard .panel {
display: inline-block;
padding: 1px;
background-color: black;
color: #DF0D14;
font-weight: bold;
font-size: 16px;
font-family: monospace;
border-top: 1px solid #707070;
border-left: 1px solid #707070;
border-right: 1px solid #F0F0F0;
border-bottom: 1px solid #F0F0F0;
}
</style>
<script>
var nbFlags;
var toDiscover;
var arrayMines = new Array();
var timer = false;
var count = 0;

setInterval(function () {
if (timer) {
$('#score-time-count').html(("00" + count).slice(-3));
count++;
}
}, 1000);

function init() {
$(document).bind("contextmenu", function (e) { - This is the line that is giving me problems
return false;
});
$('#Mines').html('');
columns = $("[name='columns']").val();
lines = $("[name='lines']").val();
nbMines = $("[name='nbMines']").val();
nbFlags = nbMines;
toDiscover = columns * lines - nbMines;
//count = 0;
$('#Demineur').width(columns * 21);
$('#Demineur').height(lines * 21 + 52);
$('#score-bomb-count').html(("00" + nbFlags).slice(-3));

//init table
for (var i = 0; i < lines; i++) {
arrayMines[i] = new Array();
for (var j = 0; j < columns; j++) {
arrayMines[i][j] = 0;
$("#Mines").append("<input type='button' class='square' id=" + i + "_" + j + " value='' onclick='clickMine(" + i + "," + j + ")' oncontextmenu='switchFlag(" + i + "," + j + ")'/>");
}
$("#Mines").append('<br>');
}

//random put mines
var i = 0;
while (i < nbMines) {
var x = Math.floor(Math.random() * lines);
var y = Math.floor(Math.random() * columns);
if (arrayMines[x][y] === 0) {
arrayMines[x][y] = 1;
i++;
}
}
timer = true;
count = 0;
}

function clickMine(i, j) {
//alert ("Clicked: "+i+";"+j);
//remove flag
if (arrayMines[i][j] > 1) {
switchFlag(i, j);
} else if (arrayMines[i][j] == 1) {
$("#" + i + "_" + j).addClass("active");
timer = false;
showBombs();
alert("BOOM !");
//init();
} else {
$("#" + i + "_" + j).addClass("active");
$("#" + i + "_" + j).attr('onclick', '');
toDiscover--;
var number = countMines(i, j);
if (number !== 0) $("#" + i + "_" + j).prop('value', number);
else for (var x = Math.max(0, i - 1); x <= Math.min(lines - 1, i + 1); x++)
for (var y = Math.max(0, j - 1); y <= Math.min(columns - 1, j + 1); y++)
if (arrayMines[x][y] < 2 && !$("#" + x + "_" + y).hasClass('active')) clickMine(x, y);

// Test victory
checkVictory();
}
}

function countMines(i, j) {
var k = 0;
for (var x = Math.max(0, i - 1); x <= Math.min(lines - 1, i + 1); x++)
for (var y = Math.max(0, j - 1); y <= Math.min(columns - 1, j + 1); y++)
if (arrayMines[x][y] == 1 || arrayMines[x][y] == 3) k++;
return k;
}

function switchFlag(i, j) {
if (!$("#" + i + "_" + j).hasClass('active')) {
if (arrayMines[i][j] < 2) {
if (nbFlags > 0) {
arrayMines[i][j] += 2;
$("#" + i + "_" + j).prop('value', "F");
$("#" + i + "_" + j).css("color", "#FF0000");
nbFlags--;
}
} else {
arrayMines[i][j] -= 2;
$("#" + i + "_" + j).prop('value', "");
$("#" + i + "_" + j).css("color", "");
nbFlags++;
}
}
$('#score-bomb-count').html(("00" + nbFlags).slice(-3));
}

function showBombs() {
for (var i = 0; i < lines; i++)
for (var j = 0; j < columns; j++) {
if (arrayMines[i][j] == 1) {
$("#" + i + "_" + j).prop('value', "*");
$("#" + i + "_" + j).css("font-size", "20px");
$("#" + i + "_" + j).css("background-color", "#FF0000");
}
$("#" + i + "_" + j).attr('onclick', 'init()');
}
}

function checkVictory() {
//alert (toDiscover);
if (toDiscover === 0) {
timer = false;
for (var i = 0; i < lines; i++)
for (var j = 0; j < columns; j++) {
if (arrayMines[i][j] == 1) {
$("#" + i + "_" + j).prop('value', "F");
$("#" + i + "_" + j).css("color", "#FF0000");
}
$("#" + i + "_" + j).attr('onclick', 'init()');
}
alert("WELL DONE!");
toDiscover = -1;
}
}
</script>
</head>
<body>
<div id="Options">Number of mines:
<input type="number" name="nbMines" value="10">Columns:
<input type="number" name="columns" value="12">&nbsp;Lines:
<input type="number" name="lines" value="12">
</div>
<div id="Demineur">
<div id="scoreboard">Mines:
<div id="score-bomb-count" class="panel left">010</div>&nbsp;&nbsp;Time:
<div id="score-time-count" class="panel right">000</div>
</div>
<div id='Mines'></div>
</div>
<br>
<button onclick="init()">Restart</button>
</body>

</html>
This is the part that is giving me problems:
Code: [Select]
function init() {
$(document).bind("contextmenu", function (e) {
return false;
});
Please help!

isn't there a coding help subforum
wait that's torque only, my bad

This isn't for Torquescript though, this is non Blockland related.

uh first of all use separate files for html, styles, and scripts
and secondly, I'm sure it would be helpful if you told us what the issue was exactly and how it's actually supposed to behave

Try copy and pasting it into Notepad++ and you'll see the problem.

Here's how the program looks when it's started:

Here's the error when I press the 'Restart' button

uhh
jquery isn't part of javascript by default...

you gotta add this above your script
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
or you can use whatever URL you want as long as it leads to a copy of the jquery library. but I suggest that one because it's on Google's CDN which means first of all it's fast, and secondly that it'll be in a lot of people's caches already

Tried that. Gave me the same error in the console.

it's working perfectly fine for me



the only thing I changed was adding jquery to the HTML (and commenting "This is the line that is giving me problems" but that wasn't the source of the real problem)

here's an exact copy of the code I'm using

<DOCTYPE HTML>
<html>
   <head>
      <style>
      input[type="number"] {
      width: 50px;
      padding: 1px;
      background-color: grey;
      color: #fff;
      font-weight: bold;
      font-size: 16px;
      font-family: monospace;
   }
   #Demineur {
      width: 252px;
      height: 252px;
      background: #999;
      padding: 0px;
      border: 2px solid #000;
   }
   input[type="button"] {
      letter-spacing: normal;
      word-spacing: normal;
      line-height: normal;
      text-transform: none;
      text-indent: 0px;
      text-shadow: none;
   }
   input[type='button'].square {
      margin: 0px;
      padding: 0px;
      width: 21px;
      height: 21px;
      font-weight: bold;
      font-size: 14px;
      background-color: #BFBFBF;
      display: inline-block;
      float: left;
      border-top: 3px solid white;
      border-left: 3px solid white;
      border-right: 3px solid #707070;
      border-bottom: 3px solid #707070;
   }
   input[type='button'].square:active, input[type='button'].square.active {
      background-color: #BFBFBF;
      border-top: 3px solid #BFBFBF;
      border-left: 3px solid #BFBFBF;
      border-right: 3px solid #BFBFBF;
      border-bottom: 3px solid #BFBFBF;
   }
   #scoreboard {
      padding: 10px;
      width: auto;
      height: 26px;
      background-color: #BFBFBF;
      text-align: left;
      border-top: 3px solid #707070;
      border-left: 3px solid #707070;
      border-right: 3px solid white;
      border-bottom: 3px solid white;
   }
   #scoreboard .panel {
      display: inline-block;
      padding: 1px;
      background-color: black;
      color: #DF0D14;
      font-weight: bold;
      font-size: 16px;
      font-family: monospace;
      border-top: 1px solid #707070;
      border-left: 1px solid #707070;
      border-right: 1px solid #F0F0F0;
      border-bottom: 1px solid #F0F0F0;
   }
      </style>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
      <script>
         var nbFlags;
         var toDiscover;
         var arrayMines = new Array();
         var timer = false;
         var count = 0;

         setInterval(function () {
            if (timer) {
               $('#score-time-count').html(("00" + count).slice(-3));
               count++;
            }
         }, 1000);

         function init() {
            $(document).bind("contextmenu", function (e) { //This is the line that is giving me problems
               return false;
            });
            $('#Mines').html('');
            columns = $("[name='columns']").val();
            lines = $("[name='lines']").val();
            nbMines = $("[name='nbMines']").val();
            nbFlags = nbMines;
            toDiscover = columns * lines - nbMines;
            //count = 0;
            $('#Demineur').width(columns * 21);
            $('#Demineur').height(lines * 21 + 52);
            $('#score-bomb-count').html(("00" + nbFlags).slice(-3));

            //init table
            for (var i = 0; i < lines; i++) {
               arrayMines = new Array();
               for (var j = 0; j < columns; j++) {
                  arrayMines[j] = 0;
                  $("#Mines").append("<input type='button' class='square' id=" + i + "_" + j + " value='' onclick='clickMine(" + i + "," + j + ")' oncontextmenu='switchFlag(" + i + "," + j + ")'/>");
               }
               $("#Mines").append('<br>');
            }

            //random put mines
            var i = 0;
            while (i < nbMines) {
               var x = Math.floor(Math.random() * lines);
               var y = Math.floor(Math.random() * columns);
               if (arrayMines
  • [y] === 0) {

                  arrayMines
  • [y] = 1;

                  i++;
               }
            }
            timer = true;
            count = 0;
         }

         function clickMine(i, j) {
            //alert ("Clicked: "+i+";"+j);
            //remove flag
            if (arrayMines[j] > 1) {
               switchFlag(i, j);
            } else if (arrayMines[j] == 1) {
               $("#" + i + "_" + j).addClass("active");
               timer = false;
               showBombs();
               alert("BOOM !");
               //init();
            } else {
               $("#" + i + "_" + j).addClass("active");
               $("#" + i + "_" + j).attr('onclick', '');
               toDiscover--;
               var number = countMines(i, j);
               if (number !== 0) $("#" + i + "_" + j).prop('value', number);
               else for (var x = Math.max(0, i - 1); x <= Math.min(lines - 1, i + 1); x++)
               for (var y = Math.max(0, j - 1); y <= Math.min(columns - 1, j + 1); y++)
               if (arrayMines
  • [y] < 2 && !$("#" + x + "_" + y).hasClass('active')) clickMine(x, y);


               // Test victory
               checkVictory();
            }
         }

         function countMines(i, j) {
            var k = 0;
            for (var x = Math.max(0, i - 1); x <= Math.min(lines - 1, i + 1); x++)
            for (var y = Math.max(0, j - 1); y <= Math.min(columns - 1, j + 1); y++)
            if (arrayMines
  • [y] == 1 || arrayMines
  • [y] == 3) k++;

            return k;
         }

         function switchFlag(i, j) {
            if (!$("#" + i + "_" + j).hasClass('active')) {
               if (arrayMines[j] < 2) {
                  if (nbFlags > 0) {
                     arrayMines[j] += 2;
                     $("#" + i + "_" + j).prop('value', "F");
                     $("#" + i + "_" + j).css("color", "#FF0000");
                     nbFlags--;
                  }
               } else {
                  arrayMines[j] -= 2;
                  $("#" + i + "_" + j).prop('value', "");
                  $("#" + i + "_" + j).css("color", "");
                  nbFlags++;
               }
            }
            $('#score-bomb-count').html(("00" + nbFlags).slice(-3));
         }

         function showBombs() {
            for (var i = 0; i < lines; i++)
            for (var j = 0; j < columns; j++) {
               if (arrayMines[j] == 1) {
                  $("#" + i + "_" + j).prop('value', "*");
                  $("#" + i + "_" + j).css("font-size", "20px");
                  $("#" + i + "_" + j).css("background-color", "#FF0000");
               }
               $("#" + i + "_" + j).attr('onclick', 'init()');
            }
         }

         function checkVictory() {
            //alert (toDiscover);
            if (toDiscover === 0) {
               timer = false;
               for (var i = 0; i < lines; i++)
               for (var j = 0; j < columns; j++) {
                  if (arrayMines[j] == 1) {
                     $("#" + i + "_" + j).prop('value', "F");
                     $("#" + i + "_" + j).css("color", "#FF0000");
                  }
                  $("#" + i + "_" + j).attr('onclick', 'init()');
               }
               alert("WELL DONE!");
               toDiscover = -1;
            }
         }
      </script>
   </head>
<body>
   <div id="Options">Number of mines:
      <input type="number" name="nbMines" value="10">Columns:
      <input type="number" name="columns" value="12">&nbsp;Lines:
      <input type="number" name="lines" value="12">
   </div>
   <div id="Demineur">
      <div id="scoreboard">Mines:
         <div id="score-bomb-count" class="panel left">010</div>&nbsp;&nbsp;Time:
         <div id="score-time-count" class="panel right">000</div>
      </div>
      <div id='Mines'></div>
   </div>
   <br>
   <button onclick="init()">Restart</button>
</body>

</html>

Weird. Can you post the code so I can compare/see what I did wrong/copy?

Nvm thanks

Just add jQuery to your header. It's not built in to Javascript,you need to add it.

It works better than it did before, but this is happening now:


I made no changes to your code, aside from some indentation stuff
Code: [Select]
<DOCTYPE HTML>
<html>
<head>
  <style>
  input[type="number"] {
  width: 50px;
  padding: 1px;
  background-color: grey;
  color: #fff;
  font-weight: bold;
  font-size: 16px;
  font-family: monospace;
   }
   #Demineur {
  width: 252px;
  height: 252px;
  background: #999;
  padding: 0px;
  border: 2px solid #000;
   }
   input[type="button"] {
  letter-spacing: normal;
  word-spacing: normal;
  line-height: normal;
  text-transform: none;
  text-indent: 0px;
  text-shadow: none;
   }
   input[type='button'].square {
  margin: 0px;
  padding: 0px;
  width: 21px;
  height: 21px;
  font-weight: bold;
  font-size: 14px;
  background-color: #BFBFBF;
  display: inline-block;
  float: left;
  border-top: 3px solid white;
  border-left: 3px solid white;
  border-right: 3px solid #707070;
  border-bottom: 3px solid #707070;
   }
   input[type='button'].square:active, input[type='button'].square.active {
  background-color: #BFBFBF;
  border-top: 3px solid #BFBFBF;
  border-left: 3px solid #BFBFBF;
  border-right: 3px solid #BFBFBF;
  border-bottom: 3px solid #BFBFBF;
   }
   #scoreboard {
  padding: 10px;
  width: auto;
  height: 26px;
  background-color: #BFBFBF;
  text-align: left;
  border-top: 3px solid #707070;
  border-left: 3px solid #707070;
  border-right: 3px solid white;
  border-bottom: 3px solid white;
   }
   #scoreboard .panel {
  display: inline-block;
  padding: 1px;
  background-color: black;
  color: #DF0D14;
  font-weight: bold;
  font-size: 16px;
  font-family: monospace;
  border-top: 1px solid #707070;
  border-left: 1px solid #707070;
  border-right: 1px solid #F0F0F0;
  border-bottom: 1px solid #F0F0F0;
   }
  </style>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  <script>
var nbFlags;
var toDiscover;
var arrayMines = new Array();
var timer = false;
var count = 0;

setInterval(function () {
if (timer) {
   $('#score-time-count').html(("00" + count).slice(-3));
   count++;
}
}, 1000);

function init() {
$(document).bind("contextmenu", function (e) { //This is the line that is giving me problems
   return false;
});
$('#Mines').html('');
columns = $("[name='columns']").val();
lines = $("[name='lines']").val();
nbMines = $("[name='nbMines']").val();
nbFlags = nbMines;
toDiscover = columns * lines - nbMines;
//count = 0;
$('#Demineur').width(columns * 21);
$('#Demineur').height(lines * 21 + 52);
$('#score-bomb-count').html(("00" + nbFlags).slice(-3));

//init table
for (var i = 0; i < lines; i++) {
   arrayMines = new Array();
   for (var j = 0; j < columns; j++) {
  arrayMines[j] = 0;
  $("#Mines").append("<input type='button' class='square' id=" + i + "_" + j + " value='' onclick='clickMine(" + i + "," + j + ")' oncontextmenu='switchFlag(" + i + "," + j + ")'/>");
   }
   $("#Mines").append('<br>');
}

//random put mines
var i = 0;
while (i < nbMines) {
   var x = Math.floor(Math.random() * lines);
   var y = Math.floor(Math.random() * columns);
   if (arrayMines
[y] === 0) {
  arrayMines
[y] = 1;
  i++;
   }
}
timer = true;
count = 0;
}

function clickMine(i, j) {
//alert ("Clicked: "+i+";"+j);
//remove flag
if (arrayMines[j] > 1) {
   switchFlag(i, j);
} else if (arrayMines[j] == 1) {
   $("#" + i + "_" + j).addClass("active");
   timer = false;
   showBombs();
   alert("BOOM !");
   //init();
} else {
   $("#" + i + "_" + j).addClass("active");
   $("#" + i + "_" + j).attr('onclick', '');
   toDiscover--;
   var number = countMines(i, j);
   if (number !== 0) $("#" + i + "_" + j).prop('value', number);
   else for (var x = Math.max(0, i - 1); x <= Math.min(lines - 1, i + 1); x++)
   for (var y = Math.max(0, j - 1); y <= Math.min(columns - 1, j + 1); y++)
   if (arrayMines
[y] < 2 && !$("#" + x + "_" + y).hasClass('active')) clickMine(x, y);

   // Test victory
   checkVictory();
}
}

function countMines(i, j) {
var k = 0;
for (var x = Math.max(0, i - 1); x <= Math.min(lines - 1, i + 1); x++)
for (var y = Math.max(0, j - 1); y <= Math.min(columns - 1, j + 1); y++)
if (arrayMines
[y] == 1 || arrayMines
[y] == 3) k++;
return k;
}

function switchFlag(i, j) {
if (!$("#" + i + "_" + j).hasClass('active')) {
   if (arrayMines[j] < 2) {
  if (nbFlags > 0) {
arrayMines[j] += 2;
$("#" + i + "_" + j).prop('value', "F");
$("#" + i + "_" + j).css("color", "#FF0000");
nbFlags--;
  }
   } else {
  arrayMines[j] -= 2;
  $("#" + i + "_" + j).prop('value', "");
  $("#" + i + "_" + j).css("color", "");
  nbFlags++;
   }
}
$('#score-bomb-count').html(("00" + nbFlags).slice(-3));
}

function showBombs() {
for (var i = 0; i < lines; i++)
for (var j = 0; j < columns; j++) {
   if (arrayMines[j] == 1) {
  $("#" + i + "_" + j).prop('value', "*");
  $("#" + i + "_" + j).css("font-size", "20px");
  $("#" + i + "_" + j).css("background-color", "#FF0000");
   }
   $("#" + i + "_" + j).attr('onclick', 'init()');
}
}

function checkVictory() {
//alert (toDiscover);
if (toDiscover === 0) {
   timer = false;
   for (var i = 0; i < lines; i++)
   for (var j = 0; j < columns; j++) {
  if (arrayMines[j] == 1) {
$("#" + i + "_" + j).prop('value', "F");
$("#" + i + "_" + j).css("color", "#FF0000");
  }
  $("#" + i + "_" + j).attr('onclick', 'init()');
   }
   alert("WELL DONE!");
   toDiscover = -1;
}
}
  </script>
   </head>
<body>
   <div id="Options">Number of mines:
  <input type="number" name="nbMines" value="10">Columns:
  <input type="number" name="columns" value="12">&nbsp;Lines:
  <input type="number" name="lines" value="12">
   </div>
   <div id="Demineur">
  <div id="scoreboard">Mines:
<div id="score-bomb-count" class="panel left">010</div>&nbsp;&nbsp;Time:
<div id="score-time-count" class="panel right">000</div>
  </div>
  <div id='Mines'></div>
   </div>
   <br>
   <button onclick="init()">Restart</button>
</body>
</html>

whatever you did made it so that the number of mines setting actually just determines how many entire columns are filled with mines


one difference was here


another was here


etc
there were some others of that same kind of issue
so just go fix all of those

Works perfectly now. Thanks!