Jump to content

tiny soduku solver no gui


aart
 Share

Recommended Posts

And your question is?  Remember this is a help forum!

BTW, your code solves only the easiest problems and needs more than "some adjustments" to correctly solve the hardest grids.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

@aart Maybe you would like to try this one ?

#include <GDIPlus.au3>
#include <Constants.au3>
#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <FontConstants.au3>
#include <WinAPISysWin.au3>
#include <Array.au3>

Const $SIZE = 9, $REGION = Sqrt($SIZE)
Const $LENGTH = $SIZE * $SIZE

Global $aGrid[$SIZE][$SIZE]
Global $aFinalGrid, $SolutionFound = False

ShowGrid($aGrid)

Func ShowGrid(ByRef $aGrid)

  Local $hGUI = GUICreate("Sudoku", 470, 510)
  GUISetBkColor(0xFFFFFF)
  GUISetFont(18, $FW_SEMIBOLD)
  GUISetState(@SW_SHOW)

  ; Draw line
  _GDIPlus_Startup()
  Global $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
  Global $hBitmap = _GDIPlus_BitmapCreateFromGraphics(470, 510, $hGraphic)
  Local $hGfxCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap)

  Local $hPen = _GDIPlus_PenCreate(0xFF000000, 3)
  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 10, 10, 460, 10, $hPen)
  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 10, 10, 10, 460, $hPen)
  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 460, 10, 460, 460, $hPen)
  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 10, 460, 460, 460, $hPen)

  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 160, 10, 160, 460, $hPen)
  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 310, 10, 310, 460, $hPen)
  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 10, 160, 460, 160, $hPen)
  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 10, 310, 460, 310, $hPen)

  _GDIPlus_PenDispose($hPen)
  $hPen = _GDIPlus_PenCreate()

  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 60, 10, 60, 460, $hPen)
  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 110, 10, 110, 460, $hPen)
  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 210, 10, 210, 460, $hPen)
  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 260, 10, 260, 460, $hPen)
  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 360, 10, 360, 460, $hPen)
  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 410, 10, 410, 460, $hPen)

  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 10, 60, 460, 60, $hPen)
  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 10, 110, 460, 110, $hPen)
  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 10, 210, 460, 210, $hPen)
  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 10, 260, 460, 260, $hPen)
  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 10, 360, 460, 360, $hPen)
  _GDIPlus_GraphicsDrawLine($hGfxCtxt, 10, 410, 460, 410, $hPen)

  Local $idInput[$SIZE][$SIZE]

  GUIRegisterMsg($WM_COMMAND, "WM_Command")

  For $line = 0 To $SIZE - 1
    For $col = 0 To $SIZE - 1
      $idInput[$line][$col] = GUICtrlCreateInput("", 23 + ($col * 50), 23 + ($line * 50), 30, 30, $ES_CENTER + $ES_NUMBER, $WS_EX_TRANSPARENT)
    Next
  Next

  Local $idButton = GUICtrlCreateButton ("Solve", 200, 475, 70, 25, $BS_CENTER+$BS_VCENTER)
  GUICtrlSetFont (-1, 10, $FW_NORMAL)
  _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, 470, 510)

  Local $nMsg

  While True
    $nMsg = GUIGetMsg()
    Switch $nMsg
      Case $GUI_EVENT_CLOSE
        ExitLoop
      Case $idButton
        For $line = 0 To $SIZE - 1
          For $col = 0 To $SIZE - 1
            $aGrid[$line][$col] = GUICtrlRead($idInput[$line][$col])
          Next
        Next
        ;_ArrayDisplay($aGrid)
        Solution($aGrid, 1)
        ;_ArrayDisplay($aFinalGrid)
        For $line = 0 To $SIZE - 1
          For $col = 0 To $SIZE - 1
            If Not GUICtrlRead($idInput[$line][$col]) Then
              GUICtrlSetData ($idInput[$line][$col], $aFinalGrid[$line][$col])
              GUICtrlSetColor ($idInput[$line][$col], 0xFF0000)
            EndIf
          Next
        Next
    EndSwitch
  WEnd

  _GDIPlus_GraphicsDispose($hGfxCtxt)
  _GDIPlus_BitmapDispose($hBitmap)
  _GDIPlus_PenDispose($hPen)
  _GDIPlus_GraphicsDispose($hGraphic)
  _GDIPlus_Shutdown()
  GUIDelete()

EndFunc   ;==>ShowGrid

Func WM_Command($hWnd, $iMsg, $wParam, $lParam)
  _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, 470, 510)
EndFunc   ;==>WM_Command

Func Solution($aTemp, $iLoc)

  If $SolutionFound Then Return
  If $iLoc > $LENGTH Then
    $SolutionFound = True
    $aFinalGrid = $aTemp
    Return
  EndIf

  Local $iLine = Int(($iLoc - 1) / $SIZE)
  Local $iCol = Mod($iLoc - 1, $SIZE)

  If $aTemp[$iLine][$iCol] Then
    Solution($aTemp, $iLoc + 1)
  Else
    For $i = 1 To $SIZE
      If Possible($aTemp, $iLine, $iCol, $i) Then
        $aTemp[$iLine][$iCol] = $i
        Solution($aTemp, $iLoc + 1)
      EndIf
    Next
  EndIf

EndFunc   ;==>Solution

Func Possible(ByRef $aGrid, $iLine, $iCol, $iValue)
  For $i = 0 To $SIZE - 1
    If $aGrid[$iLine][$i] = $iValue Then Return False
    If $aGrid[$i][$iCol] = $iValue Then Return False
  Next
  Local $iRegLine = Int($iLine / $REGION) * $REGION
  Local $iRegCol = Int($iCol / $REGION) * $REGION
  For $line = $iRegLine To $iRegLine + $REGION - 1
    For $col = $iRegCol To $iRegCol + $REGION - 1
      If $aGrid[$line][$col] = $iValue Then Return False
    Next
  Next
  Return True
EndFunc   ;==>Possible

It is working on all levels of difficulties of SudoKu :)

Link to comment
Share on other sites

Seems it needs manual input of the puzzle value.

Also try this all-SQL implementation: Sudokus.au3

It will need a copy of the sqlite3.dll library.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

hi nine

 

what you build is a brutal force quite nice and fast

that is a problem

there may be more possibilities it would be possible to detect by further investigation

if you take the gui out of it then it stay's very smal

sotest.au3

Edited by aart
Link to comment
Share on other sites

I scripted mine to screen scrape a sudoku site and automatically solve it:

edit: this is the first iteration of that script that will constantly solve sudoku puzzles, nvermind.

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

my coal is what i think is that i think is like some calling it "havy" or "insane"

the issiu there is some time's more out come from same puzzle by more Possible's so you can hide some ways from the "real out com"

you may be can make some way to "endcrypt" it from brutal force by missing numbers what can be twisted

so turn the sodukutabel and read it backwards and some "insane" or "havy" have a differend outcome

 

Link to comment
Share on other sites

I really do not understand what you are talking about.  Maybe use a translator to express your concerns.  Anyway, who do you think won: Deep Blue or Kasparov ?   This goes against Artificial Intelligence that tries to mimic human thinking.   Do not do invest in that if you can avoid it.  Human are good on some aspects but they are really bad at crunching millions of numbers.

Link to comment
Share on other sites

@aart I also have hard time understanding what you wrote.

If you mean that you want to find multiple solutions to a given sudoku puzzle, this is quite possible as well.  A even simpler variant of the SQL implementation I posted can do that.  Please note that sudokus should have only one unique solution.

Also please remark that the SQL implementation doesn't rely on brute force!

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...