Potential crashing into self bug fix?

master
sigonasr2 2 years ago
parent e6bb9bfd88
commit 163a4f25bc
  1. 6
      data
  2. 46
      index.js

@ -1,7 +1,7 @@
Hit self
923
100
26749
119
28922
923
sigonasr2
sigonasr2

@ -166,6 +166,12 @@ snake.onTick = function(obj) { // When the game updates
return { x: 0, y: 0 }
}
function setDirection(dir) {
if (snake.me()?.direction!==dir) {
snake.setDirection(dir)
}
}
function spotIsFree(dir, x, y) {
newCoords = [x, y]
for (var i of TEST_REGIONS) {
@ -319,24 +325,24 @@ snake.onTick = function(obj) { // When the game updates
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)) {
snake.setDirection(LEFT);
setDirection(LEFT);
return;
} //Preferred direction found.
} else {
if (snake.me()?.direction !== LEFT && findPath(RIGHT, snake.x + 1, snake.y)) {
snake.setDirection(RIGHT);
setDirection(RIGHT);
return;
}
}
} else {
if (diff_y > 0) {
if (snake.me()?.direction !== DOWN && findPath(UP, snake.x, snake.y - 1)) {
snake.setDirection(UP);
setDirection(UP);
return;
} //Preferred direction found.
} else {
if (snake.me()?.direction !== UP && findPath(DOWN, snake.x, snake.y + 1)) {
snake.setDirection(DOWN);
setDirection(DOWN);
return;
}
}
@ -370,19 +376,19 @@ snake.onTick = function(obj) { // When the game updates
}
if (snake.x !== 0 && snake.y !== 0) {
if (snake.me()?.direction !== DOWN && findSafePath(UP, snake.x, snake.y - 1, apple[0], apple[1])) {
snake.setDirection(UP);
setDirection(UP);
return;
}
if (snake.me()?.direction !== RIGHT && findSafePath(LEFT, snake.x - 1, snake.y, apple[0], apple[1])) {
snake.setDirection(LEFT);
setDirection(LEFT);
return;
}
if (snake.me()?.direction !== LEFT && findSafePath(RIGHT, snake.x + 1, snake.y, apple[0], apple[1])) {
snake.setDirection(RIGHT);
setDirection(RIGHT);
return;
}
if (snake.me()?.direction !== UP && findSafePath(DOWN, snake.x, snake.y + 1, apple[0], apple[1])) {
snake.setDirection(DOWN);
setDirection(DOWN);
return;
}
}
@ -393,19 +399,19 @@ snake.onTick = function(obj) { // When the game updates
EXTRA_MOVEMENT=false
if (snake.x !== 0 && snake.y !== 0) {
if (snake.me()?.direction !== DOWN && findSafePath(UP, snake.x, snake.y - 1, 0, 0)) {
snake.setDirection(UP);
setDirection(UP);
return;
}
if (snake.me()?.direction !== RIGHT && findSafePath(LEFT, snake.x - 1, snake.y, 0, 0)) {
snake.setDirection(LEFT);
setDirection(LEFT);
return;
}
if (snake.me()?.direction !== LEFT && findSafePath(RIGHT, snake.x + 1, snake.y, 0, 0)) {
snake.setDirection(RIGHT);
setDirection(RIGHT);
return;
}
if (snake.me()?.direction !== UP && findSafePath(DOWN, snake.x, snake.y + 1, 0, 0)) {
snake.setDirection(DOWN);
setDirection(DOWN);
return;
}
}
@ -413,19 +419,19 @@ snake.onTick = function(obj) { // When the game updates
EXTRA_MOVEMENT=false
console.log("Last resort...")
if (snake.me()?.direction !== DOWN && findSafePath(UP, snake.x, snake.y - 1, 149, 149)) {
snake.setDirection(UP);
setDirection(UP);
return;
}
if (snake.me()?.direction !== RIGHT && findSafePath(LEFT, snake.x - 1, snake.y, 149, 149)) {
snake.setDirection(LEFT);
setDirection(LEFT);
return;
}
if (snake.me()?.direction !== LEFT && findSafePath(RIGHT, snake.x + 1, snake.y, 149, 149)) {
snake.setDirection(RIGHT);
setDirection(RIGHT);
return;
}
if (snake.me()?.direction !== UP && findSafePath(DOWN, snake.x, snake.y + 1, 149, 149)) {
snake.setDirection(DOWN);
setDirection(DOWN);
return;
}
@ -435,19 +441,19 @@ snake.onTick = function(obj) { // When the game updates
return;
} else
if (snake.me()?.direction !== UP && spotIsFree(DOWN, x, y + 1)) {
snake.setDirection(DOWN)
setDirection(DOWN)
return
} else
if (snake.me()?.direction !== RIGHT && spotIsFree(LEFT, x - 1, y)) {
snake.setDirection(LEFT)
setDirection(LEFT)
return
} else
if (snake.me()?.direction !== LEFT && spotIsFree(RIGHT, x + 1, y)) {
snake.setDirection(RIGHT)
setDirection(RIGHT)
return
} else
if (snake.me()?.direction !== DOWN && spotIsFree(UP, x, y - 1)) {
snake.setDirection(UP)
setDirection(UP)
return
}
console.log("Dedge.")

Loading…
Cancel
Save