Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fab9/8772278 to your computer and use it in GitHub Desktop.
Save fab9/8772278 to your computer and use it in GitHub Desktop.
phase 0 unit 2 week 1 boggle class challenge
# Create a class BoggleBoard that includes the functionality of your methods from the previous challenge.
class BoggleBoard
attr_reader :dice_grid
attr_accessor :get_row, :get_col
def initialize(dice_grid)
@dice_grid = dice_grid
end
def create_word(dice_grid, *coords)
coords.map { |coord| @dice_grid[coord.first][coord.last]}.join("")
end
def get_row(row)
@dice_grid[row]
end
def get_col(col)
total_col = @dice_grid[0].length
counter = 0
col_index = col.to_i
row_index = 0
col = []
while counter < total_col
col << @dice_grid[row_index][col_index]
counter +=1
row_index +=1
end
return col
end # /get_col
end # /class
dice_grid = [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
boggle_board = BoggleBoard.new(dice_grid)
#======================= OBJECTIVE 1 ===============================
# What needed to change in order integrate methods from
# previous challenge into BoggleBoard class?
#
# 1. Write initialize block: takes 1 argument (an array, dice_grid)
# 2. Define instance variables
# 3. Write attribute readers/writers
# 4. Kept getting errors in my tests as a result of calling methods "without" objects. Remember to call the
# method "on" the object. Say p boggle_board.get_row(1) NOT p get_row(1)
# How does the boggle_board object hold the dice_grid?
# In an instance variable
#======================= OBJECTIVE 2: TESTS ===========================
puts dice_grid[0][1] == "r" # returns dice_grid[row_number][column_number]
puts dice_grid[2][1] == "c" #=> should be true
puts dice_grid[3][3] == "e" #=> should be true
puts dice_grid[2][3] == "r" #=> should be false
# implement tests for each of the methods here:
puts boggle_board.create_word(dice_grid, [1,2], [1,1], [2,1], [3,2]) == "dock" #=> true
p boggle_board.get_row(0) == ["b", "r", "a", "e"] #=> true
p boggle_board.get_col(1) == ["r", "o", "c", "a"] #=> true
# print out all the rows and columns of the board as strings
p boggle_board.get_row(0) #=> returns ["b", "r", "a", "e"]
p boggle_board.get_row(1) #=> returns ["i", "o", "d", "t"]
p boggle_board.get_row(2) #=> returns ["e", "c", "l", "r"]
p boggle_board.get_row(3) #=> returns ["t", "a", "k", "e"]
p boggle_board.get_col(0) #=> returns ["b", "i", "e", "t"]
p boggle_board.get_col(1) #=> returns ["r", "o", "c", "a"]
p boggle_board.get_col(2) #=> returns ["a", "d", "l", "k"]
p boggle_board.get_col(3) #=> returns ["e", "t", "r", "e"]
#======================= OBJECTIVE 3 ===============================
# create driver test code to retrieve a value at a coordinate here:
p dice_grid[3][2] == "k" #=> should be true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment