Skip to content
Snippets Groups Projects
Commit 6f07bac4 authored by ys678's avatar ys678
Browse files

Reworked on the checkCollisions, but still have bugs, the goal is to let the...

Reworked on the checkCollisions, but still have bugs, the goal is to let the snake die if the head hits anything except the food, but when two snakes' heads hit each other, they both die.
parent 56d3daf1
No related branches found
No related tags found
No related merge requests found
......@@ -51,72 +51,6 @@ function addFood(gameBoard) {
return gameBoard;
}
function collision(newX, newY, cell, updatedBoard) {
let collided = false;
// Check if snake hits the wall
if (
newX < 0 ||
newX >= updatedBoard[0].length ||
newY < 0 ||
newY >= updatedBoard.length
) {
console.log(`Player ${cell.pid} has collided with the wall!`);
collided = true;
}
// Check if snake hits food
else if (updatedBoard[newY][newX].type === cellTypes.FOOD) {
console.log(`Player ${cell.pid} has eaten the food!`);
// Add new body segment at the current head's position
let newBodySegment = createCell(cell.x, cell.y, cellTypes.PLAYERBODY);
newBodySegment.pid = cell.pid;
newBodySegment.next = cell.next;
cell.next = newBodySegment;
updatedBoard = addFood(updatedBoard);
}
// Check if snake hits another snake's body
else if (
updatedBoard[newY][newX].type === cellTypes.PLAYERBODY &&
updatedBoard[newY][newX].pid !== cell.pid
) {
console.log(`Player ${cell.pid} has collided with another snake's body!`);
collided = true;
}
// Check if two snake heads collide
else if (updatedBoard[newY][newX].type === cellTypes.PLAYERHEAD) {
console.log(
`Player ${cell.pid} collided head-to-head with Player ${updatedBoard[newY][newX].pid}!`,
);
collided = true;
// Remove both snakes
updatedBoard = updatedBoard.map((row) =>
row.map((c) =>
c.pid === cell.pid || c.pid === updatedBoard[newY][newX].pid
? createCell(c.x, c.y, cellTypes.EMPTY)
: c,
),
);
} else if (updatedBoard[newY][newX].type === cellTypes.BORDER) {
console.log(`Player ${cell.pid} hit a block and died!`);
collided = true;
}
if (collided) {
// Remove the snake from the board
updatedBoard = updatedBoard.map((row) =>
row.map((c) =>
c.pid === cell.pid ? createCell(c.x, c.y, cellTypes.EMPTY) : c,
),
);
return true, updatedBoard; // Snake has died
}
return false, updatedBoard; // No collision, continue moving
}
module.exports = {
cellTypes,
......@@ -167,9 +101,10 @@ module.exports = {
},
moveOneStep: (gameBoard) => {
//Save board state to allow multiple snake movements
let updatedBoard = gameBoard.map((row) => row.map((cell) => ({ ...cell })));
let deadPlayers = [];
//Loop through board until we find a PLAYERHEAD
for (var i = 0; i < gameBoard.length; i++) {
for (var j = 0; j < gameBoard[i].length; j++) {
let cell = gameBoard[i][j];
......@@ -181,32 +116,82 @@ module.exports = {
let tempVar = cell.direction;
// Determine the new position based on direction
if (cell.direction === 0)
newY -= 1; // Up
else if (cell.direction === 1)
newX += 1; // Right
else if (cell.direction === 2)
newY += 1; // Down
else if (cell.direction === 3) newX -= 1; // Left
// Check for collisions
let dead,
newBoard = collision(newX, newY, cell, updatedBoard);
if (dead) {
deadPlayers.push(cell.pid);
}
updatedBoard = newBoard;
if (!dead) {
// Update snake head position
//New position based on direction
if (cell.direction === 0) {
//Up
newY -= 1;
} else if (cell.direction === 1) {
//Right
newX += 1;
} else if (cell.direction === 2) {
//Down
newY += 1;
} else if (cell.direction === 3) {
//Left
newX -= 1;
}
//Check for collisions with walls
if (
newX < 0 ||
newX >= gameBoard[0].length ||
newY < 0 ||
newY >= gameBoard.length
) {
console.log(`Player ${cell.pid} has collided with the wall!`);
//Remove the player from the game (indicating death)
updatedBoard = updatedBoard.map((row) =>
row.map((c) =>
c.pid === cell.pid ? createCell(c.x, c.y, cellTypes.EMPTY) : c,
),
);
continue;
}
//Handle collision with food
if (gameBoard[newY][newX].type === cellTypes.FOOD) {
console.log(`Player ${cell.pid} has eaten the food!`);
//Add new body segment at the current head's position
let newBodySegment = createCell(oldX, oldY, cellTypes.PLAYERBODY);
newBodySegment.pid = cell.pid;
newBodySegment.next = cell.next;
cell.next = newBodySegment;
//Place new food somewhere else
updatedBoard = addFood(updatedBoard);
}
//Handle collision with banana
if (gameBoard[newY][newX].type == cellTypes.BANANA) {
console.log("Slip");
updatedBoard[newY][newX] = createCell(newX, newY, cellTypes.EMPTY);
updatedBoard = updatedBoard.map((row) =>
row.map((c) =>
c.pid === cell.pid ? createCell(c.x, c.y, cellTypes.EMPTY) : c,
),
);
updatedBoard = addFood(updatedBoard);
continue;
}
//Handle collision with border
if (gameBoard[newY][newX].type == cellTypes.BORDER) {
console.log("Void");
updatedBoard = updatedBoard.map((row) =>
row.map((c) =>
c.pid === cell.pid ? createCell(c.x, c.y, cellTypes.EMPTY) : c,
),
);
continue;
}
//Snake head to new position by updating cell object
let newHeadCell = createCell(newX, newY, cellTypes.PLAYERHEAD);
newHeadCell.pid = cell.pid;
newHeadCell.direction = tempVar;
newHeadCell.next = cell.next;
updatedBoard[newY][newX] = newHeadCell;
// Clear old head position
//Remove previous head position
updatedBoard[oldY][oldX] = createCell(oldX, oldY, cellTypes.EMPTY);
//Move the body segments
......@@ -217,7 +202,7 @@ module.exports = {
currentBody.x = oldX;
currentBody.y = oldY;
// Update the body position on the board
//Update cell body position
updatedBoard[oldY][oldX] = {
...currentBody,
type: cellTypes.PLAYERBODY,
......@@ -227,19 +212,27 @@ module.exports = {
currentBody = currentBody.next;
}
// Clear the old body position if it's no longer part of the snake
//Remove previous body position if it's no longer part of the snake
if (oldX !== newHeadCell.x || oldY !== newHeadCell.y) {
updatedBoard[oldY][oldX] = createCell(
oldX,
oldY,
cellTypes.EMPTY,
);
updatedBoard[oldY][oldX] = createCell(oldX, oldY, cellTypes.EMPTY);
}
}
}
}
// Check for head-to-head collisions
let collisions = gameModule.checkCollisions(updatedBoard);
if (collisions.length > 0) {
collisions.forEach(pid => {
console.log(`Player ${pid} has collided with another player!`);
updatedBoard = updatedBoard.map((row) =>
row.map((c) =>
c.pid === pid ? createCell(c.x, c.y, cellTypes.EMPTY) : c,
),
);
});
}
return { updatedBoard, deadPlayers: deadPlayers };
return updatedBoard;
},
applyBorders: (gameBoard, borderCounter) => {
......@@ -249,7 +242,7 @@ module.exports = {
//console.log(borderCounter);
//Return gameBoard when it is 2x2
if (borderCounter * 2 >= Math.min(height, width) - 2) {
if (borderCounter * 2 >= (Math.min(height, width)) - 2) {
return gameBoard;
}
......@@ -295,7 +288,27 @@ module.exports = {
},
checkCollisions: (gameBoard) => {
// returns a list of players that have collided,
// and a new gameBoard with the players removed
let collisions = [];
let headPositions = {};
// Loop through the board and find all PLAYERHEAD positions
for (let i = 0; i < gameBoard.length; i++) {
for (let j = 0; j < gameBoard[i].length; j++) {
let cell = gameBoard[i][j];
if (cell.type === cellTypes.PLAYERHEAD) {
let key = `${cell.x},${cell.y}`;
if (headPositions[key]) {
// If two heads occupy the same position, both players collide
collisions.push(cell.pid);
collisions.push(headPositions[key].pid);
} else {
headPositions[key] = cell;
}
}
}
}
// Return a list of players that have collided
return collisions;
},
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment