|
| 1 | +package Day02; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +/** |
| 7 | + * @author Inna Pirina |
| 8 | + * @since 02.12.2022 |
| 9 | + */ |
| 10 | +public class RockPaperScissorer { |
| 11 | + |
| 12 | + private final Map<String, Integer> elfScores = Map.of("A", 1, "B", 2, "C", 3); |
| 13 | + private final Map<String, Integer> myScores = Map.of("X", 1, "Y", 2, "Z", 3); |
| 14 | + |
| 15 | + private final Map<String, Integer> winningStrategy = Map.of("A", 2, "B", 3, "C", 1); |
| 16 | + private final Map<String, Integer> losingStrategy = Map.of("A", 3, "B", 1, "C", 2); |
| 17 | + |
| 18 | + private static final int WIN_SCORE = 6; |
| 19 | + private static final int DRAW_SCORE = 3; |
| 20 | + |
| 21 | + private static final String WIN = "Z"; |
| 22 | + private static final String DRAW = "Y"; |
| 23 | + private static final String LOSE = "X"; |
| 24 | + |
| 25 | + public int playRockPaperScissors(List<String> rounds) { |
| 26 | + int myTotalScore = 0; |
| 27 | + for (String round : rounds) { |
| 28 | + String[] deals = round.trim().split(" "); |
| 29 | + int elf = elfScores.get(deals[0]); |
| 30 | + int me = myScores.get(deals[1]); |
| 31 | + myTotalScore += me; |
| 32 | + |
| 33 | + int difference = Math.abs(elf - me); |
| 34 | + if ((me > elf && difference == 1) || (elf > me && difference == 2)) { |
| 35 | + myTotalScore += WIN_SCORE; |
| 36 | + } else if (me == elf) { |
| 37 | + myTotalScore += DRAW_SCORE; |
| 38 | + } |
| 39 | + } |
| 40 | + return myTotalScore; |
| 41 | + } |
| 42 | + |
| 43 | + public int playPredefinedRockPaperScissors(List<String> rounds) { |
| 44 | + int myTotalScore = 0; |
| 45 | + for (String round : rounds) { |
| 46 | + String[] deals = round.trim().split(" "); |
| 47 | + String elf = deals[0]; |
| 48 | + String me = deals[1]; |
| 49 | + |
| 50 | + switch (me) { |
| 51 | + case WIN -> myTotalScore += WIN_SCORE + winningStrategy.get(elf); |
| 52 | + case DRAW -> myTotalScore += DRAW_SCORE + elfScores.get(elf); |
| 53 | + case LOSE -> myTotalScore += losingStrategy.get(elf); |
| 54 | + default -> System.out.println("OH No!"); |
| 55 | + } |
| 56 | + } |
| 57 | + return myTotalScore; |
| 58 | + } |
| 59 | +} |
0 commit comments