endless-mines/index.html

295 lines
5.9 KiB
HTML
Raw Normal View History

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;
}
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;
text-align: center;
vertical-align: middle;
float: left;
height: 8vmin;
width: 8vmin;
background-color: #4e3c23;
border-radius: 2vmin;
cursor: default;
line-height: 10vmin;
}
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;
}
li.1mines {
color: green;
}
li.2mines {
color: blue;
}
li.3mines {
color: red;
}
</style>
<script src="jquery-2.1.3.min.js"></script>
<script>
var gameBoard = [];
var score = 0;
$( document ).ready(function() {
function newTile() {
var tile = {};
tile.revealed = false;
tile.mine = (Math.random() < 0.2);
return tile;
}
function initGameBoard() {
for (var i = 0; i < 10; i++) {
var row = [];
for (var j = 0; j < 10; j++) {
row.push(newTile());
}
gameBoard.push(row);
}
}
function drawGameBoard() {
$('#game').html("");
for (var i = 0; i < gameBoard.length; i++) {
$('#game').append("<ul></ul>");
}
$('#game ul').each(function() {
for (var i = 0; i < gameBoard[0].length; i++) {
if (gameBoard[$(this).index()][i].mine) {
$(this).append('<li class="mine"></li>');
} else {
$(this).append("<li></li>");
}
}
});
}
$.fn.checkRow = function() {
$(this).children('li').each(function() {
if ($(this).hasClass("mine")) {
return false;
}
//unclicked
if (!$(this).hasClass("flagged") && !$(this).hasClass("mine") && !$(this).hasClass("revealed")) {
return false;
}
});
return true;
}
$.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).getGameboardPos().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.push($(this).rowAbove().children().eq($(this).getX() - 1).toArray());
}
adjacentTiles.push($(this).rowAbove().children().eq($(this).getX()).toArray());
if ($(this).getX() < (gameBoard[0].length - 1)) {
adjacentTiles.push($(this).rowAbove().children().eq($(this).getX() + 1).toArray());
}
}
//this row
if ($(this).getX() > 0) {
adjacentTiles.push($(this).prev().toArray());
}
if ($(this).getX() < (gameBoard[0].length - 1)) {
adjacentTiles.push($(this).next().toArray());
}
//row below
if ($(this).getY() < (gameBoard.length - 1)) {
if ($(this).getX() > 0) {
adjacentTiles.push($(this).rowBelow().children().eq($(this).getX() - 1).toArray());
}
adjacentTiles.push($(this).rowBelow().children().eq($(this).getX()).toArray());
if ($(this).getX() < (gameBoard[0].length - 1)) {
adjacentTiles.push($(this).rowBelow().children().eq($(this).getX() + 1).toArray());
}
}
return adjacentTiles;
}
$.fn.clickAdjacentTiles = function() {
$(this).getAdjacentTiles().each(function() {
$(this).addClass("cascade");
});
$('li.cascade').click();
$('li.cascade').removeClass("cascade");
}
$.fn.countMinesText = function() {
return $(this).countMinesAdjacent().toString().replace("0", "");
}
$.fn.leftClick = function() {
if ($(this).hasClass("flagged")) {
return;
}
$(this).getGameboardPos().revealed = true;
$(this).addClass("revealed");
if ($(this).getGameboardPos().mine) {
//game over, or lose a life, or whatever
$(this).addClass("mine");
} else {
$(this).text(
$(this).countMinesText()
);
$(this).addClass($(this).countMinesAdjacent() + "mines");
}
//if no mines adjacent, cascade!
if ($(this).countMinesAdjacent() == 0) {
$(this).clickAdjacentTiles();
}
}
$.fn.rightClick = function() {
$(this).addClass("flagged");
}
$(document).on("contextmenu", "li", function(event) {
$(this).rightClick();
event.preventDefault();
});
$(document).on("mousedown", "li", function(event) {
switch (event.which) {
case 3: //right click
$(this).rightClick();
break;
case 1: //left click
default:
$(this).leftClick();
break;
}
});
initGameBoard();
drawGameBoard();
});
</script>
</head>
<body>
<div id="game"></div>
</body>
</html>