Skip to content

Instantly share code, notes, and snippets.

@plantonly
Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active January 2, 2016 17:29
Show Gist options
  • Save plantonly/8337443 to your computer and use it in GitHub Desktop.
Save plantonly/8337443 to your computer and use it in GitHub Desktop.
Create A Boggle Board Class
class BoggleBoard
attr_reader :dice_grid
def initialize(dice_grid)
@dice_grid = dice_grid
end
def create_word(*coords)
p coords.map {|coord| @dice_grid[coord.first][coord.last]}.join("")
end
def get_row(row)
p @dice_grid[row]
end
def get_col(col)
puts "#{@dice_grid.map.with_index {|element, index| @dice_grid[index][col]}}"
end
def get_diagonal(coord1, coord2)
start1 = coord1[0]
start2 = coord1[1]
finish1 = coord2[0]
finish2 = coord2[1]
is_diag1 = finish1 - start1
is_diag2 = finish2 - start2
if is_diag1 == is_diag2
if start1 < finish1
while start1 < finish1
print "#{@dice_grid[start1][start2]}"
start1 +=1
start2 +=1
end
print "#{@dice_grid[finish1][finish2]} \n"
elsif start1 > finish1
while start1 > finish1
print "#{@dice_grid[start1][start2]}"
start1 -=1
start2 -=1
end
print "#{@dice_grid[finish1][finish2]} \n"
end
else
p "Bullshit! Mr. Han man."
end
end
#Refactored diagonal
def get_diagonal(coord1, coord2)
if (coord2[0] - coord1[0]) == (coord2[1] - coord1[1])
#difference between coords needs to be the same in order to fulfill the diagonal criteria
if coord1[0] < coord2[0] #if going left to right
until coord1[0] == coord2[0] + 1 #until coord1.1 matches coord2.1 position
p @dice_grid[coord1[0]][coord1[1]] #print coord1 string
coord1[0] +=1 #increase coord1.1 by one for each loop
coord1[1] +=1 #increase coord1.2 by one for each loop
end
elsif coord1[0] > coord2[0] #if going right to left
until coord1[0] == coord2[0] -1 #until coord1.1 matches coord2.1
p @dice_grid[coord1[0]][coord1[1]] #print coord1 string
coord1[0] -=1 #decrease coord1.1 by one for each loop
coord1[1] -=1 #decrease coord1.2 by one for each loop
end
end
else
p "That ain't no diagonal!" #if not diagonal, print this message
end
end
#Refactored diagonal for any direction
def get_diagonal(coord1, coord2)
start1 = coord1[0]
start2 = coord1[1]
finish1 = coord2[0]
finish2 = coord2[1]
if (finish1 - start1) == (finish2 - start2) || (finish1 - start1) == (start2 - finish2)
if start1 < finish1 && start2 < finish2 #SE
until start1 == finish1 + 1
p @dice_grid[start1][start2]
start1 +=1
start2 +=1
end
elsif start1 > finish1 && start2 > finish2 #NW
until start2 == finish2 - 1
p @dice_grid[start1][start2]
start1 -=1
start2 -=1
end
elsif start1 > finish1 && start2 < finish2 #NE
until start2 == finish2 + 1
p @dice_grid[start1][start2]
start1 -=1
start2 +=1
end
elsif start1 < finish1 && start2 > finish2 #SW
until start1 == finish1 + 1
p @dice_grid[start1][start2]
start1 +=1
start2 -=1
end
end
else
p "That ain't no 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) #dice_grid is held as a variable in the initialize method
# implement tests for each of the methods here:
boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> returns "dock"
boggle_board.get_row(0) #=> ["b", "r", "a", "e"]
boggle_board.get_row(1) #=> ["i", "o", "d", "t"]
boggle_board.get_row(2) #=> ["e", "c", "l", "r"]
boggle_board.get_row(3) #=> ["t", "a", "k", "e"] ..."take"
boggle_board.get_col(0) #=> ["b", "i", "e", "t"]
boggle_board.get_col(1) #=> ["r", "o", "c", "a"]
boggle_board.get_col(2) #=> ["a", "d", "l", "k"]
boggle_board.get_col(3) #=> ["e", "t", "r", "e"]
boggle_board.get_diagonal([3,1], [1,3])
# create driver test code to retrieve a value at a coordinate here:
p boggle_board.dice_grid[3][2]
=begin
dice_grid = [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
[0,0][0,1][0,2][0,3]
[1,0][1,1][1,2][1,3]
[2,0][2,1][2,2][2,3]
[3,0][3,1][3,2][3,3]
Pseudocode:
Input: two coordinates (two sets of index numbers)
Output: string representing diagonal characters from dice_grid
Steps:
1. check to see if coordinates are not horizontal or vertical
2. from below guide...add one coord position, until first set matches second set
[0,0]
[1,1]
[2,2]
3. increase first set of coords by 1, printing the character for each loop
4. do the opposite for right to left/reverse diagonal strings
5. return diagonal string
Reflection:
The diagonal challenge was tricky and I found myself experimenting a lot, just to get
the input/output right with the coordinates. It wasn't until I visually illustrated the
coords (next to the grid), that I realized how much simpler the solution was.
I struggled with the first requirement of making sure the coordinates were diagonal,
even after working through the solution to output a diagonal string. This was mainly
due to the assumption that all I needed to do, was just avoid any horizontal or
vertical coords, which simply meant checking if opposites matched (e.g. if start1 ==
finish1 || start2 == finish2). This theory fell to bits, when I realized that you can
have a condition that was neither horizontal or vertical, but still wasn't completely
diagonal (e.g. [0,1], [2,2]). After falling in a coords trance for an unknown period,
I finally discovered a foolproof way; where the difference between opposite coords, has
to be the same, in order to be diagonal.
=end
@gregorygerla
Copy link

what would happen if you were passed something like [3,1],[1,3]?

@plantonly
Copy link
Author

Good catch, Gregory! Simple fix, but a silly mistake...you make me a better programmer :)

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