brewersfan1976

neighboring_cells.rb

Feb 25th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.26 KB | None | 0 0
  1. def neighboringCells(matrix)
  2.     x = 0
  3.     y = 0
  4.     array = Array.new(matrix.length) {Array.new(matrix[0].length)}
  5.    
  6.     while (x < matrix.length)
  7.           while (y < matrix[0].length)
  8.                 neighbor = 0
  9.                
  10.                 if x - 1 >= 0 then
  11.                    if matrix[x - 1][y] == 0 then
  12.                       neighbor = neighbor + 1
  13.                    end
  14.                 end
  15.                    
  16.                 if y - 1 >= 0 then
  17.                    if matrix[x][y - 1] == 0 then
  18.                       neighbor = neighbor + 1
  19.                    end
  20.                 end
  21.                    
  22.                 if y + 1 < matrix[0].length then
  23.                    if matrix[x][y + 1] == 0 then
  24.                       neighbor = neighbor + 1
  25.                    end
  26.                 end
  27.                  
  28.                 if x + 1 < matrix.length then
  29.                    if matrix[x + 1][y] == 0 then
  30.                       neighbor = neighbor + 1
  31.                    end
  32.                 end
  33.                    
  34.                 array[x][y] = neighbor
  35.                    
  36.                 y = y + 1
  37.           end
  38.                    
  39.           y = 0
  40.           x = x + 1          
  41.     end
  42.                    
  43.     return array
  44. end
Advertisement
Add Comment
Please, Sign In to add comment