Use apple depth for deeper seeking

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

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

@ -214,7 +214,7 @@ snake.onTick = function(obj) { // When the game updates
return true; return true;
} }
function findPath(dir, x, y,appleDepth=11) { function findPath(dir, x, y,appleDepth=7) {
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 - targetApp.x diff_x = x - targetApp.x
@ -279,13 +279,45 @@ snake.onTick = function(obj) { // When the game updates
return false; 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)) { 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
diff_y = y - targetY diff_y = y - targetY
if (diff_x === 0 && diff_y === 0) { if (diff_x === 0 && diff_y === 0) {
return true; 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 (Math.abs(diff_x) > Math.abs(diff_y)) {
if (diff_x > 0) { if (diff_x > 0) {

Loading…
Cancel
Save