|
|
|
@ -279,7 +279,7 @@ snake.onTick = function(obj) { // When the game updates |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function findSafePath(dir, x, y, targetX, targetY,appleDepth=7) { |
|
|
|
|
function findSafePath(dir, x, y, targetX, targetY,appleDepth=3) { |
|
|
|
|
if (spotIsFree(dir, x, y)) { |
|
|
|
|
TEST_REGIONS = [[x, y], ...TEST_REGIONS] |
|
|
|
|
diff_x = x - targetX |
|
|
|
@ -345,6 +345,7 @@ snake.onTick = function(obj) { // When the game updates |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//Make a path towards the closest apple. Always play it safe.
|
|
|
|
|
for (var i=10;i>5;i--) { |
|
|
|
|
diff_x = snake.x - bestApp.x |
|
|
|
|
diff_y = snake.y - bestApp.y |
|
|
|
|
TEST_REGIONS = [] |
|
|
|
@ -356,29 +357,30 @@ snake.onTick = function(obj) { // When the game updates |
|
|
|
|
targetApp=bestApp |
|
|
|
|
if (Math.abs(diff_x) > Math.abs(diff_y)) { |
|
|
|
|
if (diff_x > 0) { |
|
|
|
|
if (snake.me()?.direction !== RIGHT && findPath(LEFT, snake.x - 1, snake.y)) { |
|
|
|
|
if (snake.me()?.direction !== RIGHT && findPath(LEFT, snake.x - 1, snake.y,i)) { |
|
|
|
|
setDirection(LEFT); |
|
|
|
|
return; |
|
|
|
|
} //Preferred direction found.
|
|
|
|
|
} else { |
|
|
|
|
if (snake.me()?.direction !== LEFT && findPath(RIGHT, snake.x + 1, snake.y)) { |
|
|
|
|
if (snake.me()?.direction !== LEFT && findPath(RIGHT, snake.x + 1, snake.y,i)) { |
|
|
|
|
setDirection(RIGHT); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if (diff_y > 0) { |
|
|
|
|
if (snake.me()?.direction !== DOWN && findPath(UP, snake.x, snake.y - 1)) { |
|
|
|
|
if (snake.me()?.direction !== DOWN && findPath(UP, snake.x, snake.y - 1,i)) { |
|
|
|
|
setDirection(UP); |
|
|
|
|
return; |
|
|
|
|
} //Preferred direction found.
|
|
|
|
|
} else { |
|
|
|
|
if (snake.me()?.direction !== UP && findPath(DOWN, snake.x, snake.y + 1)) { |
|
|
|
|
if (snake.me()?.direction !== UP && findPath(DOWN, snake.x, snake.y + 1,i)) { |
|
|
|
|
setDirection(DOWN); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//TODO Try different apples here before trying to "just survive". There may be a better path.
|
|
|
|
|
|
|
|
|
|
console.log("Okay maybe this apple wasn't such a good idea... Try another one.") |
|
|
|
@ -435,19 +437,19 @@ snake.onTick = function(obj) { // When the game updates |
|
|
|
|
.sort((a,b)=>Math.sqrt(Math.pow(snake.x-b[0],2)+Math.pow(snake.y-b[1],2))-Math.sqrt(Math.pow(snake.x-a[0],2)+Math.pow(snake.y-a[1],2))) |
|
|
|
|
|
|
|
|
|
if (snake.x !== 0 && snake.y !== 0) { |
|
|
|
|
if (snake.me()?.direction !== DOWN && findSafePath(UP, snake.x, snake.y - 1, 0, 0)) { |
|
|
|
|
if (snake.me()?.direction !== DOWN && findSafePath(UP, snake.x, snake.y - 1, 0, 0,1)) { |
|
|
|
|
setDirection(UP); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (snake.me()?.direction !== RIGHT && findSafePath(LEFT, snake.x - 1, snake.y, 0, 0)) { |
|
|
|
|
if (snake.me()?.direction !== RIGHT && findSafePath(LEFT, snake.x - 1, snake.y, 0, 0,1)) { |
|
|
|
|
setDirection(LEFT); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (snake.me()?.direction !== LEFT && findSafePath(RIGHT, snake.x + 1, snake.y, 0, 0)) { |
|
|
|
|
if (snake.me()?.direction !== LEFT && findSafePath(RIGHT, snake.x + 1, snake.y, 0, 0,1)) { |
|
|
|
|
setDirection(RIGHT); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (snake.me()?.direction !== UP && findSafePath(DOWN, snake.x, snake.y + 1, 0, 0)) { |
|
|
|
|
if (snake.me()?.direction !== UP && findSafePath(DOWN, snake.x, snake.y + 1, 0, 0,1)) { |
|
|
|
|
setDirection(DOWN); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
@ -460,19 +462,19 @@ snake.onTick = function(obj) { // When the game updates |
|
|
|
|
.filter((a)=>a[0]!==bestApp.x&&a[1]!==bestApp.y) //I want apples furthest away to be checked first. These are probably the safest calculations to make.
|
|
|
|
|
.sort((a,b)=>Math.sqrt(Math.pow(snake.x-b[0],2)+Math.pow(snake.y-b[1],2))-Math.sqrt(Math.pow(snake.x-a[0],2)+Math.pow(snake.y-a[1],2))) |
|
|
|
|
|
|
|
|
|
if (snake.me()?.direction !== DOWN && findSafePath(UP, snake.x, snake.y - 1, 149, 149)) { |
|
|
|
|
if (snake.me()?.direction !== DOWN && findSafePath(UP, snake.x, snake.y - 1, 149, 149,1)) { |
|
|
|
|
setDirection(UP); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (snake.me()?.direction !== RIGHT && findSafePath(LEFT, snake.x - 1, snake.y, 149, 149)) { |
|
|
|
|
if (snake.me()?.direction !== RIGHT && findSafePath(LEFT, snake.x - 1, snake.y, 149, 149,1)) { |
|
|
|
|
setDirection(LEFT); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (snake.me()?.direction !== LEFT && findSafePath(RIGHT, snake.x + 1, snake.y, 149, 149)) { |
|
|
|
|
if (snake.me()?.direction !== LEFT && findSafePath(RIGHT, snake.x + 1, snake.y, 149, 149,1)) { |
|
|
|
|
setDirection(RIGHT); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (snake.me()?.direction !== UP && findSafePath(DOWN, snake.x, snake.y + 1, 149, 149)) { |
|
|
|
|
if (snake.me()?.direction !== UP && findSafePath(DOWN, snake.x, snake.y + 1, 149, 149,1)) { |
|
|
|
|
setDirection(DOWN); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|