|
|
@ -279,7 +279,7 @@ snake.onTick = function(obj) { // When the game updates |
|
|
|
return false; |
|
|
|
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)) { |
|
|
|
if (spotIsFree(dir, x, y)) { |
|
|
|
TEST_REGIONS = [[x, y], ...TEST_REGIONS] |
|
|
|
TEST_REGIONS = [[x, y], ...TEST_REGIONS] |
|
|
|
diff_x = x - targetX |
|
|
|
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.
|
|
|
|
//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_x = snake.x - bestApp.x |
|
|
|
diff_y = snake.y - bestApp.y |
|
|
|
diff_y = snake.y - bestApp.y |
|
|
|
TEST_REGIONS = [] |
|
|
|
TEST_REGIONS = [] |
|
|
@ -356,29 +357,30 @@ snake.onTick = function(obj) { // When the game updates |
|
|
|
targetApp=bestApp |
|
|
|
targetApp=bestApp |
|
|
|
if (Math.abs(diff_x) > Math.abs(diff_y)) { |
|
|
|
if (Math.abs(diff_x) > Math.abs(diff_y)) { |
|
|
|
if (diff_x > 0) { |
|
|
|
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); |
|
|
|
setDirection(LEFT); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} //Preferred direction found.
|
|
|
|
} //Preferred direction found.
|
|
|
|
} else { |
|
|
|
} 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); |
|
|
|
setDirection(RIGHT); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (diff_y > 0) { |
|
|
|
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); |
|
|
|
setDirection(UP); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} //Preferred direction found.
|
|
|
|
} //Preferred direction found.
|
|
|
|
} else { |
|
|
|
} 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); |
|
|
|
setDirection(DOWN); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
//TODO Try different apples here before trying to "just survive". There may be a better path.
|
|
|
|
//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.") |
|
|
|
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))) |
|
|
|
.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.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); |
|
|
|
setDirection(UP); |
|
|
|
return; |
|
|
|
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); |
|
|
|
setDirection(LEFT); |
|
|
|
return; |
|
|
|
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); |
|
|
|
setDirection(RIGHT); |
|
|
|
return; |
|
|
|
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); |
|
|
|
setDirection(DOWN); |
|
|
|
return; |
|
|
|
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.
|
|
|
|
.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))) |
|
|
|
.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); |
|
|
|
setDirection(UP); |
|
|
|
return; |
|
|
|
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); |
|
|
|
setDirection(LEFT); |
|
|
|
return; |
|
|
|
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); |
|
|
|
setDirection(RIGHT); |
|
|
|
return; |
|
|
|
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); |
|
|
|
setDirection(DOWN); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|