Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vschaeperkoetter/9198915 to your computer and use it in GitHub Desktop.
Save vschaeperkoetter/9198915 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
#METHODS
#initialize: input: 2d array of boggle board letters. output: none
#create_word: input: coordinates of the 2d array from first to last. output: a word spelt using the boggle board
#get_row: input: row number, starting from 0. output: all letters that are indexed in the input row.
#get_column: input: column number, starting from 0 going from left to right. output: each string that has a "str"[col] referenced.
class BoggleBoard
def initialize(board)
@board = board
end
def create_word(*coords)
coords.map { |coord| @board[coord.first][coord.last]}.join("")
end
def get_row(row)
@board[row]
end
def get_col(col)
@board.collect {|str| str[col]}
end
end
dice_grid = [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
boggle_board = BoggleBoard.new(dice_grid)
# implement tests for each of the methods here:
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) == "code" #=> true
puts boggle_board.get_row(1) == ["i", "o", "d", "t"] #=> true
puts boggle_board.get_col(1) == ["r", "o", "c", "a"] #=> true
# create driver test code to retrieve a value at a coordinate here:
boggle_board.create_word([0,1]) == "r" #=. true
#Reflection:
#When I ran the first test in IRB, it kept yielding an error and could not create a word using #create_word. After looking it over,
#i realized that I put an extra } at the end of the line of code in the method. It reminded me to be very detail oriented while
#coding and to be mindful of those mistakes. Other than that, I found this challenge fairly easy since it took all of the things
#we have learned during the past 4 weeks and comined it into one. In this challenge, the only thing that needed to be changed was
#to create an initialie method and that all methods now contain only one parameter. board is no longer a parameter because @board
#is being used as an instance veriable now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment