Skip to content
Snippets Groups Projects
Commit 02d80eee authored by ys678's avatar ys678
Browse files

Added coordinates of the snake's head and the number of body size next to it

parent ad493c8b
No related branches found
No related tags found
1 merge request!16Potential restart
......@@ -305,15 +305,18 @@ module.exports = {
let width = 11;
let height = 11;
let head = null;
let bodySize = 0;
for (var i = 0; i < updatedBoard.length; i++) {
for (var j = 0; j < updatedBoard[i].length; j++) {
let cell = updatedBoard[i][j];
if (cell.type === cellTypes.PLAYERHEAD && cell.pid === pid) {
console.log("MY Player head found at: ", cell.x, cell.y);
head = cell;
}
if (cell.type === cellTypes.PLAYERBODY && cell.pid === pid) {
bodySize++;
}
}
}
......@@ -329,12 +332,7 @@ module.exports = {
let x = head.x + j - Math.floor(width / 2);
let y = head.y + i - Math.floor(height / 2);
if (
x < 0 ||
x >= updatedBoard[0].length ||
y < 0 ||
y >= updatedBoard.length
) {
if (x < 0 || x >= updatedBoard[0].length || y < 0 || y >= updatedBoard.length) {
row.push(cellTypes.BORDER);
} else {
row.push(updatedBoard[y][x]);
......@@ -343,6 +341,11 @@ module.exports = {
playerView.push(row);
}
return playerView;
},
// Return the view along with head position and body size
return {
view: playerView,
headPosition: { x: head.x, y: head.y },
bodySize: bodySize + 1 // Include head in body size
};
}
};
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment