From 888b6a9be78aa21c43a503e57a57d63e4d02ca51 Mon Sep 17 00:00:00 2001 From: Will Luongo Date: Sun, 17 Feb 2013 22:55:33 -0500 Subject: [PATCH] Added weapon shop, general cleanup --- LotRD.rb | 31 +++++++++++++++++++++++++++++-- player.rb | 14 ++++++++++++++ weapon.json | 26 ++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 weapon.json diff --git a/LotRD.rb b/LotRD.rb index 92093c0..1483d2b 100644 --- a/LotRD.rb +++ b/LotRD.rb @@ -7,6 +7,7 @@ class LotRD def initialize() @forest_monsters = JSON.load(File.open('./forest_monsters.json').read) @armor_store = JSON.load(File.open('./armor.json').read) + @weapon_store = JSON.load(File.open('./weapon.json').read) @log = Logger.new("lotrd.log") @log.level = Logger::DEBUG @play = true @@ -40,7 +41,7 @@ def start # Prompt is the workhorse of user interactions # - # * no arguments defaults to "Hit enter to continue" + # * No arguments defaults to "Hit enter to continue" # * One argument is basically a pause with a custom prompt # * Automatically handles invalid choices, only returns a valid selection @@ -85,6 +86,8 @@ def main_menu visit_inn when "a" visit_armory + when "w" + visit_weaponry else prompt "Sorry, that isn't implemented yet. Press enter to continue." end @@ -168,7 +171,6 @@ def visit_armory item_string = item_string + "\n[#{item_no}] A #{item["name"]}. Armor value #{item["armor_value"]}. Cost: #{item["cost"]} gold." item_list.push(item_no) end - # TODO: Add option to sell armor select = prompt(item_string + "\n[L]eave the store", *item_list, :l) if select == "l" # drop out to main menu @@ -182,6 +184,31 @@ def visit_armory end end + def visit_weaponry + @log.debug(@weapon_store) + clear + puts "Welcome to the Weapon Shop!" + item_string = "" + item_no = 0 + item_list = [] + @weapon_store.each do |item| + item_no = item_no + 1 + item_string = item_string + "\n[#{item_no}] A #{item["name"]}. Attack value #{item["attack_value"]}. Cost: #{item["cost"]} gold." + item_list.push(item_no) + end + select = prompt(item_string + "\n[L]eave the store", *item_list, :l) + if select == "l" + # drop out to main menu + else + if @player.purchase_weapon(select, @weapon_store) + prompt + else + prompt + visit_weaponry + end + end + end + def visit_inn clear puts "Welcome to the Ruby Dragon Inn!" diff --git a/player.rb b/player.rb index 77e9a49..b0c190f 100644 --- a/player.rb +++ b/player.rb @@ -71,6 +71,20 @@ def purchase_armor(select, store) end end + def purchase_weapon(select, store) + old_weapon = @weapon + weapon = store[select.to_i-1] + if @gold >= weapon["cost"] + @weapon = Weapon.new(weapon["name"], weapon["use"], weapon["attack_value"], weapon["cost"]) + @gold = @gold - weapon["cost"] + old_weapon.cost + puts "Sold! You are now the proud owner of #{@weapon.name}! I even gave you #{old_weapon.cost / 2} gold for that old #{old_weapon.name}!" + return true + else + puts "I am sorry, you don't have enough gold, and I don't do charity!" + return false + end + end + def inn(cost) if @gold >= cost @gold = @gold - cost diff --git a/weapon.json b/weapon.json new file mode 100644 index 0000000..e70628d --- /dev/null +++ b/weapon.json @@ -0,0 +1,26 @@ +[ +{ + "name":"brass knuckles", + "use":"punch", + "cost":10, + "attack_value":5 +}, +{ + "name":"rubber dagger", + "use":"annoy", + "cost":30, + "attack_value":7 +}, +{ + "name":"broken keyboard", + "use":"smack", + "cost":50, + "attack_value":9 +}, +{ + "name":"hatchet", + "use":"smash, smash, smayush", + "cost":150, + "attack_value":12 +} +]