From: Matthew Moss Date: 2006-08-16T10:23:41+09:00 Subject: Re: [QUIZ] Newbie doubts about the quiz > 4) You guys could teach me a super-cool Ruby-esque way to do that that > I can't even think about right now. I haven't submitted a solution... I started working on one, but just haven't had time to complete it. But I did start with something like this: class Coord attr_reader :x, :y def initialize(x, y) @x, @y = x, y end def valid(n) # returns true if coord fits on an n-square board (0...n).include?(@x) and (0...n).include?(@y) end def to_s "(#{@x}, #{@y})" end def adjacents [ Coord.new(@x, @y - 3), Coord.new(@x, @y + 3), Coord.new(@x - 3, @y), Coord.new(@x + 3, @y), Coord.new(@x - 2, @y - 2), Coord.new(@x - 2, @y + 2), Coord.new(@x + 2, @y - 2), Coord.new(@x + 2, @y + 2) ] end end Then, let's say you randomly start at (1, 3). You can get started like: c = Coord.new(1, 3) c.adjacents.each { |d| puts d if d.valid(5) } Not terribly efficient, but you know what they say... Vi is at the heart of evil... I mean, premature optimization is terribly effective... Wait a sec... That's not right.