Skip to content

Instantly share code, notes, and snippets.

@prophile
Created February 10, 2017 00:03
Show Gist options
  • Save prophile/7f0508ef6bdad593f23c6655adf2753c to your computer and use it in GitHub Desktop.
Save prophile/7f0508ef6bdad593f23c6655adf2753c to your computer and use it in GitHub Desktop.
function drive() {
var getSquare = function(i, j) {
return $('#' + i + '_' + j);
}
// detect extents
var W, H;
for (var i = 1; i <= 100; ++i) {
if (getSquare(i, 1).length == 0) {
W = i - 2;
break;
}
}
for (var i = 1; i <= 100; ++i) {
if (getSquare(1, i).length == 0) {
H = i - 2;
break;
}
}
var flagWorklist = [];
var openWorklist = [];
for (var i = 1; i <= W; ++i) {
for (var j = 1; j <= H; ++j) {
var sq = getSquare(i, j);
if (sq.hasClass('blank') || sq.hasClass('open0') || sq.hasClass('bombflagged'))
continue;
var closes = [];
var flags = 0;
for (var xoff = -1; xoff <= 1; ++xoff) {
for (var yoff = -1; yoff <= 1; ++yoff) {
var x = i + xoff;
var y = j + yoff;
if (x <= 0)
continue;
if (y <= 0)
continue;
if (x > W)
continue;
if (y > H)
continue;
var sqC = getSquare(x, y);
if (sqC.hasClass('bombflagged')) {
flags += 1;
}
else if (sqC.hasClass('blank')) {
closes.push(sqC);
}
}
}
if (closes.length == 0)
continue;
//console.log("Exploring frontier node", sq[0].id);
var tag = null;
for (var k = 1; k <= 8; ++k) {
if (sq.hasClass('open' + k)) {
tag = k;
}
}
if (tag === null)
continue;
if (tag == flags) {
// open all surrounding closeds
for (var k = 0; k < closes.length; ++k) {
console.log("Open", closes[k][0].id, "from", i, j);
openWorklist.push(closes[k]);
}
}
else if (tag - flags == closes.length) {
// flag all surrounding closeds
for (var k = 0; k < closes.length; ++k) {
console.log("Flag", closes[k][0].id, "from", i, j);
flagWorklist.push(closes[k]);
}
}
}
}
// dedupe worklists
var dedupeWorklist = function(worklist) {
var seen = {};
return worklist.filter(function(item) {
if (seen.hasOwnProperty(item[0].id)) {
return false;
} else {
seen[item[0].id] = true;
return true;
}
});
}
flagWorklist = dedupeWorklist(flagWorklist);
openWorklist = dedupeWorklist(openWorklist);
for (var i = 0; i < flagWorklist.length; ++i) {
console.log("WL: FLAG ", flagWorklist[i][0].id);
var evt = new jQuery.Event('mousedown');
evt.target = flagWorklist[i][0];
evt.right = true;
evt.ctrlKey = true;
$(document).trigger(evt);
evt = new jQuery.Event('mouseup');
evt.target = flagWorklist[i][0];
evt.right = true;
evt.ctrlKey = true;
$(document).trigger(evt);
}
for (var i = 0; i < openWorklist.length; ++i) {
console.log("WL: OPEN ", openWorklist[i][0].id);
var evt = new jQuery.Event('mousedown');
evt.target = openWorklist[i][0];
evt.button = 0;
$(document).trigger(evt);
evt = new jQuery.Event('mousemove');
evt.target = openWorklist[i][0];
$(document).trigger(evt);
evt = new jQuery.Event('mouseup');
evt.button = 0;
evt.target = openWorklist[i][0];
$(document).trigger(evt);
}
if ($('#face').hasClass('facedead')) {
console.log("exploded :(");
}
else if ($('#face').hasClass('facewin')) {
console.log("Won, yay!");
}
else if (openWorklist.length > 0 || flagWorklist.length > 0) {
setTimeout(drive, 0);
} else {
// Wait 1 sec
setTimeout(drive, 200);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment