Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
August 29, 2015 13:56
-
-
Save blsalcido/9187631 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1boggle class challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#INITIAL CODE | |
class BoggleBoard | |
def initialize(dice_grid) | |
@dice_grid = dice_grid | |
end | |
#This method should return a word by accessing multiple coordinates in dice grid | |
def create_word(coordinates) #How to pass multiple coordinates??????? | |
#if the coordinates in same row (if row1 == row2:) | |
if row1 == row2 #if the coordinates in same row (if row1 == row2:) | |
puts "row is same" | |
row_array = get_row(row1)#get the row | |
puts row_array | |
if col1>col2 | |
temp = col1 #temp = temporary variable name..........need to change strategy | |
col1 = col2 | |
col2 = temp | |
return row_array[col1..col2] #grab the part of the row between the two col coordinates | |
elsif col1 == col2: #if the coordinates in same column | |
col_array = @get_col #get the column | |
return col_array[row1..row2]#grab the part of the column between the two coordinates | |
end | |
end | |
def get_row(row) #Should the argument name be differnt? | |
return @dice_grid[row] | |
end | |
def get_col(column) | |
return @dice_grid[column] #This is incomplete. | |
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) | |
theWord =boggle_board.create_word(2,3,1,3) #(col1,row1,col2,row2) # NEED TO FIX #create_word!! | |
#Current Code | |
class BoggleBoard | |
attr_reader :dice_grid | |
def initialize(dice_grid) | |
@dice_grid = dice_grid | |
end | |
def create_word(*coord) | |
puts coord.map {|coord| @dice_grid[coordinates.first][coordinates.last]}.join"" | |
end | |
def get_row(row) | |
puts @dice_grid[row] | |
end | |
def get_col(col) | |
@dice_grid.map {|row| row[col]} | |
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) | |
puts boggle_board.create_word([1,2],[1,1],[2,1],[3,2]) | |
#Had to change my approach almost entirely. Learned a lot on this one and now my brain hurts but, | |
#I think it was worth it. I will give a more thorough reflection tomorrow. Be advised my | |
#test code is not complete and I still have a few questions about the output I recieved from | |
#the current solution. I know this is late, I intended to be done a few hours ago but as | |
#I got sleepier and sleepier my ability to function dwindled substantially by the hour and | |
#this ended up taking me much longer than I anticipated. | |
# | |
# | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BoggleBoard ### creates class BoggleBoard | |
attr_reader :dice_grid ### makes @dice_grid readable | |
def initialize(dice_grid)### initialize class | |
@dice_grid = dice_grid ### makes dice_grid an instance variable | |
end ### ends #initialize | |
def create_word(*coords) ### defines #create_word - it will accept coordinates as arguments | |
coords.map{|coord| @dice_grid[coord.first][coord.last]}.join("") | |
end ### ^ #map takes an object and a block - it'll run the block for | |
### each element and outputs the values from the block. | |
### It's taking the first and second coordinates from board and "smooshing" them with #join. | |
### If #join wasn't there, we'd get "c o d e". | |
def get_row(row) ### defines #get_row | |
@dice_grid[row]### returns corresponding row element of nested array @dice_grid | |
end ### ends #get_row | |
def get_col(col) ### defines #get_col | |
@dice_grid.transpose[col]### returns corresponding column element of @dice_grid | |
end ### ends #get_col | |
def get_diagonal(coord1,coord2) ### defines get_diagonal - it will accept two coordinates | |
possible_diagonal_coords1 =[[0,0],[3,3]]### array of set of possible diagonal coordinates | |
possible_diagonal_coords2 = [[0,3],[3,0]]### array of set of possible diagonal coordinates | |
possible_diagonal_coords3 = [[3,0],[0,3]]### array of set of possible diagonal coordinates | |
possible_diagonal_coords4 = [[3,3],[0,0]]### array of set of possible diagonal coordinates | |
check_if_diagonal = [coord1, coord2] ### array of coordinates passed in as arguments | |
if check_if_diagonal == possible_diagonal_coords1 ### when arguments are equal to this set of coords | |
create_word([0,0], [1,1], [2,2], [3,3]) ### create this word | |
elsif check_if_diagonal == possible_diagonal_coords2 ### when arguments are equal to this set of coords | |
create_word([0,3], [1,2], [2,1], [3,0]) ### create this word | |
elsif check_if_diagonal == possible_diagonal_coords3 ### when arguments are equal to this set of coords | |
create_word([3,0], [2,1], [1,2], [0,3]) ### create this word | |
elsif check_if_diagonal == possible_diagonal_coords4 ### when arguments are equal to this set of coords | |
create_word([3,3], [2,2], [1,1], [0,0]) ### create this word | |
else raise "Those are not possible diagonal coordinates!"### if arguments are not equal to any set of possible | |
end ### ends if/else loop ### diagonal coords, raise this error message. | |
end ### ends #get_diagonal | |
end ### ends class BoggleBoard | |
dice_grid = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
boggle_board = BoggleBoard.new(dice_grid) | |
# implement tests for each of the methods here: | |
puts boggle_board.create_word([2,1], [3,1], [3,2], [3,3]) #=> "cake" ###true | |
puts boggle_board.create_word([0,1],[0,2],[1,2],[1,0],[1,1]) #=>"radio" ###true | |
puts boggle_board.get_diagonal([0,0],[3,3]) #=> "bole" ###true | |
puts boggle_board.get_diagonal([0,3],[3,0]) #=> "edct" ###true | |
puts boggle_board.get_diagonal([3,0],[0,3]) #=> "tcde" ###true | |
puts boggle_board.get_diagonal([3,3],[0,0]) #=> "elob" ###true | |
puts boggle_board.get_diagonal([0,0],[3,2]) #=> "Those coordinates are not diagonal" ###true | |
puts boggle_board.get_row(2) #=> ["e","c","l","r"] ###true | |
puts boggle_board.get_col(1) #=> ["r","o","c","a"] ###true | |
# create driver test code to retrieve a value at a coordinate here: | |
#Can you access the "k" character at row 3 column 2? | |
puts boggle_board.create_word([3,2]) == "k" ###true | |
puts boggle_board.create_word([3,1]) == "a" ###true | |
###REFLECTION### | |
# I wasn't really sure if I was supposed to check if coord2 in #get_diagonal was *diagonal to* | |
# coord1 and vice versa or if I was supposed to do what I did above - create words using the coord | |
# arguments as starting and ending points to find a diagonal "word" inside the nested array. | |
# | |
# Actually, I went down a rabbit-hole for a bit researching methods. I ended up making two extra | |
# methods for this class. Basically, they take dice_grid and switch its columns and rows, join all | |
# the elements together with ~ and then return the result. If you trace the pattern of the result | |
# in the nested array with your finger, it makes a snake shape. :) | |
# Anyway, I know I wasn't really supposed to do this but, I learned about transpose in the last | |
# exercise and used it again in this one and wanted to try it out a different way. | |
# Besides, I don't seem to have the heart to delete my cute little methods! <3 | |
# This exercise was a LOT more fun the second time around! | |
# | |
=begin | |
def snakey_letters | |
snakey_array = @dice_grid.transpose.join("~") | |
end | |
def backwards_snakey_letters | |
snakey_array = @dice_grid.transpose.join("~").reverse! | |
end | |
### puts boggle_board.snakey_letters #=> b~i~e~t~r~o~c~a~a~d~l~k~e~t~r~e ###true | |
### puts boggle_board.backwards_snakey_letters #=> "e~r~t~e~k~l~d~a~a~c~o~r~t~e~i~b ###true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment