-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudokuMultiThreaded.rb
More file actions
197 lines (173 loc) · 5.29 KB
/
Copy pathsudokuMultiThreaded.rb
File metadata and controls
197 lines (173 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
60
61
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
100
101
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
134
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#pseudo code
#let each cell keep track of it's self and remove pre filled cells
#have that cell update its own values as we sweep over the board and update them
#randomly slam the board with numbers per cell until it finds the right combination
#
#pull in the multi-threading library
require 'thread'
class Sudoku
def initialize(board_string)
@board_done = false
#clean the board right off the bat
@board_array = parse_board(board_string)
#generate the sizes for index calculations
@board_array_size = @board_array.size
#sudoku boards are perfect squares of themselves
#soo a sub cell is a sqrt of the parent
@sub_cell_size = Math.sqrt(@board_array_size)
end
def parse_board(unparsed_string)
array1 = unparsed_string.scan(/.{1}/).to_a
array1.map! do |val|
typecast_val = val.to_i
#assign the appropriate values to the cell object
#then add it to the board array
typecast_val>0 ? Cell.new(typecast_val, true) : Cell.new(nil, false)
end
array1.each_slice(9).to_a
end
def subgrid_has_value?(row_index, col_index, value)
first_row_index = row_index - (row_index % @sub_cell_size)
first_col_index = col_index - (col_index % @sub_cell_size)
last_row_index = first_row_index + @sub_cell_size - 1
last_col_index = first_col_index + @sub_cell_size - 1
#first_row_index.to_i.upto(last_row_index) do |y|
# first_col_index.to_i.upto(last_col_index) do |x|
# #p "the current search is: #{@board_array[x][y].value}"
# return true if @board_array[x][y].value == value
# end
#end
@board_array[first_row_index..last_row_index].each do |row|
row[first_col_index..last_col_index].each do |cell|
return true if cell.value == value
end
end
false
end
def solve
cell_index, cell = cell_check(@cell_index,"origin")
while !cell.nil?
cell_value = cell.increase
unless cell_value == false
if allowed_num?(cell_index, cell_value)
cell.increase!
#p cell.value
cell_index, cell = cell_check(cell_index,"next")
else
cell.increase!
end
else
cell.clear!
cell_index, cell = cell_check(cell_index,"prev")
end
end
@board_done = true
end
def solved?
@board_done
end
def cell_check(cell_index, method)
case method
when "origin"
#p (@board_array_size*@board_array_size-1)
(0..(@board_array_size*@board_array_size-1)).each do |index|
return index, cell_board_pos(index) if !cell_board_pos(index).prefilled?
end
return nil
when "next" # run though the cell numbers then return a non prefilled cell index
(cell_index+1..(@board_array_size*@board_array_size-1)).each do |index|
return index, cell_board_pos(index) if !cell_board_pos(index).prefilled?
end
return nil, nil
when "prev"
(0..cell_index-1).reverse_each do |index|
return index, cell_board_pos(index) if !cell_board_pos(index).prefilled?
end
return nil, nil
end
end
def cell_board_pos(cell_index)
@board_array[cell_index / @board_array_size.to_i][cell_index % @board_array_size.to_i]
end
#Constraints for brute force method
def allowed_num?(shift, value)
#Create a diagonal search pattern
row_index = shift / @board_array_size.to_i
col_index = shift % @board_array_size.to_i
#Check for value in row
(0..@board_array_size-1).each do |colval|
return false if @board_array[row_index][colval].value == value
end
#Check for value in col
(0..@board_array_size-1).each do |rowval|
return false if @board_array[rowval][col_index].value == value
end
#Check the subgrid
return false if subgrid_has_value?(row_index,col_index,value)
true
end
def board
output = "+---+---+---+---+---+---+---+---+---+\n"
0.upto(@board_array_size-1) do |y|
0.upto(@board_array_size-1) do |x|
output += (x == 0) ? "| #{@board_array[y][x].append_check} |" : " #{@board_array[y][x].append_check} |"
end
output += "\n+---+---+---+---+---+---+---+---+---+\n"
end
output
end
def output
print "\e[H"
print board
$stdout.flush
end
end
class Cell
attr_accessor :value
def initialize(value, prefilled = false)
@value = value; @prefilled = prefilled
end
def increase
blank? ? 1 : (@value == 9 ? false : @value.next)
end
def increase!
@value = self.increase
#p "incriment #{@value}"
end
def clear!
@value = nil
end
def blank?
@value.nil? ? true : false
end
def prefilled?
@prefilled
end
def append_check
blank? ? '-' : @value.to_s
end
end
#clear terminal
print "\e[2J"
print "\e[H"
current_line = 0
compiled_output = ""
File.open('sample.unsolved.txt').each_line do |line|
puts "SOLVING board #{current_line}"
game = Sudoku.new(line)
solver_thread=Thread.new {game.solve}
#multi threading the display system
display_thread=Thread.new do
until game.solved? == true
game.output
end
compiled_output += "\nBOARD: \##{current_line}\n#{game.board}"
game.output
puts "SOLVED board: #{current_line}"
end
#build a new thread
solver_thread.join
display_thread.join
current_line+=1
end
puts compiled_output