Add to_s to Board
This commit is contained in:
parent
d573aad4f2
commit
c8ee8c311e
4 changed files with 72 additions and 0 deletions
|
@ -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
|
||||
|
|
34
spec/solver_spec.cr
Normal file
34
spec/solver_spec.cr
Normal file
|
@ -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
|
17
src/board.cr
17
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
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
module Sudoku
|
||||
class Solver
|
||||
|
||||
def self.solve(board)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue