First version of endless functionality

STILL A BIT BUGGY
This commit is contained in:
synth-ruiner 2015-03-01 10:55:53 +00:00
parent 2eeef1579b
commit 60df86f31c
1 changed files with 98 additions and 77 deletions

View File

@ -14,6 +14,7 @@
margin: 0; margin: 0;
height: 100vh; height: 100vh;
width: 100vw; width: 100vw;
overflow: hidden;
} }
body { body {
@ -107,48 +108,25 @@
</style> </style>
<script src="jquery-2.1.3.min.js"></script> <script src="jquery-2.1.3.min.js"></script>
<script> <script>
var gameBoardWidth = 10; var gameBoardWidth = 10;
var gameBoardHeight = 10; var gameBoardHeight = 10;
var gameBoard = []; var score = 0;
var score = 0; var firstClick = true;
var firstClick = true; var currentlyIterating = false;
var mineChance = 0.2;
var inputEnabled = true;
$(document).ready(function() { $(document).ready(function() {
function newTile() {
var tile = {};
tile.revealed = false;
tile.mine = (Math.random() < 0.2);
return tile;
}
function newRow() {
var newRow = [];
for (var i = 0; i < gameBoardWidth; i++) {
newRow.push(newTile());
};
return newRow;
}
function initGameBoard() {
for (var i = 0; i < gameBoardHeight; i++) {
gameBoard.push(newRow());
}
}
function drawGameBoard() { function drawGameBoard() {
$('#game').html(""); $('#game').html("");
for (var i = 0; i < gameBoard.length; i++) { for (var i = 0; i < gameBoardHeight; i++) {
$('#game').append("<ul></ul>"); $('#game').append("<ul></ul>");
} }
$('#game ul').each(function() { $('#game ul').each(function() {
for (var i = 0; i < gameBoard[0].length; i++) { for (var i = 0; i < gameBoardWidth; i++) {
if (gameBoard[$(this).index()][i].mine) { if (Math.random() < mineChance) {
$(this).append('<li class="mine"></li>'); $(this).append('<li class="mine"></li>');
} else { } else {
$(this).append("<li></li>"); $(this).append("<li></li>");
@ -159,23 +137,84 @@
$.fn.checkRow = function() { $.fn.checkRow = function() {
//unclicked tiles //unclicked tiles
if ($(this).parent('ul').children('li:not(.revealed):not(.flagged)').length > 0) { if ($(this).children('li:not(.revealed):not(.flagged)').length > 0) {
return false; return false;
} }
//incorrectly flagged tiles //incorrectly flagged tiles
if ($(this).parent('ul').children('li.flagged:not(.mine)').length > 0) { if ($(this).children('li.flagged:not(.mine)').length > 0) {
return false; return false;
} }
//clicked mines //clicked mines
if ($(this).parent('ul').children('li.revealed.mine').length > 0) { if ($(this).children('li.revealed.mine').length > 0) {
return false; return false;
} }
return true; 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() {
//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();
});
$('ul:hidden').show();
}
$.fn.rowScore = function() { $.fn.rowScore = function() {
return $(this).children('.mine').length; return $(this).children('.mine').length;
} }
@ -201,7 +240,7 @@
} }
$.fn.isMine = function() { $.fn.isMine = function() {
return $(this).getGameboardPos().mine return $(this).hasClass("mine");
} }
$.fn.countMinesAdjacent = function() { $.fn.countMinesAdjacent = function() {
@ -227,7 +266,7 @@
adjacentTiles = adjacentTiles.add($(this).rowAbove().children().eq($(this).getX()).toArray()); adjacentTiles = adjacentTiles.add($(this).rowAbove().children().eq($(this).getX()).toArray());
if ($(this).getX() < (gameBoard[0].length - 1)) { if ($(this).getX() < (gameBoardWidth - 1)) {
adjacentTiles = adjacentTiles.add($(this).rowAbove().children().eq($(this).getX() + 1).toArray()); adjacentTiles = adjacentTiles.add($(this).rowAbove().children().eq($(this).getX() + 1).toArray());
} }
} }
@ -237,19 +276,19 @@
adjacentTiles = adjacentTiles.add($(this).prev().toArray()); adjacentTiles = adjacentTiles.add($(this).prev().toArray());
} }
if ($(this).getX() < (gameBoard[0].length - 1)) { if ($(this).getX() < (gameBoardWidth - 1)) {
adjacentTiles = adjacentTiles.add($(this).next().toArray()); adjacentTiles = adjacentTiles.add($(this).next().toArray());
} }
//row below //row below
if ($(this).getY() < (gameBoard.length - 1)) { if ($(this).getY() < (gameBoardHeight - 1)) {
if ($(this).getX() > 0) { if ($(this).getX() > 0) {
adjacentTiles = adjacentTiles.add($(this).rowBelow().children().eq($(this).getX() - 1).toArray()); adjacentTiles = adjacentTiles.add($(this).rowBelow().children().eq($(this).getX() - 1).toArray());
} }
adjacentTiles = adjacentTiles.add($(this).rowBelow().children().eq($(this).getX()).toArray()); adjacentTiles = adjacentTiles.add($(this).rowBelow().children().eq($(this).getX()).toArray());
if ($(this).getX() < (gameBoard[0].length - 1)) { if ($(this).getX() < (gameBoardWidth - 1)) {
adjacentTiles = adjacentTiles.add($(this).rowBelow().children().eq($(this).getX() + 1).toArray()); adjacentTiles = adjacentTiles.add($(this).rowBelow().children().eq($(this).getX() + 1).toArray());
} }
} }
@ -263,8 +302,7 @@
$.fn.leftClick = function() { $.fn.leftClick = function() {
//don't want first click to be a mine //don't want first click to be a mine
if (firstClick && $(this).getGameboardPos().mine) { if (firstClick && $(this).isMine()) {
$(this).getGameboardPos().mine = false;
$(this).removeClass("mine"); $(this).removeClass("mine");
} }
@ -272,13 +310,22 @@
return; return;
} }
$(this).getGameboardPos().revealed = true; if ($(this).isMine()) {
$(this).addClass("revealed"); $(this).addClass("revealed");
if ($(this).getGameboardPos().mine) {
//game over, or lose a life, or whatever //game over, or lose a life, or whatever
$(this).addClass("mine"); //...
} 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 { } else {
$(this).addClass("revealed");
$(this).text( $(this).text(
$(this).countMinesText() $(this).countMinesText()
); );
@ -291,35 +338,6 @@
} }
} }
if (false && $(this).checkRow()) {
//hide this row and add a new one at the bottom
var x = $(this).getX();
var y = $(this).getY();
//remove row from game board
//gameBoard.splice(y, 1);
console.debug(gameBoard);
//add new row at bottom
gameBoard.push(newRow());
$(this).parent('ul').slideUp("slow", function() {$(this).remove();});
//redo the numbers on the rows above and below
//add new row on bottom
$('#game').append('<ul></ul>');
for (var i = 0; i < gameBoard[0].length; i++) {
if (gameBoard[gameBoardHeight][i].mine) {
$('#game ul:last-child').append('<li class="mine"></li>');
} else {
$('#game ul:last-child').append('<li></li>');
}
}
}
firstClick = false; firstClick = false;
} }
@ -343,6 +361,8 @@
}); });
$(document).on("mousedown", "li", function(event) { $(document).on("mousedown", "li", function(event) {
if (!inputEnabled) return;
switch (event.which) { switch (event.which) {
case 3: case 3:
$(this).rightClick(); $(this).rightClick();
@ -355,9 +375,10 @@
$(this).leftClick(); $(this).leftClick();
break; break;
} }
removeClearedRows();
}); });
initGameBoard();
drawGameBoard(); drawGameBoard();
}); });
</script> </script>