Pages

Tuesday, July 23, 2013

SO_DO_KU Solving Program in VB Script

Copy this code into a notepad and save it as .vbs file. Provide input so do ku puzzle in an excel sheel as shown in the screenshot to the program.
When iput file is read, give its path to the program and also the output path where you want to place the output.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Version:0.9 StartHTML:00000107 EndHTML:00038478 EndFragment:00038438 EndFragment:00000000 

sStartTime = Now

Set oXLObj = CreateObject("Excel.Application")

oXLObj.Visible = True

Set oXLWB = oXLObj.Workbooks.Open ("D:\personal\Sudoku\PuzzleFile.xlsx")

Set oXL = oXLWB.Worksheets("Sudoku")

sNumString = "123456789"

For iRow = 1To9

   For iCoulmn = 1To9
      
      IfLen(Trim(oXL.Cells(iRow,iCoulmn))) = 0Then
      
         sFillerString = sNumString
         
         For iSubRow = 1To9
            IfLen(oXL.Cells(iSubRow,iCoulmn)) = 1Then
               sFillerString = Replace(sFillerString, oXL.Cells(iSubRow,iCoulmn),"")
            EndIf         
         Next
         
         For iSubCoulmn = 1To9
            IfLen(oXL.Cells(iRow,iSubCoulmn)) = 1then
               sFillerString = Replace(sFillerString, oXL.Cells(iRow,iSubCoulmn),"")
            EndIf
         Next
         
         oXL.Cells(iRow,iCoulmn) = sFillerString
                  
      EndIf
   
   Next
   
Next

Function FillCells(iFRomRow, iToRow, iFromColumn, iToCoulmn)
   
   For iTempRow = iFRomRowTo iToRow
   
      For iTempCoulmn = iFromColumnTo iToCoulmn
         
         IfLen(oXL.Cells(iTempRow, iTempCoulmn)) > 1Then
            
            For iTemp_Row = iFRomRowTo iToRow
   
               For iTemp_Coulmn = iFromColumnTo iToCoulmn
                  
                  IfLen(oXL.Cells(iTemp_Row, iTemp_Coulmn)) = 1And iTemp_Row <> iTempRowAnd  iTempCoulmn <> iTemp_CoulmnThen
                  
                     oXL.Cells(iTempRow, iTempCoulmn) = Replace(oXL.Cells(iTempRow, iTempCoulmn),oXL.Cells(iTemp_Row, iTemp_Coulmn),"")
                  
                  EndIf
               
               Next
               
            Next
         
         EndIf
      
      Next
      
   Next

EndFunction

Function PuzzleStatus ()

   ExitFlag = "False"
   
   For iRow = 1To9

      For iCoulmn = 1To9
         
         IfLen(Trim(oXL.Cells(iRow,iCoulmn))) <> 1Then
            
            ExitFlag = "True"
            
            ExitFor
         
         EndIf
         
      Next
      
      If ExitFlag = "True"Then
      
         ExitFor
         
      EndIf
      
   Next
   
   If ExitFlag = "True"Then
      
      PuzzleStatus = "NotSolved"
   
   ElseIf ExitFlag = "False"Then
   
      PuzzleStatus = "Solved"
      
   EndIf   

EndFunction


FillCells1, 3, 1, 3
FillCells1, 3, 4, 6
FillCells1, 3, 7, 9

FillCells4, 6, 1, 3
FillCells4, 6, 4, 6
FillCells4, 6, 7, 9

FillCells7, 9, 1, 3
FillCells7, 9, 4, 6
FillCells7, 9, 7, 9


iLoopCount = 1

DoWhile PuzzleStatus = "NotSolved"
   
   For iRow = 1To9
   
      For iCoulmn = 1To9
         
         IfLen(Trim(oXL.Cells(iRow,iCoulmn))) > 1Then
         
            sFillerString = oXL.Cells(iRow,iCoulmn)
            
            For iSubRow = 1To9
               IfLen(oXL.Cells(iSubRow,iCoulmn)) = 1Then
                  sFillerString = Replace(sFillerString, oXL.Cells(iSubRow,iCoulmn),"")
               EndIf         
            Next
            
            For iSubCoulmn = 1To9
               IfLen(oXL.Cells(iRow,iSubCoulmn)) = 1then
                  sFillerString = Replace(sFillerString, oXL.Cells(iRow,iSubCoulmn),"")
               EndIf
            Next
            
            oXL.Cells(iRow,iCoulmn) = sFillerString
            
         EndIf
      
      Next
      
   Next
   
   FillCells1, 3, 1, 3
   FillCells1, 3, 4, 6
   FillCells1, 3, 7, 9
   
   FillCells4, 6, 1, 3
   FillCells4, 6, 4, 6
   FillCells4, 6, 7, 9
   
   FillCells7, 9, 1, 3
   FillCells7, 9, 4, 6
   FillCells7, 9, 7, 9
   
   iLoopCount = 1 + iLoopCount
   
   If iLoopCount = 100Then
   
      ExitDo
      
   EndIf

Loop

sEndTime = Now

MsgBox PuzzleStatus & vbNewLine & sStartTime & vbNewLine & sEndTime

oXLWB.Save

oXLWB.Close

oXLObj.Quit

Set oXLObj = Nothing

No comments: