Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Created
January 10, 2014 00:29
-
-
Save nihwang/8344786 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1
boggle class challenge
This file contains 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
class BoggleBoard | |
def initialize(board) #this means we can have more than one board | |
@board = board | |
end | |
def create_word(*coords) | |
coords.map { |coord| @board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
puts @board[row].join("") | |
end | |
def get_col(col) | |
puts @board.transpose[col].join("") | |
end | |
def get_coord(coord1, coord2) | |
puts @board[coord1][coord2] | |
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.create_word([1,2], [1,1], [2,1], [3,2]) #=> dock | |
boggle_board.get_row(0) #=> brae | |
boggle_board.get_row(1) #=> iodt | |
boggle_board.get_row(2) #=> eclr | |
boggle_board.get_row(3) #=> take | |
boggle_board.get_col(0) #=> biet | |
boggle_board.get_col(1) #=> roca | |
boggle_board.get_col(2) #=> adlk | |
boggle_board.get_col(3) #=> etre | |
boggle_board.get_coord(3, 2) | |
#Reflection | |
=begin | |
This was very similar to the last exercise that I just completed. I thought that it was easier to write in object-oriented programming | |
because now I can have multiple instances of a boggle board instead of just one. Right now all I am still not able to figure out how to | |
write a get_diagonal method other than thinking about the pseudo code. Right now all I have figured out is that I should probably first check | |
that the coordinated given are the corners of the board to that it would actually be a diagonal. And in this case I could write a | |
raise argument error if the coordinates are not the corners of the board. Until then I will update this gist as I go | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment