MAZE GAME
Concept
The idea behind the game is that player must make their way through a sequence of difficult mazes using mathematical algorithms, each one generated randomly to provide a different challenge with each maze. The main objective of the game is for the player to navigate from the beginning point to the maze’s exit.
Gold coins are scattered throughout the maze and act as collectibles for players to find. This helps them to finish the game if they collect a certain number of gold coins. Also, 90% of the gold coins spawned on the maze must be collected for the player to exit the maze. However, they can use the teleporter without this condition.
There are teleporters placed in the maze that serve as an interesting twist to the game. Stepping upon a teleporter transports the player to a whole new maze, which changes the difficulty and forces them to adjust their approach on the fly.
Every maze in the game has a timer built into it, which creates a sense of urgency and tests the player’s ability to think quickly and make decisions under pressure. Also, the player has limited vision which makes it difficult for the player to plan their next move. This forces them to memorize their path to complete the maze.
Design
The visual and thematic components of this maze game are designed to put the player in the shoes of a dungeon master.
The image is used for the background of the maze.
This is a snippet of the maze. The design is a work in progress as I am focusing on getting the basic functionality to work first. Currently, the big yellow is the player, the small bronze-colored circles are the gold coins, the blue circles are the teleporters, and the red square is the exit. I planning on replacing these with some avatars or icons.
Frightening Part
The most frightening part when I started to code was the main functionality of the game, The Maze. I wanted to have random mazes being generated every time the user plays the game or is teleported. So that it invokes a new sense of challenge.
The use of randomness and the cyclical structure of the path-carving process is what gives the procedure its complexity. Recursive functions include function calls that repeat back on themselves, they can be challenging to study and comprehend. I have
The code below shows how I tackled this issue:
function generateRandomMaze(rows, cols) { let maze = new Array(rows); for (let y = 0; y < rows; y++) { maze[y] = new Array(cols).fill('#'); } function carvePath(x, y) { const directions = [[1, 0], [-1, 0], [0, 1], [0, -1]]; // Shuffle directions to ensure randomness shuffle(directions); directions.forEach(([dx, dy]) => { const nx = x + 2 * dx, ny = y + 2 * dy; if (nx > 0 && nx < cols - 1 && ny > 0 && ny < rows - 1 && maze[ny][nx] === '#') { maze[ny][nx] = ' '; // Carve path to the new cell maze[y + dy][x + dx] = ' '; carvePath(nx, ny); } }); } // Randomize the starting point a bit more conservatively const startX = 2 * Math.floor(Math.random() * Math.floor((cols - 2) / 4)) + 1; const startY = 2 * Math.floor(Math.random() * Math.floor((rows - 2) / 4)) + 1; // Ensure starting point is open maze[startY][startX] = ' '; carvePath(startX, startY); // Set exit maze[rows - 2][cols - 2] = 'E'; // Place teleporters and coins after maze generation to avoid overwriting addRandomItems(maze, 'T', 3); addRandomItems(maze, 'C', 5); return maze; } function shuffle(array) { for (let i = array.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [array[i], array[j]] = [array[j], array[i]]; } }
The generateRandomMaze() function creates a random intricate maze by first populating it with walls and then utilizing the depth-first search algorithm to recursively find pathways. To ensure variation in the pathways formed, it shuffles possible directions (up, down, left, and right) starting from a randomly selected point. It improves the gameplay experience by slicing through the grid, placing an exit, and dispersing interactive features like coins and teleporters at random. The carvePath function’s use of recursion and randomness is essential to creating a maze that embodies the spirit of maze exploration and strategy and is both difficult and unique each time the game is played.
NOTE: I have used a lot of functions rather than classes. The functionality of the game is 80% achieved and from here on now I will change the functions to classes and complete 100% of the game/gameplay.