generated from BattlesnakeOfficial/starter-snake-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateState.js
73 lines (63 loc) · 1.84 KB
/
generateState.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { copyState } from "./helpers.js";
export const generateNewState = (state, snakeId, move, turn) => {
// Create a deep copy of the board
const newState = copyState(state);
const ourSnakes = turn % 2 === 0 ? newState.ourSnakes : newState.enemySnakes;
// Find the snake that is moving
const snakeIndex = ourSnakes.findIndex((snake) => snake.id === snakeId);
const snake = ourSnakes[snakeIndex];
// If the snake is dead we remove the snake and return the new state
if (move === "death") {
ourSnakes.splice(snakeIndex, 1);
return newState;
}
moveSnake(newState, move, snake);
return newState;
};
const moveSnake = (state, move, snake) => {
const newHead = moveHead(snake.head, move);
snake.body.unshift(newHead);
snake.head = newHead;
const ateFood = state.food.some((food, index) => {
if (food.x === newHead.x && food.y === newHead.y) {
state.food.splice(index, 1); // Remove the food from the board
return true;
}
return false;
});
if (!ateFood) {
snake.body.pop();
snake.health -= 1;
} else {
snake.health = 100;
snake.length += 1;
}
};
const moveHead = (head, move) => {
switch (move) {
case "up":
return { x: head.x, y: head.y + 1 };
case "down":
return { x: head.x, y: head.y - 1 };
case "left":
return { x: head.x - 1, y: head.y };
case "right":
return { x: head.x + 1, y: head.y };
}
};
export const deepCopy = (obj) => {
if (obj === null || typeof obj !== "object") {
return obj;
}
const copy = Array.isArray(obj) ? [] : {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
copy[key] = deepCopy(obj[key]);
}
}
return copy;
};
export const isTerminal = (state) => {
if (!state.ourSnakes || !state.enemySnakes) return true;
return state.ourSnakes.length === 0 || state.enemySnakes.length === 0;
};