Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Created
January 29, 2014 00:43
-
-
Save JohnathanWeisner/8679542 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 :num_rows, :num_cols | |
def initialize(board) | |
@board = board | |
@num_rows = @board.count | |
@num_cols = @board[0].count | |
end | |
def create_word(*coords) | |
coords.map { |coord| @board[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
@board[row].join("") | |
end | |
def get_col(col) | |
@board.map{|row| row[col]}.join("") | |
end | |
def get_diagonal(start_cord, end_cord) | |
diag_ary = [] | |
x_diff = end_cord[0]-start_cord[0] | |
y_diff = end_cord[1]-start_cord[1] | |
if y_diff.abs == x_diff.abs | |
while y_diff != 0 | |
if y_diff > 0 && x_diff > 0 | |
diag_ary << @board[start_cord[0] + x_diff][start_cord[1] + y_diff] | |
x_diff -= 1 | |
y_diff -= 1 | |
elsif y_diff > 0 && x_diff < 0 | |
diag_ary << @board[start_cord[0] + x_diff][start_cord[1] + y_diff] | |
x_diff += 1 | |
y_diff -= 1 | |
elsif y_diff < 0 && x_diff < 0 | |
diag_ary << @board[start_cord[0] + x_diff][start_cord[1] + y_diff] | |
x_diff += 1 | |
y_diff += 1 | |
else | |
diag_ary << @board[start_cord[0] + x_diff][start_cord[1] + y_diff] | |
x_diff -= 1 | |
y_diff += 1 | |
end | |
end | |
diag_ary << @board[start_cord[0]][start_cord[1]] | |
return diag_ary.reverse.join("") | |
else | |
puts "That was not diagonal." | |
end | |
end | |
end | |
#0,0 - 3,3 3, 3 | |
#3,3 - 0,0 -3,-3 | |
#0,2 - 1,3 1, 1 | |
#3,0 - 0,3 -3, 3 | |
#2,0 - 0,2 -2, 2 | |
# 0,0 0,1 0,2 0,3 | |
# 1,0 1,1 1,2 1,3 | |
# 2,0 2,1 2,2 2,3 | |
# 3,0 3,1 3,2 3,3 | |
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 "\nLets create a word!" | |
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) # => "dock" | |
puts "\nLets get rows!" | |
boggle_board.num_rows.times{ |x| puts boggle_board.get_row(x) } | |
puts "\nLets get columns!" | |
boggle_board.num_cols.times{ |y| puts boggle_board.get_col(y) } | |
puts "\nLet me know if I'm doing this right:" | |
puts boggle_board.create_word([3,2]) # => k | |
puts "\nLets go diagonal!" | |
puts boggle_board.get_diagonal([0,0],[3,3]) # => bole | |
puts boggle_board.get_diagonal([1,0],[3,2]) # => ick | |
puts boggle_board.get_diagonal([3,0],[0,3]) # => tcde | |
puts boggle_board.get_diagonal([2,3],[0,1]) # => rdr | |
puts boggle_board.get_diagonal([0,2],[2,0]) # => aoe | |
#The benefits of OO programming is the same benefit that ruby provides. | |
#You get more functionality with less code in the end. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment