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;
height: 100vh;
width: 100vw;
overflow: hidden;
}
body {
@ -109,46 +110,23 @@
<script>
var gameBoardWidth = 10;
var gameBoardHeight = 10;
var gameBoard = [];
var score = 0;
var firstClick = true;
var currentlyIterating = false;
var mineChance = 0.2;
var inputEnabled = true;
$(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() {
$('#game').html("");
for (var i = 0; i < gameBoard.length; i++) {
for (var i = 0; i < gameBoardHeight; i++) {
$('#game').append("<ul></ul>");
}
$('#game ul').each(function() {
for (var i = 0; i < gameBoard[0].length; i++) {
if (gameBoard[$(this).index()][i].mine) {
for (var i = 0; i < gameBoardWidth; i++) {
if (Math.random() < mineChance) {
$(this).append('<li class="mine"></li>');
} else {
$(this).append("<li></li>");
@ -159,23 +137,84 @@
$.fn.checkRow = function() {
//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;
}
//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;
}
//clicked mines
if ($(this).parent('ul').children('li.revealed.mine').length > 0) {
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() {
//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() {
return $(this).children('.mine').length;
}
@ -201,7 +240,7 @@
}
$.fn.isMine = function() {
return $(this).getGameboardPos().mine
return $(this).hasClass("mine");
}
$.fn.countMinesAdjacent = function() {
@ -227,7 +266,7 @@
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());
}
}
@ -237,19 +276,19 @@
adjacentTiles = adjacentTiles.add($(this).prev().toArray());
}
if ($(this).getX() < (gameBoard[0].length - 1)) {
if ($(this).getX() < (gameBoardWidth - 1)) {
adjacentTiles = adjacentTiles.add($(this).next().toArray());
}
//row below
if ($(this).getY() < (gameBoard.length - 1)) {
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() < (gameBoard[0].length - 1)) {
if ($(this).getX() < (gameBoardWidth - 1)) {
adjacentTiles = adjacentTiles.add($(this).rowBelow().children().eq($(this).getX() + 1).toArray());
}
}
@ -263,8 +302,7 @@
$.fn.leftClick = function() {
//don't want first click to be a mine
if (firstClick && $(this).getGameboardPos().mine) {
$(this).getGameboardPos().mine = false;
if (firstClick && $(this).isMine()) {
$(this).removeClass("mine");
}
@ -272,13 +310,22 @@
return;
}
$(this).getGameboardPos().revealed = true;
if ($(this).isMine()) {
$(this).addClass("revealed");
if ($(this).getGameboardPos().mine) {
//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 {
$(this).addClass("revealed");
$(this).text(
$(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;
}
@ -343,6 +361,8 @@
});
$(document).on("mousedown", "li", function(event) {
if (!inputEnabled) return;
switch (event.which) {
case 3:
$(this).rightClick();
@ -355,9 +375,10 @@
$(this).leftClick();
break;
}
removeClearedRows();
});
initGameBoard();
drawGameBoard();
});
</script>