[RUBY QUIZ #107]

A

Alex Watt

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top