public class CSVGameData extends GameData
Reads the game data from CSV files, and loads it into the protected methods in GameData. While most the game will just assume GameData this class is essential, as it defines the file format GameData is reading. Using this design, the game could easily be extended to read different data formats (such as JSONGameData, BinaryGameData, etc).
Constructor Details
~CSVGameData public CSVGameData(String gamedata, String saveData) Constructs the CSVGameData object, by loading CSV files passed into it. Use the protected methods (yes, this can be a two line constructor).
loadGameData(gamedata);
loadSaveData(saveData);
For testing, we will often construct CSVGameData with different files than we provided - same format, just different numbers of knights, mobs, fortunes, etc. Parameters: gamedata - A game data file containing fortunes and MOBS saveData - A data file containing knights
Method Details
~loadSaveData void loadSaveData(String saveData) Loads in the data from a knights CSV file. Constructs a new Knight and adds it to the Knight List GameData.knights. Starts a counter for the IDs, with each new knight being assigned an ID in order of which they are read from the file While there are many ways to implement this method, but with that said, we wanted provide a skeleton of what we used.
int counter = 0;
Scanner file = readFile(saveData);
if(file == null) return;
while(file.hasNextLine()) {
Scanner line = new Scanner(file.nextLine());
line.useDelimiter(",");
Knight kt = new Knight(
++counter,
line.next().trim(),
line.nextInt(),
line.nextInt(),
line.nextInt(),
DiceType.valueOf(line.next()),
line.nextInt());
knights.add(kt);
}
~DiceType.valueOf was a method that was automatically generated for you as an enum. It takes in a string that looks *EXACTLY* like the enum, and converts it to the enum, so "D4" becomes DiceType.D4 Parameters: saveData - a file containing knight information
~readFile private Scanner readFile(String fileName) Optional method, but we used it so that we could isolate our try/catch statements to a single method. Parameters: fileName - name of file to be read into a scanner Returns: A scanner loaded with a FileInputStream
~loadGameData void loadGameData(String gamedata) Loads game data based on fortunes or MOBs. first line of the CSV file determines type, the rest loads directly into a MOB or Fortune object. Stores MOBs in GameData.monsters and fortunes into GameData.fortunes. Any line that doesn't start with MOB or FORTUNE is ignored. As a reminder, a gamedata.csv file could look like the following: MOB,Kobold,25,9,-1,D4
MOB,Umber Hulk,55,13,1,D6
FORTUNE,Prowess,0,0,2,D12
FORTUNE,Valor,10,0,0,-
With the pattern matching name,hp,armor,hitModifier,damage. Make sure to examine the code provided for loadSaveData() Parameters: gamedata - a game data CSV file with MOBs and Fortunes
~parseGameDataLine private void parseGameDataLine(Scanner line) Optional helper method. We used this method to handle the actual parsing of each line of the gamedata.csv. You may also further break this up into two private methods, one for MOB and one for FORTUNE lines. That could help keep your code concise.
~save public void save(String filename) Saves out the knight data as a CSV to the given filename. Simply loop through every knight, and print out the result of toCSV() for each knight! Specified by: save in class GameData Parameters: filename - name of file to save knights out to