I am making a program that solves a puzzle game, and it finds all the possible moves on a board and puts all the possible resulting boards in an object. Then it finds all the po
Recursion.
function traverse(state) {
handle(state.board);
if (state.possibleMoves) {
$.each(state.possibleMoves, function(i, possibleMove) {
traverse(possibleMove);
});
}
}
EDIT: For a breadth-first search, try something like this. It doesn't use recursion, but instead iterates over a growing queue.
function traverse(state) {
var queue = [],
next = state;
while (next) {
if (next.possibleMoves) {
$.each(next.possibleMoves, function(i, possibleMove) {
queue.push(possibleMove);
});
}
next = queue.shift();
}
}