Argh ok having problems with this. My code has just become very messy
because I can't get my head clear on this one.
I'm trying to structure the enemy classes as per your advice and so have
Grunty laid out in a manner similar to this:
class Grunty < Enemy
def initialize(hp,attack, defense, weapon)
super(hp, attack, defense)
@weapon = "AK47"
@hp =100
if Grunto.hp == 0
puts "Grunty is dead!"
else
puts "Grunty is wounded but can still fight!"
end
randy = rand(100)
health = Grunto.hp - randy
puts "Grunty's hp is #{Health}"
end
This throws up no errors and although the hit assignment is poor, it
remains functional.
I then continue to test with an example scenario battle hard coded into
the script:
Grunty = Grunt.new("hp", 300, 300, "Ak47")
puts "Grunty is battling a kitten!"
puts "The kitten attacks Grunty with an AK47 and inflicts #{randy}
points of damage!"
puts "Grunty lost #{randy} health!"
puts "Grunty's hp is #{Health} "
end
end
In theory this should work but get the following errors:
/home/roo/dev/RubyRPG/RubyRPG/lib/main.rb:19:in `initialize':
uninitialized constant Enemy::Grunt::Grunto (NameError)
from /home/roo/dev/RubyRPG/RubyRPG/lib/main.rb:31:in `new'
from /home/roo/dev/RubyRPG/RubyRPG/lib/main.rb:31
from :1
from :1
I'm not too sure why these errors are being thrown up, I assume it's not
liking the way the class has been edited?
Thanks for all the help so far Stephen, it's helped show me the
direction I should be taking!
Kind Regards,
Ruairidh McHardy
Stephen Ball wrote:
> On Sat, 01 Nov 2008 14:38:08 -0400, Ruairidh Mchardy
> <ruairidh82 / gmail.com> wrote:
>
>> battle. This also works.
>> Attachments:
>> http://www.ruby-forum.com/attachment/2882/main.rb
>>
>
> Just some quick notes:
> - you should have your enemy class track and assign damage, that way you
> can have it test the attack parameters against defense and assign
> whatever
> damage is actually dealt, rather than working out the math in the main
> program
> -- e.g. Player.attack(Grunto)
> - the enemy class should also be responsible for reporting defeat and
> damage
> -- e.g. Grunto.health # => "Grunto is undamaged", Grunto.health # =>
> "Grunty is wounded but can still fight!"
> -- e.g. if Grunto.dead? blah blah
> - right now your weapon is just a string identifier, you'll want that to
> be a class that defines its damage range and other properties
>
> You've got a fair bit of work to do yet. Locations, movement, inventory,
> NPCs, conversations, etc. Good luck!
>
> -- Stephen
--
Posted via http://www.ruby-forum.com/.