-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPSSL.java
147 lines (103 loc) · 5.38 KB
/
RPSSL.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import java.util.*;
public class RPSSL {
private enum GameCharacter {ROCK, PAPER, SCISSORS, SPOCK, LIZARD};
private static class Rule {
private GameCharacter gameCharacter;
private Set<GameCharacter> beatsGameCharacters;
Rule(final GameCharacter gameCharacter, final Set<GameCharacter> beatsGameCharacters) {
this.gameCharacter = gameCharacter;
this.beatsGameCharacters = beatsGameCharacters;
}
}
private static Map<GameCharacter, Rule> gameCharacterToRule = new HashMap<>(GameCharacter.values().length);
private Map<GameCharacter, Integer> gameCharacterToWinCount = new HashMap<>(GameCharacter.values().length);
private Map<GameCharacter, Integer> gameCharacterToLossCount = new HashMap<>(GameCharacter.values().length);
private int numPlayers, numGames;
static {
final Rule rockRule = new Rule(GameCharacter.ROCK,
new HashSet<>(Arrays.asList(GameCharacter.LIZARD, GameCharacter.SCISSORS)));
final Rule paperRule = new Rule(GameCharacter.PAPER,
new HashSet<>(Arrays.asList(GameCharacter.ROCK, GameCharacter.SPOCK)));
final Rule scissorsRule = new Rule(GameCharacter.SCISSORS,
new HashSet<>(Arrays.asList(GameCharacter.LIZARD, GameCharacter.PAPER)));
final Rule spockRule = new Rule(GameCharacter.SPOCK,
new HashSet<>(Arrays.asList(GameCharacter.ROCK, GameCharacter.SCISSORS)));
final Rule lizardRule = new Rule(GameCharacter.LIZARD,
new HashSet<>(Arrays.asList(GameCharacter.PAPER, GameCharacter.SPOCK)));
gameCharacterToRule.put(GameCharacter.ROCK, rockRule);
gameCharacterToRule.put(GameCharacter.PAPER, paperRule);
gameCharacterToRule.put(GameCharacter.SCISSORS, scissorsRule);
gameCharacterToRule.put(GameCharacter.SPOCK, spockRule);
gameCharacterToRule.put(GameCharacter.LIZARD, lizardRule);
}
RPSSL(final int numPlayers, final int numGames) {
this.numPlayers = numPlayers;
this.numGames = numGames;
}
public void play() {
for (int i = 0; i < numGames; i++) {
final Set<GameCharacter> moves = generateMoves(numPlayers);
// Tie check
if (moves == null) continue;
final GameCharacter winner = findWinner(new HashSet<>(moves));
doBookkeeping(winner, moves);
}
}
public void printResults() {
for (final GameCharacter gameCharacter : GameCharacter.values()) {
final int winCount = gameCharacterToWinCount.containsKey(gameCharacter) ?
gameCharacterToWinCount.get(gameCharacter) : 0;
final int lossCount = gameCharacterToLossCount.containsKey(gameCharacter) ?
gameCharacterToLossCount.get(gameCharacter) : 0;
final int numPicks = winCount + lossCount;
final Integer winPercentage = (numPicks == 0) ? null : (winCount * 100) / (winCount + lossCount);
System.out.println(gameCharacter + " won " + winCount + " times, lost " + lossCount +
" times and win percentage is " + ((winPercentage == null) ? "NA" : winPercentage + "%"));
}
}
private static Set<GameCharacter> generateMoves(final int numMoves) {
final Set<GameCharacter> moves = new HashSet<>(numMoves);
for (int i = 0; i < numMoves; i++) {
final GameCharacter move = pickRandomGameCharacter();
// Tie check
if (moves.contains(move)) return null;
moves.add(move);
}
return moves;
}
private static GameCharacter findWinner(final Set<GameCharacter> gameCharacters) {
if (gameCharacters.isEmpty()) return null;
final Iterator<GameCharacter> iterator = gameCharacters.iterator();
final Set<GameCharacter> losers = new HashSet<>();
for (final GameCharacter gameCharacter : gameCharacters) {
losers.addAll(gameCharacterToRule.get(gameCharacter).beatsGameCharacters);
}
gameCharacters.removeAll(losers);
return gameCharacters.isEmpty() ? null : gameCharacters.iterator().next();
}
private static GameCharacter pickRandomGameCharacter() {
final GameCharacter[] gameCharacters = GameCharacter.values();
final int randomGameCharacterValue = new Random().nextInt(gameCharacters.length);
return gameCharacters[randomGameCharacterValue];
}
private void doBookkeeping(final GameCharacter winner, final Set<GameCharacter> gameCharacters) {
// Tie check
if (winner == null) return;
if (!gameCharacterToWinCount.containsKey(winner)) gameCharacterToWinCount.put(winner, 0);
final int winnerCount = gameCharacterToWinCount.get(winner);
gameCharacterToWinCount.put(winner, winnerCount + 1);
gameCharacters.remove(winner);
for (final GameCharacter loser : gameCharacters) {
if (!gameCharacterToLossCount.containsKey(loser)) gameCharacterToLossCount.put(loser, 0);
final int loserCount = gameCharacterToLossCount.get(loser);
gameCharacterToLossCount.put(loser, loserCount + 1);
}
}
public static void main(final String[] args) {
final int numPlayers = 2;
final int numGames = 10000;
final RPSSL rpssl = new RPSSL(numPlayers, numGames);
rpssl.play();
rpssl.printResults();
}
}