From 833f0192f6223045709bc32cfbb1d542c20c1edd Mon Sep 17 00:00:00 2001 From: "sigonasr2, Sig, Sigo" Date: Wed, 17 Aug 2022 17:44:18 +0000 Subject: [PATCH] Updated all references to snake.me() to instead use last sent direction. --- data | 6 +++--- index.js | 59 ++++++++++++++++++++++++++++++-------------------------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/data b/data index d55f59e..cc1861f 100644 --- a/data +++ b/data @@ -1,7 +1,7 @@ -Hit self +Hit other 923 - 129 + 131 29815 923 - sigonasr2 + sigonasr2 \ No newline at end of file diff --git a/index.js b/index.js index 8f4076c..8a7cd50 100644 --- a/index.js +++ b/index.js @@ -34,8 +34,7 @@ ${BEST_SEEN_WHO} ` */ -horz_dir = LEFT -vert_dir = UP +myDir = RIGHT LAST_DEATH = vals[0] BEST_LENGTH = Number(vals[1]) TOTAL_ATTEMPTS = Number(vals[2]) @@ -167,8 +166,9 @@ snake.onTick = function(obj) { // When the game updates } function setDirection(dir) { - if (snake.me()?.direction!==dir) { + if (myDir!==dir) { snake.setDirection(dir) + myDir=dir } } @@ -179,9 +179,11 @@ snake.onTick = function(obj) { // When the game updates return false; } } - for (var i of snake.me()?.body) { - if (i[0] === newCoords[0] && i[1] === newCoords[1]) { - return false; + if (snake.me()) { + for (var i of snake.me()?.body) { + if (i[0] === newCoords[0] && i[1] === newCoords[1]) { + return false; + } } } for (var i of snake.blackHoles()) { @@ -195,6 +197,9 @@ snake.onTick = function(obj) { // When the game updates } } for (var i of snake.headPositions()) { + if (i[0]===snake.x&&y[1]===snake.y) { + continue; //Don't include our position in this. + } if (i[0] === newCoords[0] && i[1] === newCoords[1]) { return false; } @@ -356,24 +361,24 @@ snake.onTick = function(obj) { // When the game updates targetApp=bestApp if (Math.abs(diff_x) > Math.abs(diff_y)) { if (diff_x > 0) { - if (snake.me()?.direction !== RIGHT && findPath(LEFT, snake.x - 1, snake.y)) { + if (myDir !== RIGHT && findPath(LEFT, snake.x - 1, snake.y)) { setDirection(LEFT); return; } //Preferred direction found. } else { - if (snake.me()?.direction !== LEFT && findPath(RIGHT, snake.x + 1, snake.y)) { + if (myDir !== LEFT && findPath(RIGHT, snake.x + 1, snake.y)) { setDirection(RIGHT); return; } } } else { if (diff_y > 0) { - if (snake.me()?.direction !== DOWN && findPath(UP, snake.x, snake.y - 1)) { + if (myDir !== DOWN && findPath(UP, snake.x, snake.y - 1)) { setDirection(UP); return; } //Preferred direction found. } else { - if (snake.me()?.direction !== UP && findPath(DOWN, snake.x, snake.y + 1)) { + if (myDir !== UP && findPath(DOWN, snake.x, snake.y + 1)) { setDirection(DOWN); return; } @@ -407,19 +412,19 @@ snake.onTick = function(obj) { // When the game updates continue; //Skip this apple. It's dangerous. } if (snake.x !== 0 && snake.y !== 0) { - if (snake.me()?.direction !== DOWN && findSafePath(UP, snake.x, snake.y - 1, apple[0], apple[1])) { + if (myDir !== DOWN && findSafePath(UP, snake.x, snake.y - 1, apple[0], apple[1])) { setDirection(UP); return; } - if (snake.me()?.direction !== RIGHT && findSafePath(LEFT, snake.x - 1, snake.y, apple[0], apple[1])) { + if (myDir !== RIGHT && findSafePath(LEFT, snake.x - 1, snake.y, apple[0], apple[1])) { setDirection(LEFT); return; } - if (snake.me()?.direction !== LEFT && findSafePath(RIGHT, snake.x + 1, snake.y, apple[0], apple[1])) { + if (myDir !== LEFT && findSafePath(RIGHT, snake.x + 1, snake.y, apple[0], apple[1])) { setDirection(RIGHT); return; } - if (snake.me()?.direction !== UP && findSafePath(DOWN, snake.x, snake.y + 1, apple[0], apple[1])) { + if (myDir !== UP && findSafePath(DOWN, snake.x, snake.y + 1, apple[0], apple[1])) { setDirection(DOWN); return; } @@ -435,19 +440,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))) if (snake.x !== 0 && snake.y !== 0) { - if (snake.me()?.direction !== DOWN && findSafePath(UP, snake.x, snake.y - 1, 0, 0)) { + if (myDir !== DOWN && findSafePath(UP, snake.x, snake.y - 1, 0, 0)) { setDirection(UP); return; } - if (snake.me()?.direction !== RIGHT && findSafePath(LEFT, snake.x - 1, snake.y, 0, 0)) { + if (myDir !== RIGHT && findSafePath(LEFT, snake.x - 1, snake.y, 0, 0)) { setDirection(LEFT); return; } - if (snake.me()?.direction !== LEFT && findSafePath(RIGHT, snake.x + 1, snake.y, 0, 0)) { + if (myDir !== LEFT && findSafePath(RIGHT, snake.x + 1, snake.y, 0, 0)) { setDirection(RIGHT); return; } - if (snake.me()?.direction !== UP && findSafePath(DOWN, snake.x, snake.y + 1, 0, 0)) { + if (myDir !== UP && findSafePath(DOWN, snake.x, snake.y + 1, 0, 0)) { setDirection(DOWN); return; } @@ -460,41 +465,41 @@ 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. .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 (myDir !== DOWN && findSafePath(UP, snake.x, snake.y - 1, 149, 149)) { setDirection(UP); return; } - if (snake.me()?.direction !== RIGHT && findSafePath(LEFT, snake.x - 1, snake.y, 149, 149)) { + if (myDir !== RIGHT && findSafePath(LEFT, snake.x - 1, snake.y, 149, 149)) { setDirection(LEFT); return; } - if (snake.me()?.direction !== LEFT && findSafePath(RIGHT, snake.x + 1, snake.y, 149, 149)) { + if (myDir !== LEFT && findSafePath(RIGHT, snake.x + 1, snake.y, 149, 149)) { setDirection(RIGHT); return; } - if (snake.me()?.direction !== UP && findSafePath(DOWN, snake.x, snake.y + 1, 149, 149)) { + if (myDir !== UP && findSafePath(DOWN, snake.x, snake.y + 1, 149, 149)) { setDirection(DOWN); return; } console.log("Good luck....") TEST_REGIONS = [] EXTRA_MOVEMENT=false - if (spotIsFree(snake.me().direction, x + (snake.me().direction === RIGHT ? 1 : snake.me().direction === LEFT ? -1 : 0), y + (snake.me().direction === DOWN ? 1 : snake.me().direction === UP ? -1 : 0))) { + if (spotIsFree(myDir, x + (myDir === RIGHT ? 1 : myDir === LEFT ? -1 : 0), y + (myDir === DOWN ? 1 : myDir === UP ? -1 : 0))) { return; } else - if (snake.me()?.direction !== UP && spotIsFree(DOWN, x, y + 1)) { + if (myDir !== UP && spotIsFree(DOWN, x, y + 1)) { setDirection(DOWN) return } else - if (snake.me()?.direction !== RIGHT && spotIsFree(LEFT, x - 1, y)) { + if (myDir !== RIGHT && spotIsFree(LEFT, x - 1, y)) { setDirection(LEFT) return } else - if (snake.me()?.direction !== LEFT && spotIsFree(RIGHT, x + 1, y)) { + if (myDir !== LEFT && spotIsFree(RIGHT, x + 1, y)) { setDirection(RIGHT) return } else - if (snake.me()?.direction !== DOWN && spotIsFree(UP, x, y - 1)) { + if (myDir !== DOWN && spotIsFree(UP, x, y - 1)) { setDirection(UP) return }