By the way, to push myself to use Oz, I decided to teach an AI course with it. My plan is to work our way through the book and use Oz computational capabilities to write one or more Sudoku solving programs.

Some time ago, I have created a simple Sudoku script for my co-workers to show them Oz basics. It's just a straightforward implementation (and maybe a bit clumsy). Just run this in the OPI. Maybe change the last statement to "{ExploreAll {Script ......}}".

Cheers,
Filip


local
   fun {Script Spec}
      proc {$ Root}
         Root={Map {List.make 9}
               fun {$ Row}
                  Row={List.make 9}
                  Row ::: 1#9
                  %% All numbers in each row are distinct
                  {FD.distinct Row}
                  Row
               end
              }
         %% All numbers in each column are distinct
         for X in 1;X=<9;X+1 do
            {FD.distinct
             {FoldR Root fun {$ Row Prev} {Nth Row X}|Prev end nil}
            }
         end
         %% All numbers in each 3x3 square are distinct
         for X in 0;X=<2;X+1 do
            for Y in 0;Y=<2;Y+1 do
               {FD.distinct
                {FoldR
                 {List.filterInd Root fun {$ I Row} I>=1+Y*3 andthen I=<3+Y*3 
end}
                 fun {$ Row Prev}
                    {Append
                     {List.filterInd Row fun {$ I X} I>=1+Y*3 andthen I=<3+Y*3 
end}
                     Prev
                    }
                 end
                 nil
                }
               }
            end
         end
         %% Process specs
         {List.forAllInd Spec
          proc {$ Y R}
             Row={Nth Root Y}
          in
             {List.forAllInd R
              proc {$ X I}
                 if {Not {IsFree I}} then
                    {Nth Row X} :: I
                 end
              end
             }
          end
         }
         %% Search...
         {FD.distribute ff {Flatten Root}}
      end
   end
in
   {ForAll {Nth
            {SearchOne {Script [
                                [_ 9 _ 3 _ _ _ 1 _]
                                [2 _ 3 _ _ 1 _ _ _]
                                [_ _ _ _ 7 5 _ 8 _]
                                [_ _ 2 _ _ _ _ 5 6]
                                [_ _ _ 7 _ 8 _ _ _]
                                [7 1 _ _ _ _ 2 _ _]
                                [_ 2 _ 4 9 _ _ _ _]
                                [_ _ _ 8 _ _ 5 _ 1]
                                [_ 7 _ _ _ 2 _ 9 _]
                               ]
                       }
            }
            1
           }
    Show
   }
end
_________________________________________________________________________________
mozart-users mailing list                               
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users

Reply via email to