From 6ddfab3871774d16607b5c860a86d5197fcf0f0f Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Tue, 16 Aug 2022 19:19:49 -0500 Subject: [PATCH] Use apple depth for deeper seeking --- data | 6 +++--- index.js | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/data b/data index b688b17..1100be7 100644 --- a/data +++ b/data @@ -1,7 +1,7 @@ Hit self 923 - 119 - 28922 + 122 + 29350 923 - sigonasr2 + sigonasr2 \ No newline at end of file diff --git a/index.js b/index.js index 54db18d..ce3c699 100644 --- a/index.js +++ b/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) {