Sunday, October 4, 2015

Shopping made easy (Ruby Edition)

  Hello, everyone!

  Chapter 45 assignment is done. My game is ready, and the code is 100% complete. I have to finish writing out flavor text, and I will do it in my spare time over next couple of days.

  The only thing I had left to do today was finish code for the shop where Hero can spend gold to buy gear, and man, that proved to be the most difficult part of the game! I never expected it. I thought coding the combat sequence was tough.

  So the idea behind the shop, that it will have items for sale where the Hero can spend gold to buy items that will boost his stats. (like a better shield will increase Armor, better sword would increase Attack, etc). I had the following items that I wanted to have available in the shop.
  1. Magic Hammer: increases Attack Range for the Hero (8 gold)
  2. Magic Sword: Increases Attack Range, Hit Chance and Crit Chance (10 gold): slight boost to many stats
  3. Magic Shield: Increases Armor Class (7 gold)
  4. Magic Boots: Increases Hit Chance  (5 gold)
  5. Magic Ring: Increases Crit Chance (5 gold)"
  6. Health Potions: Increases Health Potion Count by 1 (1 gold) - no stat boost, but increases gives 1 more potion.

At first I had it written out in a hash with the name and cost:




  That works fine for asking what item you want to buy, and deducting the cost from the total hero's gold. But the problem was that I had to increase the appropriate stats depending on what item was purchased. I could do it with a bunch of "if .. else" blocks, but that is so ugly, plus what if I had 1000 items that hero could choose from?

  I finally realized that the values for the keys in my hash could be ARRAYS! So I needed a new array that would take starting values of all the stats of the hero. I called it @stats. The array in the key value pair in the @shop hash will hold the modifiers for those stats plus an extra element that would hold a string with the name of the item. When an item is purchased, the elements in the same index of both arrays are added together to get updated stats for the Hero. I had to write a method that would "add" elements of both arrays together, which I called "upgrade".

The only other thing I had to do was, change the gold cost value to negative, so it subtracts.










  Once I got over that hurdle, it was a lot easier to finish out this code. I had to add a check before every single purchase to verify that the Hero can afford the item he/she is requesting, with a message if the item is too expensive.

  I also had to make a separate code for purchasing a potion, because all it would do is increase the number of potions the Hero has by 1.

  Final thing is to update all the Hero Stats to values of elements in the @stats array after the shopping is done.

Final code for the Shop looks like this: 



You can see the full code on Github if you prefer it that way here.

 This was a fun challenge to overcome for me, and I feel like I learned a lot. Trying to complicate the game just created additional puzzles that I had to figure out.

  I will start Chapter 46 in Zed Shaw's "Learn Ruby the Hard Way" tomorrow!

  Thanks for reading!

 - Andrei




No comments:

Post a Comment