2 questions about my array/range...

Discussion in 'Ruby' started by Kain Nobel, Jul 16, 2008.

  1. Kain Nobel

    Kain Nobel Guest

    Okay, so I wrote a script that'll allow the game player to jump with the
    touch of a button. This script, I wanted a method that'll adjust the
    distance in which
    the game player jumps, based on the farthest passible tile. To define
    the method, I used a 'for i' statement with 0 to the max distance, for
    each passible 'space' he can jump, I push the value into an array named
    '@spaces', respectively.

    Code:
      def jump_dist
    @spaces = []
    case @direction
    when 2 # Down
    for i in 0...Jump::Distance
    if passable?(self.x, self.y + i, 2)
    @spaces[i] = i
    return @spaces if i == Jump::Distance
    end
    end
    when 4 # Left
    for i in 0...Jump::Distance
    if passable?(self.x - i, self.y, 4)
    @spaces[i] = i
    return @spaces if i == Jump::Distance
    end
    end
    when 6 # Right
    for i in 0...Jump::Distance
    if passable?(self.x + i, self.y, 6)
    @spaces[i] = i
    return @spaces if i == Jump::Distance
    end
    end
    when 8 # Up
    for i in 0...Jump::Distance
    if passable?(self.x, self.y - i, 8)
    @spaces[i] = i
    return @spaces if i == Jump::Distance
    end
    end
    end
    end
    ...Maybe I wrote that a little oddly, correct me if you know a better
    functional method.

    Anyways, next I test it with 'print(jump_dist)' whenever the player
    'jumps' and it returns 0...3 (3 is Jump::Distance, BTW)

    So basically, instead of returning an array, it returns a Range.

    How do I get this to return me an array of only passable 'spaces', and
    secondly how do I pull the highest value out of that array?
    --
    Posted via http://www.ruby-forum.com/.
     
    Kain Nobel, Jul 16, 2008
    #1
    1. Advertising

  2. Hi --

    On Wed, 16 Jul 2008, Kain Nobel wrote:

    > Okay, so I wrote a script that'll allow the game player to jump with the
    > touch of a button. This script, I wanted a method that'll adjust the
    > distance in which
    > the game player jumps, based on the farthest passible tile. To define
    > the method, I used a 'for i' statement with 0 to the max distance, for
    > each passible 'space' he can jump, I push the value into an array named
    > '@spaces', respectively.
    >
    >
    Code:
      def jump_dist
    >    @spaces = []
    >    case @direction
    >    when 2 # Down
    >      for i in 0...Jump::Distance
    >        if passable?(self.x, self.y + i, 2)
    >          @spaces[i] = i
    >          return @spaces if i == Jump::Distance
    >        end
    >      end
    >    when 4 # Left
    >      for i in 0...Jump::Distance
    >        if passable?(self.x - i, self.y, 4)
    >          @spaces[i] = i
    >          return @spaces if i == Jump::Distance
    >        end
    >      end
    >    when 6 # Right
    >      for i in 0...Jump::Distance
    >        if passable?(self.x + i, self.y, 6)
    >          @spaces[i] = i
    >          return @spaces if i == Jump::Distance
    >        end
    >      end
    >    when 8 # Up
    >      for i in 0...Jump::Distance
    >        if passable?(self.x, self.y - i, 8)
    >          @spaces[i] = i
    >          return @spaces if i == Jump::Distance
    >        end
    >      end
    >    end
    >  end
    >
    > ...Maybe I wrote that a little oddly, correct me if you know a better
    > functional method.


    Well, here's an untested rewrite that might save some space:

    def jump_dist
    spaces = []

    0.upto(Jump::Distance-1) do |i|
    pass = case @direction
    when DOWN then passable?(self.x, self.y + i, 2)
    when LEFT then passable?(self.x - i, self.y, 4)
    when RIGHT then passable?(self.x + i, self.y, 6)
    when UP then passable?(self.x, self.y - 1, 8)
    end
    if pass
    spaces = i
    end
    end

    return spaces
    end

    (I've turned @spaces into spaces, which is better unless you really
    need it as an instance variable.)

    > Anyways, next I test it with 'print(jump_dist)' whenever the player
    > 'jumps' and it returns 0...3 (3 is Jump::Distance, BTW)
    >
    > So basically, instead of returning an array, it returns a Range.
    >
    > How do I get this to return me an array of only passable 'spaces', and
    > secondly how do I pull the highest value out of that array?


    The reason it's returning a range is that your range is exclusive. The
    range (0...Jump::Distance) doesn't include Jump::Distance. Therefore,
    the return statements never get executed. So what's returned, instead,
    is the value of the whole for statement, which is the range itself.

    To get the last element of an array: array[-1].


    David

    --
    Rails training from David A. Black and Ruby Power and Light:
    Intro to Ruby on Rails July 21-24 Edison, NJ
    Advancing With Rails August 18-21 Edison, NJ
    See http://www.rubypal.com for details and updates!
     
    David A. Black, Jul 16, 2008
    #2
    1. Advertising

  3. Kain Nobel

    Kain Nobel Guest

    Cool, that works wonders! Thanks David! *Adds special thanks to David in
    credit*

    I tried posting this in another forum and everybody wanted me to copy
    someone else's script!
    --
    Posted via http://www.ruby-forum.com/.
     
    Kain Nobel, Jul 17, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    46
    Views:
    997
    Antoon Pardon
    Jul 25, 2006
  2. Lambda
    Replies:
    2
    Views:
    424
    James Kanze
    Jul 16, 2008
  3. Tomoyuki Kosimizu

    Range does not take an Range object.

    Tomoyuki Kosimizu, Nov 25, 2003, in forum: Ruby
    Replies:
    3
    Views:
    170
    Tomoyuki Kosimizu
    Nov 27, 2003
  4. David Bird
    Replies:
    1
    Views:
    243
    Tiago Macedo
    Jun 23, 2008
  5. Joey Zhou
    Replies:
    5
    Views:
    246
    Joey Zhou
    Apr 15, 2011
Loading...

Share This Page