diff --git a/src/game/game.js b/src/game/game.js index 72d900aa51d26b106e317d885c6f64f8d7030927..fc6b11888ea3972cbed1ea5a12de9f74963e3d02 100644 --- a/src/game/game.js +++ b/src/game/game.js @@ -4,6 +4,7 @@ const cellTypes = { PLAYERBODY: 2, FOOD: 3, BANANA: 4, + BORDER: 5, }; function createCell(x, y, type) { @@ -82,7 +83,7 @@ module.exports = { let bodyPlaced = false; if (y - 1 >= 0 && gameBoard[y - 1][x].type === cellTypes.EMPTY) { - // Place body segment + //Place body segment gameBoard[y - 1][x] = createCell(x, y - 1, cellTypes.PLAYERBODY); gameBoard[y - 1][x].pid = pid; gameBoard[y][x].next = gameBoard[y - 1][x]; @@ -100,10 +101,10 @@ module.exports = { }, moveOneStep: (gameBoard) => { - // Save board state to allow multiple snake movements + //Save board state to allow multiple snake movements let updatedBoard = gameBoard.map((row) => row.map((cell) => ({ ...cell }))); - // Loop through board until we find a PLAYERHEAD + //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]; @@ -115,22 +116,22 @@ module.exports = { let tempVar = cell.direction; - // New position based on direction + //New position based on direction if (cell.direction === 0) { - // Up + //Up newY -= 1; } else if (cell.direction === 1) { - // Right + //Right newX += 1; } else if (cell.direction === 2) { - // Down + //Down newY += 1; } else if (cell.direction === 3) { - // Left + //Left newX -= 1; } - // Check for collisions with walls + //Check for collisions with walls if ( newX < 0 || newX >= gameBoard[0].length || @@ -138,7 +139,7 @@ module.exports = { newY >= gameBoard.length ) { console.log(`Player ${cell.pid} has collided with the wall!`); - // Remove the player from the game (indicating death) + //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, @@ -147,19 +148,19 @@ module.exports = { continue; } - // Handle collision with food + //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 + //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 + //Place new food somewhere else updatedBoard = addFood(updatedBoard); } - // Handle collision with banana + //Handle collision with banana if (gameBoard[newY][newX].type == cellTypes.BANANA) { console.log("Slip"); updatedBoard[newY][newX] = createCell(newX, newY, cellTypes.EMPTY); @@ -172,17 +173,28 @@ module.exports = { continue; } - // Snake head to new position by updating cell object + //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; - // Remove previous head position + //Remove previous head position updatedBoard[oldY][oldX] = createCell(oldX, oldY, cellTypes.EMPTY); - // Move the body segments + //Move the body segments let currentBody = newHeadCell.next; while (currentBody) { let bodyOldX = currentBody.x; @@ -190,7 +202,7 @@ module.exports = { currentBody.x = oldX; currentBody.y = oldY; - // Update cell body position + //Update cell body position updatedBoard[oldY][oldX] = { ...currentBody, type: cellTypes.PLAYERBODY, @@ -200,7 +212,7 @@ module.exports = { currentBody = currentBody.next; } - // Remove previous 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); } @@ -210,6 +222,52 @@ module.exports = { return updatedBoard; }, + applyBorders: (gameBoard, borderCounter) => { + let height = gameBoard.length; + let width = gameBoard[0].length; + + //console.log(borderCounter); + + //Return gameBoard when it is 2x2 + if (borderCounter * 2 >= (Math.min(height, width)) - 2) { + return gameBoard; + } + + //Top + for (let x = 0; x < width; x++) { + gameBoard[borderCounter][x].type = cellTypes.BORDER; + gameBoard[borderCounter][x].pid = 0; + gameBoard[borderCounter][x].direction = 0; + gameBoard[borderCounter][x].next = null; + } + + //Bottom + for (let x = 0; x < width; x++) { + gameBoard[height - borderCounter - 1][x].type = cellTypes.BORDER; + gameBoard[height - borderCounter - 1][x].pid = 0; + gameBoard[height - borderCounter - 1][x].direction = 0; + gameBoard[height - borderCounter - 1][x].next = null; + } + + //Left + for (let y = 0; y < height; y++) { + gameBoard[y][borderCounter].type = cellTypes.BORDER; + gameBoard[y][borderCounter].pid = 0; + gameBoard[y][borderCounter].direction = 0; + gameBoard[y][borderCounter].next = null; + } + + //Right + for (let y = 0; y < height; y++) { + gameBoard[y][width - borderCounter - 1].type = cellTypes.BORDER; + gameBoard[y][width - borderCounter - 1].pid = 0; + gameBoard[y][width - borderCounter - 1].direction = 0; + gameBoard[y][width - borderCounter - 1].next = null; + } + + return gameBoard; + }, + getPlayerView: (gameBoard, pid) => { let updatedBoard = gameBoard.map((row) => row.map((cell) => ({ ...cell }))); // YANG: Find head with pid = pid