From: Matthew Feadler Date: 2005-12-02T07:03:22+09:00 Subject: Optimizing for Newbies Since I've gotten the go-ahead from Christer, here's a little newbie app I just wrote and am looking for advice on how to better write: =begin =Author & Maintainer Matthew A. Feadler http://matthew.feadler.com =Description arschar.rb guides the user through the process of rolling attributes for an Ars Magica character using v4 rules. =Copyright This software is Copyright 2005 Matthew A. Feadler. All rights reserved. Do as you like with it, as long as you don't claim to have written it. And, there is certainly no warranty hereabout, express or implied. =Version arschar.rb 0.1 2005/Nov/30 =end system("clear") #BEGINCLASSDEFS #Classname: Die #Purpose: Provide arbitrarily-sided die functionality #Notes: Only 6-sided dice display from 1, all others from 0 class Die def initialize(sides) @sides = sides roll if @sides == 6 @currentFace += 1 end @currentFace end def roll @currentFace = rand(@sides) @currentFace end def showing @currentFace end end #ENDCLASSDEFS #BEGINMETHODDEFS #Roll 2 10-sided die and subtract 2nd from 1st to determine pair total def rollAttributePair rolls = Array.new rolls.push(Die.new(10).roll) rolls.push(Die.new(10).roll) if (rolls[0] == 0) or (rolls[1] == 0) total = 0 return total else total = (rolls[0] - rolls[1]) return total end end #Obtain user allocation of scores and validate against AM rules def getScore(total,attr) goodAnswer = false while goodAnswer == false print " Enter score for " + attr + ": " userInput = gets.chomp.strip.to_i if (userInput < 0) and (total > 0) puts " You rolled positively; no negatives for this pair." goodAnswer = false elsif (userInput > 0) and (total < 0) puts " You rolled negatively; no positives for this pair." goodAnswer = false elsif userInput > 4 puts " Rolled scores cannot be higher than 4." goodAnswer = false elsif userInput < -4 puts " Rolled scores cannot be lower than 4." goodAnswer = false else goodAnswer = true end end return userInput end #ENDMETHODDEFS #BEGINVARIABLEDEFS #Init Array to hold shortnames of the 8 Attributes attributeShortNames = Array.new attributeShortNames[0,7]=["Int", "Per", "Str", "Sta", "Pre", "Com", "Dex", "Qik"] #Init Array to hold fullnames of the 8 Attributes attributeFullNames = Array.new attributeFullNames[0,7]=["Intelligence","Perception","Strength","Stamina","Presence","Communication","Dexterity","Quickness"] #Init Hash to hold shortname/fullname pairs for the 8 Attributes attributeNameMap = Hash.new count = 0 attributeShortNames.each do |name| attributeNameMap[name]= attributeFullNames[count] count += 1 end #Init Hash to hold name/value pairs for character Attributes #charAttributes = {'Int' => 0, 'Per' => 0, 'Str' => 0, 'Sta' => 0, 'Pre' => 0, 'Com' => 0, 'Dex' => 0, 'Qik' => 0} charAttributes = Hash.new(0) attributeShortNames.each do |name| charAttributes[name]= 0 end #ENDVARIABLEDEFS #BEGINMAIN x = 0 y = 1 while (x <= 6) and (y <= 7) attr0 = attributeShortNames[x] attr1 = attributeShortNames[y] full0 = attributeNameMap[attr0] full1 = attributeNameMap[attr1] puts "Now rolling for the " + full0 + " and " + full1 + " pair..." total = rollAttributePair if total == 0 puts " You rolled a zero for this pair. Both " + attr0 + " and " + attr1 + " will be set to zero." charAttributes[attr0]= 0 charAttributes[attr1]= 0 elsif total > 0 puts " You rolled a +" + total.to_s + " for this pair." puts " Please choose scores which are >= 0, <= 4, and which add up to " + total.to_s + "." goodAnswer = false while goodAnswer == false temp = Array.new val0 = getScore(total,attr0) val1 = getScore(total,attr1) if (val0 + val1) == total charAttributes[attr0]= val0 charAttributes[attr1]= val1 goodAnswer = true else puts " Your scores add up to more than " + total.to_s + ". Please try again." goodAnswer = false end end else puts " You rolled a " + total.to_s + " for this pair." puts " Please, choose scores which are <= 0, >= -4, and which add up to " + total.to_s + "." goodAnswer = false while goodAnswer == false temp = Array.new val0 = getScore(total,attr0) val1 = getScore(total,attr1) if (val0 + val1) == total charAttributes[attr0]= val0 charAttributes[attr1]= val1 goodAnswer = true else puts " Your scores add up to more than " + total.to_s + ". Please try again." goodAnswer = false end end end x += 2 y += 2 puts end #Print charAttributes attributeShortNames.each do |attr| puts attr.to_s + ": " + charAttributes[attr].to_s end #Pause for user input before exit puts print "Press Enter to exit..." gets #ENDMAIN Thanks to all in advance! -Matthew -- Posted via http://www.ruby-forum.com/.