Anonymous avatar Anonymous committed 65eac00

Added the beginning of the Hitori solver.

Comments (0)

Files changed (3)

hitori-solver-in-ruby/Makefile

+all:
+
+test: all
+	spec --color t/*.rb

hitori-solver-in-ruby/hitori-solver.rb

+class HitoriSolver
+
+    class Cell
+        $UNKNOWN = 0
+        $WHITE = 1
+        $BLACK = 2
+
+        attr_reader :state
+        attr_reader :value
+
+        def initialize(val)
+            @state = $UNKNOWN
+            @value = val
+        end
+
+        def mark_as_white()
+            @state = $WHITE
+        end
+
+        def mark_as_black()
+            @state = $BLACK
+        end
+
+
+    end
+
+    class Board
+        def initialize(height, width, contents) 
+            @x_len = width
+            @y_len = height
+            board = []
+            for s_row in contents do
+                d_row = []
+                for v in s_row do
+                    d_row << Cell.new(v)
+                end
+                if (d_row.length() != @x_len)
+                    raise "d_row in pos #{board.length()} has the wrong length"
+                end
+                board << d_row
+            end
+            if (board.length() != @y_len)
+                raise "There aren't #{@y_len} rows";
+            end
+            @cells = board
+        end
+        def cell_yx(y,x)
+            return @cells[y][x]
+        end
+    end
+end

hitori-solver-in-ruby/t/parse-board.rb

+require "hitori-solver.rb"
+
+class Object
+    def ok()
+        self.should == true
+    end
+    def not_ok()
+        self.should == false
+    end
+end
+
+describe "construct_board" do
+    it "board No. 1 should" do
+        # http://www.menneske.no/hitori/5x5/eng/showpuzzle.html?number=1
+        contents = [
+            [2,1,3,2,4],
+            [4,5,3,2,2],
+            [3,4,2,5,1],
+            [1,4,3,3,2],
+            [2,5,1,4,3]
+        ]
+        board = HitoriSolver::Board.new(5, 5, contents)
+        board.cell_yx(0,0).value.should == 2
+        board.cell_yx(0,1).value.should == 1
+        board.cell_yx(0,2).value.should == 3
+    end
+end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.