Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:56
-
-
Save pei-liu/9102698 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BoggleBoard | |
def initialize(dice_grid) | |
@dice_grid = dice_grid | |
end | |
def create_word(*coords) | |
coords.map{|coord| @dice_grid[coord[0]][coord[1]]}.join | |
end | |
def get_row(row) | |
@dice_grid[row].join | |
end | |
def get_col(col) | |
@dice_grid.map{|row| row[col]}.join | |
end | |
def get_letter(row, col) | |
@dice_grid[row][col] | |
end | |
def get_diagonal(start_coor, end_coor) | |
start_row = start_coor[0] | |
end_row = end_coor[0] | |
start_col = start_coor[1] | |
end_col = end_coor[1] | |
answer = get_letter(start_row, start_col) #where word will be pushed to | |
row_distance = start_row - end_row | |
col_distance = start_col - end_col | |
next_row = start_row | |
next_col = start_col | |
if row_distance.abs == col_distance.abs #checks if coords are diagonal from each other | |
while next_row != end_row | |
if row_distance > 0 | |
next_row -=1 | |
else | |
next_row +=1 | |
end | |
if col_distance > 0 | |
next_col -=1 | |
else | |
next_col +=1 | |
end | |
answer << get_letter(next_row, next_col) | |
end | |
else | |
raise "Coordinates not diagonal from each other" | |
end | |
answer | |
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([0,0], [0,1], [0,2], [1,3]) == "brat" | |
p boggle_board.create_word([0,1], [1,1], [2,2], [3,3]) == "role" | |
p boggle_board.get_row(0) == "brae" | |
p boggle_board.get_row(1) == "iodt" | |
p boggle_board.get_row(2) == "eclr" | |
p boggle_board.get_row(3) == "take" | |
p boggle_board.get_col(0) == "biet" | |
p boggle_board.get_col(1) == "roca" | |
p boggle_board.get_col(2) == "adlk" | |
p boggle_board.get_col(3) == "etre" | |
p boggle_board.get_diagonal([0,0],[3,3]) == "bole" | |
p boggle_board.get_diagonal([2,1],[0,3]) == "cde" | |
# create driver test code to retrieve a value at a coordinate here: | |
p boggle_board.get_letter(3,2) == "k" | |
#REFLECTION | |
#I was able to solve this exercise, including the bonus problem. One advantage of using OOP in the case of boggle is that | |
#the dice grid can be assigned a global variable within the class instead of having to be passed into every method. The | |
#get_diagonal method was trickier than I thought it would be. To help me identify patterns, I drew a 4x4 of coordinates. | |
#I found that when you have a diagonal start and end point, the absolute value of the difference of the axes are equal. | |
#This insight help me write code to check if the coords were in fact diagonal of each other. I'm looking forward to seeing | |
#how others solved the diagonal challenge as I don't think my solution is very elegant. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment