2015-02-28 15:40:37 +00:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>endless mines</title>
|
|
|
|
<style type="text/css">
|
|
|
|
@font-face {
|
|
|
|
font-family: Helsinki;
|
|
|
|
font-weight: normal;
|
|
|
|
src: url(helsinki.ttf);
|
|
|
|
}
|
|
|
|
|
|
|
|
html {
|
|
|
|
background-color: #272822;
|
|
|
|
margin: 0;
|
|
|
|
height: 100vh;
|
|
|
|
width: 100vw;
|
2015-03-01 10:55:53 +00:00
|
|
|
overflow: hidden;
|
2015-02-28 15:40:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
body {
|
|
|
|
height: 100vh;
|
|
|
|
margin: 0 auto;
|
|
|
|
width: 100vmin;
|
|
|
|
}
|
|
|
|
|
|
|
|
#game {
|
|
|
|
background-color: #2b2113;
|
|
|
|
float: left;
|
|
|
|
margin: 0 auto;
|
|
|
|
}
|
|
|
|
|
|
|
|
ul {
|
|
|
|
float: left;
|
|
|
|
clear: both;
|
|
|
|
list-style-type: none;
|
|
|
|
padding: 0;
|
|
|
|
margin: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
li {
|
|
|
|
color: white;
|
|
|
|
font-size: 8vmin;
|
|
|
|
font-family: Helsinki;
|
|
|
|
padding: 1vmin;
|
2015-02-28 16:53:40 +00:00
|
|
|
padding-top: 0.5vmin;
|
|
|
|
padding-bottom: 1.5vmin;
|
2015-02-28 15:40:37 +00:00
|
|
|
text-align: center;
|
|
|
|
vertical-align: middle;
|
|
|
|
float: left;
|
|
|
|
height: 8vmin;
|
|
|
|
width: 8vmin;
|
|
|
|
background-color: #4e3c23;
|
|
|
|
border-radius: 2vmin;
|
|
|
|
cursor: default;
|
|
|
|
}
|
|
|
|
|
|
|
|
li.revealed {
|
|
|
|
background-color: #2b2113;
|
|
|
|
}
|
|
|
|
|
|
|
|
li:not(.revealed):hover {
|
|
|
|
background-color: #614a2c;
|
|
|
|
}
|
|
|
|
|
|
|
|
li.flagged {
|
|
|
|
background-color: #b5fe52;
|
|
|
|
}
|
|
|
|
|
|
|
|
li.flagged:hover {
|
|
|
|
background-color: #c6fe7a;
|
|
|
|
}
|
|
|
|
|
|
|
|
li.mine.revealed {
|
|
|
|
background-color: #d23000;
|
|
|
|
}
|
|
|
|
|
2015-02-28 16:53:40 +00:00
|
|
|
li.mines1 {
|
|
|
|
color: #c6fe7a;
|
2015-02-28 15:40:37 +00:00
|
|
|
}
|
|
|
|
|
2015-02-28 16:53:40 +00:00
|
|
|
li.mines2 {
|
|
|
|
color: #7ac5fe;
|
2015-02-28 15:40:37 +00:00
|
|
|
}
|
|
|
|
|
2015-02-28 16:53:40 +00:00
|
|
|
li.mines3 {
|
|
|
|
color: #fe7ac6;
|
2015-02-28 15:40:37 +00:00
|
|
|
}
|
2015-02-28 19:20:27 +00:00
|
|
|
|
|
|
|
li.mines4 {
|
|
|
|
color: #b17afe;
|
|
|
|
}
|
|
|
|
|
|
|
|
li.mines5 {
|
|
|
|
color: #feb27a;
|
|
|
|
}
|
|
|
|
|
|
|
|
li.mines6 {
|
|
|
|
color: #7afeb2;
|
|
|
|
}
|
|
|
|
|
|
|
|
li.mines7 {
|
|
|
|
color: #d74600;
|
|
|
|
}
|
|
|
|
|
|
|
|
li.mines8 {
|
|
|
|
color: #8c4600;
|
|
|
|
}
|
2015-02-28 15:40:37 +00:00
|
|
|
</style>
|
|
|
|
<script src="jquery-2.1.3.min.js"></script>
|
|
|
|
<script>
|
2015-03-01 10:55:53 +00:00
|
|
|
var gameBoardWidth = 10;
|
|
|
|
var gameBoardHeight = 10;
|
|
|
|
var score = 0;
|
|
|
|
var firstClick = true;
|
|
|
|
var currentlyIterating = false;
|
|
|
|
var mineChance = 0.2;
|
|
|
|
var inputEnabled = true;
|
2015-02-28 15:40:37 +00:00
|
|
|
|
2015-02-28 19:20:27 +00:00
|
|
|
$(document).ready(function() {
|
2015-02-28 15:40:37 +00:00
|
|
|
function drawGameBoard() {
|
|
|
|
$('#game').html("");
|
|
|
|
|
2015-03-01 10:55:53 +00:00
|
|
|
for (var i = 0; i < gameBoardHeight; i++) {
|
2015-02-28 15:40:37 +00:00
|
|
|
$('#game').append("<ul></ul>");
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#game ul').each(function() {
|
2015-03-01 10:55:53 +00:00
|
|
|
for (var i = 0; i < gameBoardWidth; i++) {
|
|
|
|
if (Math.random() < mineChance) {
|
2015-02-28 15:40:37 +00:00
|
|
|
$(this).append('<li class="mine"></li>');
|
|
|
|
} else {
|
|
|
|
$(this).append("<li></li>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$.fn.checkRow = function() {
|
2015-02-28 19:20:27 +00:00
|
|
|
//unclicked tiles
|
2015-03-01 10:55:53 +00:00
|
|
|
if ($(this).children('li:not(.revealed):not(.flagged)').length > 0) {
|
2015-02-28 19:20:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-28 15:40:37 +00:00
|
|
|
|
2015-02-28 19:20:27 +00:00
|
|
|
//incorrectly flagged tiles
|
2015-03-01 10:55:53 +00:00
|
|
|
if ($(this).children('li.flagged:not(.mine)').length > 0) {
|
2015-02-28 19:20:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//clicked mines
|
2015-03-01 10:55:53 +00:00
|
|
|
if ($(this).children('li.revealed.mine').length > 0) {
|
2015-02-28 19:20:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-28 15:40:37 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-01 10:55:53 +00:00
|
|
|
window.removeClearedRows = function() {
|
|
|
|
var rowsToRemove = $('#game ul').filter(function() {
|
|
|
|
return $(this).checkRow();
|
|
|
|
});
|
|
|
|
|
|
|
|
var numRowsToRemove = rowsToRemove.length;
|
|
|
|
|
|
|
|
if (rowsToRemove.length == 0) return;
|
|
|
|
|
|
|
|
rowsToRemove.each(function() {
|
|
|
|
//add new row on bottom
|
|
|
|
$('#game').append('<ul style="display: none;"></ul>');
|
|
|
|
|
|
|
|
for (var i = 0; i < gameBoardWidth; i++) {
|
|
|
|
if (Math.random() < mineChance) {
|
|
|
|
$('#game ul:last-child').append('<li class="mine"></li>');
|
|
|
|
} else {
|
|
|
|
$('#game ul:last-child').append('<li></li>');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
inputEnabled = false;
|
|
|
|
|
|
|
|
//rowsToRemove.remove();
|
|
|
|
|
|
|
|
rowsToRemove.slideUp("slow", function() {
|
|
|
|
$(this).remove();
|
|
|
|
|
|
|
|
$('li.revealed:not(.mine)').each(function() {
|
|
|
|
$(this).text(
|
|
|
|
$(this).countMinesText()
|
|
|
|
);
|
|
|
|
|
|
|
|
//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");
|
|
|
|
});
|
|
|
|
|
|
|
|
inputEnabled = true;
|
|
|
|
|
|
|
|
//refresh last row to reflect new rows beneath
|
|
|
|
$('li.revealed').last().parent().children('li.revealed').mousedown();
|
2015-03-01 12:05:38 +00:00
|
|
|
|
|
|
|
//click blank tiles
|
|
|
|
$('li.revealed:not(.mine):empty').mousedown();
|
2015-03-01 10:55:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
$('ul:hidden').show();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-28 15:40:37 +00:00
|
|
|
$.fn.rowScore = function() {
|
|
|
|
return $(this).children('.mine').length;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.fn.rowAbove = function() {
|
|
|
|
return $(this).parent('ul').prev();
|
|
|
|
}
|
|
|
|
|
|
|
|
$.fn.rowBelow = function() {
|
|
|
|
return $(this).parent('ul').next();
|
|
|
|
}
|
|
|
|
|
|
|
|
$.fn.getX = function() {
|
|
|
|
return $(this).index();
|
|
|
|
}
|
|
|
|
|
|
|
|
$.fn.getY = function() {
|
|
|
|
return $(this).parent('ul').index();
|
|
|
|
}
|
|
|
|
|
|
|
|
$.fn.getGameboardPos = function() {
|
|
|
|
return gameBoard[$(this).getY()][$(this).getX()];
|
|
|
|
}
|
|
|
|
|
|
|
|
$.fn.isMine = function() {
|
2015-03-01 10:55:53 +00:00
|
|
|
return $(this).hasClass("mine");
|
2015-02-28 15:40:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$.fn.countMinesAdjacent = function() {
|
|
|
|
var count = 0;
|
|
|
|
|
|
|
|
$.each($(this).getAdjacentTiles(), function() {
|
|
|
|
if ($(this).isMine()) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.fn.getAdjacentTiles = function() {
|
2015-02-28 16:53:40 +00:00
|
|
|
var adjacentTiles = $('');
|
2015-02-28 15:40:37 +00:00
|
|
|
|
|
|
|
//row above
|
|
|
|
if ($(this).getY() > 0) {
|
|
|
|
if ($(this).getX() > 0) {
|
2015-02-28 16:53:40 +00:00
|
|
|
adjacentTiles = adjacentTiles.add($(this).rowAbove().children().eq($(this).getX() - 1).toArray());
|
2015-02-28 15:40:37 +00:00
|
|
|
}
|
|
|
|
|
2015-02-28 16:53:40 +00:00
|
|
|
adjacentTiles = adjacentTiles.add($(this).rowAbove().children().eq($(this).getX()).toArray());
|
2015-02-28 15:40:37 +00:00
|
|
|
|
2015-03-01 10:55:53 +00:00
|
|
|
if ($(this).getX() < (gameBoardWidth - 1)) {
|
2015-02-28 16:53:40 +00:00
|
|
|
adjacentTiles = adjacentTiles.add($(this).rowAbove().children().eq($(this).getX() + 1).toArray());
|
2015-02-28 15:40:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//this row
|
|
|
|
if ($(this).getX() > 0) {
|
2015-02-28 16:53:40 +00:00
|
|
|
adjacentTiles = adjacentTiles.add($(this).prev().toArray());
|
2015-02-28 15:40:37 +00:00
|
|
|
}
|
|
|
|
|
2015-03-01 10:55:53 +00:00
|
|
|
if ($(this).getX() < (gameBoardWidth - 1)) {
|
2015-02-28 16:53:40 +00:00
|
|
|
adjacentTiles = adjacentTiles.add($(this).next().toArray());
|
2015-02-28 15:40:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//row below
|
2015-03-01 10:55:53 +00:00
|
|
|
if ($(this).getY() < (gameBoardHeight - 1)) {
|
2015-02-28 15:40:37 +00:00
|
|
|
if ($(this).getX() > 0) {
|
2015-02-28 16:53:40 +00:00
|
|
|
adjacentTiles = adjacentTiles.add($(this).rowBelow().children().eq($(this).getX() - 1).toArray());
|
2015-02-28 15:40:37 +00:00
|
|
|
}
|
|
|
|
|
2015-02-28 16:53:40 +00:00
|
|
|
adjacentTiles = adjacentTiles.add($(this).rowBelow().children().eq($(this).getX()).toArray());
|
2015-02-28 15:40:37 +00:00
|
|
|
|
2015-03-01 10:55:53 +00:00
|
|
|
if ($(this).getX() < (gameBoardWidth - 1)) {
|
2015-02-28 16:53:40 +00:00
|
|
|
adjacentTiles = adjacentTiles.add($(this).rowBelow().children().eq($(this).getX() + 1).toArray());
|
2015-02-28 15:40:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return adjacentTiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.fn.countMinesText = function() {
|
|
|
|
return $(this).countMinesAdjacent().toString().replace("0", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
$.fn.leftClick = function() {
|
2015-02-28 16:53:40 +00:00
|
|
|
//don't want first click to be a mine
|
2015-03-01 10:55:53 +00:00
|
|
|
if (firstClick && $(this).isMine()) {
|
2015-02-28 19:20:27 +00:00
|
|
|
$(this).removeClass("mine");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($(this).hasClass("flagged")) {
|
|
|
|
return;
|
2015-02-28 16:53:40 +00:00
|
|
|
}
|
|
|
|
|
2015-03-01 10:55:53 +00:00
|
|
|
if ($(this).isMine()) {
|
|
|
|
$(this).addClass("revealed");
|
2015-02-28 15:40:37 +00:00
|
|
|
|
|
|
|
//game over, or lose a life, or whatever
|
2015-03-01 10:55:53 +00:00
|
|
|
//...
|
|
|
|
} else if (parseInt($(this).text()) === $(this).getAdjacentTiles().filter('.flagged').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();
|
|
|
|
});
|
2015-02-28 15:40:37 +00:00
|
|
|
} else {
|
2015-03-01 10:55:53 +00:00
|
|
|
$(this).addClass("revealed");
|
|
|
|
|
2015-02-28 15:40:37 +00:00
|
|
|
$(this).text(
|
|
|
|
$(this).countMinesText()
|
|
|
|
);
|
|
|
|
|
2015-02-28 16:53:40 +00:00
|
|
|
$(this).addClass("mines" + $(this).countMinesAdjacent());
|
2015-02-28 19:20:27 +00:00
|
|
|
|
|
|
|
//if no mines adjacent, cascade!
|
|
|
|
if ($(this).countMinesAdjacent() == 0) {
|
|
|
|
$(this).getAdjacentTiles().filter(':not(.revealed)').mousedown();
|
|
|
|
}
|
2015-02-28 15:40:37 +00:00
|
|
|
}
|
|
|
|
|
2015-02-28 19:20:27 +00:00
|
|
|
firstClick = false;
|
2015-02-28 16:53:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$.fn.middleClick = function() {
|
|
|
|
//number of flags matches number of adjacent mines
|
|
|
|
if ($(this).text() == $(this).getAdjacentTiles().filter('.flagged').length) {
|
|
|
|
$(this).getAdjacentTiles().filter(':not(.flagged)').each(function() {
|
|
|
|
$(this).mousedown();
|
|
|
|
});
|
2015-02-28 15:40:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$.fn.rightClick = function() {
|
2015-02-28 16:53:40 +00:00
|
|
|
if (!$(this).hasClass("revealed")) {
|
|
|
|
$(this).toggleClass("flagged");
|
|
|
|
}
|
2015-02-28 15:40:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$(document).on("contextmenu", "li", function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on("mousedown", "li", function(event) {
|
2015-03-01 10:55:53 +00:00
|
|
|
if (!inputEnabled) return;
|
|
|
|
|
2015-02-28 15:40:37 +00:00
|
|
|
switch (event.which) {
|
2015-02-28 16:53:40 +00:00
|
|
|
case 3:
|
2015-02-28 15:40:37 +00:00
|
|
|
$(this).rightClick();
|
|
|
|
break;
|
2015-02-28 16:53:40 +00:00
|
|
|
case 2:
|
|
|
|
$(this).middleClick();
|
|
|
|
break;
|
|
|
|
case 1:
|
2015-02-28 15:40:37 +00:00
|
|
|
default:
|
|
|
|
$(this).leftClick();
|
|
|
|
break;
|
|
|
|
}
|
2015-03-01 10:55:53 +00:00
|
|
|
|
|
|
|
removeClearedRows();
|
2015-02-28 15:40:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
drawGameBoard();
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="game"></div>
|
|
|
|
</body>
|
|
|
|
</html>
|