Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jnorton77/8246136 to your computer and use it in GitHub Desktop.
Save jnorton77/8246136 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
# Create a class BoggleBoard that includes the functionality of your methods from the previous challenge.
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]
end
def get_col(col)
@dice_grid.map {|row| row[col]}
end
def get_diagonal(begin_coord, end_coord)
if (begin_coord == [0,0] && end_coord == [3,3])
create_word([0,0], [1,1], [2,2], [3,3])
elsif (begin_coord == [3,3] && end_coord == [0,0])
create_word([3,3], [2,2], [1,1], [0,0])
elsif (begin_coord == [0,3] && end_coord == [3,0])
create_word([0,3], [1,2], [2,1], [3,0])
elsif (begin_coord == [3,0] && end_coord == [0,3])
create_word([3,0], [2,1], [1,2], [0,3])
else
puts "not diag"
end
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:
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> returns "dock"
p boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) #=> returns "code"
p boggle_board.create_word([0,1], [0,2], [1,2]) #=> creates what california slang word? "rad"
# Create 2 more calls to the create_word method to spell other words in the Boggle board.
p boggle_board.create_word([0,1], [1,1], [2,1], [3,2]) #=> returns "rock"
p boggle_board.create_word([1,3], [2,3], [3,3], [3,2]) #=> returns "trek"
# Write a method that takes a row number and returns all the elements in that row
p boggle_board.get_row(1) #=> ["i", "o", "d", "t"]
# write a method that takes a column number and returns all the elements in the column.
p boggle_board.get_col(1) #=> ["r", "o", "c", "a"]
# print all columns and rows in string format
4.times { |i| p boggle_board.get_row(i).join }
4.times { |i| p boggle_board.get_col(i).join }
# create driver test code to retrieve a value at a coordinate here:
p boggle_board.create_word([3,2]) #=> returns "k"
# print diagonals
p boggle_board.get_diagonal([0,0],[3,3]) # print diagonal starting at [0,0] and ending at [3,3], "bole"
p boggle_board.get_diagonal([3,3],[0,0]) # print diagonal starting at [3,3] and ending at [0,0], "elob"
p boggle_board.get_diagonal([3,0],[0,3]) # print diagonal starting at [3,0] and ending at [0,3], "tcde"
p boggle_board.get_diagonal([0,3],[3,0]) # print diagonal starting at [0,3] and ending at [3,0], "edct"
# REVIEW AND REFLECT
# I can see that the Object Oriented approach is easier to break apart and understand in its component pieces.
# it also looks like it would be easier to make changes to as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment