POV-Ray : Newsgroups : povray.text.scene-files : Sudoku hints Server Time
22 Dec 2024 09:09:59 EST (-0500)
  Sudoku hints (Message 1 to 2 of 2)  
From: Cousin Ricky
Subject: Sudoku hints
Date: 14 Apr 2006 16:10:01
Message: <web.443ffd18ff94e251701d00040@news.povray.org>
No, this code won't solve it for you!  (Where's the fun in that?)  It will
give hints that relieve what i consider the tedious part of the game.

That is figuring out which numbers are still available in a given space.
This isn't difficult, but it's time-consuming grunt work.  sudoku.inc
performs all the tedium and displays the available numbers in each space.
To use it, define the puzzle in Qstate, set up array Astate for the
solution, then #include "sudoku.inc".

The following sample driver shows how to set it up.  Qstate contains an
unsolved puzzle as published by Universal Press Syndicate on April 13.
Astate shows the puzzle partially solved.
______________________________________________________________________

// Sample .pov file for sudoku.inc
//+a +am2 +fn +w480 +h480

// Sudoku puzzle as given:
#declare Qstate = array[9]
{  " 2 8    9",
   "  6  72  ",
   "  86   5 ",
   "3 4    72",
   "    7    ",
   "26    5 1",
   " 1   34  ",
   "  21  9  ",
   "7    5 1 "
}

// Fill this in as you solve the puzzle:
#declare Astate = array[9]
{  "      76 ",
   "         ",
   " 7    1  ",
   "         ",
   "         ",
   "  7      ",
   "   7   2 ",
   "        7",
   "         "
}
#include "sudoku.inc"
______________________________________________________________________

Start with all blank spaces in Astate, then render the puzzle.  Most of the
spaces will display most of the numbers, but you should get some idea of
where to start.  Edit your moves into Astate, as shown above, and
re-render.  The hints will be fewer and less ambiguous.  Repeat until
solved. :-)

Note that there is no error checking!  The intention is to smooth out the
solving of the puzzle, not to do it for you.  If you make an illegal move,
or a legal, but incorrect move, you're on your own.  One sign that you've
made a bad turn somewhere is a space rendered with no hints.

The include file follows.  You promise to use it _only_ on days that you're
feeling particularly lazy, OK? ;-)
______________________________________________________________________

/* sudoku.inc
 *

 * All rights reserved. (Invoke "Fair Use" at your discretion.)
 * Created 12-nov-2005
 * Updated 14-apr-2006
 */
#include "transforms.inc"

//============================== PARAMETERS ================================

#declare zBoard = 1;
#declare zWrit = 0.99;
#declare dSq = 1;
#declare dLine = 0.05;
#declare dPanel = 3 * dSq + 4 * dLine;

#declare c_Chk1 = rgb <0.92, 1.00, 0.98>;
#declare p_Checks = array[2]
{  pigment { rgb 1 },
   pigment { c_Chk1 * 0.9 }
}
#declare p_Line = pigment { rgb 0 }
#declare p_qText = pigment { rgb <0.9, 0, 0.1> }
#declare p_aText = pigment { rgb <0, 0.15, 0.75> }
#declare p_hText = pigment { rgb 0.6 }
#declare t_Frame = texture { pigment { p_Line } }

#declare Qfont = "arialbd.ttf"; //printed font

#switch (2) //use "handwritten" look for (H)int and (A)nswer fonts
   #case (1)
      #declare Afont = "signatur.ttf"; //ACK! This font isn't
      #declare Aoblq = 0;              // on my new PC! :-(
      #declare Hfont = "signatur.ttf";
      #declare Hoblq = 0;
      #break
   #case (2)
      #declare Afont = "mistral.ttf";
      #declare Aoblq = 0.2;
      #declare Hfont = "mistral.ttf";
      #declare Hoblq = 0.2;
      #break
   #else
      #declare Afont = "comic.ttf"; //most PCs should have this
      #declare Aoblq = 0.3;
      #declare Hfont = "comic.ttf";
      #declare Hoblq = 0.3;
#end

//============================ PLAYING FIELD ===============================

background { rgb 0.5 }

#default { finish { ambient 1 diffuse 0 } }

camera
{  orthographic
   location -z
   look_at 0
   up dPanel * 3 * y
   right dPanel * 3 * x
}

#declare Thing = polygon { 5, <1, 1>, <0, 1>, <0, 0>, <1, 0>, <1, 1> }

#macro Panel (BG)
   union
   {  object
      {  Thing scale <dPanel, dPanel, 1> translate zBoard * z
         pigment { BG }
      }
      #local Line = 0;
      #while (Line < 4)
         object
         {  Thing scale <dLine, dPanel, 1>
            translate <(dSq + dLine) * Line, 0, zWrit>
         }
         object
         {  Thing scale <dPanel, dLine, 1>
            translate <0, (dSq + dLine) * Line, zWrit>
         }
         #local Line = Line + 1;
      #end
      translate <-dPanel / 2, -dPanel / 2, 0>
      pigment { p_Line }
   }
#end

#declare iP = -1;
#while (iP <= 1)
   #declare jP = -1;
   #while (jP <= 1)
      object
      {  Panel (p_Checks [mod (iP + jP + 2, 2)])
         translate <jP * dPanel, iP * dPanel>
      }
      #declare jP = jP + 1;
   #end
   #declare iP = iP + 1;
#end

//=============================== WRITING ==================================

#macro Is_writ (Entry)
   (strcmp (Entry, "1") >= 0 & strcmp (Entry, "9") <= 0)
#end

#macro Get_entry (State, Row, Col) substr (State[Row], Col + 1, 1) #end

#macro True_entry (Row, Col)
   #local Entry = Get_entry (Qstate, Row, Col);
   #if (Is_writ (Entry))
      Entry
   #else
      #local Entry = Get_entry (Astate, Row, Col);
      #if (Is_writ (Entry)) Entry #else " " #end
   #end
#end

#macro Position (iPos)
(  iPos * (dSq + dLine)
   #if (iPos > 1) + dLine #else #if (iPos < -1) - dLine #end #end
)
#end

#declare Taken = array[10];
#declare Row = 0;
#while (Row < 9)
   #declare iPanel = floor (Row / 3) * 3;
   #declare Col = 0;
   #while (Col < 9)
      #declare jPanel = floor (Col / 3) * 3;
      #declare Qentry = Get_entry (Qstate, Row, Col);
      #declare Qyes = Is_writ (Qentry);
      #declare Aentry = Get_entry (Astate, Row, Col);
      #declare Ayes = Is_writ (Aentry);
      #if (Qyes | Ayes)
         #declare Entry = text
         {  ttf #if (Qyes) Qfont Qentry 0.01, 0
            #else Afont Aentry 0.01, 0 Shear_Trans (x, <Aoblq, 1, 0>, z)
            #end
         }
         object
         {  Entry translate <Position (Col - 4), Position (4 - Row), zWrit>
             - (max_extent(Entry) + min_extent(Entry)) * <0.5, 0.5, 0>
            #if (Qyes) pigment { p_qText } #else pigment { p_aText } #end
         }
      #else // apply hints
         #declare Value = 0;
         #while (Value <= 9)
            #declare Taken[Value] = no;
            #declare Value = Value + 1;
         #end
         #declare I = 0;
         #while (I < 9)
            #declare Value = val (True_entry (Row, I));
            #if (Value > 0) #declare Taken[Value] = yes; #end
            #declare Value = val (True_entry (I, Col));
            #if (Value > 0) #declare Taken[Value] = yes; #end
            #declare Value = val
               (True_entry (mod (I, 3) + iPanel, floor (I / 3) + jPanel));
            #if (Value > 0) #declare Taken[Value] = yes; #end
            #declare I = I + 1;
         #end
         #declare Value = 1;
         #while (Value <= 9)
            #if (!Taken[Value])
               #declare Hint = text
               {  ttf Hfont str(Value,1,0) 0.01, 0
                  Shear_Trans (x / 3, <Hoblq, 1, 0> / 3, z / 3)
               }
               object
               {  Hint
                  translate
                     <Position (Col - 4)
                       + (mod (Value - 1, 3) - 1) * dSq * 0.3,
                      Position (4 - Row)
                       + (1 - floor ((Value - 1) / 3)) * dSq * 0.31,
                      zWrit>
                   - (max_extent(Hint) + min_extent(Hint)) * <0.5, 0.5, 0>
                  pigment { p_hText }
               }
            #end
            #declare Value = Value + 1;
         #end
      #end
      #declare Col = Col + 1;
   #end
   #declare Row = Row + 1;
#end
______________________________________________________________________


Post a reply to this message

From: PM 2Ring
Subject: Re: Sudoku hints
Date: 11 May 2006 02:55:00
Message: <web.4462df9d9c25acde76ba2c900@news.povray.org>
"Cousin Ricky" <ric### [at] yahoocom> wrote:
> No, this code won't solve it for you!  (Where's the fun in that?)  It will
> give hints that relieve what i consider the tedious part of the game.

I don't play Sudoku (yet), but it's always fun to see people using POV for
tasks that don't have much to do with raytracing. I've written a few
'mathematical' programs with it, like Pi & E calculators, a prime number
generator, and a scene file that renders its source code as output.

At work, I use POV SDL to build VBasic macros, since I don't know VBasic. :)


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.