Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bmurphy1/7404365 to your computer and use it in GitHub Desktop.
Save bmurphy1/7404365 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
class BoggleBoard
def initialize(dice_grid)
@board = dice_grid
end
def [](row, col)
@board[row][col]
end
def get_letter(coords)
@board[coords.first][coords.last]
end
def get_board
@board.map {|row| row.join " "}
end
def create_word(*coords)
coords.map { |coord| @board[coord.first][coord.last]}.join("")
end
def get_row(row)
@board[row].join ""
end
def get_col(col)
@board.transpose[col].join ""
end
def get_diagonal(start_coord, end_coord)
# work in progress
# case
# when condition
# 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([1,2], [1,1], [2,1], [3,2]) == "dock" #=> true
puts boggle_board.get_row(1) == "iodt" #=> true
puts boggle_board.get_col(1) == "roca" #=> true
puts boggle_board.get_board #=> Nicely formated game board
# Now print out all the rows and columns of the board as strings.
# You should end up with 8 four letter words.
4.times { |i| puts boggle_board.get_row(i) + "\n"}
4.times { |i| puts boggle_board.get_col(i) + "\n"}
# create driver test code to retrieve a value at a coordinate here:
# can either use #get_board or the #[] method.
puts boggle_board[3,2] == "k" #=> true
puts boggle_board.get_letter([3,2]) == "k" #=> true
# BONUS: Creat a #get_diagonal method. Tests:
# puts boggle_board.get_diagonal([0,0], [1,1])
# REFLECT: How is the implementation different? What are the benefits to
# using the Object Oriented approach (even if it is a bit more code?)
# OO allows for better organized code, that allows for easier changes and easier reading.
# Decided to change get_row and get_col to use array counting because create_word counts that way and I
# think it would be confusing to have one method count one way, and the others count a different way.
@bmurphy1
Copy link
Author

Output of printing all rows and columns:

brae
iodt
eclr
take
biet
roca
adlk
etre

@benneuman
Copy link

transpose is new to me and looks very useful, thanks. There's a method for everything in Ruby, isn't there? I don't even know why I'm typing this, there's probably a Ruby method to type it for me.

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