Class: Matrix
- Inherits:
-
Object
- Object
- Matrix
- Defined in:
- lib/wordgrid.rb
Overview
Adding two helper functions to the standard Matrix class which are needed by the Wordgrid class.
Instance Method Summary collapse
-
#find_cells_for_letter(letter) ⇒ Object
Adds a method to the Matrix class which returns an array of cells which match the string passed in.
-
#neighbor_cells(root_cell) ⇒ Object
Adds a method to the Matrix class which will find all the cells which border the cell requested.
Instance Method Details
#find_cells_for_letter(letter) ⇒ Object
Adds a method to the Matrix class which returns an array of cells which match the string passed in. A cell is just an array with the row and column.
-
Args :
-
letter
-> the character to search the matrix for.
-
-
Returns :
-
the cells which match the letter passed in. An array of two-value arrays.
-
-
Raises :
-
Nothing
-
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/wordgrid.rb', line 150 def find_cells_for_letter(letter) cells = [] each_with_index do |e, row, column| if (e == letter) cell = [row, column] cells.push(cell) end end return cells end |
#neighbor_cells(root_cell) ⇒ Object
Adds a method to the Matrix class which will find all the cells which border the cell requested
Neighbors are defined by the cells northwest, north, northeast, west, east, southwest, south, and southeast of the original cell. If the original cell is on the edge of the matrix, it does not wrap. In other words, for the following matrix:
A B C
D E F
G H I
the cell containing the letter E has the neighbors A,B,C,D,F,G,H,I. However, the cell containing the letter A only has the neighbors B,D,E.
-
Args :
-
root_cell
-> a two-element array (row, cell) identifying the cell whose neighbors we are seeking.
-
-
Returns :
-
an array of cells (each cell is a two-element array, containing row and cell) neighboring the original cell.
-
-
Raises :
-
Nothing
-
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/wordgrid.rb', line 184 def neighbor_cells(root_cell) row = root_cell[0] column = root_cell[1] cells = [] cells.push([row-1, column-1]) if row-1 >= 0 and column-1 >= 0 #NW cells.push([row-1, column]) if row-1 >= 0 #N cells.push([row-1, column+1]) if row-1 >= 0 and column+1 < column_size #NE cells.push([row, column-1]) if column-1 >=0 #W cells.push([row, column+1]) if column+1 < column_size #E cells.push([row+1, column-1]) if row+1 < row_size and column-1 >= 0 #SW cells.push([row+1, column]) if row+1 < row_size #S cells.push([row+1, column+1]) if row+1 < row_size and column+1 < column_size #SE return cells end |