Class: Rudoku::Board

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board) ⇒ Board

Returns a new instance of Board.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
# File 'lib/rudoku.rb', line 13

def initialize(board)
  @fields = []
  @board  = []
  @rows   = []
  @cols   = []
  @blocks = []
  @missing = []
  @stats = {
    :missing => 0,
    :solved_by_only_one_possible => 0,
    :solved_by_no_other_possible => 0,
    :solved_by_presolve => 0
  }

  # initialize rows, columns & blocks
  0.upto(8) do |i|
    @rows[i]   = Row.new(self, i)
    @cols[i]   = Col.new(self, i)
    @blocks[i] = Block.new(self, i)
  end

  i = 0
  board.each_with_index do |row, y|
    @board[y] = []

    row.each_with_index do |value, x|
      f = Field.new(self, i, x, y)
      f.value = value

      @board[y][x] = f
      @fields << f
      @missing << f if f.missing?
      i += 1
    end
  end

  # initialize the helper constructs
  @blocks.each do |block|
    block.initialize_area()
  end

  @stats[:missing] = @missing.length

  @counter = 0

  raise "Invalid field" if not valid_field?
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



11
12
13
# File 'lib/rudoku.rb', line 11

def blocks
  @blocks
end

#colsObject (readonly)

Returns the value of attribute cols.



11
12
13
# File 'lib/rudoku.rb', line 11

def cols
  @cols
end

#fieldsObject (readonly)

Returns the value of attribute fields.



11
12
13
# File 'lib/rudoku.rb', line 11

def fields
  @fields
end

#rowsObject (readonly)

Returns the value of attribute rows.



11
12
13
# File 'lib/rudoku.rb', line 11

def rows
  @rows
end

Instance Method Details

#find_first_missingObject



191
192
193
# File 'lib/rudoku.rb', line 191

def find_first_missing
  @fields.find{|f| f.missing?}
end

#get(x, y) ⇒ Object



174
175
176
# File 'lib/rudoku.rb', line 174

def get(x, y)
  @board[y][x]
end

#get_block(x, y) ⇒ Object



186
187
188
189
# File 'lib/rudoku.rb', line 186

def get_block(x, y)
  block_nr = x / 3 + y/3*3
  @blocks[block_nr] or raise "Unitialized block at #{x}/#{y} block_nr #{block_nr}"
end

#get_col(x) ⇒ Object



182
183
184
# File 'lib/rudoku.rb', line 182

def get_col(x)
  @cols[x]
end

#get_row(y) ⇒ Object



178
179
180
# File 'lib/rudoku.rb', line 178

def get_row(y)
  @rows[y]
end

#pre_solveObject



62
63
64
65
66
67
68
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
# File 'lib/rudoku.rb', line 62

def pre_solve
  begin
    #puts "try"
    changed = @missing.reject! do |f|
      # create a list of all fields which are wmissing
      # in the same block, not including t the current field f
      nrs = f.available_nrs


      #puts "#{f} hat #{nrs.length} möglichkeiten: #{nrs.join(" ")}" if nrs.length == 2
      #f.mark if nrs.length ==2
      if nrs.length == 1
        @stats[:solved_by_only_one_possible] += 1
        #f.mark
        f.value = nrs.first
        true
      else
        # use the scanning method for possible other solutions
        # returns false or true

        if v = f.nr_only_possible_in_this_field
          # f.mark
          @stats[:solved_by_no_other_possible] += 1
          f.value = v
          #f.mark
          #return
          true
        else
          false
        end
      end
    end
  end while changed

  @stats[:solved_by_presolve] =
  @stats[:solved_by_no_other_possible] +
  @stats[:solved_by_only_one_possible]
end


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rudoku.rb', line 135

def print_field(p = [])
  p = [p] unless p.kind_of?(Array)

  @board.each_with_index do |row, y|
    #row.map{|f| f.value}.map{|f| f || "_"}.each_with_index do |f, x|
    row.each_with_index do |f, x|
      t = ( f.value ? f.value.to_s : "_" ) + ( f.marked? ? "<" : " " )

      print "#{t} "

      #if p.any?{|o| x == o.x && y == o.y}
      #  print "#{f}<"
      #else
      #  print "#{f} "
      #end
      print " " if x % 3 == 2
    end
    puts
    puts if y % 3 == 2
  end

  #puts "Lösung"
  #0.upto(8) do |i|
  # print "#{@board[i][i].value} "
  #end
 #puts

  #puts
  puts "Stats"
  @stats.each do |key, value|
    puts "#{key}: #{value}"
  end

end

#solve(level = 0) ⇒ Object

returns false if there’s no possible next move, else return the valid path



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rudoku.rb', line 102

def solve(level = 0)
  fm = @missing.shift

  if fm.nil?
    return true
  end

  available_nrs = fm.available_nrs

  unless available_nrs.empty?
    # try all the available nrs on this field
    available_nrs.each do |nr|

      raise "nr is nil??" if nr.nil?
      # set it
      fm.value = nr

      # and try to fill the next field
      result = solve(level + 1)

      # return true if we found a solution
      return true if result
    end
  end

  # seems like we are in a 'sackgasse', reset the value and
  # go back to the next stepp
  fm.value = nil
  @missing.unshift(fm)

  return false
end

#valid_field?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/rudoku.rb', line 170

def valid_field?
  true
end