Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save c14jcdj/7683174 to your computer and use it in GitHub Desktop.
Save c14jcdj/7683174 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
class BoggleBoard
def initialize(grid)
@grid = grid
end
def create_word(*coords)
coords.map { |coord| @grid[coord.first][coord.last]}.join("")
end
def get_row(row)
@grid[row]
end
def get_col(col)
@grid.collect { |row| row[col] }
end
def print_row(row)
@grid[row].join("")
end
def print_col(col)
@grid.collect { |row| row[col] }.join("")
end
def get_diagonal(coor, direction )
dia = []
row = coor.first
col = coor.last
min = coor.min
if direction == "down"
start = [(row - min),(col - min)]
row_start = start.first
col_start = start.last
until (row_start == 4) || (col_start ==4 )
dia << @grid[row_start][col_start]
row_start += 1
col_start += 1
end
else
case row
when 0
until col < 0
dia << @grid[row][col]
row +=1
col -=1
end
when 1
dia << @grid[row][col] << @grid[row-1][col+1] if col == 0
dia << @grid[row][col] << @grid[row -1][col+1] << @grid[row +1][col -1] if col ==1
dia << @grid[row][col] << @grid[row -1][col+1] << @grid[row +1][col -1] << @grid[row +2][col-2] if col ==2
dia << @grid[row][col] << @grid[row+1][col -1] << @grid[row+2][col-2] if col ==3
when 2
dia << @grid[row][col] << @grid[row-1][col +1] << @grid[row-2][col+2] if col ==0
dia << @grid[row][col] << @grid[row+1][col -1] << @grid[row-1][col+1] << @grid[row-2][col+2]if col ==1
dia << @grid[row][col] << @grid[row+1][col -1] << @grid[row-1][col+1] if col ==2
dia << @grid[row][col] << @grid[row+1][col -1] if col ==3
else
dia << @grid[row][col] << @grid[row-1][col+1] << @grid[row-2][col +2] << @grid[row-3][col+3] if col == 0
dia << @grid[row][col] << @grid[row-1][col +1] << @grid[row-2][col+2] if col ==1
dia << @grid[row][col] << @grid[row-1][col+1] if col ==2
dia << @grid[row][col] if col == 3
end
end
dia.compact
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:
#create_word tests
p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> "dock"
p boggle_board.create_word([0,1], [1,1], [2,1], [3,2]) #=> "rock"
p boggle_board.create_word([0,3], [0,2], [1,3]) #=> "eat"
#get_row tests
p boggle_board.get_row(1) #=> ["i", "o", "d", "t"]
p boggle_board.get_row(3) #=> ["t", "a", "k", "e"]
#get_col tests
p boggle_board.get_col(2) #=> ["a", "d", "l", "k"]
p boggle_board.get_col(3) #=> ["e", "t", "r", "e"]
#print tests
p boggle_board.print_row(0) #=> "brae"
p boggle_board.print_row(1) #=> "iodt"
p boggle_board.print_row(2) #=> "eclr"
p boggle_board.print_row(3) #=> "take"
p boggle_board.print_col(0) #=> "biet"
p boggle_board.print_col(1) #=> "roca"
p boggle_board.print_col(2) #=> "adlk"
p boggle_board.print_col(3) #=> "etre"
# create driver test code to retrieve a value at a coordinate here:
#access_coordinate tests
p boggle_board.create_word([3,2]) == "k"
p boggle_board.create_word([0,2]) == "a"
p boggle_board.create_word([2,1]) == "c"
#Diagonal tests
p boggle_board.get_diagonal([0,0], "down") #=>["b","o","l","e"]
p boggle_board.get_diagonal([2,1], "down") #=>["i","c","k"]
p boggle_board.get_diagonal([2,1], "up") #=>["c","t","d","e"]
p boggle_board.get_diagonal([3,1], "up") #=>["a","l","t"]
####### REFLECTION #######
=begin
The implementation is different in that you need to create an instance of the class
in order to call the instance methods on the object. It is cool to be able to
initialize each instance of the class with a board and then be able to manipulate
the board with the methods. It is a lot easier to organize and write the code this way
oppossed to having to enter the board each time for each method. You can create as many
objects as you want and they are all easily stored and manipulated by the various methods.
OOP is awesome!
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment