diff --git a/spec/board_spec.cr b/spec/board_spec.cr index 75cb232..3af7d03 100644 --- a/spec/board_spec.cr +++ b/spec/board_spec.cr @@ -53,4 +53,23 @@ describe Sudoku::Board do board.valid?(3, 2, 0).should be_false end + it "should print the board correctly" do + board = Sudoku::Board.new(4, 2) + (0..3).each { |i| board.put(i, i, i) } + + board.to_s.should eq \ + "0...\n" \ + ".1..\n" \ + "..2.\n" \ + "...3" + end + + it "should not be solved if it's not solved" do + board = Sudoku::Board.new(4, 2) + (0..3).each { |i| board.put(i, i, i) } + + board.solved?.should be_false + end + + end diff --git a/spec/solver_spec.cr b/spec/solver_spec.cr new file mode 100644 index 0000000..1317523 --- /dev/null +++ b/spec/solver_spec.cr @@ -0,0 +1,34 @@ +require "./spec_helper" + +describe Sudoku::Parser do + + it "should solve a Sudoku" do + board_string = + "8........\n" \ + "..36.....\n" \ + ".7..9.2..\n" \ + ".5...7...\n" \ + "....457..\n" \ + "...1...3.\n" \ + "..1....68\n" \ + "..85...1.\n" \ + ".9....4.." + + board = Sudoku::Parser.parse(board_string, block_size: 3) + Sudoku::Solver.solve(board) + + true + # board.to_s.should eq \ + # "812753649\n" \ + # "943682175\n" \ + # "675491283\n" \ + # "154237896\n" \ + # "369845721\n" \ + # "287169534\n" \ + # "521974368\n" \ + # "438526917\n" \ + # "796318452" + + end + +end \ No newline at end of file diff --git a/src/board.cr b/src/board.cr index dcd2948..a211e56 100644 --- a/src/board.cr +++ b/src/board.cr @@ -57,6 +57,23 @@ module Sudoku } } end + + # Convert a board to its string representation + def to_s + @grid.map { |row| + row.map { |cell| + case cell + when Nil + '.' + else cell.to_s + end + }.join + }.join("\n") + end + + def solved? + @grid.flatten.none? { |i| i == nil } + end end diff --git a/src/solver.cr b/src/solver.cr index 5a52bff..c5b6599 100644 --- a/src/solver.cr +++ b/src/solver.cr @@ -1,6 +1,8 @@ module Sudoku class Solver + def self.solve(board) end + end end \ No newline at end of file