Created
April 8, 2014 21:40
-
-
Save cbhl/10197065 to your computer and use it in GitHub Desktop.
bad12-1.js
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
// 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 Q = []; | |
var n0 = { | |
'c': [me.getX(), me.getY()], | |
'p': [] | |
}; | |
var sol = null; | |
Q.push(n0); | |
Q.push(n0); | |
var flag = true; | |
while (flag) { | |
var n = Q.shift(); | |
alert('iter'); | |
alert(JSON.stringify(n)); | |
alert(n['c']); | |
var x = n['c'][0]; | |
var y = n['c'][1]; | |
if ((x == (map.getWidth() - 2)) && (y == 10)) { | |
sol = n; | |
flag = false; | |
break; | |
}else{ | |
if (x > 1) { | |
// left | |
var pl = n['p'].slice(0); | |
pl.push('left'); | |
var nl = { | |
'c': [x-1, y], | |
'p': pl | |
}; | |
Q.push(nl); | |
} | |
if (y > 1) { | |
// up | |
var pu = n['p'].slice(0); | |
pu.push('up'); | |
var nu = { | |
'c': [x, y-1], | |
'p': pu | |
}; | |
Q.push(nu); | |
} | |
if (x < (map.getWidth() - 1)) { | |
// right | |
alert('r'); | |
var pr = n['p'].slice(0); | |
pr.push('right'); | |
var nr = { | |
'c': [x+1, y], | |
'p': pr | |
}; | |
alert(JSON.stringify(nr)); | |
Q.push(nr); | |
} | |
if (y < 10) { | |
// down | |
var pd = n['p'].slice(0); | |
pd.push('down'); | |
var nd = { | |
'c': [x, y+1], | |
'p': pd | |
}; | |
Q.push(nd); | |
} | |
} | |
alert(JSON.stringify(Q)); | |
} | |
alert(sol); | |
if (sol['p'].length > 0) { | |
me.move(sol['p'][0]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment