Class: WizardsCastle::ShoppingTrip

Inherits:
Object
  • Object
show all
Defined in:
lib/wizards-castle/shopping_trip.rb

Instance Method Summary collapse

Constructor Details

#initialize(player, printer, prompter) ⇒ ShoppingTrip

Returns a new instance of ShoppingTrip.



4
5
6
7
8
# File 'lib/wizards-castle/shopping_trip.rb', line 4

def initialize(player,printer,prompter)
  @player = player
  @printer = printer
  @prompter = prompter
end

Instance Method Details

#buy_armorObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/wizards-castle/shopping_trip.rb', line 42

def buy_armor
  return if @player.gp < 1250
  @printer.gold_and_armor_report
  @printer.vendor_armors

  loop do
    answer = @prompter.ask(["N","L","C","P"],@printer.prompt_vendor_armor)
    case answer
    when "N"
      return
    when "L"
      @player.gp(-1250)
      @player.set_armor(:leather)
      return
    when "C"
      if @player.gp < 1500
        @printer.cannot_afford_chainmail
      else
        @player.gp(-1500)
        @player.set_armor(:chainmail)
        return
      end
    when "P"
      if @player.gp < 2000
        @printer.cannot_afford_plate
      else
        @player.gp(-2000)
        @player.set_armor(:plate)
        return
      end
    end
  end
end

#buy_dex_potionsObject



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/wizards-castle/shopping_trip.rb', line 142

def buy_dex_potions
  loop do
    return if @player.gp < 1000
    answer = @prompter.ask(["Y","N"],@printer.prompt_vendor_dex_potion)
    if answer=="Y"
      @player.gp(-1000)
      @player.dex(+random_stat_gain)
      @printer.dex_report
    else
      return
    end
  end
end

#buy_int_potionsObject



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/wizards-castle/shopping_trip.rb', line 128

def buy_int_potions
  loop do
    return if @player.gp < 1000
    answer = @prompter.ask(["Y","N"],@printer.prompt_vendor_int_potion)
    if answer=="Y"
      @player.gp(-1000)
      @player.int(+random_stat_gain)
      @printer.int_report
    else
      return
    end
  end
end

#buy_lampObject



156
157
158
159
160
161
162
163
164
# File 'lib/wizards-castle/shopping_trip.rb', line 156

def buy_lamp
  return if (@player.gp<1000 || @player.lamp?)
  answer = @prompter.ask(["Y","N"],@printer.prompt_vendor_buy_lamp)
  if answer=="Y"
    @player.gp(-1000)
    @player.set_lamp(true)
    @printer.you_bought_a_lamp
  end
end

#buy_str_potionsObject



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/wizards-castle/shopping_trip.rb', line 114

def buy_str_potions
  loop do
    return if @player.gp < 1000
    answer = @prompter.ask(["Y","N"],@printer.prompt_vendor_str_potion)
    if answer=="Y"
      @player.gp(-1000)
      @player.str(+random_stat_gain)
      @printer.str_report
    else
      return
    end
  end
end

#buy_weaponObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/wizards-castle/shopping_trip.rb', line 76

def buy_weapon
  return if @player.gp < 1250
  @printer.gold_and_weapon_report
  @printer.vendor_weapons

  loop do
    answer = @prompter.ask(["N","D","M","S"],@printer.prompt_vendor_weapon)
    case answer
    when "N"
      return
    when "D"
      @player.gp(-1250)
      @player.set_weapon(:dagger)
      return
    when "M"
      if @player.gp < 1500
        @printer.cannot_afford_a_mace
      else
        @player.gp(-1500)
        @player.set_weapon(:mace)
        return
      end
    when "S"
      if @player.gp < 2000
        @printer.cannot_afford_a_sword
      else
        @player.gp(-2000)
        @player.set_weapon(:sword)
        return
      end
    end
  end
end

#random_stat_gainObject



110
111
112
# File 'lib/wizards-castle/shopping_trip.rb', line 110

def random_stat_gain
  Random.rand(6)+1
end

#random_treasure_offer(i) ⇒ Object



24
25
26
# File 'lib/wizards-castle/shopping_trip.rb', line 24

def random_treasure_offer(i)
  Random.rand((i+1)*1500) + 1
end

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/wizards-castle/shopping_trip.rb', line 10

def run()
  sell_treasures
  if @player.gp < 1000
    @printer.too_poor_to_trade
    return
  end
  buy_armor
  buy_weapon
  buy_str_potions
  buy_int_potions
  buy_dex_potions
  buy_lamp
end

#sell_treasuresObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/wizards-castle/shopping_trip.rb', line 28

def sell_treasures
  a = [:ruby_red,:norn_stone,:pale_pearl,:opal_eye,:green_gem,:blue_flame,:palantir,:silmaril]
  a.each_with_index do |treasure,i|
    if @player.have_treasure?(treasure)
      offer = random_treasure_offer(i)
      answer = @prompter.ask(["Y","N"], @printer.prompt_sell_treasure(treasure,offer))
      if answer=="Y"
        @player.remove_treasure(treasure)
        @player.gp(+offer)
      end
    end
  end
end