Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ziad-abdo/9175674 to your computer and use it in GitHub Desktop.
Save ziad-abdo/9175674 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
#####I've tried several times to edit this gist so that the indentation is correct, but everytime I get it right
#####after hitting update gist it simply returns the indentation to this exaggerated form. I dont know what to do about it.
class BoggleBoard
def initialize(user_grid)
user_grid.each do |array|
raise ArgumentError.new("User grid is not a 4x4 grid") if array.length != 4
end
@game_grid = user_grid
end
def create_word(*coords)
coords.map { |coord| @game_grid[coord.first][coord.last]}.join("")
end
def get_row(row)
print @game_grid[row].join("")
print "\n"
end
def get_col(col)
holder_array = []
@game_grid.each do |row|
holder_array << row[col]
end
print holder_array.join("")
print "\n"
end
def get_coord(coords)
print @game_grid[coords.first][coords.last]
print "\n"
end
=begin
PSEUDOCODE FOR #get_diagonal
firstly, how to check for acceptable input-- how do we know if the input is requesting a
proper diagonal set of chars.
much was done on paper, but the conclusions I came to are these
in order for the input to be correct it has to satisfy these conditions
user inputs two coordinates named here as row, col and row2, col2
for [row, col && row2, col2]
if row2 == row+X || row -X
then col2 == col+X || col -X
the difference between row and row2(positive or negative)
must be the same as the difference between col and col2 (positive or negative)
I will need to be able to obtain the absolute value of the difference.
=end
def get_diagonal(coord1, coord2)
if is_diagonal?(coord1, coord2)
diagonal_array = [@game_grid[@row1][@col1]]
case @row1
when @row1 > @row2 && @col1 >@col2 #direction: down left
#@row2.upto(@row1){|row| }
when @row1 < @row2 && @col2 >@col2 #direction: up left
when @row1 > @row2 && @col1 <@col2 #direction: down right
when @row1 < @row2 && @col1 >@col2 #direction: up right
else
end
else
raise ArgumentError.new("The coordinates you have entered are not diagonal coordinates")
end
diagonal_array << @game_grid[@row2][@col2]
end
private
def is_diagonal?(coord1, coord2)
@row1 = coord1.first
@row2 = coord2.first
@col1 = coord1.last
@col2 = coord2.last
#i'm experimenting with making row_diff and col_diff global variables because
#I think I will need those values later on.
@row_diff = @row1 - @row2
@col_diff = @col1 - @col2
return false if @row_diff == 0 #this will return false if the difference is zero
#we know we dont want the difference to be zero because
#that would mean that the coordinates are in the same row
return true if @row_diff.abs == @col_diff.abs
return false
end
end #===> ends the class
dice_grid = [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
boggle_board = BoggleBoard.new(dice_grid)
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) #=> dock
print "\n\n"
boggle_board.get_row(0) #=> ["b", "r", "a", "e"] with .join it becomes: brae
boggle_board.get_row(1) #=> ["i", "o", "d", "t"] with .join it becomes: iodt
boggle_board.get_row(2) #=> ["e", "c", "l", "r"] with .join it becomes: eclr
boggle_board.get_row(3) #=> ["t", "a", "k", "e"] with .join it becomes: take
print "\n\n"
boggle_board.get_col(0) #=> ["b", "i", "e", "t"] with .join it becomes: biet
boggle_board.get_col(1) #=> ["r", "o", "c", "a"] with .join it becomes: roca
boggle_board.get_col(2) #=> ["a", "d", "l", "k"] with .join it becomes: adlk
boggle_board.get_col(3) #=> ["e", "t", "r", "e"] with .join it becomes: etre
print "\n\n"
boggle_board.get_coord([2,1]) #=>c
boggle_board.get_coord([3,1]) #=>a
boggle_board.get_coord([3,2]) #=>k
boggle_board.get_coord([3,3]) #=>e because everybody loves cake. not really though.
#but people who do like it are crazy about it. I think.
#Can you access the "k" character at row 3 column 2?
boggle_board.get_diagonal([1,3],[3,1])
boggle_board.get_diagonal([2,1],[3,0])
#boggle_board.get_diagonal([2,1],[3,3]) #=> will return error because its not a diagonal coord..
=begin REVIEW AND OTHER NOTES
This is the output that was generated before using .join("") and before trying to implement get_coord and the
get_diagonal method.
dock
["b", "r", "a", "e"]
["i", "o", "d", "t"]
["e", "c", "l", "r"]
["t", "a", "k", "e"]
["b", "i", "e", "t"]
["r", "o", "c", "a"]
["a", "d", "l", "k"]
["e", "t", "r", "e"]
Review:
This challenge did a number of things for me. Firstly, it challengd me. At first, the concept of extracting a diagonal
array from this sequence of embedded arrays seemed like it would be difficult but easy enough to figure out with some
thinking. Little did I know that I would hit wall after wall while trying to come up with a proper method to extract the
right values. I think its important to note that I didnt use google at all for this challenge, or to make this method. I
tried to make a method completely on my own with the knowledge that I had because the challenge encouraged me to do so.
I do feel as though not using google worked to my benefit because in this challenge I was able to see exactly how far I
could go before admitting defeat. Furthermore, I do think I could have been able to solve it given more time and/or a hint
or two. I am quite proud of the fact that I was able to make a working is_diagonal? method which checked to see if the
inputs designated a valid diagonal request. And because I came to this soultion without searching elsewhere I am able
to call that a small victory.
I kept the get_diagonal method in my class because I believe that I made some progress in finding a solution. I dont think
I was completely off base in finding the right way to solve the problem. I do believe there is a much easier way to
write this method but I found I was so deep in this thought process that I wasnt able to see another way of working it out.
Looking at the get_diagonal method I was moving very close to having a huge web of if/then/case statements embedded within
eachother and just having a great mess of code that would be undecipherable to any reader, which is mainly the reason I
gave up on my pursuit. I feel as though generally a good programming solution will be in some way simplistic, or it will
have a way to become more simple. I thought about creating a few more helper methods like the is_diagonal? but nothing
really came to mind that would help readibility or efficiency. As I stated before I felt as though my efforts towards making
get_diagonal work are still worth keeping on the gist and looking at in the future to see if I can possibly come up
with a good working solution. Lastly, there were times while working on the get_diagonal method that were very exciting and
envigorating. The challenge fired something up inside me that made me want to try really hard to get a solution, and
although in the end it kind of broke me to give up I look forward to more challenges like this one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment