Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Lrmanfre/8726358 to your computer and use it in GitHub Desktop.
Save Lrmanfre/8726358 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
class Boggle_Board
attr_reader :dice_grid
def initialize (dice_grid)
@dice_grid = dice_grid
end
# This takes a specfied set of coordinates in the form of multiple 2 element
# arrays.
# Without the splat operators, I get rb:11:in `create_word': wrong number of
# arguments (4 for 1) (ArgumentError).
# I understand why I get this error -- too many args, not enough parameters,
# so splat operator addresses that.
def create_word(*coords)
coords.map { |coord| dice_grid[coord.first][coord.last] }.join("")
end
# When I include a splat operator, I get rb:15:in `[]': no implicit conversion
# of Array into Integer (TypeError).
# This can take a variable that points to an array, but when I add the splat
# operator it doesn't work...I am not certain why...It would be nice to
# combine these two methods into one.
def create_word2(coords)
coords.map { |coord| dice_grid[coord.first][coord.last] }.join("")
end
def get_row(row)
dice_grid[row]
end
def get_col(col)
dice_grid.transpose[col]
end
def [](row, col)
dice_grid[row][col]
end
def print_rows
puts "Horizontal rows: "
dice_grid.each_index do |row|
dice_grid.each_index do |column|
print dice_grid[row][column]
end
puts
end
end
def print_cols
puts "Vertical columns: "
dice_grid.each_index do |row|
dice_grid.each_index do |column|
print dice_grid.transpose[row][column]
end
puts
end
end
# Verify that the coordinates given are valid and within the dice grid.
def verify_coords(coord1, coord2)
if coord1[0] < 0 || coord1[0] > dice_grid.length - 1 ||
coord2[0] < 0 || coord2[0] > dice_grid.length - 1
raise ArgumentError.new("Please enter row numbers inside the dice grid,
from 0 to #{dice_grid.length - 1}.")
elsif coord1[1] < 0 || coord1[1] > dice_grid[coord1[0]].length - 1 ||
coord2[1] < 0 || coord2[1] > dice_grid[coord2[0]].length - 1
raise ArgumentError.new("Please enter column numbers inside the dice grid,
from 0 to #{dice_grid[coord1[0]].length - 1}.")
end
end
# Determine whether we will be adding/subtracting rows/columns.
def determine_operation(coord1, coord2)
if coord1[0] > coord2[0]
if coord1[1] > coord2[1]
operation = "subtract_rows_subtract_cols"
else
operation = "subtract_rows_add_cols"
end
elsif coord1[0] < coord2[0]
if coord1[1] < coord2[1]
operation = "add_rows_add_cols"
else
operation = "add_rows_subtract_cols"
end
end
end
# Store the coordinates from coord1 to coord2 in coordinate_trace
def determine_possible_diagonal_coords(coord1, coord2, operation, coordinate_trace)
if operation == "add_rows_add_cols"
while coord1[0] < coord2[0]
# If I just tried to shovel coord1 into the array now, I would keep
# storing coord1, which after each iteration, would keep updating the
#coord1 variable in all places, and gives me an array full of the same
# numbers in each cell.
coordinate_trace << [(coord1[0] += 1), coord1[1] += 1]
# This would seem to store the incremented value of coord1[0] and
# coord[1] in those variables, but then push those VALUES, NOT THE
# VARIABLE ITSELF, into the array, which is exactly what I wanted.
end
elsif operation == "add_rows_subtract_cols"
while coord1[0] < coord2[0]
coordinate_trace << [(coord1[0] += 1), coord1[1] -= 1]
end
elsif operation == "subtract_rows_subtract_cols"
while coord1[0] > coord2[0]
coordinate_trace << [(coord1[0] -= 1), coord1[1] -= 1]
end
elsif operation == "subtract_rows_add_cols"
while coord1[0] > coord2[0]
coordinate_trace << [(coord1[0] -= 1), coord1[1] += 1]
end
end
coordinate_trace
end
def get_diagonal(coord1, coord2)
# Verify that the coordinates are valid and in the dice grid
verify_coords(coord1, coord2)
# Determine where the first coordinate is in the dice grid in relation
# to the second coordinate. Determine how to move through the dice grid
# based upon this information.
operation = determine_operation(coord1, coord2)
# Create temporary variables to store the initial value of coord1
coord1_sub0 = coord1[0]
coord1_sub1 = coord1[1]
# Create an array (coordinate_trace) that will store the values of coord1,
# BUT NOT coord1, since we will be modifying coord1 below.
coordinate_trace = [[coord1_sub0, coord1_sub1]]
# Trace a diagonal through the dice grid from coord1 towards coord2.
coordinate_trace = determine_possible_diagonal_coords(coord1, coord2, operation, coordinate_trace)
# If the final value in the dice grid equals the second coordinate, then
# the diagonal is valid, and a word should be created.
if coordinate_trace.last == coord2
create_word2(coordinate_trace)
else
"That is not a valid diagonal."
end
end
end
dice_grid = [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
boggle_board = Boggle_Board.new(dice_grid)
#--------------------------------------DRIVER CODE----------------------------#
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) == "code"
puts boggle_board.create_word([0,1], [0,2], [1,2]) == "rad"
puts boggle_board.create_word([3,0], [3,1], [3,2], [3,3]) == "take"
puts boggle_board.create_word([0,1], [1,1], [0,2], [1,2]) == "road"
puts boggle_board.create_word([2,2], [1,1], [2,1], [3,1], [3,0], [2,0]) == "locate"
puts boggle_board.get_row(1) == ["i", "o", "d", "t"]
puts boggle_board.get_row(2) == ["e", "c", "l", "r"]
puts boggle_board.get_row(3) == ["t", "a", "k", "e"]
puts boggle_board.get_col(1) == ["r", "o", "c", "a"]
puts boggle_board.get_col(2) == ["a", "d", "l", "k"]
puts boggle_board.get_col(3) == ["e", "t", "r", "e"]
puts boggle_board.[](0,1) == "r" #=> should be true
puts boggle_board.[](2,1) == "c" #=> should be true
puts boggle_board.[](3,3) == "e" #=> should be true
puts boggle_board.[](2,3) != "x" #=> should be true
puts boggle_board.[](3,2) == "k" #=> should be true
boggle_board.print_rows
boggle_board.print_cols
#----------------------------get_diagonal driver code-------------------------#
#puts boggle_board.get_diagonal([0,0],[4,0]) #=> Exception: "Please enter row numbers inside the grid"
#puts boggle_board.get_diagonal([5,0],[3,3]) #=> Exception: "Please enter row numbers inside the grid"
#puts boggle_board.get_diagonal([0,0],[4,0]) #=> Exception: "Please enter row numbers inside the grid"
#puts boggle_board.get_diagonal([0,6],[2,2]) #=> Exception: "Please enter column numbers inside the grid"
#puts boggle_board.get_diagonal([0,0],[2,8]) #=> Exception: "Please enter column numbers inside the grid"
#puts boggle_board.get_diagonal([0,-2],[2,2]) #=> Exception: "Please enter column numbers inside the grid"
#puts boggle_board.get_diagonal([0,-5],[2,10]) #=> Exception: "Please enter column numbers inside the grid"
#puts boggle_board.get_diagonal([0,0],[2,10]) #=> Exception: "Please enter column numbers inside the grid"
puts boggle_board.get_diagonal([0,0],[3,0]) == "That is not a valid diagonal."
puts boggle_board.get_diagonal([3,3],[0,3]) == "That is not a valid diagonal."
puts boggle_board.get_diagonal([1,1],[3,2]) == "That is not a valid diagonal."
puts boggle_board.get_diagonal([3,0],[2,3]) == "That is not a valid diagonal."
puts boggle_board.get_diagonal([0,0],[0,0]) == "b"
puts boggle_board.get_diagonal([2,3],[2,3]) == "r"
# TOP LEFT TO BOTTOM RIGHT -- Add rows, add colums
puts boggle_board.get_diagonal([0,0],[3,3]) == "bole"
puts boggle_board.get_diagonal([1,1],[3,3]) == "ole"
puts boggle_board.get_diagonal([0,0],[2,2]) == "bol"
puts boggle_board.get_diagonal([1,1],[2,2]) == "ol"
puts boggle_board.get_diagonal([0,2],[1,3]) == "at"
# BOTTOM LEFT TO TOP RIGHT -- Subtract rows, add colums
puts boggle_board.get_diagonal([3,0],[0,3]) == "tcde"
puts boggle_board.get_diagonal([2,1],[0,3]) == "cde"
puts boggle_board.get_diagonal([3,0],[1,2]) == "tcd"
puts boggle_board.get_diagonal([2,1],[1,2]) == "cd"
puts boggle_board.get_diagonal([3,1],[1,3]) == "alt"
# TOP RIGHT TO BOTTOM LEFT -- Add rows, subtract columns
puts boggle_board.get_diagonal([0,3],[3,0]) == "edct"
puts boggle_board.get_diagonal([1,2],[3,0]) == "dct"
puts boggle_board.get_diagonal([0,3],[2,1]) == "edc"
puts boggle_board.get_diagonal([1,2],[2,1]) == "dc"
puts boggle_board.get_diagonal([2,2],[3,1]) == "la"
# BOTTOM RIGHT TO TOP LEFT -- Subtract rows, subtract colums
puts boggle_board.get_diagonal([3,3],[0,0]) == "elob"
puts boggle_board.get_diagonal([2,2],[0,0]) == "lob"
puts boggle_board.get_diagonal([3,3],[1,1]) == "elo"
puts boggle_board.get_diagonal([2,2],[1,1]) == "lo"
puts boggle_board.get_diagonal([2,3],[0,1]) == "rdr"
=begin
What was challenging: the diagonal method. It was large and unwiedly, so I broke it up into
sub-methods. I will keep my eyes open for a better, more succinct way to determine a valid
diagonal. As for the splat operator, there is some niggling syntax issue here that I will
have to address later. Not sure why I can't combine #create_word and #create_word2.
=end
#2/3/14 - Updated to move lines 128-134 into #determine_possible_diagonal_coords.
class Boggle_Board
attr_reader :dice_grid
def initialize (dice_grid)
@dice_grid = dice_grid
end
# This takes a specfied set of coordinates in the form of multiple 2 element
# arrays.
# Without the splat operators, I get rb:11:in `create_word': wrong number of
# arguments (4 for 1) (ArgumentError).
# I understand why I get this error -- too many args, not enough parameters,
# so splat operator addresses that.
def create_word(*coords)
coords.map { |coord| dice_grid[coord.first][coord.last] }.join("")
end
# When I include a splat operator, I get rb:15:in `[]': no implicit conversion
# of Array into Integer (TypeError).
# This can take a variable that points to an array, but when I add the splat
# operator it doesn't work...I am not certain why...It would be nice to
# combine these two methods into one.
def create_word2(coords)
coords.map { |coord| dice_grid[coord.first][coord.last] }.join("")
end
def get_row(row)
dice_grid[row]
end
def get_col(col)
dice_grid.transpose[col]
end
def [](row, col)
dice_grid[row][col]
end
def print_rows
puts "Horizontal rows: "
dice_grid.each_index do |row|
dice_grid.each_index do |column|
print dice_grid[row][column]
end
puts
end
end
def print_cols
puts "Vertical columns: "
dice_grid.each_index do |row|
dice_grid.each_index do |column|
print dice_grid.transpose[row][column]
end
puts
end
end
# Verify that the coordinates given are valid and within the dice grid.
def verify_coords(coord1, coord2)
if coord1[0] < 0 || coord1[0] > dice_grid.length - 1 ||
coord2[0] < 0 || coord2[0] > dice_grid.length - 1
raise ArgumentError.new("Please enter row numbers inside the dice grid,
from 0 to #{dice_grid.length - 1}.")
elsif coord1[1] < 0 || coord1[1] > dice_grid[coord1[0]].length - 1 ||
coord2[1] < 0 || coord2[1] > dice_grid[coord2[0]].length - 1
raise ArgumentError.new("Please enter column numbers inside the dice grid,
from 0 to #{dice_grid[coord1[0]].length - 1}.")
end
end
# Determine whether we will be adding/subtracting rows/columns.
def determine_operation(coord1, coord2)
if coord1[0] > coord2[0]
if coord1[1] > coord2[1]
operation = "subtract_rows_subtract_cols"
else
operation = "subtract_rows_add_cols"
end
elsif coord1[0] < coord2[0]
if coord1[1] < coord2[1]
operation = "add_rows_add_cols"
else
operation = "add_rows_subtract_cols"
end
end
end
# Store the coordinates from coord1 to coord2 in coordinate_trace
def determine_possible_diagonal_coords(coord1, coord2, operation)
# Create temporary variables to store the initial value of coord1
coord1_sub0 = coord1[0]
coord1_sub1 = coord1[1]
# Create an array (coordinate_trace) that will store the values of coord1,
# BUT NOT coord1, since we will be modifying coord1 below.
coordinate_trace = [[coord1_sub0, coord1_sub1]]
if operation == "add_rows_add_cols"
while coord1[0] < coord2[0]
# If I just tried to shovel coord1 into the array now, I would keep
# storing coord1, which after each iteration, would keep updating the
#coord1 variable in all places, and gives me an array full of the same
# numbers in each cell.
coordinate_trace << [(coord1[0] += 1), coord1[1] += 1]
# This would seem to store the incremented value of coord1[0] and
# coord[1] in those variables, but then push those VALUES, NOT THE
# VARIABLE ITSELF, into the array, which is exactly what I wanted.
end
elsif operation == "add_rows_subtract_cols"
while coord1[0] < coord2[0]
coordinate_trace << [(coord1[0] += 1), coord1[1] -= 1]
end
elsif operation == "subtract_rows_subtract_cols"
while coord1[0] > coord2[0]
coordinate_trace << [(coord1[0] -= 1), coord1[1] -= 1]
end
elsif operation == "subtract_rows_add_cols"
while coord1[0] > coord2[0]
coordinate_trace << [(coord1[0] -= 1), coord1[1] += 1]
end
end
coordinate_trace
end
def get_diagonal(coord1, coord2)
# Verify that the coordinates are valid and in the dice grid
verify_coords(coord1, coord2)
# Determine where the first coordinate is in the dice grid in relation
# to the second coordinate. Determine how to move through the dice grid
# based upon this information.
operation = determine_operation(coord1, coord2)
# Trace a diagonal through the dice grid from coord1 towards coord2.
coordinate_trace = determine_possible_diagonal_coords(coord1, coord2, operation)
# If the final value in the dice grid equals the second coordinate, then
# the diagonal is valid, and a word should be created.
if coordinate_trace.last == coord2
create_word2(coordinate_trace)
else
"That is not a valid diagonal."
end
end
end
dice_grid = [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
boggle_board = Boggle_Board.new(dice_grid)
#--------------------------------------DRIVER CODE----------------------------#
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) == "code"
puts boggle_board.create_word([0,1], [0,2], [1,2]) == "rad"
puts boggle_board.create_word([3,0], [3,1], [3,2], [3,3]) == "take"
puts boggle_board.create_word([0,1], [1,1], [0,2], [1,2]) == "road"
puts boggle_board.create_word([2,2], [1,1], [2,1], [3,1], [3,0], [2,0]) == "locate"
puts boggle_board.get_row(1) == ["i", "o", "d", "t"]
puts boggle_board.get_row(2) == ["e", "c", "l", "r"]
puts boggle_board.get_row(3) == ["t", "a", "k", "e"]
puts boggle_board.get_col(1) == ["r", "o", "c", "a"]
puts boggle_board.get_col(2) == ["a", "d", "l", "k"]
puts boggle_board.get_col(3) == ["e", "t", "r", "e"]
puts boggle_board.[](0,1) == "r" #=> should be true
puts boggle_board.[](2,1) == "c" #=> should be true
puts boggle_board.[](3,3) == "e" #=> should be true
puts boggle_board.[](2,3) != "x" #=> should be true
puts boggle_board.[](3,2) == "k" #=> should be true
boggle_board.print_rows
boggle_board.print_cols
#----------------------------get_diagonal driver code-------------------------#
#puts boggle_board.get_diagonal([0,0],[4,0]) #=> Exception: "Please enter row numbers inside the grid"
#puts boggle_board.get_diagonal([5,0],[3,3]) #=> Exception: "Please enter row numbers inside the grid"
#puts boggle_board.get_diagonal([0,0],[4,0]) #=> Exception: "Please enter row numbers inside the grid"
#puts boggle_board.get_diagonal([0,6],[2,2]) #=> Exception: "Please enter column numbers inside the grid"
#puts boggle_board.get_diagonal([0,0],[2,8]) #=> Exception: "Please enter column numbers inside the grid"
#puts boggle_board.get_diagonal([0,-2],[2,2]) #=> Exception: "Please enter column numbers inside the grid"
#puts boggle_board.get_diagonal([0,-5],[2,10]) #=> Exception: "Please enter column numbers inside the grid"
#puts boggle_board.get_diagonal([0,0],[2,10]) #=> Exception: "Please enter column numbers inside the grid"
puts boggle_board.get_diagonal([0,0],[3,0]) == "That is not a valid diagonal."
puts boggle_board.get_diagonal([3,3],[0,3]) == "That is not a valid diagonal."
puts boggle_board.get_diagonal([1,1],[3,2]) == "That is not a valid diagonal."
puts boggle_board.get_diagonal([3,0],[2,3]) == "That is not a valid diagonal."
puts boggle_board.get_diagonal([0,0],[0,0]) == "b"
puts boggle_board.get_diagonal([2,3],[2,3]) == "r"
# TOP LEFT TO BOTTOM RIGHT -- Add rows, add colums
puts boggle_board.get_diagonal([0,0],[3,3]) == "bole"
puts boggle_board.get_diagonal([1,1],[3,3]) == "ole"
puts boggle_board.get_diagonal([0,0],[2,2]) == "bol"
puts boggle_board.get_diagonal([1,1],[2,2]) == "ol"
puts boggle_board.get_diagonal([0,2],[1,3]) == "at"
# BOTTOM LEFT TO TOP RIGHT -- Subtract rows, add colums
puts boggle_board.get_diagonal([3,0],[0,3]) == "tcde"
puts boggle_board.get_diagonal([2,1],[0,3]) == "cde"
puts boggle_board.get_diagonal([3,0],[1,2]) == "tcd"
puts boggle_board.get_diagonal([2,1],[1,2]) == "cd"
puts boggle_board.get_diagonal([3,1],[1,3]) == "alt"
# TOP RIGHT TO BOTTOM LEFT -- Add rows, subtract columns
puts boggle_board.get_diagonal([0,3],[3,0]) == "edct"
puts boggle_board.get_diagonal([1,2],[3,0]) == "dct"
puts boggle_board.get_diagonal([0,3],[2,1]) == "edc"
puts boggle_board.get_diagonal([1,2],[2,1]) == "dc"
puts boggle_board.get_diagonal([2,2],[3,1]) == "la"
# BOTTOM RIGHT TO TOP LEFT -- Subtract rows, subtract colums
puts boggle_board.get_diagonal([3,3],[0,0]) == "elob"
puts boggle_board.get_diagonal([2,2],[0,0]) == "lob"
puts boggle_board.get_diagonal([3,3],[1,1]) == "elo"
puts boggle_board.get_diagonal([2,2],[1,1]) == "lo"
puts boggle_board.get_diagonal([2,3],[0,1]) == "rdr"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment