diff --git a/README.md b/README.md index 3f7d639..17667ba 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,6 @@ Rows with exploded mines cannot be cleared. See how far you can get! Left-click to clear a tile. Right-click to flag a mine. Middle-click a number to clear any non-flagged adjacent tiles. ## stuff to add -move middle-click functionality to left-click instead - -add support for mobile devices/1-button mice (probably hold down to flag a mine) - save game using html5 local storage so player can come back later make cleared columns slide left? diff --git a/index.html b/index.html index f9707a5..c961122 100644 --- a/index.html +++ b/index.html @@ -217,6 +217,9 @@ var currentlyIterating = false; var mineChance = 0.2; var inputEnabled = true; + var clickholdMs = 200; + var timeout; //hold timer + var mouseHeld = false; $(document).ready(function() { function drawGameBoard() { @@ -309,10 +312,10 @@ }); //refresh last row to reflect new rows beneath - $('li.revealed').last().parent().children('li.revealed').mousedown(); + $('li.revealed').last().parent().children('li.revealed').mouseup(); //click blank tiles - $('li.revealed:not(.mine):empty').mousedown(); + $('li.revealed:not(.mine):empty').mouseup(); //this is a bit of a hack :( oh well $('ul:gt(' + gameBoardHeight + ')').remove(); @@ -468,7 +471,9 @@ $('#setup').hide(); }); - $.fn.leftClick = function() { + $.fn.leftClick = function(automated) { + if (!automated) automated = false; + //don't want first click to be a mine if (firstClick) { var x = $(this).getX(); @@ -495,13 +500,12 @@ //game over, or lose a life, or whatever //... - } else if (parseInt($(this).text()) === $(this).getAdjacentTiles().filter('.flagged').length) { + } else if (!automated && parseInt($(this).text()) === $(this).getAdjacentTiles().filter('.flagged, .revealed.mine').length) { //already clicked; use middle click reveal functionality - //$(this).middleClick(); - + //number of flags matches number of adjacent mines - $(this).getAdjacentTiles().filter(':not(.flagged)').each(function() { - //$(this).mousedown(); + $(this).getAdjacentTiles().filter(':not(.flagged, .revealed)').each(function() { + $(this).mouseup(); }); } else { $(this).addClass("revealed"); @@ -514,7 +518,7 @@ //if no mines adjacent, cascade! if ($(this).countMinesAdjacent() == 0) { - $(this).getAdjacentTiles().filter(':not(.revealed)').mousedown(); + $(this).getAdjacentTiles().filter(':not(.revealed)').mouseup(); } } @@ -522,12 +526,10 @@ } $.fn.middleClick = function() { - //move this functionality to left click - //number of flags matches number of adjacent mines if (parseInt($(this).text()) === $(this).getAdjacentTiles().filter('.flagged, .revealed.mine').length) { $(this).getAdjacentTiles().filter(':not(.flagged)').each(function() { - $(this).mousedown(); + $(this).mouseup(); }); } } @@ -539,17 +541,41 @@ } else { $(this).toggleClass("flagged"); } + + clearTimeout(timeout); } $(document).on("contextmenu", "li", function(event) { event.preventDefault(); }); + $(document).on("mousedown", "li", function(event) { + var x = $(this).getX(); + var y = $(this).getY(); + + timeout = setTimeout(function() { + $('ul:eq(' + y + ') li:eq(' + x + ')').rightClick(); + mouseHeld = true; + }, clickholdMs); + }); + + $(document).on("mouseleave", "li", function(event) { + clearTimeout(timeout); + }); + + $(document).on("mouseup", "li", function(event) { event.preventDefault(); + clearTimeout(timeout); + if (!inputEnabled) return; + if (mouseHeld) { + mouseHeld = false; + return; + } + switch (event.which) { case 3: $(this).rightClick(); @@ -558,9 +584,11 @@ $(this).middleClick(); break; case 1: - default: $(this).leftClick(); break; + default: + $(this).leftClick(true); //automated + break; } removeClearedRows();