Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save michaelohm/9160429 to your computer and use it in GitHub Desktop.
Save michaelohm/9160429 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
# Objective 1:
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_array=[]
@board.each {|row| column_array << row[col]}
return column_array
end
=begin
pseudocode for get_diagonal
1. define the boggle board dimensions so that if the
coordinates inputted fall outside the dimensions
it will return an error.
2. if coordinates fall within dimensions, take the difference between
the first numbers of the first and second coordinates and divide by the
difference between the second numbers of the first and second coordinates.
3. if the absolute value of the ratio obtained in step 2 is equal to 1,
we know that they are in diagonal. If not, return an error.
4. If the ratio in step two is -1, then the diagonal is going up and
if the ratio is 1, then the diagonal is going down
=end
def get_diagonal(coord1, coord2)
max_coord = @board.length - 1
if
coord1.first > max_coord || coord1.last > max_coord ||
coord2.first > max_coord || coord2.last > max_coord
return "At least one of these coordinates are outside the boggle board."
else
if (coord1.first - coord2.first).abs == (coord1.last - coord2.last).abs
if (coord1.first - coord2.first) / (coord1.last - coord2.last) == 1
puts "This diagonal is slanting down."
else
puts "This diagonal is slanting up."
end
else
return "These coordinates are not diagonal to each other."
end
end
end # end of get_diagonal method
end # end of the class
=begin
The below are some code that I tried initially but which did not work:
(0..@board.length).each do |count|
if
(coord1.first+count == coord2.first && coord1.last+count == coord2.last) ||
(coord1.first-count == coord2.first && coord1.last-count == coord2.last) ||
(coord1.first+count == coord2.first && coord1.last-count == coord2.last) ||
(coord1.first-count == coord2.first && coord1.last+count == coord2.last)
return "is diagonal"
# need to fix this to only give diagonal along the two coords
diagonal_array = []
diagonal_array << @board[coord1.first][coord1.last]
diagonal_array << @board[coord1.first+count][coord1.last+count]
diagonal_array << @board[coord1.first-count][coord1.last-count]
diagonal_array << @board[coord1.first+count][coord1.last-count]
diagonal_array << @board[coord1.first-count][coord1.last+count]
return diagonal_array
=end
=begin
if
((@board[coord1.first+count][coord1.last+count]) ||
(@board[coord1.first-count][coord1.last-count]) ||
(@board[coord1.first+count][coord1.last-count]) ||
(@board[coord1.first-count][coord1.last+count]) ==
(@board[coord2.first][coord2.last]))
puts "is diagonal"
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)
# boggle_board is a new instance of BoggleBoard class
# and dice_grid is turned into an instance variable
# of boggle_board instance.
# Objective 2:
# implement tests for each of the methods here:
p boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) #=> code
p boggle_board.create_word([0,1], [0,2], [1,2]) #=> rad
p boggle_board.create_word([2,1], [3,1], [3,2], [3,3]) #=> cake
p boggle_board.create_word([1,3], [2,3], [3,3], [3,2]) #=> trek
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> dock
p boggle_board.get_row(0).join #=> "brae"
p boggle_board.get_row(1).join #=> "iodt"
p boggle_board.get_row(2).join #=> "eclr"
p boggle_board.get_row(3).join #=> "take"
p boggle_board.get_col(0).join #=> "biet"
p boggle_board.get_col(1).join #=> "roca"
p boggle_board.get_col(2).join #=> "adlk"
p boggle_board.get_col(3).join #=> "etre"
# Objective 3:
# create driver test code to retrieve a value at a coordinate here:
p boggle_board.board[3][2] #=> coordinate of row 3 column 2: "k"
# Objective 4:
p boggle_board.get_diagonal([1,2], [2,3]) # => slants up
p boggle_board.get_diagonal([1,2], [2,4]) # => error
# Objective 5:
=begin
In the end I was unable to solve how to get the diagonal array when given two coordinates.
I ran into issues of being able to iterate in a way where no matter which two diagonal
coordinates are given, an array of remaining coordinates in the diagoncal can be identified
and added. I ran into issues of running off of the board in the iteration as well as direction
of the diagonal.
Object oriented approach was interesting and seems to be very logical. Breaking down
each real life objects into an object in the code and actually having it behave
in this way was interesting to see. For me it was the most clearly seen in the Drawer Debugger
challenge.
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment