[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Where did the monsters go?

Extreme Noob

4/22/2008 6:37:00 PM

Stuck again. My random monster-fight generator works, but it's so BASIC.
How can I do this without the if-statements? Is it a bad thing to stick
class objects into an array?

Also, I know Moblist[0].fight works, but why not Moblist[0..2].fight ?


##############################################
class Mob
def fight
"THIS IS A FIGHT METHOD"
end
end

GOBO = Mob.new
DRAGON = Mob.new
SLIME= Mob.new

Moblist = [GOBO, DRAGON, SLIME]

def rand_fight
a = rand(Moblist.length)#<-----------Has to be a better way|
if a ==0 then GOBO.fight
elsif a==1 then DRAGON.fight
elsif a==2 then SLIME.fight
end#------------------------------------------------------|

end

p rand_fight
##############################################
--
Posted via http://www.ruby-....

7 Answers

Joachim Glauche

4/22/2008 6:45:00 PM

0

Ben Galyean wrote:
> Stuck again. My random monster-fight generator works, but it's so BASIC.
> How can I do this without the if-statements? Is it a bad thing to stick
> class objects into an array?
Yep.

def rand_fight
Moblist[rand(Moblist.size)].fight
end

> Also, I know Moblist[0].fight works, but why not Moblist[0..2].fight ?
Because array[range] returns an array.

And btw...
> Moblist = [GOBO, DRAGON, SLIME]
Why are you using constants for all this?

You can also initialize your list something like
moblist = []
3.times do
moblist << Mob.new
end
--
Posted via http://www.ruby-....

Daniel Waite

4/22/2008 6:52:00 PM

0

Ben Galyean wrote:
> Stuck again. My random monster-fight generator works, but it's so BASIC.
> How can I do this without the if-statements? Is it a bad thing to stick
> class objects into an array?
>
> Also, I know Moblist[0].fight works, but why not Moblist[0..2].fight ?
>
>
> ##############################################
> class Mob
> def fight
> "THIS IS A FIGHT METHOD"
> end
> end
>
> GOBO = Mob.new
> DRAGON = Mob.new
> SLIME= Mob.new
>
> Moblist = [GOBO, DRAGON, SLIME]
>
> def rand_fight
> a = rand(Moblist.length)#<-----------Has to be a better way|
> if a ==0 then GOBO.fight
> elsif a==1 then DRAGON.fight
> elsif a==2 then SLIME.fight
> end#------------------------------------------------------|
>
> end
>
> p rand_fight
> ##############################################

As the great Julian Raschke once said, one level of indirection solves
everything. :)

> Also, I know Moblist[0].fight works, but why
> not Moblist[0..2].fight ?

Because [0..2] is a Range object, which does not respond to the fight
method. You could monkey patch (*ducks*) Range to make it work, but a
better idea would be to take a step back and create a class responsible
for representing multiple monsters. GroupOfMonsters or MonsterParty,
perhaps? ;)

As with your rand(Moblist.length) bit, that should also be put into its
own class (or several classes). Think about what you're really trying to
say, then figure out the players (objects) and who is responsible for
doing what.

If you're trying to write anything remotely resembling a game, you've a
lot of work ahead of you.

Have you looked at _why's creature code?
http://poignantguide.net/ruby/chap...
--
Posted via http://www.ruby-....

Jon Garvin

4/22/2008 6:53:00 PM

0

Ben Galyean wrote:
> Stuck again. My random monster-fight generator works, but it's so BASIC.
> How can I do this without the if-statements? Is it a bad thing to stick
> class objects into an array?
>
> Also, I know Moblist[0].fight works, but why not Moblist[0..2].fight ?
>
>
> ##############################################
> class Mob
> def fight
> "THIS IS A FIGHT METHOD"
> end
> end
>
> GOBO = Mob.new
> DRAGON = Mob.new
> SLIME= Mob.new
>
> Moblist = [GOBO, DRAGON, SLIME]
>
> def rand_fight
> a = rand(Moblist.length)#<-----------Has to be a better way|
> if a ==0 then GOBO.fight
> elsif a==1 then DRAGON.fight
> elsif a==2 then SLIME.fight
> end#------------------------------------------------------|
>
> end
>
> p rand_fight
> ##############################################
>
Try this....

def rand_fight
monster = Moblist[rand(Moblist.length)]
monster.fight
end

or...

def rand_fight
Moblist[rand(Moblist.length)].fight
end


--

http://www.5va...

http://www.workingwithrails.com/p...


Phlip

4/22/2008 6:54:00 PM

0

Ben Galyean wrote:

> Stuck again. My random monster-fight generator works, but it's so BASIC.
> How can I do this without the if-statements? Is it a bad thing to stick
> class objects into an array?

In general, the answer to excess if-statements is Object Oriented programming.

In specific, however...

> a = rand(Moblist.length)#<-----------Has to be a better way|

mob = Moblist.sort_by{rand}

mob.each{|a| a.fight }

Todd Benson

4/22/2008 7:00:00 PM

0

On Tue, Apr 22, 2008 at 1:36 PM, Ben Galyean <bengalyean@hotmail.com> wrote:
> Stuck again. My random monster-fight generator works, but it's so BASIC.
> How can I do this without the if-statements? Is it a bad thing to stick
> class objects into an array?
>
> Also, I know Moblist[0].fight works, but why not Moblist[0..2].fight ?
>
>
> ##############################################
> class Mob
> def fight
> "THIS IS A FIGHT METHOD"
> end
> end
>
> GOBO = Mob.new
> DRAGON = Mob.new
> SLIME= Mob.new
>
> Moblist = [GOBO, DRAGON, SLIME]
>
> def rand_fight
> a = rand(Moblist.length)#<-----------Has to be a better way|
> if a ==0 then GOBO.fight
> elsif a==1 then DRAGON.fight
> elsif a==2 then SLIME.fight
> end#------------------------------------------------------|
>
> end
>
> p rand_fight
> ##############################################

I'm not sure why you use constants, but...

class C
def fight
puts 'hi, nice to meet you'
puts 'I am " << self.__id__.to_s
end
end

a = Array.new(3) {C.new}
puts a[rand(a.size)].fight


Just to illustrate.

Todd

Todd Benson

4/22/2008 7:47:00 PM

0

On Tue, Apr 22, 2008 at 2:00 PM, Todd Benson <caduceass@gmail.com> wrote:
>
> On Tue, Apr 22, 2008 at 1:36 PM, Ben Galyean <bengalyean@hotmail.com> wrote:
> > Stuck again. My random monster-fight generator works, but it's so BASIC.
> > How can I do this without the if-statements? Is it a bad thing to stick
> > class objects into an array?
> >
> > Also, I know Moblist[0].fight works, but why not Moblist[0..2].fight ?
> >
> >
> > ##############################################
> > class Mob
> > def fight
> > "THIS IS A FIGHT METHOD"
> > end
> > end
> >
> > GOBO = Mob.new
> > DRAGON = Mob.new
> > SLIME= Mob.new
> >
> > Moblist = [GOBO, DRAGON, SLIME]
> >
> > def rand_fight
> > a = rand(Moblist.length)#<-----------Has to be a better way|
> > if a ==0 then GOBO.fight
> > elsif a==1 then DRAGON.fight
> > elsif a==2 then SLIME.fight
> > end#------------------------------------------------------|
> >
> > end
> >
> > p rand_fight
> > ##############################################
>
> I'm not sure why you use constants, but...
>
> class C
> def fight
> puts 'hi, nice to meet you'
> puts 'I am " << self.__id__.to_s

Once again that weird todd person makes a slight mishap :) That double
quote is supposed to be a single one. And I really wanted to say
something a little more sinister, like "So, we meet again }>),
muahaha." but the nice thing is kind of funny.

Todd

Phlip

4/23/2008 3:03:00 PM

0

Jon Garvin wrote:

> def rand_fight
> monster = Moblist[rand(Moblist.length)]
> monster.fight
> end

If the OP is simulating a melee, such as a D20 battle, each monster should get
one full action per fight segment. Repeatedly pick a random monster might allow
some to get more than one action, and some none. That's why I sorted an array by
randomness.