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 3×3 grid of 3×3 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 3×3 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 3×3<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”)
DECLARE integer Sleep IN WIN32API integer
public oW as Word.Application
ow.WindowState= 0 && wdWindowStateNormal
*!* 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
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(200) && allow enough time for ie to render page
oW.Selection.InsertAfter(“Sudoku rules are simple. Enter digits from 1-9 in blank spaces. Each row, column and 3×3 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.Left=0 && move word left
ow.Selection.HomeKey(6) && go to top of doc
SET CLASSLIB TO HOME()+”ffc\_gdiplus”
LOCAL og as gpgraphics
LOCAL oImage as gpimage
x=308 && adjust these coordinates
CATCH TO oerr
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
hdc = GetDC(hWnd)
hdcCompat = CreateCompatibleDC(hDC)
hbm = CreateCompatibleBitmap(hDC, nWidth, nHeight)
BitBlt(hdcCompat,0,0,nWidth,nHeight,hdc, nLeft, nTop, SRCCOPY)
GdipCloneBitmapAreaI(0,0,nWidth, nHeight, 0, nBitMap, @ntemp)