This is a very basic text-based maze game written in JavaScript that runs in the console. It prompts the user for their name and then guides them through a simple choice in a maze.
- Save the code: Save the provided JavaScript code as a
.jsfile (e.g.,maze_game.js). - Install
prompt-sync: If you don't have it already, you need to install theprompt-syncmodule. Open your terminal or command prompt and run:npm install prompt-sync
- Run the game: Navigate to the directory where you saved the file in your terminal and run the game using Node.js:
node maze_game.js
- Follow the prompts: The game will ask for your name and then present you with choices. Type your answers (
yes/no,left/right) and press Enter to proceed.
- Name Input: The game first asks for your name. It validates that the input is not empty and contains only letters.
- Play Confirmation: It then asks if you want to play the game (
yes/no). - Maze Choice: If you choose to play, you enter a maze and are presented with a choice: go
leftorright. - Left Path:
- If you choose
left, you encounter a bridge and are asked if you want tocrossit (yes/no).- Choosing
yesleads to the bridge breaking, and you lose. - Choosing
nois the correct path, and you win.
- Choosing
- If you choose
- Right Path:
- If you choose
right, you fall off a cliff and lose.
- If you choose
- Ending: The game ends with a message indicating whether you won or lost, or a thank you message if you chose not to play.
const prompt = require("prompt-sync")(): This line imports theprompt-syncmodule, which allows the program to take input from the user in the console.- Name Validation: The first
whileloop continuously prompts for the user's name until a valid input (non-empty and containing only letters) is provided. - Play Confirmation Validation: The second
whileloop asks if the user wants to play and validates the input to be either "yes" (or "y") or "no" (or "n"). - Game Logic (if
shouldWePlayis yes):- The main game logic is inside another
whileloop that continues until the game ends (win or lose). - It prompts the user to choose
leftorright. - Left Path Logic: If the user chooses
left, a nestedwhileloop asks about crossing the bridge. - Right Path Logic: If the user chooses
right, the game ends with a loss. - Input validation is included for both the
left/rightandcrosschoices.
- The main game logic is inside another
- No Play Scenario: If the user chooses not to play, a thank you message is displayed.
This is a very basic game and can be expanded in many ways, such as:
- Adding more complex maze structures with multiple choices.
- Introducing items or obstacles.
- Keeping track of scores or lives.
- Implementing different win/lose conditions.
- Improving the user interface (though this is a console-based game).