Use apple depth for deeper seeking
This commit is contained in:
parent
163a4f25bc
commit
6ddfab3871
38
index.js
38
index.js
@ -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,13 +279,45 @@ 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) {
|
||||
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 (diff_x > 0) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user