From: Stephen Ball Date: 2008-11-03T22:57:29+09:00 Subject: Re: Text Based RPG in Ruby On Sat, 01 Nov 2008 14:38:08 -0400, Ruairidh Mchardy wrote: > Hi all, > > I'm currently coding a text-based RPG in Ruby which is great fun but I'm > having some difficulty in structuring it. > > So far I have a main.rb file containing a class for the basic enemy and > then an extension for the enemy of the lowest level (Grunt). To test I > created an instance of Grunt and assigned it an hp of 100. > I then create a var for the assignment of damages (random number > between 1 and 100) and finally test it all out by hard-coding an example > battle. This also works. > > My problem is that I feel my code is not clean at all and that the > structure feels inefficient. As I will be extending the game in the > future I want to make sure that every aspect of it is neat and easy to > maintain. > > Attached is my script, does anyone have any suggestions? > > Thanks! > > 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