Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cnocon/7362911 to your computer and use it in GitHub Desktop.
Save cnocon/7362911 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
class BoggleBoard
def initialize (board)
@board=board
end
def create_word(*coords)
puts coords.map { |coord| @board[coord.first][coord.last]}.join("")
end
def get_row(row)
@board[row-1]
end
def get_col(col)
@board.map { |row| row[col-1]}
end
def get_diagonal(row1,col1,row2,col2)
slope = (row1 - row2) / (col1 - col2)
if slope.abs == 1 #is a true diagonal
length = (row1 - row2).abs + 1
word = []
until word.length == length
word << @board[row1][col1]
if col1 < col2 && row1 < row2 # 0,0,3,3
row1 += 1
col1 += 1
elsif col1 < col2 && row1 > row2 # 0,3,3,0
col1 += 1
row1 -= 1
elsif col1 > col2 && row1 < row2 # 3,0,0,3
col1 -= 1
row1 += 1
elsif col1 > col2 && row1 > row2 # 3,3,0,0
row1 -= 1
col1 -= 1
end
end
word.join
else
puts "These coordinates can't make a diagonal."
end
end
end
dice_grid = [["b", "r", "a", "e"],
["i", "o", "a", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
boggle_board = BoggleBoard.new(dice_grid)
#for tthiis to work the first coordinate must belong to the first letter of the word
p boggle_board.get_diagonal(0,0,3,3) # => "bole"
p boggle_board.get_diagonal(3,0,0,3) # => "tcae"
p boggle_board.get_diagonal(3,3,0,0) # => "elob"
p boggle_board.get_diagonal(0,3,3,0) # => "eact"
#p new_board.get_diagonal(3,2,1,0) # => "rad"
#p boggle_board.create_word([2,1], [1,1], [1,2], [0,3]) #=> returns "code"
#p boggle_board.create_word([0,1], [0,2], [1,2]) #=> creates what california slang word? ("rad")
#p boggle_board.create_word([0,0], [1,1], [2,2],[1,2]) #=> returns "bold"
#p boggle_board.create_word([1,2], [0,3], [0,2],[0,1]) #=> returns "dear"
#
#p boggle_board.get_row(1) #=> ["b","r","a","e"]
#p boggle_board.get_row(2) #=> ["i", "o", "d", "t"]
#p boggle_board.get_row(3) #=> ["e", "c", "l", "r"]
#p boggle_board.get_row(4) #=> ["t", "a", "k", "e"]
#
#p boggle_board.get_col(1) #=> should be ["b","i","e","t"] and not ["r", "o", "c", "a"] as in the example
#p boggle_board.get_col(2) #=> should be ["r","o","c","a"]
#p boggle_board.get_col(3) #=> should be ["a","d","l","k"]
#p boggle_board.get_col(4) #=> should be ["e","t","r","e"]
#TOTAL OUTPUT ("take" is already a word )
#p boggle_board.get_row(1).join(",") #=> ["b","r","a","e"]
#p boggle_board.get_row(2).join(",") #=> ["i", "o", "d", "t"]
#p boggle_board.get_row(3).join(",") #=> ["e", "c", "l", "r"]
#p boggle_board.get_row(4).join(",") #=> ["t", "a", "k", "e"]
#p boggle_board.get_col(1).join(",") #=> should be ["b","i","e","t"] and not ["r", "o", "c", "a"] as in the example
#p boggle_board.get_col(2).join(",") #=> should be ["r","o","c","a"]
#p boggle_board.get_col(3).join(",") #=> should be ["a","d","l","k"]
#p boggle_board.get_col(4).join(",") #=> should be ["e","t","r","e"]
#ACCESS A COORDINATE
# implement tests for each of the methods here:
# create driver test code to retrieve a value at a coordinate here:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment