Sunday, October 4, 2015

Better Arrays mean Easier coding

When I started this game I knew I needed strings. When you use one object on another object I wanted to supply either some useful information, or some comical sentence. This mean't that for every object in the game I would need to have a corresponding array that would contain the Text for the two objects.

I did this by creating a 2 dimensional array. Very easy for sure. And here is part of it....

 public static sUseStringsTable[,] CompleteUseStrings = new sUseStringsTable[,] {
  { // gold
   new sUseStringsTable(false, ""), //GOLD
   new sUseStringsTable(false, ""), //SILVER,
   new sUseStringsTable(false, ""), //AXE,
   new sUseStringsTable(false, ""), //SLEDGEHAMMER,
   new sUseStringsTable(false, ""), //SHOVEL,
   new sUseStringsTable(false, ""), //HOUSE_KEY,
   new sUseStringsTable(false, ""), //WATERINGCAN,
   new sUseStringsTable(false, ""), //E_KEY,
   new sUseStringsTable(false, ""), //PICKER
   // start consumables
   new sUseStringsTable(false, ""), //APPLE,
   new sUseStringsTable(false, ""), //WOOD,
   new sUseStringsTable(false, ""), //STONE,
   new sUseStringsTable(false, ""), //WATER,
   new sUseStringsTable(false, ""), //MUSHROOM,
   new sUseStringsTable(false, ""), //tulip
   new sUseStringsTable(false, ""), //daffodil
   new sUseStringsTable(false, ""), //daisy
   new sUseStringsTable(false, ""), //watermelon
   new sUseStringsTable(false, ""), //carrot
   // start seeds
   new sUseStringsTable(false, ""), //TULIP_SEED,
   new sUseStringsTable(false, ""), //DAFFODIL_SEED,
   new sUseStringsTable(false, ""), //DAISY_SEED,
   new sUseStringsTable(false, ""), //WATERMELON_SEED,
   new sUseStringsTable(false, ""), //CARROT_SEED,
   // world items
   new sUseStringsTable(false, ""), //PLANT_DIRT,
   new sUseStringsTable(false, ""), // tree
   new sUseStringsTable(false, ""), //house
   new sUseStringsTable(false, ""), //wooden log
   new sUseStringsTable(false, ""), //baby duck
   new sUseStringsTable(false, ""), //baby sheep
   new sUseStringsTable(false, ""), //Delila
   new sUseStringsTable(false, ""), //Rock
  },
  { //silver
   new sUseStringsTable(false, ""), //GOLD
   new sUseStringsTable(false, ""), //SILVER,
   new sUseStringsTable(false, ""), //AXE,
   new sUseStringsTable(false, ""), //SLEDGEHAMMER,
   new sUseStringsTable(false, ""), //SHOVEL,
   new sUseStringsTable(false, ""), //HOUSE_KEY,
   new sUseStringsTable(false, ""), //WATERINGCAN,
   new sUseStringsTable(false, ""), //E_KEY,
   new sUseStringsTable(false, ""), //PICKER
   // start consumables
   new sUseStringsTable(false, ""), //APPLE,
   new sUseStringsTable(false, ""), //WOOD,
   new sUseStringsTable(false, ""), //STONE,
   new sUseStringsTable(false, ""), //WATER,
   new sUseStringsTable(false, ""), //MUSHROOM,
   new sUseStringsTable(false, ""), //tulip
   new sUseStringsTable(false, ""), //daffodil
   new sUseStringsTable(false, ""), //daisy
   new sUseStringsTable(false, ""), //watermelon
   new sUseStringsTable(false, ""), //carrot
   // start seeds
   new sUseStringsTable(false, ""), //TULIP_SEED,
   new sUseStringsTable(false, ""), //DAFFODIL_SEED,
   new sUseStringsTable(false, ""), //DAISY_SEED,
   new sUseStringsTable(false, ""), //WATERMELON_SEED,
   new sUseStringsTable(false, ""), //CARROT_SEED,
   // world items
   new sUseStringsTable(false, ""), //PLANT_DIRT,
   new sUseStringsTable(false, ""), // tree
   new sUseStringsTable(false, ""), //house
   new sUseStringsTable(false, ""), //wooden log
   new sUseStringsTable(false, ""), //baby duck
   new sUseStringsTable(false, ""), //baby sheep
   new sUseStringsTable(false, ""), //Delila
   new sUseStringsTable(false, ""), //Rock
  },
  { //axe
   new sUseStringsTable(false, ""), //GOLD
   new sUseStringsTable(false, ""), //SILVER,
   new sUseStringsTable(false, "Giving an Axe the Axe"), //AXE,
   new sUseStringsTable(false, ""), //SLEDGEHAMMER,
   new sUseStringsTable(false, ""), //SHOVEL,
   new sUseStringsTable(false, ""), //HOUSE_KEY,
   new sUseStringsTable(false, ""), //WATERINGCAN,
   new sUseStringsTable(false, ""), //E_KEY,
   new sUseStringsTable(false, ""), //PICKER
   // start consumables
   new sUseStringsTable(false, "You swing at the Apple multiple times, but miss every time. The Apple is spared."), //APPLE,
   new sUseStringsTable(false, "That has already been chopped, why would you want to chop it again?"), //WOOD,
   new sUseStringsTable(false, "This isn't a sword you know."), //STONE,
   new sUseStringsTable(false, "Alas, my master! Careful the Axe doesn't fall into the water."), //WATER,
   new sUseStringsTable(false, "Not effective against badgers."), //MUSHROOM,
   new sUseStringsTable(false, ""), //tulip
   new sUseStringsTable(false, ""), //daffodil
   new sUseStringsTable(false, ""), //daisy
   new sUseStringsTable(false, ""), //watermelon
   new sUseStringsTable(false, ""), //carrot
   // start seeds
   new sUseStringsTable(false, ""), //TULIP_SEED,
   new sUseStringsTable(false, ""), //DAFFODIL_SEED,
   new sUseStringsTable(false, ""), //DAISY_SEED,
   new sUseStringsTable(false, ""), //WATERMELON_SEED,
   new sUseStringsTable(false, ""), //CARROT_SEED,
   // world items
   new sUseStringsTable(false, "What do you want? An Axe tree?"), //PLANT_DIRT,
   new sUseStringsTable(false, "This tree is much too sturdy for that Axe, and your muscles"), // tree
   new sUseStringsTable(false, "You cannot chop your way in!"), //house
   new sUseStringsTable(true,  "Do you want to swing the Axe mightily and skillfully, to chop the wooden log into nice small planks of wood?"), //wooden log
   new sUseStringsTable(false, "I Axed you not to make me go Quackers!"), //baby duck
   new sUseStringsTable(false, "Using the Axe in Sheep form the counter helix still fires off."), //baby sheep
   new sUseStringsTable(false, "What a simply horrid thing to do!"), //Delila
   new sUseStringsTable(false, ""), //Rock
  },


I apologize for the sheer amount of code above. You see the BIGGEST problem with this 2 dimensional array is that all arrays are of equal size. This means as I have added more and more items into the game, this array has got OUT of HAND!  The example above only shows a few objects, but you can see how massive this could become. Imagine 100 items x 100 items. that is a 10,000 possible options right there.... simply a massive table.

The real downer about this is that it is cumbersome and hard to maintain. It also has the side effect that I do not want to create another Item, as I would have to visit every sub-dimension to add that new object to keep all the arrays the same size, bah!

What you can also see above is that the arrays are MOSTLY EMPTY. Whaaaat? Yes, it is true. Some objects simply cannot be used with others, Or I don't have anything witty to write for that combination.

I spent some time working on a replacement. I started by figuring out what I wanted, and simply enough I wanted to remove the empty combinations. A good plan like this can get ya excited and focused on what to do. Then I went looking at array documentation for C#. (always good to refresh my memory on this stuff!).

I FOUND THE ANSWER...
Jagged Arrays - this was my answer. It did not take very long for me to create these new arrays. And yes I had to write some extra search code to find the actual match instead of just using a second index from the Item type. Still very easy and as you can see below it works so much more clearly and succinctly.

 public static sUseStringsTable2[][] CompleteUseStrings = new sUseStringsTable2[][] {
  new sUseStringsTable2 [] { // gold
  },
  new sUseStringsTable2 [] { // silver
  },
  new sUseStringsTable2 [] { // axe
   new sUseStringsTable2(ITEM_IDS.C_APPLE , false, "You swing at the Apple multiple times, but miss every time. The Apple is spared."), //APPLE,
   new sUseStringsTable2(ITEM_IDS.C_WOOD , false, "That has already been chopped, why would you want to chop it again?"), //WOOD,
   new sUseStringsTable2(ITEM_IDS.C_STONE , false, "This isn't a sword you know."), //STONE,
   new sUseStringsTable2(ITEM_IDS.C_WATER , false, "Alas, my master! Careful the Axe doesn't fall into the water."), //WATER,
   new sUseStringsTable2(ITEM_IDS.C_MUSHROOM , false, "Not effective against badgers."), //MUSHROOM,
   new sUseStringsTable2(ITEM_IDS.W_PLANT_DIRT , false, "What do you want? An Axe tree?"), //PLANT_DIRT,
   new sUseStringsTable2(ITEM_IDS.W_TREE , false, "This tree is much too sturdy for that Axe, and your muscles"), // tree
   new sUseStringsTable2(ITEM_IDS.W_HOUSE , false, "You cannot chop your way in!"), //house
   new sUseStringsTable2(ITEM_IDS.W_WOODEN_LOG , true,  "Do you want to swing the Axe mightily and skillfully, to chop the wooden log into nice small planks of wood?"), //wooden log
   new sUseStringsTable2(ITEM_IDS.W_BABY_DUCK , false, "I Axed you not to make me go Quackers!"), //baby duck
   new sUseStringsTable2(ITEM_IDS.W_BABY_SHEEP , false, "Using the Axe in Sheep form the counter helix still fires off."), //baby sheep
   new sUseStringsTable2(ITEM_IDS.W_DELILA , false, "What a simply horrid thing to do!"), //Delila
   new sUseStringsTable2(ITEM_IDS.W_POND , false, "Water makes Axes go rusty, and you don't want a rusty Axe do you?"), //   W_POND,
  },


As you can easily see this covers all that data from the above example, but in a much smaller format. So easy to use, so incredibly easy to add another item to the list as well.

I am so pleased I spent way too much time redoing this list now, as it would only take me a lot longer if I left it till I had another 60 Items in it :o

Laters
Da Voodoochief

No comments:

Post a Comment