Friday, December 5, 2008

Java: CharSi Update #11

Next step in this program was to transfer the flag code to the other Class files so that I can easily search on any item set and display that list in the item JList. Once that was done I did make a decision about the Weapon class file. Leaving the application the way it was would have been burdensome to find a particular item. So I bit the bullet and created massive if-else statements to incorporate the fifteen different weapon types in the code. Along with that I had to create a new GUI piece to allow the choice of which items to search under, leaving them all unselected to start with.

Once that was done it was time to start working on the true workhorse of this application, the ability to take the information from the text file and save it in the application to be added to the stat fields when needed. I elected to do this with a series of arrays and a single text file. Before I could do this however I had to alter the way some mods were written so that I could store them correctly. I made sure all variable ranges were encompasses by parentheses () and these were the only values that had such characters in their particular line. Same goes for the set items, {} brackets were used to signify those items so they could be dealt with differently.

This ended up being a much larger project than I had originally anticipated, not that I though this piece would be small mind you. The main method for this process, StoreItemStats, was designed to take the text and deal with it one line at a time. Each line was compared to the possible mod text and if it matched then the appropriate array field was updated with the value provided in the text. Sounds simple? Well not quite that easy.

First there are a ton of mods to deal with so a couple hundred String variables had to be created for the comparison. Then several arrays were created, each for a different type of mod so they could be identified easier, such as modsOfNote and stats. Once a mod was identified then it had to be determined if the mod was variable or not.

else if(currentString.contains(ML)){
if(currentString.contains(RANGE)){
modsOfNote[7] = DetermineRangeValue(currentString, "Mana stolen per hit");
}else
modsOfNote[7] = Integer.parseInt(currentString.substring(0,currentString.indexOf("%")));
}

The above is a call to see if the mod Mana Leech is present. If the line of text contains a "(" character then it is a range then the method DetermineRangeValue is called, as the program needs to determine what the exact value of this mod should be. That is where the user comes into play. The user will be provided a dialogue box to choose the desired value, then that value is stored in the appropriate array slot. This seemed a much easier route to go than to have the Reroll and Perfect JButton/JCheckBox components do this in a more random fashion. This way the user can simply create whatever version of the item they want, from low end to perfect and anywhere in between. It means I'll have to remove those components in the near future and unclutter the GUI a bit.

The coding itself wasn't so difficult, it was the amount of data I needed to sort through that gave me a headache (and a stiff back). The giant If-Else statement ended up being 1820 lines of code, which basically quadrupled the size of the Class file with one method. Some of the If-Else statements had to be nested together as their text was too similar to distinguish easily. For example there are several mods that reference "fire damage" or simply "damage", so to ensure the right value got into the right array slot those mods were nested together.


Now that this piece of the code is written the application works pretty much like a giant item database. Easy to search for a particular item but not much else beyond that. However now that I have all of the mod information stored into appropriate arrays (for the Armor class at least), the rest should go a bit quicker. I plan on working only on the Armor class for now so that I can get that one pretty much completed before transferring all of that code to the other defensive equipment classes. So along that train of thought, my next step will be to get the JList for equipped items working and ensure that I can't accidentally equip more than one item at a time. That and I need to get the Remove Item JButton working as well as remove the components that aren't necessary anymore.

No comments: