Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save harryw7/8392742 to your computer and use it in GitHub Desktop.
Save harryw7/8392742 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle 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)
@board.map {|row| row[col]}
end
def get_diagonal(coord1, coord2)
diag = []
if self.diagonal?(coord1, coord2)
if coord1 == coord2
row_counter = 0
col_counter = 0
while row_counter < @board.length
diag << @board[row_counter][col_counter]
row_counter += 1
col_counter += 1
end
return diag
else
row_counter = 0
col_counter = @board.length - 1
while row_counter < @board.length
diag << @board[row_counter][col_counter]
row_counter += 1
col_counter -= 1
end
return diag
end
else
puts "This is not a diagonal coordinate"
end
end
def diagonal?(coord1, coord2)
diagonal_coords = []
row_counter = 0
col_counter = 0
while row_counter < @board.length
diagonal_coords << [row_counter, col_counter]
row_counter += 1
col_counter += 1
end
row_counter = 0
col_counter = @board.length - 1
while row_counter < @board.length
diagonal_coords << [row_counter, col_counter]
row_counter += 1
col_counter -= 1
end
return diagonal_coords.include?([coord1, coord2]) ? true : false
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 an instance of the BoggleBoard class, with dice_grid
p boggle_board.board
# implement tests for each of the methods here:
p boggle_board.create_word([0,1],[1,1],[0,2],[1,2]) #=> "road"
counter = 0
while counter < 4
p boggle_board.get_row(counter).join("")
counter += 1
end
counter = 0
while counter < 4
p boggle_board.get_col(counter).join("")
counter += 1
end
# create driver test code to retrieve a value at a coordinate here:
p boggle_board.board[3][2]
p boggle_board.diagonal?(3,2)
p boggle_board.get_diagonal(0,3)
# Reflect
# The get_diagonal method was by far the most difficult part of this challenge. I initially struggled with how to construct
# an array of coordinates that are diagonal because I wasn't sure how to insert an array into an array, i.e. creating a
# nested array, but I realized all I needed to do was to have the square brackets around my inputs and use that as an element.
# Also, in both my while loops, I had to have two counter variables but the while loop exit condition only needed for one of them.
# This was something very useful that I learned
# Using object-oriented programming has the benefit of making the process repeatable and without having to show so much code to the user
@harryw7
Copy link
Author

harryw7 commented Jan 13, 2014

Output to part 2):

"road"
"brae"
"iodt"
"eclr"
"take"
"biet"
"roca"
"adlk"
"etre"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment