File tree Expand file tree Collapse file tree 2 files changed +34
-1
lines changed
main/java/com/thealgorithms/puzzlesandgames
test/java/com/thealgorithms/puzzlesandgames Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments