Use apple depth for deeper seeking

master
sigonasr2 2 years ago
parent 163a4f25bc
commit 6ddfab3871
  1. 4
      data
  2. 36
      index.js

@ -1,7 +1,7 @@
Hit self
923
119
28922
122
29350
923
sigonasr2

@ -214,7 +214,7 @@ snake.onTick = function(obj) { // When the game updates
return true;
}
function findPath(dir, x, y,appleDepth=11) {
function findPath(dir, x, y,appleDepth=7) {
if (spotIsFree(dir, x, y)) {
TEST_REGIONS = [[x, y], ...TEST_REGIONS]
diff_x = x - targetApp.x
@ -279,14 +279,46 @@ snake.onTick = function(obj) { // When the game updates
return false;
}
function findSafePath(dir, x, y, targetX, targetY,emergency=false) {
function findSafePath(dir, x, y, targetX, targetY,appleDepth=7) {
if (spotIsFree(dir, x, y)) {
TEST_REGIONS = [[x, y], ...TEST_REGIONS]
diff_x = x - targetX
diff_y = y - targetY
if (diff_x === 0 && diff_y === 0) {
if (--appleDepth===0) {
return true;
} else {
var myRegions = [...TEST_REGIONS]
while (APPLES_LIST.length>0) {
var newApple = APPLES_LIST.shift()
TEST_REGIONS=[...myRegions]
targetApp={x:newApple[0],y:newApple[1]}
diff_x = x - targetApp.x
diff_y = y - targetApp.y
if (Math.abs(diff_x) > Math.abs(diff_y)) {
if (diff_x > 0) {
if (dir !== RIGHT && findSafePath(LEFT, x - 1, y,targetApp.x,targetApp.y,appleDepth)) {
return true;
} //Preferred direction found.
} else {
if (dir !== LEFT && findSafePath(RIGHT, x + 1, y,targetApp.x,targetApp.y,appleDepth)) {
return true;
}
}
} else {
if (diff_y > 0) {
if (dir !== DOWN && findSafePath(UP, x, y - 1,targetApp.x,targetApp.y,appleDepth)) {
return true;
} //Preferred direction found.
} else {
if (dir !== UP && findSafePath(DOWN, x, y + 1,targetApp.x,targetApp.y,appleDepth)) {
return true;
}
}
}
}
}
}
if (Math.abs(diff_x) > Math.abs(diff_y)) {
if (diff_x > 0) {
if (dir !== RIGHT && findSafePath(LEFT, x - 1, y, targetX, targetY)) {

Loading…
Cancel
Save