Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sudoku solver rev 1
# module SudokuSolver
class Gameboard
attr_reader :board, :rows, :columns, :quadrants
def initialize(initial_values)
@board = []
@rows = []
@columns = []
@quadrants = []
generate_cells(initial_values)
generate_rows
generate_columns
generate_quadrants
solver
end
def generate_cells(initial_values)
i = 0
initial_values.each_char do |number|
@board << Cell.new(number.to_i, i)
i += 1
end
end
def generate_rows
board = @board
9.times do
@rows << Container.new(board[0..8])
board = board[9..-1]
end
end
def generate_columns
board = @board
(1..9).each do |x|
holder = []
(0..8).each do |i|
holder << board[i*9 + x - 1]
holder
end
@columns << Container.new(holder)
end
end
def generate_quadrants
board = @board
(0..2).each do |zone_y|
(0..2).each do |zone_x|
holder = []
(0..2).each do |x|
(0..2).each do |y|
holder << board[(27*zone_y + 3*zone_x + 9*x + y)]
holder
end
end
@quadrants << Container.new(holder)
end
end
end
def collect_column_values(cell_column)
@columns[cell_column].subgrid.collect do |cell|
cell.value
end
end
def collect_row_values(cell_row)
@rows[cell_row].subgrid.collect do |cell|
cell.value
end
end
def collect_quadrant_values(quadrant)
@quadrants[quadrant].subgrid.collect do |cell|
cell.value
end
end
def all_collected_values(cell_column, cell_row, cell_quadrant)
collect_column_values(cell_column) | collect_row_values(cell_row) | collect_quadrant_values(cell_quadrant)
end
def update_all_cells
@board.each do |cell|
if cell.value == 0
cell.update_possible_values(all_collected_values(cell.column, cell.row, cell.quadrant))
end
end
end
def solver
all_values = @board.collect {|cell| cell.value}
if all_values.include?(0) == true
update_all_cells
solver
else
print_board
end
end
def print_board
board = @board
9.times do
board[0..8].each {|cell| print cell.value}
puts
board = board[9..-1]
end
# (0..8).each do |row|
# 3.times do |groups|
# puts "#{@board[0*row, 3]} | #{@board[3*row + 3, 3]} | #{@board[7*row + 6, 3]}"
# end
# end
end
end
class Container
attr_accessor :subgrid
def initialize(object)
@subgrid = object
end
end
class Cell
attr_accessor :value
attr_reader :column, :quadrant, :row
def initialize(value, position)
@value = value
@row = position / 9
@column = position % 9
@quadrant = ((position / 27)*3 + ((position/3)%3))
if @value == 0
@possible_values = []
@possible_values = [1,2,3,4,5,6,7,8,9]
end
end
def set_values
if @possible_values.length == 1
@value = @possible_values.pop
end
end
def set_row_value(index)
@row = index
end
def set_column_value(index)
@column = index
end
def set_quadrant_value(index)
@quadrant = index
end
def update_possible_values(all_collected_values)
@possible_values -= all_collected_values # game.all_collected_values(@column, @row, @quadrant)
if @possible_values.length == 1
@value = @possible_values[0]
end
end
end
game = Gameboard.new("619030040270061008000047621486302079000014580031009060005720806320106057160400030")
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment