Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save micodel/8739032 to your computer and use it in GitHub Desktop.
Save micodel/8739032 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
# INITIAL CODE:
# -------------
class Boggle
def initialize(dice_grid)
@board = dice_grid
end
def create_word(*coords)
coords.map { |coord| @board[coord.first][coord.last]}.join("")
end
def get_letter(row, col)
@board[row][col]
end
def get_row(row)
@board[row].inspect
end
# row - 1 to make it more human friendly
def get_column(column)
column_array = Array.new
@board.each { |e| column_array.push(e[column]) }
column_array.inspect # Remove #inspect to see as a column.
end
# Can return diagonal from selected starting top corner.
def get_diagonal(corner="left")
diag_array = Array.new
corner = corner.downcase
counter = 0
if corner == "left"
4.times { diag_array.push(@board[counter][counter]) ; counter += 1 }
elsif corner == "right"
4.times { diag_array.push(@board[3 - counter][counter]) ; counter += 1 }
else
puts "Please choose 'right' or 'left' corner."
end #if
diag_array.inspect
end
end #class Boggle
# DRIVER CODE:
# ------------
puts "DRIVER CODE"
dice_grid = [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
game_001 = Boggle.new(dice_grid)
# Get Letter Test:
# ----------------
#0.
print "\n" ; puts "Get Letter Test"
print game_001.get_letter(2, 1) == "c" ; puts " #=> should be true"
print game_001.get_letter(2, 3) == "x" ; puts " #=> should be false"
print game001.get_letter(3, 2) == "k" ; puts "If accessed the "k" character return should be TRUE"
#1.
puts game_001.create_word([2,1], [1,1], [1,2], [0,3]) #=> returns "code"
puts game_001.create_word([0,1], [0,2], [1,2]) #=> creates what california slang word?
puts game_001.create_word([1,3], [0,3], [0,2], [0,1]) # Should return "tear"
puts game_001.create_word([3,0], [3,1], [3,2], [3,3]) # Should return "take"
#2.
puts game_001.get_row(1) #=> ["i", "o", "d", "t"]
#3.
puts game_001.get_column(1) #=> "r"
#=> "o"
#=> "c"
#=> "a"
# OR => ["r", "o", "c", "a"] /w #inspect
puts game_001.get_column(0) #=> ["b", "i", "e", "t"]
puts game_001.get_column(2) #=> ["a", "d", "l", "k"]
#Bonus.
puts game_001.get_diagonal("right")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment