Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Improved guessinging and interface
- Loading branch information
Showing
4 changed files
with
151 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,68 @@ | ||
function main () { | ||
var field = new MineSweeper.Field(16, 16, 40); | ||
var solver = new MineSweeper.Solver(field); | ||
solver.onGuess = function () { | ||
console.log("Guess"); | ||
}; | ||
|
||
/* | ||
function loop () { | ||
setTimeout(function () { | ||
solver.step(); | ||
render(); | ||
if (field.left > 0) { loop(); } | ||
}, 200); | ||
var WIDTH = 16, HEIGHT = 16, MINES = 40; | ||
var field, solver, lost; | ||
|
||
function byId (id) { | ||
return document.getElementById(id); | ||
} | ||
*/ | ||
|
||
var fieldEl = document.getElementById('field'); | ||
var fieldEl = byId('field'); | ||
var statusEl = byId('status'); | ||
|
||
function reset () { | ||
field = new MineSweeper.Field(WIDTH, HEIGHT, MINES); | ||
solver = new MineSweeper.Solver(field); | ||
lost = null; | ||
solver.guess = function () { | ||
statusEl.innerHTML += "Guessing … "; | ||
MineSweeper.Solver.prototype.guess.apply(this); | ||
}; | ||
field.blowUp = function (x, y) { | ||
lost = [x, y]; | ||
MineSweeper.Solver.prototype.blowUp.apply(this); | ||
}; | ||
statusEl.innerHTML = "Ready!"; | ||
render(); | ||
} | ||
function render() { | ||
fieldEl.innerHTML = solver.render(); | ||
if (lost) { | ||
var tds = fieldEl.getElementsByTagName('td'); | ||
var td = tds[field.getIndex(lost[0], lost[1])]; | ||
td.className = 'mine'; | ||
} | ||
} | ||
|
||
function step () { | ||
if (!lost && field.left > 0) { | ||
wrap(function () { solver.step(); }); | ||
} | ||
} | ||
|
||
function run () { | ||
if (!lost) { | ||
wrap(function () { solver.solve(); }); | ||
} | ||
} | ||
|
||
function wrap (fn) { | ||
statusEl.innerHTML = ''; | ||
try { fn(); } catch (exc) {} | ||
if (lost) { | ||
statusEl.innerHTML += "BOOOM!"; | ||
} else if (field.left === 0) { | ||
statusEl.innerHTML += "Field cleared!"; | ||
} else { | ||
statusEl.innerHTML += field.left + " cells left."; | ||
} | ||
render(); | ||
} | ||
|
||
var stepBtn = document.getElementById('step-btn'); | ||
stepBtn.onclick = function () { solver.step(); render(); }; | ||
byId('step-btn').onclick = step; | ||
byId('run-btn').onclick = run; | ||
byId('reset-btn').onclick = reset; | ||
|
||
render(); | ||
//loop(); | ||
reset(); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters