@hardik_raj_baral/number-guessing-game 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +89 -0
  2. package/game.js +132 -0
  3. package/index.js +35 -0
  4. package/package.json +29 -0
package/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # Number Guessing Game
2
+
3
+ A fun command-line number guessing game built with Node.js!
4
+
5
+ **Project URL:** [https://roadmap.sh/projects/number-guessing-game](https://roadmap.sh/projects/number-guessing-game)
6
+
7
+ ## Description
8
+
9
+ The computer thinks of a number between 1 and 100, and you have to guess it! The game offers three difficulty levels with varying numbers of lives (attempts):
10
+
11
+ - **Easy**: 10 lives
12
+ - **Medium**: 5 lives
13
+ - **Hard**: 3 lives
14
+
15
+ ## Features
16
+
17
+ - 🎮 Three difficulty modes
18
+ - 📊 Attempt counter to track your guesses
19
+ - 🔄 Play multiple rounds without restarting
20
+ - 💡 Hints: The game tells you if your guess is too high or too low
21
+
22
+ ## Requirements
23
+
24
+ - Node.js (v12 or higher)
25
+
26
+ ## Installation
27
+
28
+ 1. Clone or download this project
29
+ 2. Navigate to the project directory:
30
+ ```bash
31
+ cd number-guessing-game
32
+ ```
33
+
34
+ ## How to Play
35
+
36
+ 1. Run the game:
37
+ ```bash
38
+ node number-guessingGame.js
39
+ ```
40
+
41
+ 2. Choose a difficulty level (1 for Easy, 2 for Medium, 3 for Hard)
42
+
43
+ 3. Start guessing the number between 1 and 100
44
+
45
+ 4. The game will tell you:
46
+ - If your guess is too high: "Go lower"
47
+ - If your guess is too low: "Go higher"
48
+
49
+ 5. Guess the correct number before running out of lives to win!
50
+
51
+ 6. After each game, you can choose to play again or exit
52
+
53
+ ## Game Rules
54
+
55
+ - You must enter a number between 1 and 100
56
+ - Each incorrect guess costs you one life
57
+ - The game ends when you either:
58
+ - Correctly guess the number (WIN)
59
+ - Run out of lives (LOSE)
60
+
61
+ ## Example Gameplay
62
+
63
+ ```
64
+ Welcome to the Number Guessing Game!
65
+ I'm thinking of a number between 1 and 100.
66
+
67
+ Choose a difficulty
68
+ 1. Easy
69
+ 2. Medium
70
+ 3. Hard
71
+ Enter your choice: 1
72
+
73
+ You have chosen easy mode, you have 10 lives
74
+ Computer has guessed the number between 1 - 100
75
+ Enter your guess: 50
76
+ Go higher
77
+ Enter your guess: 75
78
+ Go lower
79
+ Enter your guess: 62
80
+ You win! You guessed the number in 3 attempts
81
+ ```
82
+
83
+ ## License
84
+
85
+ Feel free to use and modify this game as you like!
86
+
87
+ ## Author
88
+
89
+ Created as a game development project.
package/game.js ADDED
@@ -0,0 +1,132 @@
1
+ import inquirer from "inquirer";
2
+
3
+ async function askGuess() {
4
+ const { guess } = await inquirer.prompt([
5
+ {
6
+ type: "input",
7
+ name: "guess",
8
+ message: "Enter your guess :",
9
+ validate: (value) => {
10
+ const num = Number(value);
11
+ if (isNaN(num)) return "Please enter a valid number";
12
+ if (num < 1 || num > 100)
13
+ return "Please enter a number between 1 and 100";
14
+ return true;
15
+ },
16
+ },
17
+ ]);
18
+ return Number(guess);
19
+ }
20
+
21
+ export async function easy() {
22
+ console.log("you have choose easy mode you have 10 lives");
23
+ const guess = Math.floor(Math.random() * 100);
24
+ console.log("Computer has guessed the Number between 1 - 100");
25
+ let lives = 10;
26
+ let attempts = 0;
27
+ while (lives > 0) {
28
+ const ans = await askGuess();
29
+ if (ans === guess) {
30
+ console.log(`you win!!!. You guessed the number in ${attempts} attempts`);
31
+ break;
32
+ } else if (ans < guess) {
33
+ console.log("Go higher ");
34
+ } else {
35
+ console.log("Go lower");
36
+ }
37
+ lives--;
38
+ attempts++;
39
+ console.log(`You have ${lives} lives left`);
40
+ }
41
+ if (lives === 0) {
42
+ console.log("you Loose T_T !!! ");
43
+ }
44
+ await restart();
45
+ }
46
+
47
+ export async function medium() {
48
+ console.log("you have choose medium mode you have 5 lives");
49
+ const guess = Math.floor(Math.random() * 100);
50
+ console.log("Computer has guessed the Number between 1 - 100");
51
+ let lives = 5;
52
+ let attempts = 0;
53
+
54
+ while (lives > 0) {
55
+ const ans = await askGuess();
56
+ if (ans === guess) {
57
+ console.log(`you win!!!. You guessed the number in ${attempts} attempts`);
58
+ break;
59
+ } else if (ans < guess) {
60
+ console.log("Go higher ");
61
+ } else {
62
+ console.log("Go lower");
63
+ }
64
+ lives--;
65
+ attempts++;
66
+ console.log(`You have ${lives} lives left`);
67
+ }
68
+ if (lives === 0) {
69
+ console.log("you Loose T_T !!! ");
70
+ }
71
+ await restart();
72
+ }
73
+
74
+ export async function hard() {
75
+ console.log("you have choose hard mode you have 3 lives");
76
+ const guess = Math.floor(Math.random() * 100);
77
+ console.log("Computer has guessed the Number between 1 - 100");
78
+ let lives = 3;
79
+ let attempts = 0;
80
+ while (lives > 0) {
81
+ const ans = await askGuess();
82
+ if (ans === guess) {
83
+ console.log(`you win!!!. You guessed the number in ${attempts} attempts`);
84
+ break;
85
+ } else if (ans < guess) {
86
+ console.log("Go higher ");
87
+ } else {
88
+ console.log("Go lower");
89
+ }
90
+ lives--;
91
+ attempts++;
92
+ console.log(`You have ${lives} lives left`);
93
+ }
94
+ if (lives === 0) {
95
+ console.log("you Loose T_T !!! ");
96
+ }
97
+ await restart();
98
+ }
99
+
100
+ export async function start() {
101
+ const { level } = await inquirer.prompt([
102
+ {
103
+ type: "list",
104
+ name: "level",
105
+ message: "Choose your difficulty level",
106
+ choices: [
107
+ { name: "Easy", value: "easy" },
108
+ { name: "Medium", value: "medium" },
109
+ { name: "Hard", value: "hard" },
110
+ ],
111
+ },
112
+ ]);
113
+ if (level === "easy") {
114
+ await easy();
115
+ } else if (level === "medium") {
116
+ await medium();
117
+ } else {
118
+ await hard();
119
+ }
120
+ }
121
+
122
+ async function restart() {
123
+ const { ans } = await inquirer.prompt([
124
+ {
125
+ type: "confirm",
126
+ name: "ans",
127
+ message: "Do you want to play again?",
128
+ default: false,
129
+ },
130
+ ]);
131
+ return ans ? start() : console.log("Thank you for playing! Goodbye!");
132
+ }
package/index.js ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { easy, medium, hard, start } from "./game.js";
4
+
5
+ import { Command } from "commander";
6
+
7
+ const program = new Command();
8
+
9
+ program
10
+ .name("number-guessing-game")
11
+ .description("A fun command-line number guessing game built with Node.js!")
12
+ .version("1.0.0");
13
+
14
+ program
15
+ .command("number-guessing-game")
16
+ .description("Start the number guessing game")
17
+ .option("-d, --difficulty [level]", "You have chosen difficulty level")
18
+ .action((option) => {
19
+ console.log("The game has started");
20
+ console.log(
21
+ "Welcome to the Number Guessing Game!\nI'm thinking of a number between 1 and 100.",
22
+ );
23
+ const { difficulty } = option;
24
+ if (difficulty === "easy") {
25
+ easy();
26
+ } else if (difficulty === "medium") {
27
+ medium();
28
+ } else if (difficulty === "hard") {
29
+ hard();
30
+ } else {
31
+ start();
32
+ }
33
+ });
34
+
35
+ program.parse(process.argv);
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@hardik_raj_baral/number-guessing-game",
3
+ "version": "1.0.0",
4
+ "description": "A fun command-line number guessing game built with Node.js!",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "bin": {
10
+ "number-guessing-game": "./index.js",
11
+ "ngg":"./index.js"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/HardikRajBaral/number-guessing-game.git"
16
+ },
17
+ "keywords": [],
18
+ "author": "",
19
+ "license": "ISC",
20
+ "type": "module",
21
+ "bugs": {
22
+ "url": "https://github.com/HardikRajBaral/number-guessing-game/issues"
23
+ },
24
+ "homepage": "https://github.com/HardikRajBaral/number-guessing-game#readme",
25
+ "dependencies": {
26
+ "commander": "^14.0.3",
27
+ "inquirer": "^8.2.7"
28
+ }
29
+ }