Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:56
-
-
Save albaer/9178469 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle 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 | |
attr_reader :board | |
def initialize(board) | |
@board = board | |
end | |
def create_word(*coords) | |
coords.map { |coord| @board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
return @board[row] | |
end | |
def get_col(col) | |
column = [] | |
@board.each { |row| column << row[col] } | |
return column | |
end | |
def get_diagonals | |
left_diagonal = [] | |
(0...board.size).each {|index| left_diagonal << @board[index][index] } | |
right_diagonal = [] | |
(0...board.size).each {|index| right_diagonal << @board[@board.size - index - 1][index] } | |
@board | |
return [left_diagonal, right_diagonal] | |
end | |
end | |
# DRIVER CODE | |
dice_grid = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
boggle_board = BoggleBoard.new(dice_grid) | |
# Now print out all the rows and columns of the board as strings. | |
(0...boggle_board.board.size).each do |index| | |
row_array = boggle_board.get_row(index) | |
row_string = row_array.join | |
puts row_string | |
col_array = boggle_board.get_col(index) | |
col_string = col_array.join | |
puts col_string | |
end | |
# Output from above: | |
# brae | |
# biet | |
# iodt | |
# roca | |
# eclr | |
# adlk | |
# take | |
# etre | |
puts "accessing coordinates:" | |
puts boggle_board.board[3][2] == "k" | |
puts boggle_board.board[0][0] == "b" | |
puts boggle_board.board[2][1] == "c" | |
puts "creating words:" | |
puts boggle_board.create_word([2,1], [3,1], [3,2], [3,3]) == "cake" | |
puts boggle_board.create_word([0,0], [1,0], [2,1], [3,2], [3,3], [2,3]) == "bicker" | |
puts boggle_board.create_word([2,2], [1,1], [2,1], [3,2], [3,3], [2,3]) == "locker" | |
puts "getting rows:" | |
puts boggle_board.get_row(1) == ["i", "o", "d", "t"] | |
puts boggle_board.get_row(3) == ["t", "a", "k", "e"] | |
puts boggle_board.get_row(0) == ["b", "r", "a", "e"] | |
puts "getting columns:" | |
puts boggle_board.get_col(1) == ["r", "o", "c", "a"] | |
puts boggle_board.get_col(3) == ["e", "t", "r", "e"] | |
puts boggle_board.get_col(0) == ["b", "i", "e", "t"] | |
puts "getting diagonals:" | |
puts boggle_board.get_diagonals == [["b", "o", "l", "e"], ["t", "c", "d", "e"]] | |
# REFLECTION | |
# I wasn't sure what they meant by the get_diagonal method, since any entry would | |
# be part of two different diagonals, so I just created a method that printed the long | |
# diagonals. Since this is a solo challenge, I won't be able to check other people's | |
# solutions, but I'm curious how they interpreted those instructions since I feel | |
# like I'm missing something. Now that I am getting used to working within classes, | |
# I find it is much easier to organize my methods. When I was working on the first | |
# boggle challenge, I even found myself thinking, "Wouldn't this make more sense if | |
# we put this all in a Boggle class?" I'm still a little iffy on making variables | |
# accessible in the right places, but it's better than it was a week or so ago. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment