Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:57
-
-
Save Widdershin/9777298 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
def sign(number) | |
if number > 0 | |
return 1 | |
elsif number < 0 | |
return -1 | |
end | |
0 | |
end | |
class BoggleBoard | |
def initialize(dice_grid) | |
@board=dice_grid | |
end | |
def create_word(*coords) | |
coords.map { |coord| lookup coord }.join("") | |
end | |
def get_row(row) | |
@board[row] | |
end | |
def get_col(col) | |
@board.transpose[col] | |
end | |
def lookup(coord) | |
@board[coord.first][coord.last] | |
end | |
def get_diagonal(start_pos, end_pos) | |
if (start_pos.first - end_pos.first).abs != (start_pos.last - end_pos.last).abs #valid diagonal? | |
return #return if it's not | |
end | |
direction = end_pos.map.with_index{ |m,i| m - start_pos[i] } | |
direction.map! { |coord| sign(coord) } | |
puts "Direction: #{direction}" | |
results = [lookup(start_pos)] | |
position = start_pos | |
puts "start pos: #{start_pos}, end pos: #{end_pos}" | |
until position == end_pos | |
puts "position: #{position}, direction: #{direction}" | |
position.map!.with_index{ |m,i| m + direction[i] } | |
results << lookup(position) | |
end | |
results | |
end | |
end | |
#0 1 2 3 | |
dice_grid = [["b", "r", "a", "e"], # 0 | |
["i", "o", "d", "t"], # 1 | |
["e", "c", "l", "r"], # 2 | |
["t", "a", "k", "e"]] # 3 | |
boggle_board = BoggleBoard.new(dice_grid) | |
# implement tests for each of the methods here: | |
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) #=> returns "code" | |
puts boggle_board.get_row(0).display #=> returns b, r, a, e | |
puts boggle_board.get_col(2).display #=> returns a, d, l, k | |
puts boggle_board.lookup([1, 2]) #=> d | |
puts boggle_board.get_diagonal([0, 0], [3, 3]).display #=> b, o, l, e | |
puts boggle_board.get_diagonal([0, 0], [3, 0]).display #=> null, invalid | |
puts boggle_board.get_diagonal([3, 0], [0, 3]).display #=> diagonal, returns t, c, d, e | |
# create driver test code to retrieve a value at a coordinate here: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment