From: "Simon Kröger" Date: 2005-12-03T09:04:34+09:00 Subject: Re: Optimizing for Newbies This isn't meant as 'the' solution of your problem, its just a bit more like it would look like if i had to write the same program (i was bored, obviously): --------------------------------------------------------------------- require 'enumerator' $stdout.sync = true system("clear") #Roll 2 10-sided die and subtract 2nd from 1st to determine pair total def rollAttributePair rolls = Array.new(2){rand(10)} return 0 if rolls.any?{|r|r.zero?} rolls[0] - rolls[1] end #Obtain user allocation of scores and validate against AM rules def getScore(total,attr) loop do print " Enter score for " + attr + ": " userInput = gets.chomp.strip.to_i if (userInput < 0) && (total > 0) puts " You rolled positively; no negatives for this pair." elsif (userInput > 0) && (total < 0) puts " You rolled negatively; no positives for this pair." elsif userInput > 4 puts " Rolled scores cannot be higher than 4." elsif userInput < -4 puts " Rolled scores cannot be lower than 4." else return userInput end end end Attribute = Struct.new(:long, :short) long = %w(Intelligence Perception Strength Stamina Presence Communication Dexterity Quickness) short = %w(Int Per Str Sta Pre Com Dex Qik) attributes = long.zip(short).map{|a| Attribute.new *a} #Init Hash to hold name/value pairs for character Attributes charAttributes = Hash.new(0) attributes.each_slice(2) do |attr| puts "Now rolling for the #{attr[0].long} and #{attr[1].long} pair..." total = rollAttributePair if total == 0 puts " You rolled a zero for this pair. Both #{attr[0].short} and #{attr[1].short} will be set to zero." attr.each{|a| charAttributes[a] = 0} else puts " You rolled a #{total} for this pair." if total > 0 puts " Please choose scores which are >= 0, <= 4, and which add up to #{total}." else puts " Please, choose scores which are <= 0, >= -4, and which add up to #{total}." end loop do attr.each{|a| charAttributes[a] = getScore(total, a.short)} break if (charAttributes[attr[0]] + charAttributes[attr[1]]) == total puts " Your scores add up to more than #{total}. Please try again." end end puts end #Print charAttributes attributes.each{|attr| puts attr.long + ": #{charAttributes[attr]}"} #Pause for user input before exit print "\nPress Enter to exit..." gets --------------------------------------------------------------------- maybe it's helpfull, maybe not... cheers Simon