Skip to content

Instantly share code, notes, and snippets.

@piersroberts
Last active August 29, 2015 14:23
Show Gist options
  • Save piersroberts/9555c5efc256a53d70f6 to your computer and use it in GitHub Desktop.
Save piersroberts/9555c5efc256a53d70f6 to your computer and use it in GitHub Desktop.
<html>
<head>
<style>
span{
color:black;
font-family:Courier;
text-align:center;
width:2em;
line-height:2em;
float:left;
}
span.end{
display:block;
clear:both;
}
.score-0{color:#ddd;}
.score-1{background-color:#B0FF00;}
.score-2{background-color:#E0FF00;}
.score-3{background-color:#FFF000;}
.score-4{background-color:#FFC000;}
.score-5{background-color:#FF9000;}
.score-6{background-color:#FF6000;}
.score-7{color:white;background-color:#FF3000;}
.score-8{color:white;background-color:#FF0000;}
.score-💣{background-color:#000;color:#fff;}
</style>
<script>
var fill = function (value) {
if (this == null) {
throw new TypeError("this is null or not defined");
}
var O = [];
var len = this.length >>> 0;
var start = arguments[1];
var relativeStart = start >> 0;
var k = relativeStart < 0 ?
Math.max(len + relativeStart, 0) :
Math.min(relativeStart, len);
var end = arguments[2];
var relativeEnd = end === undefined ?
len : end >> 0;
var final = relativeEnd < 0 ?
Math.max(len + relativeEnd, 0) :
Math.min(relativeEnd, len);
while (k < final) {
O[k] = value;
k++;
}
return O;
};
if (!Array.prototype.fill) {
Array.prototype.fill = fill;
}
function minesweeper(width,height,mines){
var render = function (width,height,mines){
console.time('generatingHtml');
var html = '<div style="width:'+(width*2)+'em">';
var className = '';
for(var i = 0; i < height*width; i++){
var score = mines[i] > 8 ? '💣' : mines[i];
classes = [];
classes.push('score-'+score);
classes.push(!((i)% width) ? 'end' : null);
squareHtml = '<span class="'+classes.join(' ')+'">'+score+'</span>'
html += squareHtml;
}
html += '</div>';
document.body.innerHTML = html;
console.timeEnd('generatingHtml');
}
var generateSquares = function(area,mines){
console.time('generateSquares');
do{
var minePos = Math.floor(Math.random()*area);
if (squares[minePos] < 9){
squares[minePos] = mines.pop();
squares[minePos-width-1] += !!((minePos) % width) ? 1 : 0; //nw
squares[minePos-width]++; //n
squares[minePos-width+1] += !!((minePos+1) % width) ? 1 : 0; //ne
squares[minePos-1] += !!((minePos) % width) ? 1 : 0; //w
squares[minePos+1] += !!((minePos+1) % width) ? 1 : 0; //e
squares[minePos+width-1] += !!((minePos) % width) ? 1 : 0; //sw
squares[minePos+width]++ //s
squares[minePos+width+1] += !!((minePos+1) % width) ? 1 : 0; //se
}
} while (mines.length > 0);
console.timeEnd('generateSquares');
return squares;
}
var area = height*width;
var mines = [].fill.call({ length: mines },9);
var squares = [].fill.call({ length: area },0);
if(area > mines.length){
squares = generateSquares(area,mines);
}
render(width,height,squares);
}
</script>
</head>
<body>
<script>
console.time('main');
var squares = minesweeper(10,20,30);
console.timeEnd('main');
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment