From d8b409d162a1c6bbc8ef56692f2b59f0b88d012e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADgia=20Alves?= Date: Mon, 27 Oct 2025 00:30:31 -0300 Subject: [PATCH 1/7] Add RockPaperScissors game implementation (Jokenpo) --- .../puzzlesandgames/RockPaperScissors.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java diff --git a/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java b/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java new file mode 100644 index 000000000000..9ff0eca2ffa5 --- /dev/null +++ b/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java @@ -0,0 +1,70 @@ +package com.thealgorithms.puzzlesandgames; + +import java.util.Random; +import java.util.Scanner; + +/** + * RockPaperScissors (Jokenpô) + * Simple terminal game where the user plays Rock, Paper, Scissors against the computer. + * Rules: + * - Rock beats Scissors + * - Scissors beats Paper + * - Paper beats Rock + * Example: + * User: rock + * Computer: scissors + * Result: You win! + * Author: Lígia Alves (Hacktoberfest 2025) + */ +public class RockPaperScissors { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + Random random = new Random(); + + String[] options = {"rock", "paper", "scissors"}; + + System.out.println("=== Rock Paper Scissors Game ==="); + System.out.println("Type 'rock', 'paper', or 'scissors'. Type 'exit' to quit."); + + while (true) { + System.out.print("\nYour choice: "); + String userChoice = scanner.nextLine().trim().toLowerCase(); + + if (userChoice.equals("exit")) { + System.out.println("Thanks for playing! "); + break; + } + + // Validate input + if (!userChoice.equals("rock") && !userChoice.equals("paper") && !userChoice.equals("scissors")) { + System.out.println("Invalid choice! Try again."); + continue; + } + + // Computer chooses randomly + String computerChoice = options[random.nextInt(3)]; + System.out.println("Computer chose: " + computerChoice); + + // Determine result + String result = getResult(userChoice, computerChoice); + System.out.println(result); + } + + scanner.close(); + } + + private static String getResult(String userChoice, String computerChoice) { + if (userChoice.equals(computerChoice)) { + return "It's a tie!"; + } else if ( + (userChoice.equals("rock") && computerChoice.equals("scissors")) || + (userChoice.equals("scissors") && computerChoice.equals("paper")) || + (userChoice.equals("paper") && computerChoice.equals("rock")) + ) { + return "You win! :D "; + } else { + return "You lose! :( "; + } + } +} \ No newline at end of file From 8697b53b0f3e6499f87fb6bde9f670aef349df0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADgia=20Alves?= Date: Mon, 27 Oct 2025 00:41:35 -0300 Subject: [PATCH 2/7] Add Wikipedia reference comment to RockPaperScissors.java --- .../com/thealgorithms/puzzlesandgames/RockPaperScissors.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java b/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java index 9ff0eca2ffa5..b748a6da68d2 100644 --- a/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java +++ b/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java @@ -4,6 +4,7 @@ import java.util.Scanner; /** + * * RockPaperScissors (Jokenpô) * Simple terminal game where the user plays Rock, Paper, Scissors against the computer. * Rules: From 2c01e3b9306593d694e87de90ae55262632714e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADgia=20Alves?= Date: Mon, 27 Oct 2025 15:15:13 -0300 Subject: [PATCH 3/7] chore: format RockPaperScissors.java with clang-format --- .../puzzlesandgames/RockPaperScissors.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java b/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java index b748a6da68d2..567705664696 100644 --- a/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java +++ b/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java @@ -58,14 +58,9 @@ public static void main(String[] args) { private static String getResult(String userChoice, String computerChoice) { if (userChoice.equals(computerChoice)) { return "It's a tie!"; - } else if ( - (userChoice.equals("rock") && computerChoice.equals("scissors")) || - (userChoice.equals("scissors") && computerChoice.equals("paper")) || - (userChoice.equals("paper") && computerChoice.equals("rock")) - ) { + } else if ((userChoice.equals("rock") && computerChoice.equals("scissors")) || (userChoice.equals("scissors") && computerChoice.equals("paper")) || (userChoice.equals("paper") && computerChoice.equals("rock"))) { return "You win! :D "; - } else { + } else return "You lose! :( "; - } } -} \ No newline at end of file +} From 665142a9df2457f10935b7e65ecaf1cce949bf40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADgia=20Alves?= Date: Mon, 27 Oct 2025 15:40:01 -0300 Subject: [PATCH 4/7] chore: Add getRandomChoice method for test compatibility --- .../puzzlesandgames/RockPaperScissors.java | 7 ++++- .../RockPaperScissorsTest.java | 28 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/thealgorithms/puzzlesandgames/RockPaperScissorsTest.java diff --git a/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java b/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java index 567705664696..de27ca3c104d 100644 --- a/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java +++ b/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java @@ -55,7 +55,7 @@ public static void main(String[] args) { scanner.close(); } - private static String getResult(String userChoice, String computerChoice) { + public static String getResult(String userChoice, String computerChoice) { if (userChoice.equals(computerChoice)) { return "It's a tie!"; } else if ((userChoice.equals("rock") && computerChoice.equals("scissors")) || (userChoice.equals("scissors") && computerChoice.equals("paper")) || (userChoice.equals("paper") && computerChoice.equals("rock"))) { @@ -63,4 +63,9 @@ private static String getResult(String userChoice, String computerChoice) { } else return "You lose! :( "; } + public static String getRandomChoice() { + String[] options = {"rock", "paper", "scissors"}; + Random random = new Random(); + return options[random.nextInt(options.length)]; + } } diff --git a/src/test/java/com/thealgorithms/puzzlesandgames/RockPaperScissorsTest.java b/src/test/java/com/thealgorithms/puzzlesandgames/RockPaperScissorsTest.java new file mode 100644 index 000000000000..a2307e673094 --- /dev/null +++ b/src/test/java/com/thealgorithms/puzzlesandgames/RockPaperScissorsTest.java @@ -0,0 +1,28 @@ +package com.thealgorithms.puzzlesandgames; +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class RockPaperScissorsTest { + + @Test + void testGetResultTie() { + assertEquals("It's a tie!", RockPaperScissors.getResult("rock", "rock")); + } + + @Test + void testGetResultWin() { + assertEquals("You win! :D ", RockPaperScissors.getResult("rock", "scissors")); + } + + @Test + void testGetResultLose() { + assertEquals("You lose! :( ", RockPaperScissors.getResult("rock", "paper")); + } + + @Test + void testGetRandomChoiceIsValid() { + String choice = RockPaperScissors.getRandomChoice(); + assertTrue(choice.equals("rock") || choice.equals("paper") || choice.equals("scissors")); + } +} From 85ef422db775f015d4ede4a5aed6dccac0ea6562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADgia=20Alves?= Date: Mon, 27 Oct 2025 15:52:28 -0300 Subject: [PATCH 5/7] style: fix Checkstyle violations (utility constructor, braces, imports) --- .../thealgorithms/puzzlesandgames/RockPaperScissors.java | 7 ++++++- .../puzzlesandgames/RockPaperScissorsTest.java | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java b/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java index de27ca3c104d..144c007c5b88 100644 --- a/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java +++ b/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java @@ -19,6 +19,10 @@ */ public class RockPaperScissors { + private RockPaperScissors() { + throw new UnsupportedOperationException("Utility class"); + } + public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Random random = new Random(); @@ -60,8 +64,9 @@ public static String getResult(String userChoice, String computerChoice) { return "It's a tie!"; } else if ((userChoice.equals("rock") && computerChoice.equals("scissors")) || (userChoice.equals("scissors") && computerChoice.equals("paper")) || (userChoice.equals("paper") && computerChoice.equals("rock"))) { return "You win! :D "; - } else + } else { return "You lose! :( "; + } } public static String getRandomChoice() { String[] options = {"rock", "paper", "scissors"}; diff --git a/src/test/java/com/thealgorithms/puzzlesandgames/RockPaperScissorsTest.java b/src/test/java/com/thealgorithms/puzzlesandgames/RockPaperScissorsTest.java index a2307e673094..14f86cecb85d 100644 --- a/src/test/java/com/thealgorithms/puzzlesandgames/RockPaperScissorsTest.java +++ b/src/test/java/com/thealgorithms/puzzlesandgames/RockPaperScissorsTest.java @@ -1,5 +1,6 @@ package com.thealgorithms.puzzlesandgames; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; From b26e3a4363806d799cb377b67f27fd6b92990e7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADgia=20Alves?= Date: Mon, 27 Oct 2025 16:38:39 -0300 Subject: [PATCH 6/7] chore: Add final function in RockPaperScissors class to pass Checkstyle --- .../com/thealgorithms/puzzlesandgames/RockPaperScissors.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java b/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java index 144c007c5b88..d138ad0893d4 100644 --- a/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java +++ b/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java @@ -17,7 +17,7 @@ * Result: You win! * Author: Lígia Alves (Hacktoberfest 2025) */ -public class RockPaperScissors { +public final class RockPaperScissors { private RockPaperScissors() { throw new UnsupportedOperationException("Utility class"); From 2534e08ee4475d56d7e9d3137158efdb394da33c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADgia=20Alves?= Date: Mon, 27 Oct 2025 22:24:50 -0300 Subject: [PATCH 7/7] chore:Remove main method for PMD and project style compliance --- .../puzzlesandgames/RockPaperScissors.java | 37 +------------------ 1 file changed, 1 insertion(+), 36 deletions(-) diff --git a/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java b/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java index d138ad0893d4..716a26d48f80 100644 --- a/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java +++ b/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java @@ -23,42 +23,6 @@ private RockPaperScissors() { throw new UnsupportedOperationException("Utility class"); } - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - Random random = new Random(); - - String[] options = {"rock", "paper", "scissors"}; - - System.out.println("=== Rock Paper Scissors Game ==="); - System.out.println("Type 'rock', 'paper', or 'scissors'. Type 'exit' to quit."); - - while (true) { - System.out.print("\nYour choice: "); - String userChoice = scanner.nextLine().trim().toLowerCase(); - - if (userChoice.equals("exit")) { - System.out.println("Thanks for playing! "); - break; - } - - // Validate input - if (!userChoice.equals("rock") && !userChoice.equals("paper") && !userChoice.equals("scissors")) { - System.out.println("Invalid choice! Try again."); - continue; - } - - // Computer chooses randomly - String computerChoice = options[random.nextInt(3)]; - System.out.println("Computer chose: " + computerChoice); - - // Determine result - String result = getResult(userChoice, computerChoice); - System.out.println(result); - } - - scanner.close(); - } - public static String getResult(String userChoice, String computerChoice) { if (userChoice.equals(computerChoice)) { return "It's a tie!"; @@ -68,6 +32,7 @@ public static String getResult(String userChoice, String computerChoice) { return "You lose! :( "; } } + public static String getRandomChoice() { String[] options = {"rock", "paper", "scissors"}; Random random = new Random();