Joe W. wrote:
unknown wrote:
In message [email protected], 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.