Class GameData
Object
GameData
- Direct Known Subclasses:
CSVGameData
GameData handles the data for the game. It is abstract, as a child class needs to define how that
data is going to be loaded into the data objects, but most of the data objects
can run independently of the the child class. The rest of the game will
assume the GameData class only.
-
Field Summary
FieldsModifier and TypeFieldDescriptionList of the active knights, they are references, not copies.List of fortunes.List of all the knights availableprivate static final int
the max number of active knights (defined by specifications to be 4)List of MOBs/Monstersprivate static final Random
Random number generator, used for grabbing random items for the structures. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected Knight
findKnight
(String nameOrId, List<Knight> list) Finds a knight based on nameOrId based on the a List of knights passed into it.Gets an active knight based on a string or id.Returns list of knights currently set as active.Gets an knight from the all knights list based on a string or id.Returns all knights.Gets a random fortune fromfortunes
Since fortunes.size() gives you the total fortunes, and random.nextInt(N) gives you a random number between 0-(N-1), combine them!Gets a random monster frommonsters
assuming the max number of monsters is less than or equal to activeKnights.size().getRandomMonsters
(int number) Builds a list of random monsters of size number.void
removeActive
(Knight kt) Removes a knight from theactiveKnights
list and resets the damage on the knight! Remember, list.remove returns true if the remove was successful.abstract void
Required for the implementing class to be able to save the fileboolean
Adds a knight to theactiveKnights
list, as long as there are no more than 4 knights in the list.
-
Field Details
-
MAX_ACTIVE
private static final int MAX_ACTIVEthe max number of active knights (defined by specifications to be 4)- See Also:
-
random
Random number generator, used for grabbing random items for the structures. For example, grabbing a random fortune would befortunes.get(random.nextInt(fortunes.size()))
-
fortunes
List of fortunes. -
monsters
List of MOBs/Monsters -
knights
List of all the knights available -
activeKnights
List of the active knights, they are references, not copies.
-
-
Constructor Details
-
GameData
public GameData()
-
-
Method Details
-
getKnights
Returns all knights.- Returns:
- all knights stored in
knights
-
getActiveKnights
Returns list of knights currently set as active.- Returns:
- Essentially returns
activeKnights
-
getActive
Gets an active knight based on a string or id. The string can be word in the knights name, and will return the first knight that it comes across that matches that string. The id is supposed to be unique, and will find the knight with that id, immediately returning the knight. Uses findKnight to accomplish the task.- Parameters:
nameOrId
- string or ID as a string- Returns:
- the active knight if it exists, or null if it is not found
- See Also:
-
getKnight
Gets an knight from the all knights list based on a string or id. The string can be word in the knights name, and will return the first knight that it comes across that matches that string. The id is supposed to be unique, and will find the knight with that idea, immediately returning the knight. Uses findKnight to accomplish the task.- Parameters:
nameOrId
- string or ID as a string- Returns:
- the knight if it exists, or null if it is not found
- See Also:
-
findKnight
Finds a knight based on nameOrId based on the a List of knights passed into it. The name can be any part of the name (contains), but the ID must exactly match. Case does not matter for names. Note for students: getId() returns an Integer (not int), so you can call toString, and just compare Strings. That is valid, no need to parse.- Parameters:
nameOrId
- a name or id stringlist
- the list of knights to search - oftenknights
oractiveKnights
- Returns:
- the single knight if found, or null if not found.
- See Also:
-
setActive
Adds a knight to theactiveKnights
list, as long as there are no more than 4 knights in the list.- Parameters:
kt
- knight to add- Returns:
- true if the addition was successful, false if the knight wasn't added due to too many knights already being in the list
-
removeActive
Removes a knight from theactiveKnights
list and resets the damage on the knight! Remember, list.remove returns true if the remove was successful.- Parameters:
kt
- knight to remove- See Also:
-
getRandomFortune
Gets a random fortune fromfortunes
Since fortunes.size() gives you the total fortunes, and random.nextInt(N) gives you a random number between 0-(N-1), combine them!- Returns:
- a Fortune from the fortunes list
-
getRandomMonsters
Gets a random monster frommonsters
assuming the max number of monsters is less than or equal to activeKnights.size(). Careful about an OB1 error here!- Returns:
- a list of MOBs no greater than activeKnights.size()
-
getRandomMonsters
Builds a list of random monsters of size number. Note, that monsters should be copied into the return List, so they can be modified individually.- Parameters:
number
- the number of monsters to randomly grab and copy- Returns:
- a list of MOB/monsters (copies)
- See Also:
-
save
Required for the implementing class to be able to save the file- Parameters:
filename
- name of file to save
-