[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: I need serious help!

seebs

7/15/2007 10:54:00 PM

In message <73de925a9135b8350e6ee18f9b5d62ad@ruby-forum.com>, Joe Wiltrout writes:
>But with useless advice on Hello World
>program things, it makes it hard to really care how you feel.

Except the advice wasn't useless. Pearls before swine, maybe, but the flaw
is not in the advice itself.

-s

5 Answers

Joe Wiltrout

7/15/2007 11:06:00 PM

0

unknown wrote:
> In message <73de925a9135b8350e6ee18f9b5d62ad@ruby-forum.com>, Joe
> Wiltrout writes:
>>But with useless advice on Hello World
>>program things, it makes it hard to really care how you feel.
>
> Except the advice wasn't useless. Pearls before swine, maybe, but the
> flaw
> is not in the advice itself.
>
> -s


Not seeing your point. Your saying that the advice givers are the stupid
ones? Or the delivery of the advice was in thw wrong?

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

Matthew Rudy Jacobs

7/15/2007 11:37:00 PM

0

Joe Wiltrout wrote:
> unknown wrote:
>> In message <73de925a9135b8350e6ee18f9b5d62ad@ruby-forum.com>, Joe
>> Wiltrout writes:
>>>But with useless advice on Hello World
>>>program things, it makes it hard to really care how you feel.
>>
>> Except the advice wasn't useless. Pearls before swine, maybe, but the
>> flaw
>> is not in the advice itself.
>>
>> -s
>
>
> Not seeing your point. Your saying that the advice givers are the stupid
> ones? Or the delivery of the advice was in thw wrong?

Let's just relax.

The reason "hello world" was brought out was because it's a standard
"first program" that people write.

Telling you to go write "hello world" they were saying;
"take a minute, go look at how ruby works"

the next step is to go look at variables, control structure and so on.
because if you don't understand how to implement logic in the language,
then you can't write a game.

for example, let's pretend we're writing a game now;
install ruby and run "irb" from the command line;


let's define a map of locations that looks like;

aa(blocked) ba ca
ab bb cb(trap)
ac

they have a left, right, up, down method that points to the location in
that direction if there is one.

they also have a "blocked" and "trap" method

so lets define our Location class

class Location
attr_accessor :left, :right, :up, :down, :trap, :blocked
end

and we'll declare our instances;

aa, ba, ca, ab, bb, cb, ac = *(7.to_a.map{|i| Location.new})

aa.blocked = true
aa.right = ba
aa.down = ab

ba.left = aa
ba.right = ca
ba.down = bb

ca.left = ba
ca.down = cb

ab.up = aa
ab.right = bb
ab.down = ac

.... do the rest ......

cb.trap = true


now we have all of the locations defined

in irb we can write

> aa.right
-> <# Location #>

and it will return us the object we call ba
we can check this;

> aa.right == ba
-> true

> aa.blocked
-> true


so we can write a method that moves us around here.

first we'll define an instance variable to say where we are presently;

> @current_location = ba

> def move(direction)
>
> want_to_move = @current_location.send(direction)
>
> if want_to_move.nil?
> puts "You can't go that way"
>
> elsif want_to_move.blocked
> puts "sorry, you can't go that way, it's blocked"
>
> elsif wants_to_move.trap
> puts "ahh, you got trapped"
> @current_location = want_to_move
> else
> puts "you've moved #{direction}. what next?"
> @current_location = want_to_move
> end
> end


now in the console we can right

move(:right)
-> "you've moved right, what next?"

move(:down)
-> "ahh, you got trapped"

and so on.

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

Matthew Rudy Jacobs

7/15/2007 11:39:00 PM

0

correction:
aa, ba, ca, ab, bb, cb, ac = (1..7).to_a.map{|i| Location.new}

> aa, ba, ca, ab, bb, cb, ac = *(7.to_a.map{|i| Location.new})

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

Joe Wiltrout

7/15/2007 11:50:00 PM

0

Ok. When you put it that way, it's much simpler. But could you rename
your aa, ab, etc to names such as Lake, Stream, Forest, Mountain,
Penobscot? Or are we stuck with aa ab ac bb ba bc ca cb ca et, c..

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

Matthew Rudy Jacobs

7/15/2007 11:54:00 PM

0

yeah

they'd have to be lowercase though.

ideally you'd define

class Location
attr_reader :name
....

def initialize(name)
@name = name
end
end

then you'd define

> lake = Location.new("lake")

> lake.name
-> "lake"

and such...

you'd probably want to use an array of locations instead,
but that'd be harder to understand maybe

Joe Wiltrout wrote:
> Ok. When you put it that way, it's much simpler. But could you rename
> your aa, ab, etc to names such as Lake, Stream, Forest, Mountain,
> Penobscot? Or are we stuck with aa ab ac bb ba bc ca cb ca et, c..


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