Game Code Overview
The following code pertains to a grid-based game that I am currently developing, primarily for educational purposes. This game is played on an 8 x 8 grid where the player navigates from one cell to another while encountering obstacles such as walls.
Key Game Variables
The Player Object This object holds the current position of the player within the game.
var player = {
location: {
x: 3,
y: 3
}
}
The Grid
board[x][y] // Represents an individual cell in the game
Directions
const NORTH = 0;
const EAST = 1;
const SOUTH = 2;
const WEST = 3;
Obstacles (Walls)
board[x][y][NORTH] = 1; // Indicates a wall along the north side of the cell
Initial Testing Phase
During the initial testing phase, everything was progressing smoothly while checking if the player moved off the board. However, challenges arose when implementing checks for obstacles like walls.
To begin the testing process, I reset the game board and removed all walls. It appears that this action might be causing an error message:
https://i.sstatic.net/8FStx.png
https://i.sstatic.net/yJ7an.png
Code Snippet
const NORTH = 0;
const EAST = 1;
const SOUTH = 2;
const WEST = 3;
const ROOMWIDTH = 7; // Zero-indexed array
const ROOMHEIGHT = 7;
var moveResonse = {
valid: 0,
reason: "",
helpText: ""
}
var player = {
location: {
x: 3,
y: 3
}
}
var board = [];
for(var x=0; x<8; x++){
for(y=0; y<8; y++){
for(var d=0; d<4;d++){
board[x][y][d] = 0;
}
}
}
// Initial TESTING: Place a WALL on the west side of cell [3,3]
board[3][3][0] = 0;
board[3][3][1] = 0;
board[3][3][2] = 0;
board[3][3][3] = 1;
// Another WALL added to the east side of cell [2,3]
board[2][3][0] = 0;
board[2][3][1] = 1;
board[2][3][2] = 0;
board[2][3][3] = 0;
function move(direction) {
// 1. Check if the user is moving outside the board boundaries
switch (direction) {
case NORTH:
if (player.location.y - 1 < 0) {
moveResponse = {
valid: 0,
reason: "Out of bounds",
helpText: "You can't move NORTH beyond the grid!"
}
}
return moveResponse;
break;
case EAST:
if (player.location.x + 1 > ROOMWIDTH) {
moveResponse = {
valid: 0,
reason: "Out of bounds",
helpText: "You can't move EAST beyond the grid!"
}
}
return moveResponse;
break;
case SOUTH:
if (player.location.y + 1 > ROOMHEIGHT) {
moveResponse = {
valid: 0,
reason: "Out of bounds",
helpText: "You can't move SOUTH beyond the grid!"
}
}
return moveResponse;
break;
case WEST:
if (player.location.x - 1 < 0) {
moveResponse = {
valid: 0,
reason: "Out of bounds",
helpText: "You can't move WEST beyond the grid!"
}
}
return moveResponse;
break;
}
// 2. Check for any obstacles present (walls)
if(board[player.location.x][player.location.y][direction] == 1){
moveResponse = {
valid: 0,
reason: "Obstacle",
helpText: "You can't proceed in that direction, there's a WALL blocking your path!"
}
return moveResponse;
}
}
alert(`Attempting to move WEST: ${move(WEST).helpText}`);
Previously, a question of mine was closed due to being perceived as opinion-based. My intention is not to seek opinions but rather assistance in resolving technical issues. If that entails exploring different approaches, I am open to learning and improving. Your support in this journey is greatly valued.