Skip to content

Instantly share code, notes, and snippets.

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 tihuan/8701076 to your computer and use it in GitHub Desktop.
Save tihuan/8701076 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
class BoggleBoard
attr_reader :board
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)
column = []
board.each {|row| column << row[col]}
p column
end
def get_diag(co1, co2)
gather_diag(co1,co2) if check_coords(co1,co2)
end
private
def is_diag?(co1, co2, coord = co1.dup)
co1_co2_relative_position = co1 <=> co2
return false if co1 == co2
return true if coord == co2
begin #when coord is out of bound, returns false
board[coord.first][coord.last]
rescue
return false
end
if co1_co2_relative_position == 1 #If co1 is right to co2, coord counts backwards
coord.map!{|n| n -= 1}
is_diag?(co1, co2, coord)
else #If co1 is left to co2, coord counts forwards
coord.map!{|n| n += 1}
is_diag?(co1, co2, coord)
end
end
def gather_diag(co1, co2)
sort_coords = [co1, co2].sort
small_coord = sort_coords.first
large_coord = sort_coords.last
diagonal = []
coord = small_coord
while coord.first <= large_coord.first
diagonal << board[coord.first][coord.last]
coord.map! {|n| n += 1}
end
p diagonal
end
def check_coords(co1,co2)
if co1 == co2
puts "They are the same!"
elsif co1.first == co2.first
puts "These two coordinates are in the same row."
elsif co1.last == co2.last
puts "These two coordinates are in the same column."
elsif is_diag?(co1,co2) == false
puts "These two coordinates are not diagonal."
else
true
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:
puts boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) #=> returns "code"
puts boggle_board.create_word([0,1], [0,2],[1,2]) #=> returns "rad"
puts boggle_board.create_word([2,1],[3,1],[3,2],[3,3]) #=> returns "cake"
puts boggle_board.create_word([2,2],[1,1],[2,1],[3,2],[3,3],[2,3]) #=> returns "locker"
puts
p boggle_board.get_row(0) #=> returns ["b", "r", "a", "e"]
p boggle_board.get_row(1) #=> returns ["i", "o", "d", "t"]
p boggle_board.get_row(2) #=> returns ["e", "c", "l", "r"]
p boggle_board.get_row(3) #=> returns ["t", "a", "k", "e"] <- "take"
puts
boggle_board.get_col(0) #=> returns ["b", "i", "e", "t"]
boggle_board.get_col(1) #=> returns ["r", "o", "c", "a"]
boggle_board.get_col(2) #=> returns ["a", "d", "l", "k"]
boggle_board.get_col(3) #=> returns ["e", "t", "r", "e"] <- French être means "to be"
# create driver test code to retrieve a value at a coordinate here:
puts
puts boggle_board.create_word([2,1]) #=> "c"
puts boggle_board.create_word([1,1]) #=> "o"
puts boggle_board.create_word([1,2]) #=> "d"
puts boggle_board.create_word([0,3]) #=> "e"
#get_diag
puts
boggle_board.get_diag([0,2],[1,3]) #=> returns ["a", "t"]
boggle_board.get_diag([0,1],[2,3]) #=> returns ["r", "d", "r"]
boggle_board.get_diag([0,0],[3,3]) #=> returns ["b", "o", "l", "e"]
boggle_board.get_diag([3,3],[0,0]) #=> returns ["b", "o", "l", "e"]
boggle_board.get_diag([3,2],[0,0]) #=> returns "These two coordinates are not diagonal."
boggle_board.get_diag([0,2],[0,0]) #=> returns "These two coordinates are in the same row."
boggle_board.get_diag([3,0],[0,0]) #=> returns "These two coordinates are in the same column."
#REFLECTS
# 1. I feel OOP is easier to maintain and understand code, because if written properly methods hold their single responsibilties so it's easier to debug.
# In addition, OOP means methods can be recyled for other purposes, which means the program can be more concise and readable.
# Overall, OOP is amazing!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment