From 14b3b2bb74ef8b471ca9b80d46052c3010ac6c07 Mon Sep 17 00:00:00 2001 From: jenyyy4 Date: Wed, 8 Oct 2025 16:50:07 +0530 Subject: [PATCH 1/5] added NumberGuess Game --- .../puzzlesandgames/NumberGuess.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java diff --git a/src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java b/src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java new file mode 100644 index 000000000000..555f72b61447 --- /dev/null +++ b/src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java @@ -0,0 +1,48 @@ +package com.thealgorithms.puzzlesandgames; +import java.util.Scanner; + +public class NumberGuess { + public static void playGame() { + Scanner sc = new Scanner(System.in); + int number = (int)(Math.random() * 100) + 1; + int guess = 0, tries = 0; + + System.out.println("🎯 Welcome to the Number Guessing Game!"); + System.out.println("I've picked a number between 1 and 100. Try to guess it!\n"); + + while (true) { + System.out.print("Enter your guess: "); + + if (!sc.hasNextInt()) { + System.out.println("Please enter a valid number."); + sc.next(); + continue; + } + + guess = sc.nextInt(); + tries++; + + if (guess < 1 || guess > 100) { + System.out.println("⚠️ Please guess a number between 1 and 100."); + continue; + } + + if (guess < number) + System.out.println("Too low! 📉"); + else if (guess > number) + System.out.println("Too high! 📈"); + else { + System.out.println("🎉 Correct! The number was " + number + "."); + System.out.println("You took " + tries + " tries."); + break; + } + } + + sc.close(); + } + + public static void main(String[] args) { + playGame(); + System.out.println("\nThanks for playing! 👋"); + } +} \ No newline at end of file From 6c9d83f53b3349d9bab0dc9c6232fd15d32e06bd Mon Sep 17 00:00:00 2001 From: jenyyy4 Date: Wed, 8 Oct 2025 16:54:37 +0530 Subject: [PATCH 2/5] added url of algos and formatted --- .../thealgorithms/puzzlesandgames/NumberGuess.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java b/src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java index 555f72b61447..3f5eda5db08e 100644 --- a/src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java +++ b/src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java @@ -1,10 +1,16 @@ +/** + * Simple Number Guessing Game. + * The player guesses a random number between 1 and 100. + * Reference: https://en.wikipedia.org/wiki/Guess_the_number + */ + package com.thealgorithms.puzzlesandgames; import java.util.Scanner; public class NumberGuess { public static void playGame() { Scanner sc = new Scanner(System.in); - int number = (int)(Math.random() * 100) + 1; + int number = (int) (Math.random() * 100) + 1; int guess = 0, tries = 0; System.out.println("🎯 Welcome to the Number Guessing Game!"); @@ -12,7 +18,7 @@ public static void playGame() { while (true) { System.out.print("Enter your guess: "); - + if (!sc.hasNextInt()) { System.out.println("Please enter a valid number."); sc.next(); @@ -45,4 +51,4 @@ public static void main(String[] args) { playGame(); System.out.println("\nThanks for playing! 👋"); } -} \ No newline at end of file +} From 0d03c2d9c4ea50c4acb9f905d7908e0c8fb9480e Mon Sep 17 00:00:00 2001 From: jenyyy4 Date: Wed, 8 Oct 2025 17:01:27 +0530 Subject: [PATCH 3/5] fixed checkstyle --- .../puzzlesandgames/NumberGuess.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java b/src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java index 3f5eda5db08e..671241982da0 100644 --- a/src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java +++ b/src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java @@ -7,11 +7,16 @@ package com.thealgorithms.puzzlesandgames; import java.util.Scanner; -public class NumberGuess { +public final class NumberGuess { + private NumberGuess() { + throw new AssertionError("Cannot instantiate NumberGuess"); + } + public static void playGame() { Scanner sc = new Scanner(System.in); int number = (int) (Math.random() * 100) + 1; - int guess = 0, tries = 0; + int guess = 0; + int tries = 0; System.out.println("🎯 Welcome to the Number Guessing Game!"); System.out.println("I've picked a number between 1 and 100. Try to guess it!\n"); @@ -33,10 +38,12 @@ public static void playGame() { continue; } - if (guess < number) + if (guess < number) { System.out.println("Too low! 📉"); - else if (guess > number) + } + else if (guess > number) { System.out.println("Too high! 📈"); + } else { System.out.println("🎉 Correct! The number was " + number + "."); System.out.println("You took " + tries + " tries."); From 7623de91fe1bec6836dea9c855e4da68237bfbfb Mon Sep 17 00:00:00 2001 From: jenyyy4 Date: Wed, 8 Oct 2025 17:03:29 +0530 Subject: [PATCH 4/5] formatted --- .../java/com/thealgorithms/puzzlesandgames/NumberGuess.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java b/src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java index 671241982da0..fdb3a1bc32e1 100644 --- a/src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java +++ b/src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java @@ -40,11 +40,9 @@ public static void playGame() { if (guess < number) { System.out.println("Too low! 📉"); - } - else if (guess > number) { + } else if (guess > number) { System.out.println("Too high! 📈"); - } - else { + } else { System.out.println("🎉 Correct! The number was " + number + "."); System.out.println("You took " + tries + " tries."); break; From b549dbca171d89b8443e82343723c9bf550ed81d Mon Sep 17 00:00:00 2001 From: jenyyy4 Date: Wed, 8 Oct 2025 17:09:17 +0530 Subject: [PATCH 5/5] removed main --- .../java/com/thealgorithms/puzzlesandgames/NumberGuess.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java b/src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java index fdb3a1bc32e1..a7f3695e2760 100644 --- a/src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java +++ b/src/main/java/com/thealgorithms/puzzlesandgames/NumberGuess.java @@ -51,9 +51,4 @@ public static void playGame() { sc.close(); } - - public static void main(String[] args) { - playGame(); - System.out.println("\nThanks for playing! 👋"); - } }