Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
December 29, 2015 14:29
-
-
Save christina-taggart/7684369 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1
boggle 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
#I kept getting an error when passing boggle_board as an argument into initialize | |
#so I had to repeat @boggle_board as an instance variable, which I will fix when I understand what exactly happened | |
@boggle_board = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
class BoggleBoard | |
def initialize | |
#@boggle_board = boggle_board | |
@boggle_board = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
end | |
def create_word(board, *coords) | |
coords.map { |coord| board[coord.first][coord.last]}.join("") | |
#.first gets the first element of the coordinate array | |
#.join joins the separate elements of the array into one string ex: "a", "b" => "ab" | |
#.map maps the coordinate element from the board: | |
#ex: [1, 2, 3].map { |n| n * n } #=> [1, 4, 9] | |
end | |
def get_row(row) | |
@boggle_board[row].join("") | |
end | |
def get_col(col) | |
@boggle_board.map {|x| x[col]}.join("") | |
end | |
def get_coord(row, column) | |
return @boggle_board[row][column] | |
end | |
end | |
#driver code | |
board = BoggleBoard.new | |
puts board.create_word(@boggle_board, [2,1], [1,1], [1,2], [0,3]) #=> returns "code" | |
puts board.create_word(@boggle_board, [0,1], [0,2], [1,2]) #=> returns "rad" | |
puts board.create_word(@boggle_board, [2,2], [3,1], [3,2], [3,3]) #=> returns "lake" | |
puts board.create_word(@boggle_board, [1,3], [0,3], [0,2], [0,1]) #=> returns "tear" | |
puts board.get_row(0) #=> ["b", "r", "a, "e"] | |
puts board.get_row(1) #=> ["i", "o", "d", "t"] | |
puts board.get_row(2) #=> ["e", "c", "l", "r"] | |
puts board.get_col(0) #=> ["b", "i", "e", "t"] | |
puts board.get_col(1) #=> ["r", "o", "c", "a"] | |
puts board.get_col(2) #=> ["a", "d", "l", "k"] | |
puts board.get_coord(3,2) #=> "k" | |
puts board.get_coord(0,0) #=> "b" | |
puts board.get_coord(1,1) #=> "o" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment