Skip to content

Instantly share code, notes, and snippets.

@cbhl
Created April 8, 2014 21:41
Show Gist options
  • Save cbhl/10197095 to your computer and use it in GitHub Desktop.
Save cbhl/10197095 to your computer and use it in GitHub Desktop.
bad12-2.js
// move randomly
// var moves = map.getAdjacentEmptyCells(me.getX(), me.getY());
// getAdjacentEmptyCells gives array of ((x, y), direction) pairs
// me.move(moves[map.getRandomInt(0, moves.length - 1)][1]);
var idx = function(x, y) {
return (y * map.getWidth())+x;
};
var d = function dfs(x, y, visited, path) {
if (( x == (map.getWidth() - 2) ) && (y == 10)) {
return true;
}else if (map.getObjectTypeAt(x,y) == 'block') {
return false;
}else{
if (!visited[idx(x+1,y)]) {
alert('r');
var vr = visited.clone(0);
var pr = path.clone(0);
vr[idx(x+1,y)] = true;
pr.push('r');
if (dfs(x+1, y, vr, pr)) {
return true;
}
}
if (!visited[idx(x,y+1)]) {
var vd = visited.clone(0);
var pd = path.clone(0);
vd[idx(x,y+1)] = true;
pd.push('d');
if (dfs(x, y+1, vd, pd)) {
return true;
}
}
if (!visited[idx(x,y-1)]) {
var vu = visited.clone(0);
var pu = path.clone(0);
vu[idx(x,y-1)] = true;
pu.push('u');
if (dfs(x, y-1, vu, pu)) {
return true;
}
}
if (!visited[idx(x-1,y)]) {
var vl = visited.clone(0);
var pl = path.clone(0);
vl[idx(x-1,y)] = true;
pl.push('l');
if (dfs(x-1, y, vl, pl)) {
return true;
}
}
return false;
}
};
alert(d(me.getX(), me.getY(), new Array(map.getWidth()*11), []));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment