Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Created
February 4, 2014 05:15
-
-
Save ieatkimchi/8798488 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 Boggle | |
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] | |
end | |
def get_col(col) | |
@boggle_board.transpose[col] | |
# transpose is like a specific usage of zip | |
# while zip is the general usage | |
end | |
def get_diagonal(a,b) | |
return (0..3).map{ |n| @boggle_board[n][n] } if a[0] == a[1] and b[0] == b[1] | |
return (0..3).map{ |n| @boggle_board.reverse[n][n] } if (a[0] + a[1]) == (b[0] + b[1]) | |
end | |
end | |
# [0,0]... "i" , [2,2]... "c", [1,1] ... "a", [3,3] Limited to defining a left-diagonal. | |
# What is the pattern for the right-diagonal? | |
# "t"... [0,3], "o"... [2,1], "e"... [3,0] | |
# if you reverse the array, the right-diagonal will turn into a left-diagonal and therefore | |
# adopts the same pattern. | |
# 0 + 3 = 3, 2 + 1 = 3, 3 + 0 = 3. | |
# "o", and "t" are diagonals, but they are not part of the right-diagonal... so what is their pattern? | |
# 1.........1. | |
# 5.....5.....5. | |
dice_grid = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
test = Boggle.new(dice_grid) | |
#test = Boggle.new | |
puts test.create_word([2,1], [1,1], [1,2], [0,3]) #=> returns "code" | |
puts test.create_word([0,1], [0,2], [1,2]) #=> creates what california slang word# | |
puts test.create_word([2,1], [3,1], [3,0]) #=> "cat" | |
puts test.create_word([2,1], [3,1], [3,2], [3,3]) #=> "cake" | |
puts test.get_row(1) == ["i", "o", "d", "t"] | |
puts test.get_row(3) == ["t", "a", "k", "e"] | |
puts test.get_col(1) == ["r", "o", "c", "a"] | |
puts test.get_col(3) == ["e", "t", "r", "e"] | |
puts test.get_diagonal([1,1],[2,2]) == ["b", "o", "l", "e"] | |
puts test.get_diagonal([0,3],[2,1]).reverse == ["e", "d", "c", "t"] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment