Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mknudsen01/7646140 to your computer and use it in GitHub Desktop.
Save mknudsen01/7646140 to your computer and use it in GitHub Desktop.
Creating a Boggle class
class BoggleBoard
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_column(column)
#@board.map{|row| row[column]}
@board.transpose[column]
end
def get_letter(row,column)
@board[row][column]
end
def get_diagonal_adjacent(row, column)
diagonals = []
max_x = @board[0].length - 1
max_y = @board.length - 1
diagonals.push(@board[row-1][column-1]) if row > 0 and column > 0
diagonals.push(@board[row-1][column+1]) if row > 0 and column < max_y
diagonals.push(@board[row+1][column-1]) if row < max_x and column > 0
diagonals.push(@board[row+1][column+1]) if row < max_x and column < max_y
diagonals
end
def get_diagonal_stripe(coord1,coord2)
x1, x2 = coord1.first, coord2.first
y1, y2 = coord1.last, coord2.last
max_x = @board[0].length - 1
max_y = @board.length - 1
diagonal_stripe = []
if (y2-y1)/(x2-x1) == 1 #diagonal runs SE to NW
#find the northwesternmost point on diagonal stripe
while x1 > 0 and y1 > 0
x1 -= 1
y1 -= 1
end
#move southeast as far as possible
until x1 > max_x or y1 > max_y
diagonal_stripe << @board[x1][y1]
x1 += 1
y1 += 1
end
diagonal_stripe
elsif (y2-y1)/(x2-x1) == -1 #diagonal runs SW to NE
#find the southwesternmost point on the diagonal stripe
while x1 < max_x and y1 > 0
x1 += 1
y1 -= 1
end
#move northeast
until x1 < 0 or y1 > max_y
diagonal_stripe << @board[x1][y1]
x1 -= 1
y1 += 1
end
diagonal_stripe
else raise ArgumentError.new("Not a diagonal!")
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]) == 'dock'
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],[1,1],[2,2],[1,2]) == 'cold'
p boggle_board.create_word([0,1],[1,1],[0,2],[1,2]) == 'road'
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_column(0).join('') == 'biet'
p boggle_board.get_column(1).join('') == 'roca'
p boggle_board.get_column(2).join('') == 'adlk'
p boggle_board.get_column(3).join('') == 'etre'
p boggle_board.get_diagonal_adjacent(1,1) == ['b','a','e','l']
p boggle_board.get_diagonal_adjacent(1,3) == ['a','l']
p boggle_board.get_diagonal_adjacent(3,1) == ['e','l']
p boggle_board.get_diagonal_adjacent(1,0) == ['r','c']
p boggle_board.get_diagonal_adjacent(3,3) == ['l']
p boggle_board.get_diagonal_stripe([1,2],[0,1]) == ['r','d','r']
p boggle_board.get_diagonal_stripe([0,0],[2,2]) == ['b','o','l','e']
p boggle_board.get_diagonal_stripe([3,1],[2,2]) == ['a','l','t']
p boggle_board.get_diagonal_stripe([1,1],[0,2]) == ['e','o','a']
# create driver test code to retrieve a value at a coordinate here:
p boggle_board.get_letter(1,2) == 'd'
p boggle_board.get_letter(3,3) == 'e'
# To complete this problem, it was necessary to figure out how to put the separate methods into a
# class. This involved creating the class and initializing it, changing all of the variables in the
# methods to the instance variable of the board, and creating a get_diagonal method.
#
# By creating a class of objects, the code has become object-oriented, with the object being a boggle
# board. Now, it is possible to create a boggle board once, and we will be able to run all of the
# methods on it, whereas before we would have to type in the boggle board two or three times whenever
# we wanted to switch. What's more, before, only one boggle board could be used at a time. Now, with
# OOP, we are able to set any number of boggle boards, as long as we create a new instance of the
# BoggleBoard class. We could find row 2 of boggle_board1 and then on the next line get the diagonals
# of a spot on boggle_board2. Making the code object-oriented makes it much more re-usable.
#
# The get_diagonal method was a bit of a challenge in that I'm not sure if there is a way to do it
# more concisely. Right now, I'm just checking each diagonal value (which means checking four times)
# and then adding it as long as the diagonal value is there. I will be reading through some others'
# code to see if they had a better or more concise implementation.
#
# I don't think I learned any new ruby tricks, as it was all things that I've seen before. However,
# it was interesting to see how much more powerful object-oriented programming is.
#
# After looking at some others' code, I found that finding the diagonals may mean to find all of the
# points on the line created by two diagonal coordinates. I was not sure which was the desired method,
# so I made one that finds the adjacent diagonals from a point and one that finds the stripe of
# diagonals on a line created by two diagonal coordinates.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment