Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Optimizing for Newbies

0 views
Skip to first unread message

Simon Strandgaard

unread,
Dec 1, 2005, 5:48:20 PM12/1/05
to
On 12/1/05, Matthew Feadler <mat...@feadler.com> wrote:
> 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:
[snip]


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


maybe write it like this (untested)

def roll_attribute_pair
r1 = Die.new(10).roll
r2 = Die.new(10).roll
return 0 if (r1 == 0) or (r2 == 0)
(r1 - r2)
end


--
Simon Strandgaard


Simon Kröger

unread,
Dec 1, 2005, 6:03:28 PM12/1/05
to

or if you like your Array:

def rollAttributePair
rolls = Array.new(2){Die.new(10).roll}
return 0 if rolls.any?{|r|r.zero?}
rolls[0] - rolls[1]
end

you may also look at 'Struct' for the charAttributes
(may just be better suited than a hash)

cheers

Simon

Simon Strandgaard

unread,
Dec 1, 2005, 5:57:33 PM12/1/05
to
On 12/1/05, Simon Strandgaard <neo...@gmail.com> wrote:
> On 12/1/05, Matthew Feadler <mat...@feadler.com> wrote:
> > 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:
> [snip]

#Init Array to hold shortnames of the 8 Attributes
attributeShortNames = Array.new
attributeShortNames[0,7]=["Int", "Per", "Str", "Sta", "Pre", "Com",
"Dex", "Qik"]


can simplified:

attributeShortNames=["Int", "Per", "Str", "Sta", "Pre", "Com", "Dex", "Qik"]

which again can be even more simpified:

attributeShortNames=%w(Int Per Str Sta Pre Com Dex Qik)


#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


can be simplified:

attributeNameMap = Hash[*(attributeShortNames.zip(attributeFullNames).flatten)]

--
Simon Strandgaard


Damphyr

unread,
Dec 2, 2005, 11:28:29 AM12/2/05
to
Matthew Feadler wrote:
> 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.
Jeeez, I haven't seen Ars Magica in a long, long while. Will probably
look at the code, but I just had to admire the fact that it still exists
(I've got almost all of 3rd Eds).
I always loved it (a lot more than D&D a bit less then CoC :) )
This brings back memories.
V.-
--
http://www.braveworld.net/riva


____________________________________________________________________
http://www.freemail.gr - δωρεάν υπηρεσία ηλεκτρονικού ταχυδρομείου.
http://www.freemail.gr - free email service for the Greek-speaking.


Matthew Feadler

unread,
Dec 2, 2005, 1:24:10 PM12/2/05
to
damphyr wrote:
> Jeeez, I haven't seen Ars Magica in a long, long while. Will probably
> look at the code, but I just had to admire the fact that it still exists
> (I've got almost all of 3rd Eds).
> I always loved it (a lot more than D&D a bit less then CoC :) )
> This brings back memories.

In reverse order:

<chuckles> Glad I could spark the recollection, Damphyr. It certainly
is my favorite system.

Simon, I was unfamiliar with 'Struct' before your mention. Loking
briefly here: http://www.rubycentral.com/book/ref_c_struct.html, it
doesn't seem there's a way to establish relationships between the
elements of the 'Struct', which I think defeats my purpose. Please
enlighten me if I've misunderstood. Also, these

rolls = Array.new(2){Die.new(10).roll}
return 0 if rolls.any?{|r|r.zero?}

are very cool. I was originally under the impression that the first
line would create a two-element array *with the same Die instance in
each element*, which would, of course, always result in a 0 total
further down the code. I've tested it now, and realize that each
element contains a seperate Die instance. VERY handy knowledge. The
second line is an example of Ruby's lovely ? notation, which I've
noticed all over, but have yet to really grasp/master. Thanks for this
example.

Neoneye, I had never seen the %w in this

attributeShortNames=%w(Int Per Str Sta Pre Com Dex Qik)

before, but looking here: http://www.rubycentral.com/book/intro.html I
found it. Exactly the sort of Ruby shorthand I'm looking for. Now this

Hash[*(attributeShortNames.zip(attributeFullNames).flatten)]

is marvellous. A quick google
(http://www.google.com/search?hl=en&lr=&q=ruby+zip+method&btnG=Search)
doesn't give much for the zip method, so it's still a bit magical to me.
Any references to recommend? As for the flatten method, looking here:
http://www.rubycentral.com/book/ref_c_array.html, I see how it works.
Very lovely.

Thanks tremendously, all. You've given me much food for thought and
practice. But don't stop advising on my account. ;)

-Matthew

--
Posted via http://www.ruby-forum.com/.


James Edward Gray II

unread,
Dec 2, 2005, 1:39:51 PM12/2/05
to
On Dec 2, 2005, at 12:24 PM, Matthew Feadler wrote:

> Also, these
>
> rolls = Array.new(2){Die.new(10).roll}
> return 0 if rolls.any?{|r|r.zero?}
>
> are very cool. I was originally under the impression that the first
> line would create a two-element array *with the same Die instance in
> each element*, which would, of course, always result in a 0 total
> further down the code.

Nope. The block is executed to create each element.

> The second line is an example of Ruby's lovely ? notation, which I've
> noticed all over, but have yet to really grasp/master. Thanks for
> this
> example.

By convention, a method ending in a ? returns a true/false answer to
the question implied by the call.

> A quick google
> (http://www.google.com/search?hl=en&lr=&q=ruby+zip+method&btnG=Search)
> doesn't give much for the zip method, so it's still a bit magical
> to me.
> Any references to recommend?

If you have Ruby's documentation installed just feed your command-line:

ri Array.zip

Or you can look it up here:

http://www.ruby-doc.org/core/classes/Array.html#M000391

James Edward Gray II


Matthew Feadler

unread,
Dec 2, 2005, 2:06:26 PM12/2/05
to
bbazzarrakk wrote:

>> rolls = Array.new(2){Die.new(10).roll}


>
> Nope. The block is executed to create each element.
>

This highlights exactly what I wasn't grasping about that line
previously: the {} delimits a block. I was (wrongly, of course)
equating it to

rolls = Array.new(2,Die.new)

Now I get it. Thanks!

>
> If you have Ruby's documentation installed just feed your command-line:
>
> ri Array.zip
>
> Or you can look it up here:
>
> http://www.ruby-doc.org/core/classes/Array.html#M000391

Both excellent reference sources of which I was not aware. Much
appreciated. Now the zip method makes perfect sense.

Simon Kröger

unread,
Dec 2, 2005, 7:04:34 PM12/2/05
to
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


0 new messages