diff --git a/index.html b/index.html index 125e72c..8b47ea2 100644 --- a/index.html +++ b/index.html @@ -47,6 +47,20 @@ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#768087', endColorstr='#53595e',GradientType=0 ); } + #stats button { + margin: 1vmin; + float: right; + font-family: Helsinki; + font-size: 3vmin; + border-radius: 2vmin; + padding: 1.75vmin; + box-shadow: 0.5vmin 0.5vmin 0 #000; + border: 0.4vmin solid #454e52; + position: relative; + top: auto; + left: auto; + } + #stats div { float: left; font-size: 5.4vmin; @@ -57,19 +71,10 @@ #gameOver, #setup { position: absolute; width: 100vmin; - height: 90vmin; + height: 100vh; background-color: rgba(0,0,0,0.3); } - #setup { - height: 100vmin; - } - - #setup h1 { - margin-top: 6vmin; - margin-bottom: 6vmin; - } - #setup h2 { margin-top: 0; } @@ -121,6 +126,11 @@ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#768087', endColorstr='#53595e',GradientType=0 ); } + button.cancel { + right: 20vmin; + left: auto; + } + h1, h2 { text-align: center; } @@ -128,7 +138,8 @@ h1 { color: #a4f4b4; font-size: 12vmin; - margin: 16vmin 0; + margin-top: 6vmin; + margin-bottom: 6vmin; text-shadow: 1vmin 1vmin 0 #fe7ac6; /* x y blur-radius colour */ } @@ -174,6 +185,7 @@ li.mine.revealed { background-color: #d23000; + color: #000; } li.mines1 { @@ -234,15 +246,16 @@ function drawGameBoard() { $('#game').html(""); - for (var i = 0; i < gameBoardHeight; i++) { - $('#game').append(""); + //determine aspect ratio so as to fit the screen + gameBoardHeight = Math.floor((1 / getAspectRatio()) * 10) - 1; + + if (gameBoardHeight <= 9) { + gameBoardHeight = 9; } - $('#game ul').each(function() { - for (var i = 0; i < gameBoardWidth; i++) { - $(this).append(newTile()); - } - }); + for (var i = 0; i < gameBoardHeight; i++) { + $('#game').append(newRow()); + } } function newTile() { @@ -253,43 +266,43 @@ } } + function newRow() { + var row = ''; + + return row; + } + + $.fn.check = function() { //unclicked tiles - if ($(this).children('li:not(.revealed):not(.flagged)').length > 0) { + if ($(this).filter('li:not(.revealed):not(.flagged)').length > 0) { return false; } //incorrectly flagged tiles - if ($(this).children('li.flagged:not(.mine)').length > 0) { + if ($(this).filter('li.flagged:not(.mine)').length > 0) { return false; } //clicked mines - if ($(this).children('li.revealed.mine').length > 0) { + if ($(this).filter('li.revealed.mine').length > 0) { return false; } return true; } + $.fn.checkRow = function() { + return $(this).children().check(); + } + $.fn.checkColumn = function() { - //unclicked tiles - if ($(this).column().filter(':not(.revealed):not(.flagged)').length > 0) { - return false; - } - - //incorrectly flagged tiles - if ($(this).column().filter('.flagged:not(.mine)').length > 0) { - return false; - } - - //clicked mines - if ($(this).column().filter('.revealed.mine').length > 0) { - return false; - } - - return true; + return $(this).column().check(); } window.removeClearedRows = function() { @@ -297,51 +310,17 @@ return $(this).checkRow(); }); - var numRowsToRemove = rowsToRemove.length; - - if (numRowsToRemove == 0) return; - - rowsToRemove.addClass("removing"); - rowsToRemove.each(function() { score += $(this).children('.mine').length; - }); - rowsToRemove.slideUp("slow", function() { - $(this).remove(); - //add new row on bottom + $(this).addClass("removing"); - $('#game').append(''); - - for (var i = 0; i < gameBoardWidth; i++) { - $('#game ul').last().append(newTile()); - } - - refreshMineCounts(); - - //click blank tiles - $('li.revealed:not(.mine):empty').mouseup(); - }); - } - - window.removeClearedColumns = function() { - var columnsToRemove = $('ul:not(.removing):eq(0) li:not(.removing)').filter(function() { - return $(this).checkColumn(); - }); - - if (columnsToRemove.length == 0) return; - - columnsToRemove.addClass("removing"); - - columnsToRemove.each(function() { - score += $(this).column().filter('.mine').length; - }); - - columnsToRemove.each(function() { - $(this).column().animate({width: 0, borderRadius: 0, padding: 0}, "slow", function() { - $(this).parent().append(newTile()); + $(this).slideUp("slow", function() { $(this).remove(); + //add new row on bottom + $('#game').append(newRow()); + refreshMineCounts(); //click blank tiles @@ -350,27 +329,49 @@ }); } + window.removeClearedColumns = function() { + var columnsToRemove = $('ul:not(.removing):eq(0) li:not(.removing)').filter(function() { + return $(this).checkColumn(); + }); + + columnsToRemove.each(function() { + score += $(this).column().filter('.mine').length; + + $(this).column().addClass("removing"); + + //animation for top row + deletion + // then animation for others? + //... + + $(this).animate({width: 0, borderRadius: 0, padding: 0}, "slow", function() { + $(this).column().remove(); + $('ul').each(function() { + $(this).append(newTile()); + }); + + refreshMineCounts(); + + //click blank tiles + $('li.revealed:not(.mine, .removing):empty').mouseup(); + }); + + $(this).column().animate({width: 0, borderRadius: 0, padding: 0}, "slow"); + }); + } + function refreshMineCounts() { - $('li.revealed:not(.mine)').each(function() { - $(this).text( - $(this).countMinesText() - ); + $('ul:not(.removing) li.revealed:not(.mine, .removing)').each(function() { + var mineCount = $(this).countMinesText(); + + $(this).text(mineCount); //remove "mines1" etc - - var mine = $(this).hasClass("mine"); - - $(this).removeAttr("class"); - - if (mine) { - $(this).addClass("mine"); - } else { - $(this).addClass( - "mines" + $(this).countMinesText() - ); - } - - $(this).addClass("revealed"); + $(this).attr( + 'class', + $(this).attr('class').replace( + /mines[0-9]/, "mines" + mineCount + ) + ); }); } @@ -398,6 +399,20 @@ ); } + function getAspectRatio() { + return $(window).width() / $(window).height(); + } + + function isPortrait() { + return getAspectRatio() > 1; + } + + $.fn.updateMineCount = function() { + $(this).text( + $(this).countMinesText + ); + } + $.fn.rowScore = function() { return $(this).children('.mine').length; } @@ -435,53 +450,31 @@ } $.fn.countMinesAdjacent = function() { - var count = 0; - - $.each($(this).getAdjacentTiles(), function() { - if ($(this).isMine()) { - count++; - } - }); - - return count; + return $(this).getAdjacentTiles().filter('.mine').length; } $.fn.getAdjacentTiles = function() { var adjacentTiles = $(''); + var x = $(this).getX(); + var y = $(this).getY(); - //row above - if ($(this).getY() > 0) { - if ($(this).getX() > 0) { - adjacentTiles = adjacentTiles.add($(this).rowAbove().children().eq($(this).getX() - 1).toArray()); - } + if (y > 0) { + var tileAbove = $(this).rowAbove().children().eq(x); - adjacentTiles = adjacentTiles.add($(this).rowAbove().children().eq($(this).getX()).toArray()); - - if ($(this).getX() < (gameBoardWidth - 1)) { - adjacentTiles = adjacentTiles.add($(this).rowAbove().children().eq($(this).getX() + 1).toArray()); - } + adjacentTiles = adjacentTiles.add(tileAbove.prev()); + adjacentTiles = adjacentTiles.add(tileAbove ); + adjacentTiles = adjacentTiles.add(tileAbove.next()); } - //this row - if ($(this).getX() > 0) { - adjacentTiles = adjacentTiles.add($(this).prev().toArray()); - } + adjacentTiles = adjacentTiles.add($(this).prev()); + adjacentTiles = adjacentTiles.add($(this).next()); - if ($(this).getX() < (gameBoardWidth - 1)) { - adjacentTiles = adjacentTiles.add($(this).next().toArray()); - } + if (y < (gameBoardHeight - 1)) { + var tileBelow = $(this).rowBelow().children().eq(x); - //row below - if ($(this).getY() < (gameBoardHeight - 1)) { - if ($(this).getX() > 0) { - adjacentTiles = adjacentTiles.add($(this).rowBelow().children().eq($(this).getX() - 1).toArray()); - } - - adjacentTiles = adjacentTiles.add($(this).rowBelow().children().eq($(this).getX()).toArray()); - - if ($(this).getX() < (gameBoardWidth - 1)) { - adjacentTiles = adjacentTiles.add($(this).rowBelow().children().eq($(this).getX() + 1).toArray()); - } + adjacentTiles = adjacentTiles.add(tileBelow.prev()); + adjacentTiles = adjacentTiles.add(tileBelow ); + adjacentTiles = adjacentTiles.add(tileBelow.next()); } return adjacentTiles; @@ -506,14 +499,31 @@ }); $('#gameOver button').on("click", function() { - //reset game board drawGameBoard(); $('#gameOver').hide(); + $('button.cancel').hide(); + $('button.start').removeAttr('style'); $('#setup').show(); }); - $('#setup button').on("click", function() { + $('button.reset').on("click", function() { + //prompt user with setup screen + $('#setup').show(); + $('button.cancel').show(); + $('button.start').css('left', '20vmin'); + }); + + $('button.cancel').on("click", function() { + //prompt user with setup screen + $('#setup').hide(); + $('button.cancel').hide(); + $('button.start').removeAttr('style'); + }); + + $('button.start').on("click", function() { + drawGameBoard(); + //reset stats firstClick = true; @@ -552,6 +562,7 @@ if ($(this).isMine()) { $(this).addClass("revealed"); + $(this).html("☠"); //skull & crossbones //game over, or lose a life, or whatever //... @@ -559,15 +570,11 @@ //already clicked; use middle click reveal functionality //number of flags matches number of adjacent mines - $(this).getAdjacentTiles().filter(':not(.flagged, .revealed)').each(function() { - $(this).mouseup(); - }); + $(this).getAdjacentTiles().filter(':not(.flagged, .revealed)').mouseup(); } else { $(this).addClass("revealed"); - $(this).text( - $(this).countMinesText() - ); + $(this).updateMineCount(); $(this).addClass("mines" + $(this).countMinesAdjacent()); @@ -583,9 +590,7 @@ $.fn.middleClick = function() { //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).mouseup(); - }); + $(this).getAdjacentTiles().filter(':not(.flagged, .revealed)').mouseup(); } } @@ -597,7 +602,7 @@ clearTimeout(timeout); } - $(document).on("contextmenu", "li", function(event) { + $(document).on("contextmenu", "body", function(event) { event.preventDefault(); }); @@ -643,7 +648,7 @@ } removeClearedRows(); - //removeClearedColumns(); + removeClearedColumns(); checkGameOver(); updateScore(); @@ -654,17 +659,32 @@ drawGameBoard(); $('#gameOver').hide(); + $('button.cancel').hide(); }); + -
Score: 0
Mines left: 0
+
+

game over

+

a game by max bradbury

+

inspirations include minesweeper, tetris and 2048

+

tell your friends

+

stay in school

@@ -687,7 +707,8 @@
- + + \ No newline at end of file