Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.thealgorithms.puzzlesandgames;

import java.util.Random;
import java.util.Scanner;

/**
* <a href="https://en.wikipedia.org/wiki/Rock_paper_scissors"></a>
* 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)];
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Loading