-
Notifications
You must be signed in to change notification settings - Fork 0
/
HierarchyChecker.java
47 lines (45 loc) · 1.97 KB
/
HierarchyChecker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package RockPaperScissors;
public class HierarchyChecker {
/**
* Checks to see who won a match by using the rock, paper, scissors hierarchy:
* <p>-Rock beats scissors
* <p>-Scissors beat paper
* <p>-Paper beats rock
* @param playerOne a String that represents the object chosen by playerOne or the user
* ("r" for rock, "p" for paper, "s" for scissors)
* @param playerTwo a String that represents the object chosen by playerTwo or generated by
* the npc ("r" for rock, "p" for paper, "s" for scissors)
* @return a boolean that represents who won (true for playerOne/user, false for playerTwo/npc)
*/
public static boolean whoWon(String playerOne, String playerTwo) {
switch (playerOne) {
//if user entered rock:
case "r" : {
//if the npc entered paper: they win, if the npc entered scissors: they lose
switch (playerTwo) {
case "p" : return false; //npc won
case "s" : return true; //user won
}
}
//if the user entered paper
case "p" : {
//if the npc entered rock: the lose, if the npc entered scissors: they win
switch (playerTwo) {
case "r" : return true; //user won
case "s" : return false; //npc won
}
}
//if the user entered scissors
case "s" : {
//if the npc entered rock: they win, if the npc entered paper: they lose
switch (playerTwo) {
case "r" : return false; //npc won
case "p" : return true; //user won
}
}
}
//this return statement will not be reached because playerOne/user's object has been
//checked and is valid and playerTwo/npc's object has been checked and is valid
return false;
}
}