@not_thefirst/jsmaze
v0.3.4
Published
Simple maze module in JavaScript.
Downloads
71
Readme
Simple maze module in JavaScript
The name said it all. It's a simple maze module, written in JavaScript.
Documentation:
base class Maze
Properties:
- maze: The array containing the maze's content;
- playerPosition: The current position of the player in the maze array;
Methods:
- move(direction): Move the player in the specified direction;
- getUnicodeMaze(): Returns a text block containing the maze's content.
- getMazeImage(cellSize, wallColor, pathColor, playerColor, endPointColor): Returns a buffer containing the image visualization of the maze's data.
constructor(width, height)
Creates a maze instance with the width of 'width' and height of 'height', data will be saved into Maze.maze. Note that due to the used algorithm (recursive backtracking), the width/height will always be even.\
Maze.maze
The array containing the maze's data.
Maze.playerPosition
Position of the player in the maze's array. Have 2 properties: x and y, representing its position of maze[x][y] in the array.
Maze.move(direction)
Moves the player in the specified direction. Returns/prints nothing on success.
Parameters:
- direction: The direction of the move. Can be up, down, right and left.
Maze.getUnicodeMaze()
Returns the maze's content in a Unicode text block that can be logged to console.
The maze's wall will be represented as '#', the blank area will be '0', player will be 'S' and the end point will be 'E'.
Maze.getMazeImage(cellSize, wallColor, pathColor, playerColor, endPointColor)
Returns a buffer containing the image visualization of the maze's data, which can be saved to the computer with the image extension of png.
Parameters:
- cellSize: The cell size, in pixels. Will default to 20.
- wallColor: The color of a wall cell. Written in the format of #RRGGBB ('#' is required). Will default to #000000 if the parameter is not passed/or not matching the format.
- pathColor: The color of a wall cell. Written in the format of #RRGGBB. Will default to #FFFFFF.
- playerColor: The color of a wall cell. Written in the format of #RRGGBB. Will default to #00FF00.
- endPointColor: The color of a wall cell. Written in the format of #RRGGBB. Will default to #FF0000.
Here's the instruction on how to use it:
The module can be included using this following line in your project:
const Maze = include('@not_thefirst/jsmaze');To create a maze, simply use:
let mazeInstance = new Maze(sizeX, sizeY);
// But to access the maze content, you would have to use:
let mazeContent = mazeInstance.maze;You can also retrieve the maze content in Unicode characters for logging into consoles:
// ...
let unicodeMaze = mazeInstance.getUnicodeMaze();
console.log(unicodeMaze); // this should print the Unicode maze, with S being the player, E being the end point.There's a built-in function to move the player. When creating a maze, the player's position is always at (1, 1) of the generated maze array.
There're 4 directions: up, down, left and right. Example usage:
// ...
let mazeInstance = new Maze(5, 5); // 5x5 maze for demonstration
console.log(mazeInstance.getUnicodeMaze());
/*
Let's , after generation, the maze will look like this:
#####
#S#E#
#0#0#
#000#
#####
*/
// attempt to put garbage for direction
mazeInstance.move('saddwdsd');
// prints 'Invalid direction'
// attempt to move up
mazeInstance.move('up');
// prints 'Invalid move! Hit a wall.'
// attempt to move down
mazeInstance.move('down');
// nothing prints, stating a successful move
console.log(mazeInstance.getUnicodeMaze());
/*
#####
#0#E#
#S#0#
#000#
#####
*/You can also save the image into a file:
const fs = require('fs');
const { Maze } = require('@not_thefirst/jsmaze');
// generates a 20x20 maze
let mazeInstance = new Maze(20, 20);
// gets the maze image with cellSize = 20, colors are auto defaulted
let imgBuffer = mazeInstance.getMazeImage(20);
// outputs a file named maze.png in the working directory
fs.writeFileSync('./maze.png', imgBuffer);