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..716a26d48f80 --- /dev/null +++ b/src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java @@ -0,0 +1,41 @@ +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 final class RockPaperScissors { + + private RockPaperScissors() { + throw new UnsupportedOperationException("Utility class"); + } + + 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"))) { + return "You win! :D "; + } 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..14f86cecb85d --- /dev/null +++ b/src/test/java/com/thealgorithms/puzzlesandgames/RockPaperScissorsTest.java @@ -0,0 +1,29 @@ +package com.thealgorithms.puzzlesandgames; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +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")); + } +}