Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save christina-taggart/7684369 to your computer and use it in GitHub Desktop.
Save christina-taggart/7684369 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
#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