:) I'm in the process of developing a maze using JS and P5, which involves utilizing a two-dimensional array filled with numbers 0-8. Specifically, 0 represents empty spaces, 1 denotes walls, 2 signifies the character you control, 3 marks the exit point, and 4-8 represent items that appear randomly. To successfully navigate through the maze and reach the exit (positioned at 3), all items must be collected. This is achieved by changing the value of locations containing items back to 0 upon interaction. Therefore, for the player to exit the maze, every value within the array should be less than 4. At this stage, I am seeking assistance on how to check if this condition is met.
I initially attempted to use the "every()" method, but it seems this only functions correctly with standard arrays. My assumption now is that I require a for loop to accomplish this task; however, I lack clarity on how to execute it properly. This is where I could benefit from some guidance!
The configuration of my maze comprises 18 rows and columns, exemplified by the following layout:
let maze = [
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,2,0,0,0,0,1,0,1,0,0,0,0,1,0,0,1,0,1,0,0,0,3],
[1,1,1,1,1,0,1,0,0,0,1,1,0,0,0,0,1,0,1,0,1,0,1]
]
As for the random spawning of items, this functionality has already been successfully implemented. However, I encountered difficulties when attempting to verify if each value is less than or equal to 3. The implementation using "every()" looked like this:
function checkBoard(mazenumbers){
return mazenumbers <= 3;
}
function alertMazenumbers() {
alert(maze.every(checkBoard));
}
This code snippet should trigger an alert once the player reaches the exit location, as illustrated below:
else if(direction === 'right') {
if(maze[playerPos.y][playerPos.x + 1] == 3) {
alertMazenumbers();
}
The desired outcome is to receive a true alert if all values are less than or equal to 3, and a false alert otherwise. Presently, despite receiving the alert message, the result always appears as false even when all items have been cleared, suggesting true should be displayed instead.