Skip to content

Instantly share code, notes, and snippets.

@thataustin
Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active December 28, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thataustin/7419728 to your computer and use it in GitHub Desktop.
Save thataustin/7419728 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
class BoggleBoard
def initialize (board)
@board = board
end
def create_word(*coords)
coords.map { |coord| @board[coord.first][coord.last]}.join("")
end
def get_row(row)
@board[row]
end
def get_col(col)
@board.collect { |row| row[col] }
end
def [] (x, y)
@board[x][y]
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)
# implement tests for each of the methods here:
puts "Testing create_word"
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) == "dock"
puts boggle_board.create_word([0,1], [0,2], [1,2]) == "rad"
puts boggle_board.create_word([0,0], [1,0], [0,1], [1, 2]) == "bird"
puts boggle_board.create_word([1,3], [2,3], [0,3], [3, 2]) == "trek"
puts "Testing get_row"
puts boggle_board.get_row(1) == ["i", "o", "d", "t"]
puts boggle_board.get_row(2) == ["e", "c", "l", "r"]
puts "Testing get_col"
puts boggle_board.get_col(1) == ["r", "o", "c", "a"]
puts boggle_board.get_col(2) == ["a", "d", "l", "k"]
puts "Printing Rows: "
4.times { |index| puts boggle_board.get_row(index).join(' ') }
puts "Printing Cols: "
4.times { |index| puts boggle_board.get_col(index).join(' ') }
# create driver test code to retrieve a value at a coordinate here:
puts "Testing array access"
puts boggle_board[3, 2] == 'k'
puts boggle_board[0, 0] == 'b'
# Bonus: diagonal
class BoggleBoard
def get_diagonal(coord1, coord2)
raise(ArgumentError, "arguments must be arrays") unless coord1.is_a?(Array) && coord2.is_a?(Array)
raise(ArgumentError, "argument out of bounds") if @board[coord1[0]][coord1[1]].nil? || @board[coord2[0]][coord2[1]].nil?
raise(ArgumentError, "arguments must be diagonal from one another") if (coord1[0] - coord2[0]).abs != (coord1[1] - coord2[1]).abs
# populate direction array with symbols representing the x and y directions we need to travel in starting from coord1 and traveling towards coord2
directions = {}
directions[:y] = (coord1[0] > coord2[0] ? :- : :+)
directions[:x] = (coord1[1] > coord2[1] ? :- : :+)
result_string = ''
while coord1 != coord2
result_string += self[coord1[0], coord1[1]] # add the character that's at the current position
coord1[0] = eval(coord1[0].to_s + " " + directions[:y].to_s + " 1") # add or subtract 1 from y value to advance 1 closer to coord2
coord1[1] = eval(coord1[1].to_s + " " + directions[:x].to_s + " 1") # add or subtract 1 from x value to advance 1 closer to coord2
end
result_string += self[coord2[0], coord2[1]] # add final coordinate
result_string
end
end
# test that Argument Error is thrown when args to get_diagonal aren't arrays
def test_for_argument_error(coord1, coord2, board)
begin
board.get_diagonal(coord1, coord2)
rescue ArgumentError
true
end
end
puts "Testing diagonal exceptions"
# Test that non-array args throw error
puts test_for_argument_error(1, 2, boggle_board)
puts test_for_argument_error([1], 2, boggle_board)
puts test_for_argument_error(1, [2], boggle_board)
# Test that out-of-bound args throw Argument Error
puts test_for_argument_error([1, 8], [2, 0], boggle_board)
puts test_for_argument_error([1, 0], [2, 8], boggle_board)
# Test that non-diagonal args throw error
puts test_for_argument_error([0, 0], [1, 0], boggle_board)
puts test_for_argument_error([3, 0], [3, 1], boggle_board)
puts "Testing diagonal results"
puts boggle_board.get_diagonal([0,0], [3,3]) == "bole"
puts boggle_board.get_diagonal([0,3], [2,1]) == "edc"
puts boggle_board.get_diagonal([2,3], [2,3]) == "r"
puts boggle_board.get_diagonal([2,3], [1,2]) == "rd"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment