Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Caroisawesome/8727340 to your computer and use it in GitHub Desktop.
Save Caroisawesome/8727340 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
class BoggleBoard
def initialize(boggle_board = nil) # can accept no arguments in case
# user wants to generate_new_board
@boggle_board = boggle_board
end
def create_word(*coordinates)
p coordinates.collect{ |item| @boggle_board[item.first][item.last] }.join('')
end
def get_row(row)
@boggle_board[row - 1].join('')
end
def get_column(column_number)
col_array = [ ]
@boggle_board.each_index{|index| col_array << @boggle_board[index][column_number - 1]}
col_array.join('')
end
def get_letter(coordinate)
@boggle_board[coordinate[0]][coordinate[1]]
end
def get_diagonal(coordinate1, coordinate2)
diagonal = []
if coordinate1.uniq.size == 1 && coordinate2.uniq.size == 1
left_corner = [0,0]
right_corner = [3,3]
while left_corner != right_corner
diagonal << get_letter(left_corner)
left_corner.map!{|row_column| row_column + 1}
end
diagonal.push(get_letter(right_corner)).join('')
elsif coordinate2.inject(:+) == 3 && coordinate2.inject(:+) == 3
row, column = 3, 0
while [row,column] != [0,3]
diagonal << get_letter([row,column])
row -= 1
column += 1
end
diagonal.push(get_letter([0,3])).join('')
else
raise(ArgumentError, "coordinates do not match a diagonal")
end
end
def generate_new_board()
@boggle_board = Array.new(4){ Array.new(4){ ('a'..'z').to_a.shuffle[0]} }
p get_row(1)
p get_row(2)
p get_row(3)
p get_row(4)
end
end
dice_grid = [["b", "r", "a", "e"], # [0,0] [0,1] [0,2] [0,3]
["i", "o", "d", "t"], # [1,0] [1,1] [1,2] [1,3]
["e", "c", "l", "r"], # [2,0] [2,1] [2,2] [2,3]
["t", "a", "k", "e"]] # [3,0] [3,1] [3,2] [3,3]
boggle_board = BoggleBoard.new(dice_grid)
# implement tests for each of the methods here:
puts boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) # => "dock"
puts boggle_board.get_row(1) # => "brae"
puts boggle_board.get_row(2) # => "iodt"
puts boggle_board.get_row(3) # => "eclr"
puts boggle_board.get_row(4) # => "take" REAL ENGLISH WORD ALERT!
puts boggle_board.get_column(1) # => "biet"
puts boggle_board.get_column(2) # => "roca" REAL SPANISH WORD ALERT!
puts boggle_board.get_column(3) # => "adlk"
puts boggle_board.get_column(4) # => "etre" REAL FRENCH WORD ALERT?
puts boggle_board.get_letter([3,2]) # => "k"
puts boggle_board.get_diagonal([2,1],[1,2]) # => "tcde"
puts boggle_board.generate_new_board
new_boggle = BoggleBoard.new
puts new_boggle.generate_new_board
puts new_boggle.get_column(3)
# create driver test code to retrieve a value at a coordinate here:
puts boggle_board.get_letter([3,2]) # => "k"
# REFLECTION!!!!
# I didn't even know we were doing procedural programming the whole time!!!!
# The main difference that I gathered was that procedural programming focuses on organizing
# steps that must be completed to solve the probelm. It is a more systematic approach
# that uses more computational and logical functions to complete tasks. Object oriented
# programming seems to be focused on structuring the around objects,
# and having those objects interact with each other in a heirarchical manner. To me it
# feels like something that is based off of a more human means of organizing, rather than
# logical or mathmatical functions? Though it can often produce more code and slower processing
# time, a benefit to OOP would be an overall structure that is easier to conceptualize. Anyone
# approaching the code for the first time would be able to easily figure out how and where to
# alter the code to achieve a certain effect without spending much time with the code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment