Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
December 27, 2015 23:09
-
-
Save benneuman/7403825 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
#PSEUDOCODE | |
#take 'dice_grid' as input to initialize, set @dice_grid = dice_grid | |
#define #create_word - no longer needs 'board' parameter, can just use @dice_grid. only need *coords param | |
#define #get_row and #get_col like before, but no 'board' parameter, act on @dice_grid | |
#define #get_coord: take array.length == 2 as input, return @dice_grid[array[0][array[1]] | |
class BoggleBoard | |
def initialize(dice_grid) | |
@dice_grid = dice_grid | |
end | |
def create_word(*coords) | |
coords.map { |coord| @dice_grid[coord.first][coord.last]}.join("") | |
end | |
def get_row(row) | |
@dice_grid[row - 1] | |
end | |
def get_col(col) | |
@dice_grid.collect {|row| row[col - 1]} | |
end | |
end | |
dice_grid = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
boggle_board = BoggleBoard.new(dice_grid) | |
#tests | |
puts boggle_board.get_row(1) == ['b', 'r', 'a', 'e'] | |
puts boggle_board.get_row(3) == ['e', 'c', 'l', 'r'] | |
puts boggle_board.get_col(1) == ['b', 'i', 'e', 't'] | |
puts boggle_board.get_col(3) == ['a', 'd', 'l', 'k'] | |
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) == "code" | |
puts boggle_board.create_word([2,1], [2,2], [1,1], [1,2]) == "clod" | |
puts boggle_board.create_word([0,0], [0,1], [0,2], [1,3]) == "brat" | |
#print all rows and columns | |
(1..4).each do |x| | |
puts boggle_board.get_row(x).join("") | |
puts boggle_board.get_col(x).join("") | |
end | |
#val at coordinate | |
puts boggle_board.create_word([3,1]) | |
puts boggle_board.create_word([0,3]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment