Class: Grid
- Inherits:
-
Object
- Object
- Grid
- Defined in:
- lib/rcrossword/grid.rb
Instance Method Summary collapse
-
#cell(line, column) ⇒ Object
returns the content of one cell.
-
#initialize(width, height) ⇒ Grid
constructor
A new instance of Grid.
-
#insert(line, column, orientation, word) ⇒ Object
inserts a word.
- #insert_possible?(line, column, orientation, word) ⇒ Boolean
-
#line(n) ⇒ Object
returns the content of one line.
-
#set(line, column, character) ⇒ Object
sets one cell with a character.
-
#to_s ⇒ Object
returns an ASCII representation of the matrix.
Constructor Details
#initialize(width, height) ⇒ Grid
Returns a new instance of Grid.
2 3 4 5 6 7 8 9 |
# File 'lib/rcrossword/grid.rb', line 2 def initialize(width,height) @width = width @height = height @matrix = Array.new(height+1) for i in 1..@height @matrix[i] = "."*(@width+1) end end |
Instance Method Details
#cell(line, column) ⇒ Object
returns the content of one cell
17 18 19 20 21 22 23 |
# File 'lib/rcrossword/grid.rb', line 17 def cell(line,column) if column >= 1 and column <= @width self.line(line)[column-1..column-1] else nil end end |
#insert(line, column, orientation, word) ⇒ Object
inserts a word. orientation is either :horizontal or :vertical
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/rcrossword/grid.rb', line 69 def insert(line,column,orientation,word) raise ArgumentError if orientation != :horizontal and orientation != :vertical l = line c = column if ! insert_possible?(l,c,orientation,word) #puts "cannot insert #{word}" return false end self.set(l,c,'#') # explanation cell l += 1 if orientation == :vertical c += 1 if orientation == :horizontal for pos in 0...word.size # Kollision? if cell(l,c) != '.' and cell(l,c) != word[pos..pos].upcase return false end self.set(l,c,word[pos..pos].upcase) l += 1 if orientation == :vertical c += 1 if orientation == :horizontal end if orientation == :horizontal and c <= @width if cell(l,c) != '#' self.set(l,c,'/') end end if orientation == :vertical and l <= @height if cell(l,c) != '#' self.set(l,c,'/') end end true end |
#insert_possible?(line, column, orientation, word) ⇒ Boolean
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rcrossword/grid.rb', line 35 def insert_possible?(line,column,orientation,word) raise ArgumentError if orientation != :horizontal and orientation != :vertical l = line c = column if cell(l,c) != '.' return false end l += 1 if orientation == :vertical c += 1 if orientation == :horizontal for pos in 0...word.size # Kollision? if cell(l,c) != '.' and cell(l,c) != word[pos..pos].upcase return false end #self.set(l,c,word[pos..pos].upcase) l += 1 if orientation == :vertical c += 1 if orientation == :horizontal end # danach entweder Erklaerungszelle oder Stopzelle oder schon letzte Position im Grid if orientation == :horizontal and c > @width #OK elsif orientation == :vertical and l > @height #OK else if cell(l,c) == '#' or cell(l,c) == '/' or cell(l,c) == '.' #OK else return false end end true end |
#line(n) ⇒ Object
returns the content of one line
12 13 14 |
# File 'lib/rcrossword/grid.rb', line 12 def line(n) @matrix[n][1..-1] end |
#set(line, column, character) ⇒ Object
sets one cell with a character
26 27 28 29 30 31 32 33 |
# File 'lib/rcrossword/grid.rb', line 26 def set(line,column,character) begin @matrix[line][column] = character rescue NoMethodError puts "Fehler: line #{line}, column #{column}, character #{character}" raise end end |
#to_s ⇒ Object
returns an ASCII representation of the matrix
103 104 105 106 107 108 109 |
# File 'lib/rcrossword/grid.rb', line 103 def to_s result = '' for line in 1..@height result << self.line(line)+"\n" end result end |