Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
December 28, 2015 00:39
-
-
Save gsperka/7414574 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(dice_grid) | |
@dice_grid = dice_grid | |
end | |
def create_word(board, *coords) | |
coords.map { |coord| @dice_grid[coord.first][coord.last]}.join("") | |
end | |
def get_row(number) | |
@dice_grid[number] | |
end | |
def get_col(col) | |
column = [] | |
@dice_grid | |
@dice_grid.each do |row| | |
column_element = row[col] | |
column << column_element | |
end | |
column | |
end | |
def get_diagonal(row, col) | |
diag_array = [] | |
diag_array << @dice_grid[row][col] | |
until row == 3 || col == 3 | |
diag_array << @dice_grid[row += 1][col += 1] | |
end | |
diag_array | |
end | |
def print | |
0..@dice_grid.length.times do |i| | |
puts get_row(i+1).join | |
puts get_col(i+1).join | |
end | |
end | |
end | |
# implement tests for each of the methods here: | |
boggle_board = BoggleBoard.new([["b", "r", "a", "e"],["i", "o", "d", "t"], ["e", "c", "l", "r"],["t", "a", "k", "e"]]) | |
p boggle_board.get_row(2) #=> returns ["e", "c", "l", "r"] | |
p boggle_board.get_row(3) #=> returns ["t", "a", "k", "e"] | |
p boggle_board.get_col(2) #=> returns ["r","o","c","a"] | |
p boggle_board.get_col(3) #=> returns ["e","t","r","e"] | |
puts boggle_board.create_word(boggle_board, [1,2], [1,1], [2,1], [3,2]) #=> returns "dock" | |
puts boggle_board.create_word(boggle_board, [0,1], [0,2], [1,2]) #=> creates what california slang word? | |
# created words: | |
puts boggle_board.create_word(boggle_board, [0,0],[1,0],[3,0],[2,0]) #=> "bite" | |
puts boggle_board.create_word(boggle_board, [0,1],[1,1],[2,1],[3,2]) #=> "rock" | |
# print all rows and columns: | |
boggle_board.print | |
#brae | |
#biet | |
#iodt | |
#roca | |
#eclr | |
#adlk | |
#take | |
#etre | |
# create driver test code to retrieve a value at a coordinate here: | |
# access k at row 3 col 2 | |
p boggle_board.create_word([3,2]) | |
# print diagonal | |
p boggle_board.get_diagonal(1,2) #=> ["d", "r"] | |
p boggle_board.get_diagonal(0,3) #=> ["e"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment