Class: Rudoku::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/rudoku.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board, i, x, y) ⇒ Field

Returns a new instance of Field.



199
200
201
202
203
204
205
206
207
208
209
# File 'lib/rudoku.rb', line 199

def initialize(board, i, x, y)
  @board = board
  @index = i
  @x = x
  @y = y
  @marked = false

  @row   = @board.get_row(y) or raise
  @col   = @board.get_col(x) or raise
  @block = @board.get_block(x, y) or raise
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



197
198
199
# File 'lib/rudoku.rb', line 197

def block
  @block
end

#indexObject (readonly)

Returns the value of attribute index.



197
198
199
# File 'lib/rudoku.rb', line 197

def index
  @index
end

#valueObject

Returns the value of attribute value.



197
198
199
# File 'lib/rudoku.rb', line 197

def value
  @value
end

#xObject (readonly)

Returns the value of attribute x.



197
198
199
# File 'lib/rudoku.rb', line 197

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



197
198
199
# File 'lib/rudoku.rb', line 197

def y
  @y
end

Instance Method Details

#add(v) ⇒ Object



236
237
238
239
240
# File 'lib/rudoku.rb', line 236

def add(v)
  @block.add(v)
  @row.add(v)
  @col.add(v)
end

#available_nrsObject



246
247
248
# File 'lib/rudoku.rb', line 246

def available_nrs
  @row.available_nrs & @col.available_nrs & @block.available_nrs
end

#markObject



211
212
213
# File 'lib/rudoku.rb', line 211

def mark
  @marked = true
end

#marked?Boolean

Returns:

  • (Boolean)


215
216
217
# File 'lib/rudoku.rb', line 215

def marked?
  @marked
end

#missing?Boolean

Returns:

  • (Boolean)


242
243
244
# File 'lib/rudoku.rb', line 242

def missing?
  @value.nil?
end

#nr_only_possible_in_this_fieldObject



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/rudoku.rb', line 254

def nr_only_possible_in_this_field
  neighbours = block.missing_fields.reject{|m| m == self}

  available_nrs.each do |nr|
    # iterate over all neighbours and check if theres a Nr (nr)
    # which can't be anywere else
    if neighbours.all?{|n| not n.available_nrs.include?(nr) }
      # okay, nr isn't possible in all the other missing
      # fields in the neighbourhood, that means it can only be
      # on f
      return nr
    end
  end

  nil
end

#remove(v) ⇒ Object



230
231
232
233
234
# File 'lib/rudoku.rb', line 230

def remove(v)
  @block.remove(v)
  @row.remove(v)
  @col.remove(v)
end

#to_sObject



250
251
252
# File 'lib/rudoku.rb', line 250

def to_s
  x.to_s + ":" + y.to_s
end