I love doing crossword puzzles: I’m a huge fan of Merle Reagle (I have all his books www.sundaycrosswords.com) and I love the NY Times Sunday puzzles, both of which come in the Seattle Times. We also love listening to the NPR Sunday Puzzle by Puzzlemaster Will Shortz, from whom we first heard about Sudoku a few weeks ago.

Crossword puzzles require a fairly large amount of prior knowledge to solve. Sudoku requires almost no prior knowledge:  just logic and deduction.

 

My family has been enjoying Sudoko puzzles. In fact, about a month ago, both the newspapers to which we subscribe started carrying daily Sudoku puzzles within a week of each other (USA Today, Seattle Times). If you haven’t tried one give it a whirl. A Sudoku puzzle consists of a 3x3 grid of 3x3 grids of squares, some of which are prefilled with digits. Sudoku rules are simple. Enter digits from 1-9 in blank spaces. Each row, column and 3x3 square must have 1 of each digit. Each Sudoku has a unique solution that can be reached logically without guessing.

 

Before I wrote a program to generate Sudoku puzzles, I needed to learn about them first. There are many web sites that provide Sudoku puzzles. I started by copying them to a word document (using Alt-PrtScrn to capture to the clipboard), resizing them to fit many on a page, then printing them out. That became rather tedious, so I automated the process.

 

Here is some code to capture Sudoku puzzles from a web site and add them to a Word document, 9 to a page. Why 9 to a page? Because Sudoku is all about 3x3<g>.

The code automates Internet Explorer and Word. Just change the URL to a web site that produces Sudoku puzzles and adjust the x,y,width,height coordinates. Be sure to heed any copyright notices.

 

I’ll post code that helps solve a Sudoku puzzle next.

 

*Start IE in upper left,

PUBLIC oie AS internetexplorer.application

oIE = CREATEOBJECT("internetexplorer.application")

oIE.visible=1

oIE.top=0

oIE.left=0

oIE.width=650

oIE.height=650

DECLARE integer Sleep IN WIN32API integer

public oW as Word.Application

oW=CREATEOBJECT("word.application")

ow.WindowState= 0  && wdWindowStateNormal

oW.visible=1

oW.left=650

ow.Top=0

oW.Documents.Add

*!*   oW.ActiveWindow.ActivePane.View.Zoom.Percentage = 75

*!*   oW.ActiveWindow.ActivePane.View.Type= 3  && wdPrintView

    With ow.ActiveDocument.PageSetup as Word.PageSetup

        .TopMargin = 12.25

        .BottomMargin = 12.25

        .LeftMargin = 12.25

        .RightMargin = 12.25

    ENDWITH

*     oW.ActiveDocument.Sections(1).Footers(1).PageNumbers.Add(2)

FOR i = 1 TO 2*9  && # of pages @ 9 per page

      oIE.Navigate2("http://blogs.msdn.com/calvin_hsia") &&Adjust this URL to point to a sudoku web site

      DO WHILE oIE.ReadyState!=4    &&READYSTATE_COMPLETE

            sleep(1000)

      ENDDO

      sleep(200)  && allow enough time for ie to render page

      ScrnScrape()

      IF MOD(i-1,9)=0

            IF i>1

                  oW.Selection.InsertBreak(7)   &&pagebreak

            ENDIF

            oW.Selection.InsertAfter("Sudoku rules are simple. Enter digits from 1-9 in blank spaces. Each row, column and 3x3 square must have 1 of each digit.")

            oW.Selection.InsertAfter("Each Sudoku has a unique solution that can be reached logically without guessing."+CHR(13))

            oW.Selection.InsertAfter(REPLICATE(CHR(13),3))

          oW.Selection.EndKey(6)

      endif

    oW.Selection.InlineShapes.AddPicture("c:\temp.jpg",.f.,.t.)

    oW.Selection.EndKey(6)

    oW.Selection.MoveLeft(1,1,1)

   

    oW.Selection.InlineShapes(1).Width= 190

    oW.Selection.InlineShapes(1).Height= 210

    oW.Selection.EndKey(6)

   

ENDFOR

ow.Left=0   && move word left

ow.Selection.HomeKey(6) && go to top of doc

 

PROCEDURE ScrnScrape()

      CLEAR

      TRY

            cFileName="c:\temp.jpg"

            ERASE (cFileName)

            SET CLASSLIB TO HOME()+"ffc\_gdiplus"

            LOCAL og as gpgraphics

            LOCAL oImage as gpimage

            x=308 && adjust these coordinates

            y=230

            w=310

            h=378

            oImage=GetRegion(0,x,y,w,h)  

            og=CREATEOBJECT("gpgraphics")

            og.CreateFromImage(oImage)

            og.DrawImageScaled(oImage,0,0,100,100)

            oImage.SaveToFile(cFileName,"image/jpeg")

            oImage=CREATEOBJECT("gpimage","c:\temp.jpg")

           

            og.CreateFromHWND(_screen.HWnd)

            og.DrawImageAt(oImage,0,0)

            oImage.SaveToFile(cFileName,"image/jpeg")

      CATCH TO oerr

            ?oerr.message

      ENDTRY

      fDoingScrape=.f.

      RETURN

PROCEDURE GetRegion(hWnd as Integer, nLeft as Integer, nTop as Integer, nWidth as Integer, nHeight as Integer)

      LOCAL hdc, hbSave, hdcCompat, hbm

      DECLARE integer GetDC IN user32 integer hWnd

      DECLARE integer ReleaseDC IN user32 integer hWnd, integer hDC

      DECLARE integer CreateCompatibleDC IN gdi32 integer hDC

      DECLARE integer CreateCompatibleBitmap IN gdi32 integer hDC, integer Width, integer Height

      DECLARE integer SelectObject IN gdi32 integer hdc, integer

      DECLARE integer DeleteObject IN gdi32 integer hdc,

      DECLARE integer BitBlt IN gdi32 integer hdc, integer nXDest, integer nYDest, integer nWidth, integer nHeight, ;

            integer hdcSrc, integer nXSrc, integer nYSrc, integer nRop

      DECLARE INTEGER GdipCreateBitmapFromHBITMAP in gdiplus integer hbm,integer hpal,integer @ bitmap

      DECLARE INTEGER GdipCloneBitmapAreaI in gdiplus integer X,integer Y,integer Width,integer Height,integer enumPixelFormats_format,integer srcBitmap,integer @ dstBitmap

#define SRCCOPY             0x00CC0020 && from wingdi.h /* dest = source                   */

      LOCAL og as gpgraphics,ofont as gpfont,nBitMap, nTemp

      og=CREATEOBJECT("gpgraphics")

      og.CreateFromHWND(_screen.HWnd)

      oImage=CREATEOBJECT("gpimage")

      hdc = GetDC(hWnd)

      hdcCompat = CreateCompatibleDC(hDC)

      hbm = CreateCompatibleBitmap(hDC, nWidth, nHeight)

      hbSave=SelectObject(hdcCompat,hbm)

      BitBlt(hdcCompat,0,0,nWidth,nHeight,hdc, nLeft, nTop, SRCCOPY)

      SelectObject(hdcCompat, hbSave)

      nBitMap=0

      GdipCreateBitmapFromHBITMAP(hbm,0, @nBitMap)

      nTemp=0

      GdipCloneBitmapAreaI(0,0,nWidth, nHeight, 0, nBitMap, @ntemp)

      oImage.SetHandle(nTemp)

     

      DeleteObject(hbm)

      ReleaseDC(hWnd, hdcCompat)

      ReleaseDC(hWnd, hDC)

RETURN oImage