Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:56
-
-
Save akfoster/9166638 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
=begin | |
Create a class called BoggleBoard | |
initialize should accept an argument(board) and set an instance variable(@boggle_board) equal to the argument | |
create_word should accept coordinates(coords) as arguments - for each coordinate, should take the | |
letter from the board and then join the letters together | |
get_row should return the array at the index given as the argument(row) | |
get_col should return an array with letters from each array at the index given by the argument(col) | |
get_diag should return an array with letters from each array based on the index as determined by the starting index | |
and by the direction given for the diagonal(diag_direction) | |
If the direction is left and down, the row increases by 1 and the column decreases by 1 | |
If the direction is left and up, the row decreases by 1 and the column decreases by 1 | |
If the direction is right and down, the row increases by 1 and the column increases by 1 | |
If the direction is right and up, the row decreases by 1 and the column increases by 1 | |
(based on the instructions for the challenge, it seems like we might be expected to take two sets of coordinates, | |
determine whether they create a diagonal, then return the letters between them - if I can refactor the method to work that way by | |
Sunday, I'll add that) | |
=end | |
class BoggleBoard | |
def initialize(board) | |
@boggle_board = board | |
end | |
def create_word(*coords) | |
coords.map { |coord| @boggle_board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
@boggle_board[row].to_s | |
end | |
def get_col(col) | |
boggle_column = [] | |
@boggle_board.each {|boggle_array| boggle_column << boggle_array[col]} | |
boggle_column.to_s | |
end | |
def get_diag(start_diag, diag_direction) | |
boggle_diag = [@boggle_board[start_diag.first][start_diag.last]] | |
case diag_direction | |
when "left_down" | |
left_down_row = start_diag.first + 1 | |
left_down_col = start_diag.last - 1 | |
while left_down_row < @boggle_board.length | |
boggle_diag << @boggle_board[left_down_row][left_down_col] | |
left_down_row = left_down_row + 1 | |
left_down_col = left_down_col - 1 | |
end | |
when "left_up" | |
left_up_row = start_diag.first - 1 | |
left_up_col = start_diag.last - 1 | |
while left_up_row >= 0 | |
boggle_diag << @boggle_board[left_up_row][left_up_col] | |
left_up_row = left_up_row - 1 | |
left_up_col = left_up_col - 1 | |
end | |
when "right_down" | |
right_down_row = start_diag.first + 1 | |
right_down_col = start_diag.last + 1 | |
while right_down_row < @boggle_board.length | |
boggle_diag << @boggle_board[right_down_row][right_down_col] | |
right_down_row = right_down_row + 1 | |
right_down_col = right_down_col + 1 | |
end | |
when "right_up" | |
right_up_row = start_diag.first - 1 | |
right_up_col = start_diag.last + 1 | |
while right_up_row >= 0 | |
boggle_diag << @boggle_board[right_up_row][right_up_col] | |
right_up_row = right_up_row - 1 | |
right_up_col = right_up_col + 1 | |
end | |
end | |
return boggle_diag.to_s | |
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.get_diag([0,0], "right_down") #=> returns ["b", "o", "l", "e"] | |
puts boggle_board.get_diag([0,3], "left_down") #=> returns ["e", "d", "c", "t"] | |
puts boggle_board.get_diag([3,0], "right_up") #=> returns ["t", "c", "d", "e"] | |
puts boggle_board.get_diag([3,3], "left_up") #=> returns ["e", "l", "o", "b"] | |
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> returns "dock" | |
puts boggle_board.get_row(0) #=> returns ["b", "r", "a", "e"] | |
puts boggle_board.get_row(1) #=> returns ["i", "o", "d", "t"] | |
puts boggle_board.get_row(2) #=> returns ["e", "c", "l", "r"] | |
puts boggle_board.get_row(3) #=> returns ["t", "a", "k", "e"] | |
puts boggle_board.get_col(0) #=> returns ["b", "i", "e", "t"] | |
puts boggle_board.get_col(1) #=> returns ["r", "o", "c", "a"] | |
puts boggle_board.get_col(2) #=> returns ["a", "d", "l", "k"] | |
puts boggle_board.get_col(3) #=> returns ["e", "t", "r", "e"] | |
# create driver test code to retrieve a value at a coordinate here: | |
puts boggle_board.create_word([3,2]) #=> returns "k" | |
=begin | |
Reflection | |
On the previous challenge, I rewrote it to be a class because it seemed easier | |
than writing individual methods. That made this exercise quite easy. When each method is | |
created individually, the variable for the boggle board needs to be copied into each one. | |
When a class is used, the variable for the boggle board is created once and made available | |
to each method. | |
Since my refactored code got quite long, it's below: | |
=end | |
=begin | |
Create a class called BoggleBoard | |
initialize should accept an argument(board) and set an instance variable(@boggle_board) equal to the argument | |
create_word should accept coordinates(coords) as arguments - for each coordinate, should take the | |
letter from the board and then join the letters together | |
If there are less than 3 letters, should return "That word is not long enough." | |
If one letter is not touching the next, should return "That is not a valid word." | |
get_row should return the array at the index given as the argument(row) | |
get_col should return an array with letters from each array at the index given by the argument(col) | |
If it is only given one row, it should return the letter from that array at the index given by the argument(col) | |
get_diag should return an array with letters from each array based on the index as determined by the starting index | |
and by the direction given for the diagonal(diag_direction) | |
If the direction is left and down, the row increases by 1 and the column decreases by 1 | |
If the direction is left and up, the row decreases by 1 and the column decreases by 1 | |
If the direction is right and down, the row increases by 1 and the column increases by 1 | |
If the direction is right and up, the row decreases by 1 and the column increases by 1 | |
(based on the instructions for the challenge, it seems like we might be expected to take two sets of coordinates, | |
determine whether they create a diagonal, then return the letters between them - if I can refactor the method to work that way by | |
Sunday, I'll add that) | |
is_diag should return the direction for the diagonal for get_diag based on two coordinates passed into get_diag | |
If the two coordinates do not form a diagonal, should return "That is not a diagonal." | |
Otherwise, should pass the appropriate diagonal direction(diag_direction) | |
=end | |
class BoggleBoard | |
def initialize(board) | |
@boggle_board = board | |
end | |
def create_word(*coords) | |
index = 0 | |
until index == coords.length-1 || @touching == false | |
first_coords = coords[index] | |
second_coords = coords[index + 1] | |
letters_touch?(first_coords, second_coords) | |
index += 1 | |
end | |
if coords.length < 3 | |
return "That word is not long enough." | |
elsif @touching == false | |
return "That is not a valid word." | |
else | |
coords.map { |coord| @boggle_board[coord.first][coord.last]}.join("") | |
end | |
end | |
private | |
def letters_touch? (first_coords, second_coords) | |
first_letters_touch(first_coords, second_coords) && second_letters_touch(first_coords, second_coords) ? @touching = true : @touching = false | |
end | |
def first_letters_touch (first_coords, second_coords) | |
first_coords.first == second_coords.first || first_coords.first + 1 == second_coords.first || first_coords.first - 1 == second_coords.first | |
end | |
def second_letters_touch (first_coords, second_coords) | |
first_coords.last == second_coords.last ||first_coords.last + 1 == second_coords.last || first_coords.last - 1 == second_coords.last | |
end | |
public | |
def get_row(row) | |
@boggle_board[row] | |
end | |
def get_col(col) | |
@boggle_board.transpose[col] | |
end | |
def get_letter(row, col) | |
@boggle_board[row][col] | |
end | |
def get_diag(start_diag, end_diag) | |
@start_diag = start_diag | |
@end_diag = end_diag | |
is_diag(@start_diag, @end_diag) | |
boggle_diag = [@boggle_board[start_diag.first][start_diag.last]] | |
down_row = start_diag.first + 1 | |
left_col = start_diag.last - 1 | |
up_row = start_diag.first - 1 | |
right_col = start_diag.last + 1 | |
case @diag_direction | |
when "left_down" | |
while down_row <= end_diag.first | |
boggle_diag << @boggle_board[down_row][left_col] | |
down_row = down_row + 1 | |
left_col = left_col - 1 | |
end | |
when "left_up" | |
while up_row >= end_diag.first | |
boggle_diag << @boggle_board[up_row][left_col] | |
up_row = up_row - 1 | |
left_col = left_col - 1 | |
end | |
when "right_down" | |
while down_row <= end_diag.first | |
boggle_diag << @boggle_board[down_row][right_col] | |
down_row = down_row + 1 | |
right_col = right_col + 1 | |
end | |
when "right_up" | |
while up_row >= end_diag.first | |
boggle_diag << @boggle_board[up_row][right_col] | |
up_row = up_row - 1 | |
right_col = right_col + 1 | |
end | |
end | |
if boggle_diag.length == 1 | |
return "That is not a diagonal." | |
else | |
return boggle_diag | |
end | |
end | |
private | |
def is_diag (start_diag, end_diag) | |
down_row = start_diag.first + 1 | |
left_col = start_diag.last - 1 | |
up_row = start_diag.first - 1 | |
right_col = start_diag.last + 1 | |
until down_row == end_diag.first || up_row == end_diag.first | |
down_row = down_row + 1 | |
left_col = left_col - 1 | |
up_row = up_row - 1 | |
right_col = right_col + 1 | |
end | |
if down_row == end_diag.first && left_col == end_diag.last | |
return @diag_direction = "left_down" | |
elsif down_row == end_diag.first && right_col == end_diag.last | |
return @diag_direction = "right_down" | |
elsif up_row == end_diag.first && left_col == end_diag.last | |
return @diag_direction = "left_up" | |
elsif up_row == end_diag.first && right_col == end_diag.last | |
return @diag_direction = "right_up" | |
else | |
return @diag_direction = "That is not a diagonal." | |
end | |
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) | |
puts boggle_board.get_diag([0,0], [3,3]) == ["b", "o", "l", "e"] | |
puts boggle_board.get_diag([0,3], [3,0]) == ["e", "d", "c", "t"] | |
puts boggle_board.get_diag([3,0], [0,3]) == ["t", "c", "d", "e"] | |
puts boggle_board.get_diag([3,3], [0,0]) == ["e", "l", "o", "b"] | |
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) == "dock" | |
puts boggle_board.create_word([0,0], [0,1], [0,2], [3,0]) == "That is not a valid word." | |
puts boggle_board.get_row(0) == ["b", "r", "a", "e"] | |
puts boggle_board.get_row(1) == ["i", "o", "d", "t"] | |
puts boggle_board.get_row(2) == ["e", "c", "l", "r"] | |
puts boggle_board.get_row(3) == ["t", "a", "k", "e"] | |
puts boggle_board.get_col(0) == ["b", "i", "e", "t"] | |
puts boggle_board.get_col(1) == ["r", "o", "c", "a"] | |
puts boggle_board.get_col(2) == ["a", "d", "l", "k"] | |
puts boggle_board.get_col(3) == ["e", "t", "r", "e"] | |
puts boggle_board.create_word([3,2]) == "That word is not long enough." | |
puts boggle_board.get_diag([0,3], [3,1]) == "That is not a diagonal." | |
puts boggle_board.get_diag([0,0], [2,2]) == ["b", "o", "l"] | |
puts boggle_board.get_letter(3,2) == "k" | |
=begin | |
Reflection | |
The refactoring of this ended up getting pretty complicated. I wanted to make sure create_word | |
checked for the validity of words, which also meant I couldn't use it for the driver code to find a particular | |
letter. I tried to chain the get_row and get_col to do so, but couldn't figure out a way to re-write them to allow that | |
to work. While I'm sure there's a way to do that, I ended up just writing a new method. | |
I also wrote new methods to check whether letters were touching so that I could check for the validity of a word and whether | |
two given coordinates were on a diagonal. All of these ended up being private methods since I was calling | |
them from other methods to check for certain things. | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment