Skip to content

Commit

Permalink
updated version, uses now array.filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshy Cyriac committed Jun 3, 2012
1 parent adb0f35 commit 4440b76
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions js/nine/nine.js
Expand Up @@ -58,22 +58,23 @@ function copy(board) {
*/
function solve(board) {
var moves = [];
while (candidates(board).length > 0) {
var c = candidates(board);
var index = getRandomInt(c.length - 1);
var nextMove = c[index];
var candidates = cells(board).filter(candidate);
while (candidates.length > 0) {
var index = getRandomInt(candidates.length - 1);
var nextMoveCell = candidates[index];
// console.log("Found #candidates " + c + " index " + index + ", trying position " + nextMove);
moves.push(nextMove);
fireOn(board, nextMove);
}
moves.push(nextMoveCell.position);
fireOn(board, nextMoveCell.position);
candidates = cells(board).filter(candidate);
}
console.log("Solution found");
console.log("Moves " + moves);
console.log("Candidates " + candidates(board));
printBoard(board);
return moves;
}

function fireOn(board, position) {
if (position === undefined) return;
switch (position) {
case 0:
return toggle(board, [0,1,3]);
Expand Down Expand Up @@ -110,8 +111,9 @@ function getRandomInt(max) {
* @param board
* @return array of positions where the fire is off
*/
function candidates(board) {
function candidatess(board) {
var c = [];
cells(board).forEach(isCandidate);
for (var i=0; i<3; i++) {
for (var j=0; j<3; j++) {
if (board[i][j].status === "off") c.push(board[i][j].position);
Expand All @@ -120,6 +122,10 @@ function candidates(board) {
return c;
}

function candidate(cell) {
return (cell.status === "off");
}

function position2index(position) {
var i = Math.floor(position / 3);
var j = position % 3;
Expand Down Expand Up @@ -159,4 +165,14 @@ function printBoard(board) {
b += '\n';
}
console.log(b);
}
}

function cells(board) {
var cells = [];
for (var i=0; i<3; i++) {
for (var j=0; j<3; j++) {
cells.push(board[i][j]);
}
}
return cells;
}

0 comments on commit 4440b76

Please sign in to comment.