Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:56
-
-
Save sbeverly/9163659 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dice_grid = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
class BoggleBoard | |
attr_accessor :boggle_board | |
def initialize(dice_grid = true) | |
if dice_grid == false # Create random board if dice_grid is false. | |
@boggle_board = [] | |
rows.times {@boggle_board << ("a".."z").to_a.sample(columns)} | |
else | |
@boggle_board = dice_grid | |
end | |
end | |
def create_word(*coords) | |
coords.map { |coord| @boggle_board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row_number) | |
@boggle_board[row_number] | |
end | |
def get_column(column_number) | |
@boggle_board.map {|row| row[column_number]} | |
end | |
# Method currently does not work. Generate no output. Even if it worked the way I wanted, it would be | |
# dependent on the user providing the correct first and last coordinates corresponding to a diagonal line. | |
def get_diagonal(coord, coord2) | |
if coord.last < coord2.last and coord.first < coord2.first | |
counter = 0 | |
until counter == (coord.first.to_i - coord2.first.to_i).abs | |
@boggle_board.map {|letter| letter[coord.first + counter][coord.last + counter]} | |
counter += 1 | |
end | |
return @diagonal | |
elsif coord.last > coord2.last and coord.first < coord2.first | |
counter = (coord.first.to_i - coord2.first.to_i).abs | |
until counter == 0 | |
@boggle_board.map {|letter| letter[coord.first - counter][coord.last - counter]} | |
counter -= 1 | |
end | |
end | |
end | |
def show_board | |
@boggle_board.each {|x| print x.join(" ") + "\n"} | |
end | |
end | |
boggle_board = BoggleBoard.new(dice_grid) | |
# implement tests for each of the methods here: | |
puts boggle_board.create_word([1, 2], [1, 1], [2, 1], [3,2]) # => Should puts 'dock' | |
print boggle_board.get_row(0) #=> Should print ["b", "r", "a", "e"] | |
puts boggle_board.get_diagonal([0,0], [3,3]) #=> Getting no output from this currently, did not have enough time to work out the problem. | |
# create driver test code to retrieve a value at a coordinate here: | |
puts boggle_board.boggle_board[0][0] #=> 'b' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment