Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Flamdoodle/9179259 to your computer and use it in GitHub Desktop.
Save Flamdoodle/9179259 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
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)
return @board[row]
end
def get_col(col)
column = []
for i in 0..3
column << @board[i][col]
end
return column
end
def get_coord(row, col)
@board.at(row).at(col)
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([1,2], [1,1], [2,1], [3,2]) #=> dock
print boggle_board.get_row(2) #=> ["e", "c", "l", "r"]
print boggle_board.get_col(2) #=> ["a", "d", "l", "k"]
for i in 0..3
print boggle_board.get_row(i).join('')
puts '/n'
print boggle_board.get_col(i).join('')
puts '/n'
end
# create driver test code to retrieve a value at a coordinate here:
# I defined a method #get_coord to retrieve the value at those coordinates, to make it easier.
puts boggle_board.get_row(3).at(2) #=> k
puts boggle_board.get_coord(3,2) #=> k
# Review:
# Object-oriented programming seems to give you a lot more flexibility with what you do and seems more powerful, despite
# it taking slightly longer to write. Sorry that this is vague, it's just a feeling that I got while using it. You get to
# create so many methods on objects that you create, rather than needing to stay within Ruby's pre-existing ones.
@Flamdoodle
Copy link
Author

These are all the outputs I got from printing out the rows and columns of dice_roll (order is row1, col1, row2, col2, etc):
biet
iodt
roca
eclr
adlk
take
etre

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