Wednesday, 7 September 2011

Code: TrainingCamp

A first cut of the TrainingCamp class looks like this:

class TrainingCamp
    def initialize
        @girls = GirlList.new
    end
  
    def do_turn
    #
    #    part of me wants to break this down, hour by hour
    #    I can see good things from that. Staggering
    #    changeovers for instance, means never risking
    #    having three girls gang up on you...
    #
        @girls.do_shift(Shift::Morning)
        @girls.do_shift(Shift::Afternoon)
        @girls.do_shift(Shift::Evening)
        @girls.do_shift(Shift::Night)
    end

    def add_girl(girl)
        @girls.add(girl)
    end
end
Again, nothing surprising there. I've added a method to add girls to the camp. The calling code looks like this this:
camp = TrainingCamp.new
camp.add_girl(Girl.new( :name => "Anne", :obedience => 51))
camp.add_girl(Girl.new( :name => "Betty" ))

while true
    camp.do_turn
    sleep 1
end
That'll let me run tests with one turn a second when the rest of the classes are working.

I also added a Shift class. Since that's just a holder for the shift constants, I'll we can deal with it here:
class Shift
    Morning        = "Morning"
    Afternoon     = "Afternoon"
    Evening        = "Evening"
    Night            = "Night"
end

I could just use the strings, but this way they get syntax checked and I can spot typos. I also had the impression that Ruby stored all strings as single instances, so passing strings around was no worse than passing ints, and that using strings was the preferred Ruby idiom for constants. But now I think on it, I may be getting that mixed up with Lua. Never mind, in any event, it saves me having a lookup table to convert int constants into strings

No comments: