On Tue, Mar 4, 2008 at 6:04 PM, August0866 <august0866 / hotmail.com> wrote:
> On Mar 4, 2:35 pm, Todd Benson <caduce... / gmail.com> wrote:
>
>
> > On Tue, Mar 4, 2008 at 12:23 PM, Todd Benson <caduce... / gmail.com> wrote:
>  >
>  > >  Not sure, but I think the logic.  (@smarts.to_f < 14 or @speed.to_f <
>  > >  14) will be true the same time (@smarts.to_f > 14 and @speed.to_f <
>  > >  14) is true.  Your decision set is not mutually exclusive because of
>  > >  that first 12 (What if your 13?).  Just a guess...
>  >
>  > One other thing, though.  Maybe you want your code to cascade the
>  > decision like that.  In any case, your code works just fine on my
>  > system, as long as I properly #rand my power, smarts, and speed :-)
>  > On 1.8.6, the if/elsif construct seems to correctly jump out.  In
>  > other words, if my power is 13, then it will will leave the loop with
>  > the second result and never get to the fourth.
>  >
>  > My distribution for 10_000 people for one run using rand(20 for each
>  > attribute turns out to be...
>  >
>  > adventurer: 211
>  > laborer: 2260
>  > courier: 416
>  > teacher: 1261
>  > commoner: 5852
>  >
>  > ..which doesn't look too bad.
>  >
>  > Todd
>
>  Thanks Todd,
>
>  how would i go about defining a job and its requirements and then
>  match the npc to the job
>
>  <pseudo>
>             job[[Adventurer,14,14,14],[Courier,14,10,14] .... ]
>
>  the numbers are the minimum stats for the job these jobs are actually
>  categories i am going to break them down to specific occupations
>  next

I'll make a couple assumptions here.  I think you want to have minimum
requirements, and then have the job assignment go to the least
requirement.  In other words, in your above pseudo, if I had stats
like [14,15,14], I would be an adventurer, and if I had stats
[10,14,14] I would fail to be either (but maybe something else).
Least common denominator?

I would approach it probably in an eccentric way.  Most people would
suggest side by side iteration of Arrays (which, IIRC, was added as a
feature to Ruby 1.9).  That is probably the best way to do it.  I'm
kind of a set theory guy, though, so tend towards slower but
conceptually more sound (to me) methods.

I'd probably build a matrix of the person's stats and use that...


require 'matrix'
joblist = ['adventurer', 'courier']
attributes = ['power', 'smarts', 'speed']
n, j = attributes.length, joblist.length
a = [[14, 14, 14], [14, 10, 14]]
some_poor_guys_stats_copies = Array.new(j, Array.new(n) {rand(20) + 1})
s = Matrix[*some_poor_guys_stats_copies]
e = Matrix[*a]
poss = []
(s - e).to_a.each_with_index do |v, i|
  poss << i if (v.select {|j| j >= 0}).length == n
end
p joblist[poss.min] rescue p "Commoner"

It's not even close to clean, but I like using Matrix for this kind of
thing instead of the side by side iterating.

I suppose it's way better to use a Hash, too, like so...

requirements = {
  :adventurer => {
    :power => 14,
    :smarts => 14.
    :speed => 14
  }
  :courier => {
    :power => 14,
    :smarts => 10,
    :speed => 14
}

person = {
  :power => rand(die_number),
  :smarts => rand(die_number),
  :speed => rand(die_number)
}

I'm pretty sure you can use the Hash.new block form to build these if
you wanted to.

I should mention also that many people frown upon using the rescue
word for conditional use (in other words, my code above is not good
for application use, but probably ok for a script).

Todd