-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRPSArena.java
More file actions
143 lines (131 loc) · 5.02 KB
/
RPSArena.java
File metadata and controls
143 lines (131 loc) · 5.02 KB
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
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;
/**
* Lab 8
*
* Class that simulates a battle arena for a Rock-Paper-Scissors tournament.
* The user can print out information about all the Contestants, or can pit
* two Contestants against each other to see who would win. Victories are determined
* by what hand choices the Contestants have.
*
* @author Stephen
* @version 2018-03-12
*/
public class RPSArena
{
/**
* Maps short strings (abbreviations) to members of the Contestant enumeration.
* We use LinkedHashMap to preserve ordering of inputs, which HashMap does
* not do by default. In most other ways, however, this Map works pretty much
* the same as a normal HashMap.
*/
private LinkedHashMap<String, Contestant> CONTESTANT_MAP;
/**
* Constructor: Creates a predetermined set of Contestants. We choose to create
* one Contestant for each entry in the Contestant enum.
* The abbreviations should be:
* -"GEO": george
* -"JIL": jill
* -"MAT": matthew
* -"BET": betty
*/
public RPSArena()
{
// TODO: implement this.
// Initialize the CONTESTANT_MAP
CONTESTANT_MAP = new LinkedHashMap<String, Contestant>();
// Populate the CONTESTANT_MAP
CONTESTANT_MAP.put("GEO", Contestant.GEORGE);
CONTESTANT_MAP.put("JIL", Contestant.JILL);
CONTESTANT_MAP.put("MAT", Contestant.MATTHEW);
CONTESTANT_MAP.put("BET", Contestant.BETTY);
}
/**
* Attempt to get a hero from contestantMap by using the abbreviated Contestant name.
*
* @param key The abbreviation for the Contestant in the contestantMap.
* @return The Contestant affiliated with the abbreviated name if the key exists in the contestantMap.
* Null otherwise.
*/
public Contestant getContestant(String key)
{
return CONTESTANT_MAP.get(key);
}
/**
* Gets the abbreviated names of the Contestant.
*
* It may be helpful to look at the java documentation for LinkedHashMaps here:
* https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html
*
* @return the abbreviations for the Contestants (keyset of contestantMap).
*/
public Set<String> getContestantAbbreivations()
{
return CONTESTANT_MAP.keySet();
}
/**
* Print out a Contestant's three-letter abbreviation and the Contestant's description for all contestants that
* have the same hand choice as the choice specified.
*
* If you are having issues looping over a map:
* https://www.geeksforgeeks.org/iterate-map-java/
*
* @param choice The hand choice for which we want to print out Contestants choosing the same hand choice.
* @return The list of select Contestants as a string. Order is consistent across calls.
* Format is: "<Abbreviation> - <Contestant Description>", using a new line after each entry.
* If no contestants have the given hand choice, an empty string is returned.
*/
public String listContestantsWithHandChoice(HandChoice choice)
{
String ret = "";
for(Entry<String, Contestant> entry : CONTESTANT_MAP.entrySet())
{
Contestant player = entry.getValue();
if(player.getChoice().equals(choice))
{
ret += String.format("%s - %s\n", entry.getKey(), player.toString());
}
//else
//{
// return null;
//}
}
return ret;
}
/**
* Battles two Contestants against each other. A victor is determined by Hand Choice. Specifically,
* if ContestantA's hand choice wins against ContestantB's hand choice, HeroA will win the battle.
* If the hand choices are the same type, the battle will end in a tie.
*
* We can assert that if choice A wins choice B, then choice B loses choice type A.
* We can assert that choice A and choice B cannot be defeat each other.
*
* @param contestantA The first Contestant in the battle.
* @param contestantB The second Contestant in the battle.
* @return The Contestant that wins the battle, null if the Contestants tie.
*/
public static Contestant battleContestants(Contestant contestantA, Contestant contestantB)
{
// TODO: implement this.
// Get the Contestant choices:
HandChoice choiceOfContestantA = contestantA.getChoice();
HandChoice choiceOfContestantB = contestantB.getChoice();
// Check to see if ContestantA's choice type wins against ContestantB's:
if(choiceOfContestantA.winsAgainst().equals(choiceOfContestantB))
{
return contestantA;
}
// Check to see if ContestantB's choice type wins against ContestantA's:
if(choiceOfContestantB.winsAgainst().equals(choiceOfContestantA))
{
return contestantB;
}
//Tie otherwise (includes choices that are the same):
else
{
return null;
}
}
}