Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mcivorsteiner/8568608 to your computer and use it in GitHub Desktop.

Select an option

Save mcivorsteiner/8568608 to your computer and use it in GitHub Desktop.

Revisions

  1. mcivorsteiner revised this gist Jan 24, 2014. 1 changed file with 74 additions and 3 deletions.
    77 changes: 74 additions & 3 deletions 0.2.1-boggle_class_from_methods.rb
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,93 @@
    class BoggleBoard
    attr_reader :board

    #your code here
    def initialize(board)
    @board = board
    end

    def create_word(*coords)
    coords.map { |coord| @board[coord.first][coord.last]}.join("")
    end

    def get_row(row)
    @board[row]
    end

    def get_col(col)
    col_arr = []
    @board.each {|row| col_arr << row[col]}
    col_arr
    end

    def get_diagonal(coord_1, coord_2)
    slope = ((coord_1[0]-coord_2[0]).to_f/(coord_1[1] - coord_2[1]))
    # returning ArgumentError if slope of coordinates not equal to 1 or -1 (not diagonal)
    return ArgumentError.new("coordinates not diagonal") if slope.abs != 1

    row_qty = @board.length
    col_qty = @board[0].length
    curr_row = coord_1[0]
    curr_col = coord_1[1]

    # running loop to determine left-most coordinate in diagonal
    until curr_col == 0 || ((curr_row == row_qty-1) && (slope == -1)) || ((curr_row == 0) && (slope == 1))
    curr_row -= slope
    curr_col -= 1
    end

    diag_arr = []
    # starting with left-most coordinate in the diagonal, pushing all element in diagonal to diag_arr
    while (curr_row >= 0) && (curr_row < row_qty) && (curr_col < col_qty)
    diag_arr << @board[curr_row][curr_col]
    curr_row += slope
    curr_col += 1
    end
    diag_arr
    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)

    # implement tests for each of the methods here:

    p boggle_board.create_word([0,1], [1,1], [2,1], [3,2]) == "rock"
    p boggle_board.create_word([2,1], [1,1], [2,2], [1,2]) == "cold"
    p boggle_board.create_word([1,2], [1,1], [2,1], [3,2]) == "dock"

    p boggle_board.get_row(1) == ["i", "o", "d", "t"]
    p boggle_board.get_row(0) == ["b", "r", "a", "e"]
    p boggle_board.get_row(3) == ["t", "a", "k", "e"]

    p boggle_board.get_col(1) == ["r", "o", "c", "a"]
    p boggle_board.get_col(0) == ["b", "i", "e", "t"]
    p boggle_board.get_col(3) == ["e", "t", "r", "e"]

    # calls of #get_diagonal method
    p boggle_board.get_diagonal([3,0], [0,2]).class == ArgumentError
    p boggle_board.get_diagonal([2,1], [1,2]) == ["t", "c", "d", "e"]
    p boggle_board.get_diagonal([2,0], [1,1]) == ["e", "o", "a"]
    p boggle_board.get_diagonal([3,2], [2,3]) == ["k", "r"]
    p boggle_board.get_diagonal([2,3], [1,2]) == ["r", "d", "r"]
    p boggle_board.get_diagonal([3,3], [0,0]) == ["b", "o", "l", "e"]

    # create driver test code to retrieve a value at a coordinate here:
    p boggle_board.board[3][2] == "k"

    # print all rows as strings:
    (0..3).to_a.each { |n| p boggle_board.get_row(n).join } #=> prints "brae" "iodt" "eclr" "take"
    # print all columns as strings:
    (0..3).to_a.each { |n| p boggle_board.get_col(n).join } #=> prints "biet" "roca" "adlk" "etre"


    # REFLECTION
    # This challenge was pretty simple until I started the #get_diagonal method. I understood the
    # question as requiring the method to return an array of the elements in the diagonal from left to
    # right. I had issues with the boolean statements in the until and while loops. I did learn a lot
    # from the process of finding the bugs.
  2. @dbc-challenges dbc-challenges revised this gist Oct 31, 2013. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion 0.2.1-boggle_class_from_methods.rb
    Original file line number Diff line number Diff line change
    @@ -10,4 +10,13 @@ class BoggleBoard
    ["e", "c", "l", "r"],
    ["t", "a", "k", "e"]]

    boggle_board = BoggleBoard.new(dice_grid)
    boggle_board = BoggleBoard.new(dice_grid)

    # implement tests for each of the methods here:



    # create driver test code to retrieve a value at a coordinate here:



  3. @dbc-challenges dbc-challenges created this gist Oct 31, 2013.
    13 changes: 13 additions & 0 deletions 0.2.1-boggle_class_from_methods.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    class BoggleBoard

    #your code here

    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)