[RUBY QUIZ #107]

Discussion in 'Ruby' started by Alex Watt, Jan 1, 2007.

  1. Alex Watt

    Alex Watt Guest

    Unfortunately, I've only done 1 extra credit thing (wildcard characters). I
    couldn't think of a good way of doing the snaking text without adding many
    more lines, so I didn't.

    class Array
    def positions(value)
    positions = []
    each_index { |i| positions << i if self == value }
    positions
    end
    end

    class WordSearch
    def initialize(wordSearch,*words)
    @wordSearch = wordSearch
    @rows = @wordSearch.split(/\s+/).delete_if { |row| row == "" }
    @letters = @wordSearch.gsub(/\s+/,"").split(//)
    @words = *words
    @letterPositions = []
    @rowLength = @rows[0].length
    @numberOfRows = @rows.length
    end
    def solve
    @words.each { |word|
    @letters.positions(word[0].chr).each { |letterPos|
    tooCloseToRight = (@rowLength - (letterPos % @rowLength)) <
    word.length
    tooCloseToLeft = letterPos % @rowLength < word.length
    tooCloseToTop = (letterPos / @rowLength) < word.length
    tooCloseToBottom = (@numberOfRows - (letterPos / @rowLength).to_i) <
    word.length
    search(word,letterPos,1) unless tooCloseToRight
    # to the right
    search(word,letterPos,-1) unless tooCloseToLeft
    # to the left

    search(word,letterPos,-(@rowLength - 1)) unless tooCloseToTop or
    tooCloseToRight # top right diagonal
    search(word,letterPos,-(@rowLength)) unless tooCloseToTop
    # above
    search(word,letterPos,-(@rowLength + 1)) unless tooCloseToTop or
    tooCloseToLeft # top left diagonal

    search(word,letterPos,@rowLength + 1) unless tooCloseToBottom or
    tooCloseToRight # bottom right diagonal
    search(word,letterPos,@rowLength) unless tooCloseToBottom
    # below
    search(word,letterPos,@rowLength - 1) unless tooCloseToBottom or
    tooCloseToLeft # bottom left diagonal
    }
    }
    end
    def search(word,pos,direction)
    positions = []
    for i in (0...word.length)
    positions << (pos + direction*i) if word.chr == @letters[pos +
    direction*i] or word.chr == "*" or @letters[pos + direction*i] == "*"
    end
    @letterPositions << positions if positions.length == word.length
    end
    def printWordSearch
    @letterPositions.flatten!
    @letters.each_index { |i|
    character = @letterPositions.include?(i) ? @letters : "+"
    print character
    if (i + 1) % @rows[0].length == 0
    print "#{i/@rows[0].length + 1}".rjust(6)
    puts
    end
    }
    end
    end

    wordSearch = []
    loop do
    row = gets.chomp
    wordSearch << row
    break if row == ""
    end
    wordSearch.delete_at(-1)
    words = gets.chomp.upcase.split(/\s*,\s*/)
    w = WordSearch.new(wordSearch.join("\n"),words)
    w.solve
    w.printWordSearch

    _________________________________________________________________
    Find Singles In Your Area This Christmas With Match.com! msnuk.match.com
     
    Alex Watt, Jan 1, 2007
    #1
    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. raylopez99
    Replies:
    5
    Views:
    532
    Thornhill
    Aug 27, 2008
  2. Innkeeper

    IP 131.107.115.28

    Innkeeper, Nov 16, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    387
    Innkeeper
    Nov 16, 2008
  3. David Tran
    Replies:
    9
    Views:
    195
    David Tran
    Jan 21, 2005
  4. Ruby Quiz

    [QUIZ] Word Search (#107)

    Ruby Quiz, Dec 29, 2006, in forum: Ruby
    Replies:
    6
    Views:
    153
    William James
    Jan 3, 2007
  5. Ruby Quiz

    [SUMMARY] Word Search (#107)

    Ruby Quiz, Jan 4, 2007, in forum: Ruby
    Replies:
    0
    Views:
    103
    Ruby Quiz
    Jan 4, 2007
Loading...

Share This Page