Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Console App: Guessing Game | Console Applications
Node.js Express: API & CLI Apps
course content

Зміст курсу

Node.js Express: API & CLI Apps

Node.js Express: API & CLI Apps

1. Introduction
2. Console Applications
3. Express.js Framework
4. Developing REST API

bookConsole App: Guessing Game

In this chapter, get ready to level up your console application skills as we dive into the creation of an exciting 🎮 Guess the Number 🔢 game app. This interactive game will challenge players to exercise their intuition by guessing a randomly generated number within a predefined range. Along the way, we'll unravel the mysteries of fundamental concepts such as

  • 🎲 Random number generation;
  • ✅ Input validation;
  • 🤝 User interaction;
  • 💾 Even saving game results to a file.

🏆 Challenge Awaits

Imagine delving into an app that promises excitement and intrigue. Players are invited to guess a mysteriously chosen number within a predefined range. The app offers instant feedback on each guess and a meticulous tally of the attempts made.

This hands-on example is your chance to refine your skills in building CLI Apps and showcases the art of crafting interactive programs.

🚀 Resulting App

See the magic in action! Below is a GIF illustrating the exciting Guess the Number Game app that you'll be crafting:

Building a Guessing Game Console App

You're presented with two paths. The first option beckons you to embark on the journey without assistance, while the second offers a helpful guide to ensure your success. Whether you dive in boldly or follow the structured guide, you're in for a fascinating experience that will leave you with a functional and captivating console app.

Masterplan

  • 👉 Step 1: Setup and Initializations;
  • 👉 Step 2: Define Game Parameters;
  • 👉 Step 3: Define Utility Functions;
  • 👉 Step 4: Game Logic;
  • 👉 Step 5: Save Game Result;
  • 👉 Step 6: Start the Game;
  • 🎉 Conclusion;
  • 🏁 Full App Code.

Step 1: Setup and Initializations

Prepare the canvas by creating a new directory and a file named app.js. Within this file, we conjure the necessary modules:

Create a Readline interface:

Explanation: We import the necessary modules: readline for user interaction and fs.promises for file operations. Then, we create a Readline interface named rl to handle input and output.

Step 2: Define Game Parameters

Set the minimum and maximum numbers:

Generate the secret number:

Initialize the attempts counter:

Explanation: We define the range of numbers (minNumber and maxNumber) within which the secret number will be generated. The secret number is randomly generated using Math.random() and assigned to secretNumber. The attempts variable is initialized to keep track of the user's attempts.

Step 3: Define Utility Functions

Equip with a utility function for impeccable validation:

Explanation: The isValidGuess function checks whether the user's guess is a valid number within the specified range (minNumber to maxNumber).

Step 4: Game Logic

The game's core mechanics through the playGame function:

Explanation: The playGame function forms the core game loop. It uses rl.question to prompt the user for a guess. If the guess is valid, the function checks if the guess matches the secret number. If not, it provides feedback to the user and continues the game loop recursively.

Step 5: Save Game Result

Implement the logic of saving the user's game achievements into the game_results.txt file.

Explanation: The saveGameResult function uses fs.promises to append the game result to a file named game_results.txt. It provides feedback on the success or failure of saving the result.

Step 6: Start the Game

Create the welcome message and launch the game:

Explanation: We display a welcome message and start the game loop by calling the playGame function.

🎉 Conclusion: Victory Lap

By developing the Guess the Number Game App, you've gained valuable experience in designing interactive and engaging console applications. This example showcases the fusion of user input, random number generation, validation, and file manipulation, resulting in a compelling gaming experience.

👨‍💻 Full App Code

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 8
some-alt