Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added control.rb
Empty file.
Empty file added model.rb
Empty file.
50 changes: 28 additions & 22 deletions runner.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
require_relative 'sudoku'

# The sudoku puzzles that your program will solve can be found
# in the sudoku_puzzles.txt file.
#
# Currently, Line 18 defines the variable board_string to equal
# the first puzzle (i.e., the first line in the .txt file).
# After your program can solve this first puzzle, edit
# the code below, so that the program tries to solve
# all of the puzzles.
#
# Remember, the file has newline characters at the end of each line,
# so we call String#chomp to remove them.
board1 = "1-58-2----9--764-52--4--819-19--73-6762-83-9-----61-5---76---3-43--2-5-16--3-89--"
board2 = "--5-3--819-285--6-6----4-5---74-283-34976---5--83--49-15--87--2-9----6---26-495-3"
board3 = "29-5----77-----4----4738-129-2--3-648---5--7-5---672--3-9--4--5----8-7---87--51-9"
board4 = "-8--2-----4-5--32--2-3-9-466---9---4---64-5-1134-5-7--36---4--24-723-6-----7--45-"
board5 = "6-873----2-----46-----6482--8---57-19--618--4-31----8-86-2---39-5----1--1--4562--"
board6 = "---6891--8------2915------84-3----5-2----5----9-24-8-1-847--91-5------6--6-41----"
board7 = "-3-5--8-45-42---1---8--9---79-8-61-3-----54---5------78-----7-2---7-46--61-3--5--"
board8 = "-96-4---11---6---45-481-39---795--43-3--8----4-5-23-18-1-63--59-59-7-83---359---7"
board9 = "----754----------8-8-19----3----1-6--------34----6817-2-4---6-39------2-53-2-----"
board10 = "3---------5-7-3--8----28-7-7------43-----------39-41-54--3--8--1---4----968---2--"
board11 = "3-26-9--55--73----------9-----94----------1-9----57-6---85----6--------3-19-82-4-"
board12 = "-2-5----48-5--------48-9-2------5-73-9-----6-25-9------3-6-18--------4-71----4-9-"
board13 = "--7--8------2---6-65--79----7----3-5-83---67-2-1----8----71--38-2---5------4--2--"
board14 = "----------2-65-------18--4--9----6-4-3---57-------------------73------9----------"
board15 = "---------------------------------------------------------------------------------"

board_string = File.readlines('sudoku_puzzles.txt').first.chomp
# board_string1 = File.readlines('sudoku_puzzles.txt').first.chomp

solved_board = Sudoku.new(board_string)
#change the board number to the one you want to test
board = Sudoku.new(board1)

p solved_board.board

p solved_board.get_box

p solved_board.board
#board 1 and 2 get solved recursively.
board.solve

if board.solved?
puts "The board was solved!"
p board.pretty_board
else
p board.board
puts "The board wasn't solved :("
end



# if solved?(solved_board)
# puts "The board was solved!"
# puts pretty_board(solved_board)
# else
# puts "The board wasn't solved :("
# end
131 changes: 102 additions & 29 deletions sudoku.rb
Original file line number Diff line number Diff line change
@@ -1,47 +1,120 @@
require 'pry'

class Sudoku
attr_accessor :board
attr_reader :possibles


STARTING_POINTS = {
[0,0] => {0=>0},
[0,1] => {1=>3},
[0,2] => {2=>6},
[1,0] => {3=>27},
[1,1] => {4=>30},
[1,2] => {5=>33},
[2,0] => {6=>54},
[2,1] => {7=>57},
[2,2] => {8=>60}
}


def initialize(board_string)
@board = get_board(board_string)
@board = board_string
@possibles = '123456789'
end

# Takes a board as a string in the format
# you see in the puzzle file. Returns
# something representing a board after
# your solver has tried to solve it.
# How you represent your board is up to you!
def solve(board_string)
get_board(board_string)
def solve
if solved?
return 'You Did It!'
else
board.split('').each_with_index do |element, idx|
if element == '-'
possible_nums = current_possibles(idx)
return 0 if possible_nums.length == 0
p board
############## The bug is here ###############################
possible_nums.chars.each_with_index do|i, index|
update_board(idx, i)
current = Sudoku.new(board).solve
update_board(idx, "-") if current == 0 && idx != 0
return if current == 'You Did It!'
end
#######################################################################
end
end
end
end

# Returns a boolean indicating whether
# or not the provided board is solved.
# The input board will be in whatever
# form `solve` returns.
def solved?(board)





def solved?
return true unless board.include?('-') || board.split('').reduce(0) {|sum, num| sum + num.to_i} != 405
false
end

def get_board(board_string)
def board_to_array(board_string)
board_string.chars.each_slice(9).to_a
end

def get_cols(index)
board.transpose[index]
def find_super_box_index(value_index)
box_row = value_index / 27
box_col = value_index / 3 % 3
STARTING_POINTS[[box_row,box_col]].values.first
end

def get_box(box_start)
x = box_start
super_box = ''
3.times do
super_box += board[x..x+2]
x += 9
end
super_box
end

def get_cols(idx)
col_num = idx % 9
board_to_array(board).transpose[col_num].join
end

def get_rows(idx)
row_num = idx / 9
board_to_array(board)[row_num].join
end

def get_row(index)
board[index]
def current_possibles(idx)
box = get_box(find_super_box_index(idx))
row = get_rows(idx)
col = get_cols(idx)
delete_these = box + row + col

possibles.delete delete_these.delete("-")
end

def get_box
end
# Takes in a board in some form and
# returns a _String_ that's well formatted
# for output to the screen. No `puts` here!
# The input board will be in whatever
# form `solve` returns.
def pretty_board(board)
end
end
def single_solution?(possible_nums)
possible_nums.length == 1
end


def update_board(idx, possible)
@board[idx] = possible

# @board[idx] = possible_nums[0].to_s
end


def pretty_board
board_array = []
board_array << board_to_array(@board)
board_array.map { |row| row.join(' ') }.join("\n")
end
end







Empty file added view.rb
Empty file.