endless-mines/index.html

450 lines
10 KiB
HTML

<!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;
overflow: hidden;
font-family: Helsinki;
}
body {
height: 100vh;
margin: 0 auto;
width: 100vmin;
}
#game {
background-color: #1b1c17;
float: left;
margin: 0 auto;
height: 90vmin;
}
#stats {
height: 10vmin;
width: 100vmin;
float: left;
border-radius: 2vmin;
background: #768087;
background: -moz-linear-gradient(top, #768087 0%, #53595e 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#768087), color-stop(100%,#53595e));
background: -webkit-linear-gradient(top, #768087 0%,#53595e 100%);
background: -o-linear-gradient(top, #768087 0%,#53595e 100%);
background: -ms-linear-gradient(top, #768087 0%,#53595e 100%);
background: linear-gradient(to bottom, #768087 0%,#53595e 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#768087', endColorstr='#53595e',GradientType=0 );
}
#stats div {
float: left;
font-size: 5.4vmin;
margin: 2vmin 4vmin;
width: auto;
}
ul {
float: left;
clear: both;
list-style-type: none;
padding: 0;
margin: 0;
}
li {
color: white;
font-size: 8vmin;
padding: 1vmin;
padding-top: 0.5vmin;
padding-bottom: 1.5vmin;
text-align: center;
vertical-align: middle;
float: left;
height: 8vmin;
width: 8vmin;
background-color: #454e52;
border-radius: 2vmin;
cursor: default;
}
li.revealed {
background-color: #1b1c17;
}
li:not(.revealed):hover {
background-color: #a0a9af;
}
li.flagged {
background-color: #b5fe52;
}
li.flagged:hover {
background-color: #c6fe7a;
}
li.mine.revealed {
background-color: #d23000;
}
li.mines1 {
color: #c6fe7a;
}
li.mines2 {
color: #7ac5fe;
}
li.mines3 {
color: #fe7ac6;
}
li.mines4 {
color: #b17afe;
}
li.mines5 {
color: #feb27a;
}
li.mines6 {
color: #7afeb2;
}
li.mines7 {
color: #d74600;
}
li.mines8 {
color: #8c4600;
}
</style>
<script src="jquery-2.1.3.min.js"></script>
<script>
var gameBoardWidth = 10;
var gameBoardHeight = 9;
var score = 0;
var bombs = 1;
var firstClick = true;
var currentlyIterating = false;
var mineChance = 0.2;
var inputEnabled = true;
$(document).ready(function() {
function drawGameBoard() {
$('#game').html("");
for (var i = 0; i < gameBoardHeight; i++) {
$('#game').append("<ul></ul>");
}
$('#game ul').each(function() {
for (var i = 0; i < gameBoardWidth; i++) {
if (Math.random() < mineChance) {
$(this).append('<li class="mine"></li>');
} else {
$(this).append("<li></li>");
}
}
});
}
$.fn.checkRow = function() {
//unclicked tiles
if ($(this).children('li:not(.revealed):not(.flagged)').length > 0) {
return false;
}
//incorrectly flagged tiles
if ($(this).children('li.flagged:not(.mine)').length > 0) {
return false;
}
//clicked mines
if ($(this).children('li.revealed.mine').length > 0) {
return false;
}
return true;
}
window.removeClearedRows = function() {
var rowsToRemove = $('#game ul').filter(function() {
return $(this).checkRow();
});
var numRowsToRemove = rowsToRemove.length;
if (rowsToRemove.length == 0) return;
rowsToRemove.each(function() {
score += $(this).children('.mine').length;
//add new row on bottom
$('#game').append('<ul></ul>');
for (var i = 0; i < gameBoardWidth; i++) {
if (Math.random() < mineChance) {
$('#game ul').last().append('<li class="mine"></li>');
} else {
$('#game ul').last().append('<li></li>');
}
}
});
inputEnabled = false;
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");
});
//refresh last row to reflect new rows beneath
$('li.revealed').last().parent().children('li.revealed').mousedown();
//click blank tiles
$('li.revealed:not(.mine):empty').mousedown();
//this is a bit of a hack :( oh well
$('ul:gt(' + gameBoardHeight + ')').remove();
});
//this is a bit of a hack :( oh well
$('ul:gt(' + gameBoardHeight + ')').remove();
inputEnabled = true;
}
function updateScore() {
$('#score').text("Score: " + score);
}
function updateMinesLeft() {
//unflagged mines - revealed mines - flagged not-mines
$('#mines').text(
"Mines left: "
+ ($('.mine:not(.flagged)').length
- $('.mine.revealed').length
- $('li:not(.mine).flagged').length)
);
}
$.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() {
return $(this).hasClass("mine");
}
$.fn.countMinesAdjacent = function() {
var count = 0;
$.each($(this).getAdjacentTiles(), function() {
if ($(this).isMine()) {
count++;
}
});
return count;
}
$.fn.getAdjacentTiles = function() {
var adjacentTiles = $('');
//row above
if ($(this).getY() > 0) {
if ($(this).getX() > 0) {
adjacentTiles = adjacentTiles.add($(this).rowAbove().children().eq($(this).getX() - 1).toArray());
}
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());
}
}
//this row
if ($(this).getX() > 0) {
adjacentTiles = adjacentTiles.add($(this).prev().toArray());
}
if ($(this).getX() < (gameBoardWidth - 1)) {
adjacentTiles = adjacentTiles.add($(this).next().toArray());
}
//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());
}
}
return adjacentTiles;
}
$.fn.countMinesText = function() {
return $(this).countMinesAdjacent().toString().replace("0", "");
}
$.fn.leftClick = function() {
//don't want first click to be a mine
if (firstClick && $(this).isMine()) {
$(this).removeClass("mine");
}
if ($(this).hasClass("flagged")) {
return;
}
if ($(this).isMine()) {
$(this).addClass("revealed");
//game over, or lose a life, or whatever
//...
} 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();
});
} else {
$(this).addClass("revealed");
$(this).text(
$(this).countMinesText()
);
$(this).addClass("mines" + $(this).countMinesAdjacent());
//if no mines adjacent, cascade!
if ($(this).countMinesAdjacent() == 0) {
$(this).getAdjacentTiles().filter(':not(.revealed)').mousedown();
}
}
firstClick = false;
}
$.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();
});
}
}
$.fn.rightClick = function() {
if ($(this).hasClass("revealed")) {
//deploy a bomb!
//...
} else {
$(this).toggleClass("flagged");
}
}
$(document).on("contextmenu", "li", function(event) {
event.preventDefault();
});
$(document).on("mousedown", "li", function(event) {
event.preventDefault();
if (!inputEnabled) return;
switch (event.which) {
case 3:
$(this).rightClick();
break;
case 2:
$(this).middleClick();
break;
case 1:
default:
$(this).leftClick();
break;
}
removeClearedRows();
updateScore();
updateMinesLeft();
});
drawGameBoard();
});
</script>
</head>
<body>
<div id="game"></div>
<div id="stats">
<div id="score">Score: 0</div>
<div id="bombs" style="display: none;">Bombs: 0</div>
<div id="mines">Mines left: 0</div>
</div>
</body>
</html>