The Easiest Way to Save and Share Code Snippets on the web

Ruby Quiz #211 Solution by Chien Po

ruby

posted: Jun, 29th 2009 | jump to bottom

#!/usr/bin/env ruby
 
class Board
  MAX = 10
  CHR = '@'
 
  #
  # Load game board and build coordinate list
  #
  def initialize(io)
    @board  = []
    @coords = []
    io.each { |line| @board << line }
    x, y    = first
    abort "No block characters ('#{CHR}') found in board!" unless x and y
    add(x, y, 1)
  end
 
  #
  # Print game board and coordinate list
  #
  def display
    puts "\nGame Board:", @board
    puts "\nCoordinate List:"
    puts @coords.map { |c| "(#{c.first.to_s.rjust(2)}, #{c.last.to_s.rjust(2)})" }, "\n"
  end
 
  private
    #
    # Find first block character
    #
    def first
      x, y = nil, 0
      @board.each do |line|
        x = line =~ /@/ and break
        y += 1
      end
      [x, y]
    end
 
    #
    # Adds provided coords to collection if unique and referencing a block
    # character. Recursively adds adjacent squares as well.
    #
    def add(x, y, n)
      return unless x >= 0 and y >= 0 and n <= MAX
      return unless x < @board.first.length and y < @board.length
      return unless @board[y][x].chr == CHR and not @coords.include?([x, y])
      @coords << [x, y]
      (x-1).upto(x+1) do |nx|
        (y-1).upto(y+1) do |ny|
          add(nx, ny, n+1)
        end
      end
    end
end
 
Board.new(DATA).display
 
__END__
....................
....................
.........@@.........
........@...........
........@@@@@.......
.............@......
............@.......
....................
....................
....................
 
75 views