Skip to content

Commit 665142a

Browse files
committed
chore: Add getRandomChoice method for test compatibility
1 parent 2c01e3b commit 665142a

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/main/java/com/thealgorithms/puzzlesandgames/RockPaperScissors.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,17 @@ public static void main(String[] args) {
5555
scanner.close();
5656
}
5757

58-
private static String getResult(String userChoice, String computerChoice) {
58+
public static String getResult(String userChoice, String computerChoice) {
5959
if (userChoice.equals(computerChoice)) {
6060
return "It's a tie!";
6161
} else if ((userChoice.equals("rock") && computerChoice.equals("scissors")) || (userChoice.equals("scissors") && computerChoice.equals("paper")) || (userChoice.equals("paper") && computerChoice.equals("rock"))) {
6262
return "You win! :D ";
6363
} else
6464
return "You lose! :( ";
6565
}
66+
public static String getRandomChoice() {
67+
String[] options = {"rock", "paper", "scissors"};
68+
Random random = new Random();
69+
return options[random.nextInt(options.length)];
70+
}
6671
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.thealgorithms.puzzlesandgames;
2+
import static org.junit.jupiter.api.Assertions.*;
3+
4+
import org.junit.jupiter.api.Test;
5+
6+
class RockPaperScissorsTest {
7+
8+
@Test
9+
void testGetResultTie() {
10+
assertEquals("It's a tie!", RockPaperScissors.getResult("rock", "rock"));
11+
}
12+
13+
@Test
14+
void testGetResultWin() {
15+
assertEquals("You win! :D ", RockPaperScissors.getResult("rock", "scissors"));
16+
}
17+
18+
@Test
19+
void testGetResultLose() {
20+
assertEquals("You lose! :( ", RockPaperScissors.getResult("rock", "paper"));
21+
}
22+
23+
@Test
24+
void testGetRandomChoiceIsValid() {
25+
String choice = RockPaperScissors.getRandomChoice();
26+
assertTrue(choice.equals("rock") || choice.equals("paper") || choice.equals("scissors"));
27+
}
28+
}

0 commit comments

Comments
 (0)