Commits

Anonymous committed a7b80b8

Added the beginning of the Kakuro board parser

Comments (0)

Files changed (2)

solver/ruby/kakuro-board.rb

+module Kakuro
+
+    class Cell
+
+        attr_reader :id
+
+        def initialize(id, content)
+            @id = id
+            @is_solid = (content =~ /\\/)
+        end
+
+        def solid?
+            return @is_solid
+        end
+
+    end
+
+    class Board
+
+        def initialize()
+            @next_cell_id = 0
+            @cells = Array.new
+
+            @matrix = Array.new
+            @height = nil
+            @width = nil
+        end
+
+        def parse(board_string)
+            board_string.split(/\n+/).each do |line|
+                _parse_line(line)
+            end
+        end
+
+        def _next_cell_id()
+            ret = @next_cell_id
+            @next_cell_id += 1
+            return ret
+        end
+
+        def _parse_line(line)
+            # TODO : Make sure the widths of all the lines are the same.
+            width = 0
+            row = []
+            while line.sub!(/\A\s*\[([^\]]+)\]\s*/, "")
+                content = $1
+
+                cell = Cell.new(_next_cell_id(), content)
+
+                @cells << cell
+
+                width += 1
+
+                row << cell.id
+            end
+            @width = width
+            @matrix << row
+        end
+
+        def cell_yx(row,col)
+            return @cells[@matrix[row][col]];
+        end
+
+    end
+
+end

solver/ruby/t/parse-board.rb

+require "kakuro-board.rb"
+
+describe "Parse 1" do
+    before do
+        @board = Kakuro::Board.new
+        @board.parse(<<'EOF')
+[\] [\] [29\] [\34\] [\] [21\] [8\] [\] [\]
+[\] [10\17] [] [] [3\3] [] [] [\] [\]
+[\30] [] [5] [] [] [2] [] [3\] [11\]
+[\16] [] [] [6] [] [3] [12\11] [] []
+[\] [4\5] [] [] [13\10] [] [] [] []
+[\34] [] [7] [] [4] [] [] [11\] [\]
+[\4] [] [] [3\12] [] [] [1] [] [\] 
+[\] [\] [\6] [] [] [\11] [] [] [\]
+[\] [\] [\3] [] [] [\] [\] [\] [\]
+EOF
+    end
+
+    it "cell(0,0) is block" do
+        @board.cell_yx(0,0).solid?.should
+    end
+end
+
+
+