Updated all references to snake.me() to instead use last sent direction.
This commit is contained in:
parent
e086651495
commit
833f0192f6
6
data
6
data
@ -1,7 +1,7 @@
|
|||||||
Hit self
|
Hit other
|
||||||
923
|
923
|
||||||
129
|
131
|
||||||
29815
|
29815
|
||||||
923
|
923
|
||||||
sigonasr2
|
sigonasr2
|
||||||
|
|
59
index.js
59
index.js
@ -34,8 +34,7 @@ ${BEST_SEEN_WHO}
|
|||||||
`
|
`
|
||||||
*/
|
*/
|
||||||
|
|
||||||
horz_dir = LEFT
|
myDir = RIGHT
|
||||||
vert_dir = UP
|
|
||||||
LAST_DEATH = vals[0]
|
LAST_DEATH = vals[0]
|
||||||
BEST_LENGTH = Number(vals[1])
|
BEST_LENGTH = Number(vals[1])
|
||||||
TOTAL_ATTEMPTS = Number(vals[2])
|
TOTAL_ATTEMPTS = Number(vals[2])
|
||||||
@ -167,8 +166,9 @@ snake.onTick = function(obj) { // When the game updates
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setDirection(dir) {
|
function setDirection(dir) {
|
||||||
if (snake.me()?.direction!==dir) {
|
if (myDir!==dir) {
|
||||||
snake.setDirection(dir)
|
snake.setDirection(dir)
|
||||||
|
myDir=dir
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,9 +179,11 @@ snake.onTick = function(obj) { // When the game updates
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (var i of snake.me()?.body) {
|
if (snake.me()) {
|
||||||
if (i[0] === newCoords[0] && i[1] === newCoords[1]) {
|
for (var i of snake.me()?.body) {
|
||||||
return false;
|
if (i[0] === newCoords[0] && i[1] === newCoords[1]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (var i of snake.blackHoles()) {
|
for (var i of snake.blackHoles()) {
|
||||||
@ -195,6 +197,9 @@ snake.onTick = function(obj) { // When the game updates
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (var i of snake.headPositions()) {
|
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]) {
|
if (i[0] === newCoords[0] && i[1] === newCoords[1]) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -356,24 +361,24 @@ snake.onTick = function(obj) { // When the game updates
|
|||||||
targetApp=bestApp
|
targetApp=bestApp
|
||||||
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) {
|
||||||
if (snake.me()?.direction !== RIGHT && findPath(LEFT, snake.x - 1, snake.y)) {
|
if (myDir !== RIGHT && findPath(LEFT, snake.x - 1, snake.y)) {
|
||||||
setDirection(LEFT);
|
setDirection(LEFT);
|
||||||
return;
|
return;
|
||||||
} //Preferred direction found.
|
} //Preferred direction found.
|
||||||
} else {
|
} 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);
|
setDirection(RIGHT);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (diff_y > 0) {
|
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);
|
setDirection(UP);
|
||||||
return;
|
return;
|
||||||
} //Preferred direction found.
|
} //Preferred direction found.
|
||||||
} else {
|
} 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);
|
setDirection(DOWN);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -407,19 +412,19 @@ snake.onTick = function(obj) { // When the game updates
|
|||||||
continue; //Skip this apple. It's dangerous.
|
continue; //Skip this apple. It's dangerous.
|
||||||
}
|
}
|
||||||
if (snake.x !== 0 && snake.y !== 0) {
|
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);
|
setDirection(UP);
|
||||||
return;
|
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);
|
setDirection(LEFT);
|
||||||
return;
|
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);
|
setDirection(RIGHT);
|
||||||
return;
|
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);
|
setDirection(DOWN);
|
||||||
return;
|
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)))
|
.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.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);
|
setDirection(UP);
|
||||||
return;
|
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);
|
setDirection(LEFT);
|
||||||
return;
|
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);
|
setDirection(RIGHT);
|
||||||
return;
|
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);
|
setDirection(DOWN);
|
||||||
return;
|
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.
|
.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)))
|
.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);
|
setDirection(UP);
|
||||||
return;
|
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);
|
setDirection(LEFT);
|
||||||
return;
|
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);
|
setDirection(RIGHT);
|
||||||
return;
|
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);
|
setDirection(DOWN);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log("Good luck....")
|
console.log("Good luck....")
|
||||||
TEST_REGIONS = []
|
TEST_REGIONS = []
|
||||||
EXTRA_MOVEMENT=false
|
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;
|
return;
|
||||||
} else
|
} else
|
||||||
if (snake.me()?.direction !== UP && spotIsFree(DOWN, x, y + 1)) {
|
if (myDir !== UP && spotIsFree(DOWN, x, y + 1)) {
|
||||||
setDirection(DOWN)
|
setDirection(DOWN)
|
||||||
return
|
return
|
||||||
} else
|
} else
|
||||||
if (snake.me()?.direction !== RIGHT && spotIsFree(LEFT, x - 1, y)) {
|
if (myDir !== RIGHT && spotIsFree(LEFT, x - 1, y)) {
|
||||||
setDirection(LEFT)
|
setDirection(LEFT)
|
||||||
return
|
return
|
||||||
} else
|
} else
|
||||||
if (snake.me()?.direction !== LEFT && spotIsFree(RIGHT, x + 1, y)) {
|
if (myDir !== LEFT && spotIsFree(RIGHT, x + 1, y)) {
|
||||||
setDirection(RIGHT)
|
setDirection(RIGHT)
|
||||||
return
|
return
|
||||||
} else
|
} else
|
||||||
if (snake.me()?.direction !== DOWN && spotIsFree(UP, x, y - 1)) {
|
if (myDir !== DOWN && spotIsFree(UP, x, y - 1)) {
|
||||||
setDirection(UP)
|
setDirection(UP)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user