Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:55
-
-
Save timoshka88/8726268 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
class BoggleBoard | |
attr_reader :board # make the board possible to view by using the attribute reader | |
def initialize (board) | |
puts "Board is ready" | |
@board = board | |
end | |
#initializing the default variable of a newly created instance. And every time a new instance is created, the user will get | |
# a notification | |
def create_word(*coords) #puts create_word(boggle_board, [2,1], [1,1], [1,2], [0,3]) #=> returns "code" | |
coords.map {|coord| @board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) #get_row(1) #=> ["i", "o", "d", "t"] | |
raise ArgumentError.new("You can choose only from 0 to 3") if row >= 4 | |
@board.values_at(row).flatten | |
end | |
def get_col(col) #get_col(1) #=> ["r", "o", "c", "a"] | |
raise ArgumentError.new("You can choose only from 0 to 3") if col >= 4 | |
@column_values = [] | |
@board_no_nested_arrays = @board.flatten | |
while col < @board_no_nested_arrays.length | |
@column_values = @column_values.push(@board_no_nested_arrays.values_at(col)) | |
col += 4 | |
end | |
@column_values.flatten | |
end | |
def get_diagonal(row = 0, col = 0) | |
@diagonal = [] | |
while row < 4 && col < 4 #4 since the lenght of our nested array is == 4 | |
@diagonal = @diagonal.push(@board[row][col]) | |
row +=1 | |
col +=1 | |
end | |
return @diagonal | |
end | |
end | |
dice_grid = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
##------------------------------------------------------------ | |
##Refactored | |
class BoggleBoard | |
attr_reader :board # make the board possible to view | |
def initialize (board) | |
puts "Board is ready" | |
@board = board | |
end | |
def create_word(*coords) #puts create_word(boggle_board, [2,1], [1,1], [1,2], [0,3]) #=> returns "code" | |
coords.map {|coord| @board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) #get_row(1) #=> ["i", "o", "d", "t"] | |
raise ArgumentError.new("You can choose only from 0 to 3") if row >= 4 | |
@board[row] | |
end | |
def get_col(row) #get_col(1) #=> ["r", "o", "c", "a"] | |
raise ArgumentError.new("You can choose only from 0 to 3") if row >= 4 | |
array_of_column_values = [] | |
@board[row].each_index{|i| array_of_column_values << @board[i][row]} | |
array_of_column_values | |
end | |
def get_diagonal(row = 0, col = 0) | |
diagonal = [] | |
while row < 4 && col < 4 | |
diagonal = diagonal.push(@board[row][col]) | |
row +=1 | |
col +=1 | |
end | |
return diagonal | |
end | |
end | |
##------------------------------------------------------------- | |
##Driver Test Code | |
boggle_board = BoggleBoard.new(dice_grid) #=> "Board ready" | |
p boggle_board.board[3][2] #=> "k". by calling the #board method | |
p boggle_board.create_word([2,1],[1,1],[1,2],[0,3]) #=>"code" | |
p boggle_board.get_col(2) #=>["a", "d", "l", "k"] | |
p boggle_board.get_row(1) #=> ["i", "o", "d", "t"] | |
p boggle_board.get_diagonal(1,2) #=> ["d", "r"] | |
##REFLECTION | |
#As in the previous challenge, where I just had to create methods, I refactored my solution to more easy code, more elegant | |
#and understandable. | |
#Second of all, I understood that I don't need to create an instance variable for everything, I can just create the main | |
#instance variable which will be the default variables for any created instance. | |
#comparing to the exercise, where I just had to create methods, and where I passed in two parameters, here I could pass in only one. | |
#One of the most big things for me was to work a bit more with the initialize method, to understand it more. | |
#To see what it does for each newly created instance. So, it was a really good practice. | |
#I'm mostly glad that in all of these exercise I almost didn't use the web. Most of the methods have been already used, | |
#iteration has been practiced, so I really enjoyed it. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment