Forked from dbc-challenges/0.2.1-boggle_class_from_methods.rb
Last active
December 31, 2015 22:09
-
-
Save drewfwest/8051828 to your computer and use it in GitHub Desktop.
Create A Boggle Board 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
=begin | |
We've already worked with hashes and arrays as data structures. | |
They are handy ways of collecting and organizing data, | |
and ideal for modeling a group of objects. | |
In this challenge, we're going to model a grid or a board that has | |
coordinates for each of it's cells. A logical way of modeling a board | |
is to use a nested array, where a row and column are its coordinates. | |
Let's explore a boggle board as an example. | |
boggle_board = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
If we wanted to access the "r" character in the first row, | |
we'd use the syntax below because "r" is in row 0 and under column 1. | |
(Remember that arrays start counting from 0). | |
puts boggle_board[0][1] == "r" # returns boggle_board[row_number][column_number] | |
puts boggle_board[2][1] == "c" #=> should be true | |
puts boggle_board[3][3] == "e" #=> should be true | |
puts boggle_board[2][3] == "x" #=> should be false | |
In boggle, you can spell out words by collecting letters that are | |
immediately next to one another. (up, down, across, or diagonally). | |
One of the words possible in the above boggle board is "code." | |
#1) Access multiple elements of a nested array | |
#2) Write a method that takes a row number and returns all the elements in the row. | |
#3) Now write a method that takes a column number and returns all the elements in the column. | |
=end | |
#INITIAL | |
#With Jordan Luyke and Drew West | |
class Boggle | |
@@boggle_board = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
#1) Access multiple elements of a nested array | |
def create_word(*coords) | |
coords.map { |coord| @@boggle_board[coord.first][coord.last]}.join("") | |
end | |
#2) Write a method that takes a row number and returns all the elements in the row. | |
def get_row(row) | |
return @@boggle_board[row] | |
end | |
#3) Now write a method that takes a column number and returns all the elements in the column. | |
def get_col(col) | |
column_array = Array.new | |
for index in 0...@@boggle_board.length | |
column_array << @@boggle_board[index][col] | |
end | |
return column_array | |
end | |
end | |
# DRIVER CODE | |
boggle = Boggle.new | |
# Create 2 more calls to the create_word method to spell other words in the Boggle board. | |
puts boggle.create_word([0,0], [1,1], [0,2], [0,1], [1,2]) == "board" | |
puts boggle.create_word([3,0], [3,1], [2,1], [3,2], [2,2], [3,3]) == "tackle" | |
# Write 2 more tests to make sure it will work with other rows. Make sure you include expectations. | |
puts boggle.get_row(0) == ["b", "r", "a", "e"] | |
puts boggle.get_row(1) == ["i", "o", "d", "t"] | |
puts boggle.get_row(2) == ["e", "c", "l", "r"] | |
puts boggle.get_row(3) == ["t", "a", "k", "e"] | |
# Write two more tests for this method and include it in your gist. | |
puts boggle.get_col(0) == ["b", "i", "e", "t"] | |
puts boggle.get_col(1) == ["r", "o", "c", "a"] | |
puts boggle.get_col(2) == ["a", "d", "l", "k"] | |
puts boggle.get_col(3) == ["e", "t", "r", "e"] | |
=begin | |
PSEUDOCODE | |
STEPS | |
In order to create a class we took out input for create_word and | |
made @@boggle_board a class variable. | |
@@boggle_board indicates a class variable (defined outside of the class methods) | |
@boggle_borad indicates an instance variable (defined inside one of the class methods) | |
The tricky get_col can be solved with a for loop (Jordan's favorite) | |
You iterate for each value in the @@boggle_board array | |
and select the column value to return. The method will return only the column | |
value indicated. | |
We were getting an error for => for index in 0...@@boggle_board.length | |
because we were using ".." instead of "..." | |
Be careful because .length is counting the length of the array as 1, 2, 3, 4 | |
but index counts 0, 1, 2, 3 | |
=end | |
#REFACTOR | |
#With Brittan McGinnis and Drew West | |
class Boggle | |
@@dice_grip = [["b", "r", "a", "e"], | |
["i", "o", "d", "t"], | |
["e", "c", "l", "r"], | |
["t", "a", "k", "e"]] | |
#1) Access multiple elements of a nested array | |
def create_word(*coords) | |
coords.map { |coord| @@dice_grip[coord.first][coord.last]}.join("") | |
end | |
#2) Write a method that takes a row number and returns all the elements in the row. | |
def get_row(row_num) | |
@@dice_grip[row_num] | |
end | |
#3) Now write a method that takes a column number and returns all the elements in the column. | |
def get_col(col_num) | |
@dice_grid.map { |e| e[col_num] } | |
end | |
# e simply stands in as an element. The loop iterates over every occurance of e, | |
# and since e is not defined, it is simply set to the base unit, which in this case | |
# will be each element in the array (each sub array within the array) | |
#4) Access a coordinate | |
def get_char(*coords) | |
coords.map do |coord| | |
@dice_grid[coord.first][coord.last] | |
end | |
end | |
#This works, but I'm sure there is a simpler way to grab a single coordinate. | |
#5) Bonus: Create a #get_diagonal method | |
def get_diag(*first_coord, last_coord) | |
until first_coord == last_coord | |
@dice_grid.map {|diag_coord| diag_coord[first_coord.first][first_coord.last] } | |
first_coord.first += 1 | |
first_coord.last += 1 | |
end | |
end | |
# This doesn't present any errors, but it runs an infinite loop | |
# The thing that is appealing about this approach is that it should | |
# work for any diagonal. 2 more things are needed to create a working diagonal finder: | |
# 1) A way for the code to know which direction to move from one diagonal coordinate to the other | |
# 2) The method needs a mechanism to swith the direction of the grid movement based | |
# on the relationship of the coordinates. For instance, a diagonal from [0,0] to [3,3] | |
# will require += 1 movement on both x and y axis, but a movement from [0,3] to [3,0] | |
# would require += 1 on the y axis and -= 1 on the x axis. | |
end | |
boggle = BoggleBoard.new | |
puts boggle.create_word([0,0], [1,1], [0,2], [0,1], [1,2]) #== "board" | |
puts boggle.create_word([3,0], [3,1], [2,1], [3,2], [2,2], [3,3]) #== "tackle" | |
puts boggle.create_word([0,0], [1,1], [0,2], [0,1], [1,2]) #== "board" | |
puts boggle.create_word([3,0], [3,1], [2,1], [3,2], [2,2], [3,3]) # == "tackle | |
print boggle.get_row(1) #=> ["i", "o", "d", "t"] | |
print "\n" #=> newline | |
print boggle.get_row(0)#=> ['b', 'r, 'a, 'e'] | |
print "\n" #=> newline | |
print boggle.get_col(0)#=>['b','i', 'e', 't'] | |
print "\n" | |
#Now print out all the rows and columns of the board as strings | |
print boggle.get_row(0) #=> brae | |
print "\n" #=> newline | |
print boggle.get_row(1)#=> iodt | |
print "\n" #=> newline | |
print boggle.get_row(2)#=> eclr | |
print "\n" #=> newline | |
print boggle.get_row(3)#=> take | |
print "\n" #=> newline | |
print boggle.get_col(0)#=> biet | |
print "\n" #=> newline | |
print boggle.get_col(1)#=> roca | |
print "\n" #=> newline | |
print boggle.get_col(2)#=> adlk | |
print "\n" #=> newline | |
print boggle.get_col(3)#=> etre | |
#REFLECTION | |
# http://www.techotopia.com/index.php/Ruby_Variable_Scope | |
# Name Begins With Variable Scope | |
# $ A global variable | |
# @ An instance variable | |
# [a-z] or _ A local variable | |
# [A-Z] A constant | |
# @@ A class variable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment