Advertisement
mrlover21

Sudoku Solver From CMD

Dec 29th, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.82 KB | None | 0 0
  1. @echo off
  2.  
  3. ::Entering invalid puzzles will make the program act strangely or crash.
  4. ::It's better to get puzzles from the internet than to try to make them up.
  5.  
  6. ::Sets the title, and enables delayed expansion.
  7.  
  8. SetLocal EnableDelayedExpansion
  9. Title Sudoku Solver
  10.  
  11. echo Welcome to ScrewTheLotOfYou's Batch Sudoku Solver.
  12. echo.
  13. echo.
  14. echo.
  15. echo Loading...
  16.  
  17.  
  18. ::Glossary:
  19.  
  20. :: Space: Each place that number can be placed. 81 of these on a sudoku puzzle.
  21. :: Box: Each 3x3 grid. There are 9 of these.
  22. :: Row: From left to right. 9 squares in each row. 9 rows alogether.
  23. :: Column: From top to bottom. 9 squares in each column. 9 columns altogether.
  24.  
  25. :: X: X is the column number of a given space. Numbers 1-9 from left to right.
  26. :: Y: Y is the row number of a given space. Numbers 1-9 from top to bottom.
  27.  
  28. :: Puzzle (grid): The grid of numbers that have been entered by the user or worked out by the program. These numbers are the final positions of each number.
  29. :: Solution (grid): The grid of numbers that the program has worked out that COULD go in each space with the current Puzzle grid.
  30. :: R (grid): This stands for Row Grid. Contains the possible locations that each number from 1-9 could reside.
  31. :: C (grid): This stands for Column Grid. Contains the possible locations that each number from 1-9 could reside.
  32. :: B (grid): This stands for Box Grid. Contains the possible locations that each number from 1-9 could reside.
  33.  
  34. :: Variables that are part of the Puzzle grid are in the format: PX-Y=Value
  35. :: Variables that are part of the Solution grid are in the format: SX-Y=Possible solutions
  36. :: Variables that are part of the R grid are in the format: RY-Value=Possible X values
  37. :: Variables that are part of the C grid are in the format: CX-Value=Possible Y values
  38. :: Variables that are part of the B grid are in the format: BBoxNumber-Value=Possible XY values
  39. :: The R, C and S variables are a list of single digit numbers, not separated. The B variables are a list of two digit numbers, each separated by a space.
  40.  
  41.  
  42.  
  43.  
  44. ::Sets up the Puzzle Row Grid, a 9x9 square.
  45. ::Sets up the Puzzle Columns, a 9x9 square.
  46. ::Sets up the Column and Row grids.
  47.  
  48. for /L %%I in (1,1,9) do (
  49.  
  50. for /L %%J in (1,1,9) do (
  51. set PuzzleRow%%I=!PuzzleRow%%I!#P%%J-%%I#
  52. set PuzzleColumn%%I=!PuzzleColumn%%I!#P%%I-%%J#
  53.  
  54. for /L %%K in (1,1,9) do (
  55. set R%%I-%%J=!R%%I-%%J!%%K
  56. set C%%I-%%J=!C%%I-%%J!%%K
  57. )
  58. )
  59. set PuzzleDisplayRow%%I=!PuzzleRow%%I:~0,21!][!PuzzleRow%%I:~21,21!][!PuzzleRow%%I:~42,21!
  60. set PuzzleDisplayRow%%I=!PuzzleDisplayRow%%I: =!
  61.  
  62. )
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. ::Sets up all 9 3x3 Puzzle boxes and all 9 3x3 solution boxes.
  70.  
  71. ::Boxes are more complicated because they don't follow a simple rule, e.g. column 2 x=2.
  72. ::This is why I'm setting up solution list boxes.
  73.  
  74. set BoxNumber=0
  75.  
  76.  
  77.  
  78. for /l %%I in (0,1,2) do (
  79. for /l %%J in (0,1,2) do (
  80. set /a StartY=%%I * 3 + 1
  81. set /a EndY=%%I * 3 + 3
  82. set /a StartX=%%J * 3 + 1
  83. set /a EndX=%%J * 3 + 3
  84. call :SetUpBoxes !StartY! !EndY! !StartX! !EndX!
  85. )
  86. )
  87.  
  88.  
  89. goto SetUpAllSolutionSpaces
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. :SetUpBoxes
  97.  
  98. ::%1=StartY %2=EndY %3=StartX %4=EndX
  99.  
  100. ::SpaceCount is set to 1 each time so each solution box can have the format SolutionBox(BoxNumber)-SpaceCount
  101. ::This allows me to cycle through each space in a box as quickly as a row or column.
  102.  
  103. set SpaceCount=1
  104.  
  105. set /a BoxNumber+=1
  106.  
  107. for /l %%I in (%1,1,%2) do (
  108. for /l %%J in (%3,1,%4) do (
  109. set PuzzleBox%BoxNumber%=!PuzzleBox%BoxNumber%!#P%%J-%%I#
  110. set SolutionBox%BoxNumber%-!SpaceCount!=S%%J-%%I
  111.  
  112. for /L %%K in (1,1,9) do (
  113. set B%BoxNumber%-%%K=!B%BoxNumber%-%%K!%%J%%I
  114. )
  115.  
  116. set /a SpaceCount=!SpaceCount!+1
  117. )
  118. )
  119. exit /b
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127. :SetUpAllSolutionSpaces
  128.  
  129. ::Sets up all the solution spaces, so each one starts off as 1-9.
  130.  
  131. for /l %%I in (1,1,9) do (
  132. for /l %%J in (1,1,9) do (
  133. set S%%I-%%J=123456789
  134. set P%%I-%%J=
  135. )
  136. )
  137.  
  138.  
  139.  
  140. ::goto SetupSolutionGrid
  141.  
  142.  
  143.  
  144.  
  145. set P1-1=
  146.  
  147. :BeginEntering
  148.  
  149. set InputCount=0
  150.  
  151. cls
  152.  
  153.  
  154.  
  155.  
  156. ::Asks the user to input each space. Calls :InputEachSpace for the user to input. Calls to display the grid so far. Clears the screen. It does all this 9x9 times.
  157. ::The function RefreshPuzzleDisplay is further down, as it is used now and later.
  158.  
  159. for /L %%I in (1,1,9) do for /L %%J in (1,1,9) do echo Enter the puzzle below.& echo Just press enter if the space is blank.& echo.& set P%%J-%%I=&& Call :RefreshPuzzleDisplay& Call :InputEachSpace %%J %%I& cls
  160. echo.
  161. echo.
  162. echo Processing puzzle...
  163. goto SetUpSolutionGrid
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170. :: This function is called as the user inputs each part of the grid.
  171.  
  172. :InputEachSpace
  173. ::Recieves arguments %1=X and %2=Y
  174. echo.
  175. echo Column:%1 Row:%2
  176. echo.
  177. ::The format is Px-y (P stands for Puzzle. As these variables are part of the puzzle grid, not the solutions grid).
  178. set /p P%1-%2=
  179. set P%1-%2=!P%1-%2:~0,1!
  180. if NOT !P%1-%2! leq 9 set P%1-%2=
  181. if NOT !P%1-%2! geq 1 set P%1-%2=
  182.  
  183. if NOT "!P%1-%2!"==" " set /a InputCount+=1&& set DefinedSpaces!InputCount!=P%1-%2=!P%1-%2!
  184.  
  185. exit /b
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194. :SetUpSolutionGrid
  195.  
  196. :: Adjusts the solutions for each row, column and box containing a space defined by the user.
  197. :: Works out which box the space is in using the formula: BoxNumber= ( (x+2)/3) ) + ( ((y-1)/3)*3 )
  198. :: This works because set /a does not use decimals when dividing, so only the number of whole divisions is returned.
  199. :: For example, set /a 2 / 3 returns 0. set /a 8 / 3 returns 2.
  200.  
  201.  
  202. :: Set FoundSolutions to a single space, as trying to use envirnoment variable substition on a non existent variable causes trouble.
  203.  
  204. set FoundSolutions=
  205.  
  206. ::Sorry about it all being on one line, but the FOR command crashes if I try and put it in brackets on serparate lines. Think it's something to do with the brackets in the set /a command.
  207. ::The function AdjustSolutionGrid is quite far down, as it's used now and later.
  208.  
  209. for /l %%I in (1,1,%InputCount%) do set /a BoxNumber= ( ( (!DefinedSpaces%%I:~1,1! + 2 ) / 3) + ( ( ( !DefinedSpaces%%I:~3,1! - 1 ) / 3 ) * 3 ) )&& call :AdjustSolutionGrid !DefinedSpaces%%I:~1,1! !DefinedSpaces%%I:~3,1! !DefinedSpaces%%I:~5,1! !BoxNumber!
  210.  
  211.  
  212.  
  213.  
  214.  
  215. :BeginSolving
  216. cls
  217. echo Ready to solve puzzle.
  218. echo.
  219. echo.
  220. echo After each number is found the program will pause.
  221. echo Press enter to begin.
  222. Pause>nul
  223. cls
  224. call :RefreshPuzzleDisplay
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243. :ProcessFoundSolutions
  244. ::This is where the FoundSolutions variable is processed.
  245. ::If it is empty, other algorithms are tried.
  246.  
  247.  
  248.  
  249. if "%FoundSolutions: =%"=="" goto CheckForLineBoxRules
  250.  
  251. for /f %%I in ("%FoundSolutions%") do (
  252. set SolutionTemp=%%I
  253. set FoundSolutions=!FoundSolutions:%%I=!
  254. )
  255.  
  256. set /a BoxNumber= ( ( (!SolutionTemp:~0,1! + 2 ) / 3) + ( ( ( !SolutionTemp:~1,1! - 1 ) / 3 ) * 3 ) )
  257.  
  258. call :AdjustSolutionGrid %SolutionTemp:~0,1% %SolutionTemp:~1,1% %SolutionTemp:~2,1% %BoxNumber%
  259.  
  260. set /a InputCount+=1
  261.  
  262. set P%SolutionTemp:~0,1%-%SolutionTemp:~1,1%=%SolutionTemp:~2,1%
  263. cls
  264. call :RefreshPuzzleDisplay
  265. echo.
  266. echo.
  267. echo A %SolutionTemp:~2,1% has been added at (%SolutionTemp:~0,1%,%SolutionTemp:~1,1%)
  268. echo.
  269. pause
  270. goto ProcessFoundSolutions
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279. ::This is the function that displays the puzzle on screen. No arguments are recieved.
  280.  
  281. :RefreshPuzzleDisplay
  282. echo. ^|^|%PuzzleDisplayRow1:#=!%^|^|
  283. echo. ^|^|%PuzzleDisplayRow2:#=!%^|^|
  284. echo. ^|^|%PuzzleDisplayRow3:#=!%^|^|
  285. echo ^|^|-------------^|^|
  286. echo. ^|^|%PuzzleDisplayRow4:#=!%^|^|
  287. echo. ^|^|%PuzzleDisplayRow5:#=!%^|^|
  288. echo. ^|^|%PuzzleDisplayRow6:#=!%^|^|
  289. echo ^|^|-------------^|^|
  290. echo. ^|^|%PuzzleDisplayRow7:#=!%^|^|
  291. echo. ^|^|%PuzzleDisplayRow8:#=!%^|^|
  292. echo. ^|^|%PuzzleDisplayRow9:#=!%^|^|
  293. exit /b
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304. :AdjustSolutionGrid
  305.  
  306.  
  307.  
  308. ::Recieves arguments %1=X, %2=Y, %3=Value and %4=Box Number.
  309.  
  310. ::This Function adjusts the solution grid by removing solutions from the row, column and box.
  311. ::It also takes out possible locations of values in affected boxes and the row, column and box in which the value resides.
  312. ::If this function finds a space that has just one solution, or a number that has just one possible location, it sets FoundSolutions to include its co-ordinates and value, for processing later.
  313.  
  314. set S%1-%2=
  315. set R%2-%3=
  316. set C%1-%3=
  317. set B%4-%3=
  318.  
  319. ::I know a few of the commands here are incredibly complicated and hard to follow. Can't be helped.
  320. ::Once again the box calculation algorithm had to go on the line because of the bracket disagreement.
  321.  
  322. ::No commentary can be typed inside a FOR loop. Sorry.
  323.  
  324. for /l %%I in (1,1,9) do set /a AffectedBox1= ( ( (%1 + 2 ) / 3) + ( ( ( %%I - 1 ) / 3 ) * 3 ) )&& set /a AffectedBox2= ( ( (%%I + 2 ) / 3) + ( ( ( %2 - 1 ) / 3 ) * 3 ) )&& (
  325.  
  326.  
  327. set B%4-%%I=!B%4-%%I:%1%2 =!
  328. set C%1-%%I=!C%1-%%I:%2=!
  329. set R%2-%%I=!R%2-%%I:%1=!
  330.  
  331. call set B!AffectedBox1!-%3=%%B!AffectedBox1!-%3:%1%%I =%%
  332. call set B!AffectedBox2!-%3=%%B!AffectedBox2!-%3:%%I%2 =%%
  333.  
  334.  
  335. set C%%I-%3=!C%%I-%3:%2=!
  336. set R%%I-%3=!R%%I-%3:%1=!
  337. set S%1-%%I=!S%1-%%I:%3=!
  338. set S%%I-%2=!S%%I-%2:%3=!
  339.  
  340.  
  341. call set !SolutionBox%4-%%I!=%%!SolutionBox%4-%%I!:%3=%%
  342.  
  343. call set C!SolutionBox%4-%%I:~1,1!-%3=%%C!SolutionBox%4-%%I:~1,1!-%3:!SolutionBox%4-%%I:~3,1!=%%
  344. call set R!SolutionBox%4-%%I:~3,1!-%3=%%R!SolutionBox%4-%%I:~3,1!-%3:!SolutionBox%4-%%I:~1,1!=%%
  345.  
  346.  
  347. for /f %%J in ("!R%%I-%3!") do (
  348. if "!R%%I-%3:~1!"=="" (
  349. set FoundSolutions=!FoundSolutions:%%J%%I%3=! %%J%%I%3
  350. )
  351. )
  352.  
  353. for /f %%J in ("!C%%I-%3!") do (
  354. if "!C%%I-%3:~1!"=="" (
  355. set FoundSolutions=!FoundSolutions:%%I%%J%3=! %%I%%J%3
  356. )
  357. )
  358.  
  359. for /f %%J in ("!S%1-%%I!") do (
  360. if "!S%1-%%I:~1!"=="" (
  361. set FoundSolutions=!FoundSolutions:%1%%I%%J=! %1%%I%%J
  362. )
  363. )
  364.  
  365. for /f %%J in ("!S%%I-%2!") do (
  366. if "!S%%I-%2:~1!"=="" (
  367. set FoundSolutions=!FoundSolutions:%%I%2%%J=! %%I%2%%J
  368. )
  369. )
  370.  
  371. call set Temp=%%B!AffectedBox1!-%3: =%%
  372.  
  373. if NOT "!Temp!"=="" if "!Temp:~2!"=="" (
  374. call set FoundSolutions=%%FoundSolutions:!Temp!%3=%% !Temp!%3
  375. )
  376.  
  377.  
  378. call set Temp=%%B!AffectedBox2!-%3: =%%
  379.  
  380. if NOT "!Temp!"=="" if "!Temp:~2!"=="" (
  381. call set FoundSolutions=%%FoundSolutions:!Temp!%3=%% !Temp!%3
  382. )
  383.  
  384. )
  385.  
  386. exit /b
  387.  
  388.  
  389.  
  390.  
  391. :CheckForLineBoxRules
  392.  
  393. ::This is an alternate algorithm to just checking for single solutions and algorithms.
  394.  
  395. ::It is based on the idea that if all solutions for one number of one row/column lie in one box, then only solutions on that row/column are possible in that box.
  396. ::The opposite is true, that if all solutions for a certain number in a box line up horisontally or vertically (So they lie on one column/row), only solutions in that box are possible for that row/column.
  397.  
  398.  
  399. for /l %%I in (1,1,9) do (
  400. for /l %%J in (1,1,9) do (
  401. if NOT "!B%%I-%%J: =!"=="" (
  402. if "!B%%I-%%J:~9!"=="" (
  403. if "!B%%I-%%J:~0,1!" equ "!B%%I-%%J:~3,1!" (
  404. if "!B%%I-%%J:~6,1!"=="" (
  405. call :RemoveColumnSolutions %%I !B%%I-%%J:~0,1! %%J
  406. ) else (
  407. if "!B%%I-%%J:~0,1!" equ "!B%%I-%%J:~6,1!" (
  408. call :RemoveColumnSolutions %%I !B%%I-%%J:~0,1! %%J
  409. )
  410. )
  411. ) else (
  412. if "!B%%I-%%J:~1,1!" equ "!B%%I-%%J:~4,1!" (
  413. if "!B%%I-%%J:~7,1!"=="" (
  414. call :RemoveRowSolutions %%I !B%%I-%%J:~1,1! %%J
  415. ) else (
  416. if "!B%%I-%%J:~1,1!" equ "!B%%I-%%J:~7,1!" (
  417. call :RemoveRowSolutions %%I !B%%I-%%J:~1,1! %%J
  418. )
  419. )
  420. )
  421. )
  422. )
  423. )
  424.  
  425.  
  426. if NOT "!C%%I-%%J: =!"=="" (
  427. if "!C%%I-%%J:~3!"=="" (
  428. call :CheckSameBoxCOLUMN %%J %%I !C%%I-%%J:~0,1! !C%%I-%%J:~1,1! !C%%I-%%J:~2,1!
  429. )
  430. )
  431.  
  432. if NOT "!R%%I-%%J: =!"=="" (
  433. if "!R%%I-%%J:~3!"=="" (
  434. call :CheckSameBoxROW %%J %%I !R%%I-%%J:~0,1! !R%%I-%%J:~1,1! !R%%I-%%J:~2,1!
  435. )
  436. )
  437.  
  438. )
  439. )
  440.  
  441.  
  442. goto ReprocessSolutions
  443.  
  444.  
  445.  
  446. :CheckSameBoxCOLUMN
  447. ::Recieves %1=Value %2=X %3=Y1 %4=Y2 %5=Y3
  448.  
  449. if "%4"=="" exit /b
  450.  
  451. set /a AffectedBox1= ( ( (%2 + 2 ) / 3) + ( ( ( %3 - 1 ) / 3 ) * 3 ) )
  452. set /a AffectedBox2= ( ( (%2 + 2 ) / 3) + ( ( ( %4 - 1 ) / 3 ) * 3 ) )
  453. set /a AffectedBox3= ( ( (%2 + 2 ) / 3) + ( ( ( %5 - 1 ) / 3 ) * 3 ) )
  454.  
  455. if NOT "%AffectedBox1%"=="%AffectedBox2%" exit /b
  456.  
  457. if NOT "%5"=="" (
  458. if NOT "%AffectedBox1%"=="%AffectedBox3%" exit /b
  459. )
  460.  
  461. for /l %%I in (1,1,9) do (
  462. if NOT "!SolutionBox%AffectedBox1%-%%I:~1,1!"=="%2" (
  463. call set !SolutionBox%AffectedBox1%-%%I!=%%!SolutionBox%AffectedBox1%-%%I!:%1=%%
  464. )
  465. )
  466.  
  467. set Temp=
  468.  
  469. for /l %%I in (0,3,27) do (
  470.  
  471. if "!B%AffectedBox1%-%1:~%%I,1!"=="" (
  472. set B%AffectedBox1%-%1=!Temp!
  473. exit /b
  474. )
  475.  
  476. if "!B%AffectedBox1%-%1:~%%I,1!"=="%2" (
  477. set Temp=!Temp!!B%AffectedBox1%-%1:~%%I,3!
  478. )
  479. )
  480. exit /b
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488. :CheckSameBoxROW
  489. ::Recieves %1=Value %2=Y %3=X1 %4=X2 %5=X3
  490.  
  491. if "%4"=="" exit /b
  492.  
  493. set /a AffectedBox1= ( ( (%3 + 2 ) / 3) + ( ( ( %2 - 1 ) / 3 ) * 3 ) )
  494. set /a AffectedBox2= ( ( (%4 + 2 ) / 3) + ( ( ( %2 - 1 ) / 3 ) * 3 ) )
  495. set /a AffectedBox3= ( ( (%5 + 2 ) / 3) + ( ( ( %2 - 1 ) / 3 ) * 3 ) )
  496.  
  497. if NOT "%AffectedBox1%"=="%AffectedBox2%" exit /b
  498.  
  499. if NOT "%5"=="" (
  500. if NOT "%AffectedBox1%"=="%AffectedBox3%" exit /b
  501. )
  502.  
  503. for /l %%I in (1,1,9) do (
  504. if NOT "!SolutionBox%AffectedBox1%-%%I:~3,1!"=="%2" (
  505. call set !SolutionBox%AffectedBox1%-%%I!=%%!SolutionBox%AffectedBox1%-%%I!:%1=%%
  506. )
  507. )
  508.  
  509. set Temp=
  510.  
  511. for /l %%I in (0,3,27) do (
  512.  
  513. if "!B%AffectedBox1%-%1:~%%I,1!"=="" (
  514. set B%AffectedBox1%-%1=!Temp!
  515. exit /b
  516. )
  517.  
  518. if "!B%AffectedBox1%-%1:~%%I,2!"=="!B%AffectedBox1%-%1:~%%I,1!%2" (
  519. set Temp=!Temp!!B%AffectedBox1%-%1:~%%I,3!
  520. )
  521. )
  522. exit /b
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529. :RemoveColumnSolutions
  530. ::Recieves %1=Box Number %2=X %3=Value
  531.  
  532. for /l %%I in (1,1,9) do set /a AffectedBox= ( ( (%2 + 2 ) / 3) + ( ( ( %%I - 1 ) / 3 ) * 3 ) )&& (
  533. if NOT !AffectedBox!==%1 (
  534. set C%2-%3=!C%2-%3:%%I=!
  535. call set B!AffectedBox!-%3=%%B!AffectedBox!-%3:%2%%I =%%
  536. set S%2-%%I=!S%2-%%I:%3=!
  537. )
  538. )
  539. exit /b
  540.  
  541.  
  542. :RemoveRowSolutions
  543. ::Recieves %1=Box Number %2=Y %3=Value
  544.  
  545. for /l %%I in (1,1,9) do set /a AffectedBox= ( ( (%%I + 2 ) / 3) + ( ( ( %2 - 1 ) / 3 ) * 3 ) )&& (
  546. if NOT !AffectedBox!==%1 (
  547. set R%2-%3=!R%2-%3:%%I=!
  548. call set B!AffectedBox!-%3=%%B!AffectedBox!-%3:%%I%2 =%%
  549. set S%%I-%2=!S%%I-%2:%3=!
  550. )
  551. )
  552. exit /b
  553.  
  554.  
  555.  
  556. :ReprocessSolutions
  557.  
  558. ::Here, all the solutions, boxes, rows and columns are checked for single solutions/locations. This is because the Line-Box rule may have created single solutions.
  559.  
  560.  
  561. for /l %%I in (1,1,9) do (
  562. for /l %%J in (1,1,9) do (
  563.  
  564. for /f %%K in ("!R%%I-%%J!") do (
  565. if "!R%%I-%%J:~1!"=="" set FoundSolutions=!FoundSolutions:%%K%%I%%J=! %%K%%I%%J
  566. )
  567.  
  568.  
  569. for /f %%K in ("!B%%I-%%J!") do (
  570. if "!B%%I-%%J:~3!"=="" set FoundSolutions=!FoundSolutions:%%K%%J=! %%K%%J
  571. )
  572.  
  573.  
  574. for /f %%K in ("!C%%I-%%J!") do (
  575. if "!C%%I-%%J:~1!"=="" set FoundSolutions=!FoundSolutions:%%I%%K%%J=! %%I%%K%%J
  576. )
  577.  
  578.  
  579. for /f %%K in ("!S%%J-%%I!") do (
  580. if "!S%%J-%%I:~1!"=="" set FoundSolutions=!FoundSolutions:%%J%%I%%K=! %%J%%I%%K
  581. )
  582.  
  583.  
  584. )
  585. )
  586.  
  587.  
  588. if NOT "%FoundSolutions: =%"=="" goto ProcessFoundSolutions
  589.  
  590.  
  591.  
  592. :EndSolving
  593.  
  594. ::The code only reaches this point if the code cannot solve the puzzle.
  595.  
  596.  
  597. ::If all 81 numbers are filled then the puzzle has been solved.
  598.  
  599. if %InputCount%==81 (
  600. echo Puzzle Solved^!
  601. ) ELSE (
  602. echo Sorry, your puzzle could not be solved.
  603. echo It may have been entered incorrectly, or it is too difficult for this program.
  604. )
  605.  
  606.  
  607.  
  608. :RequestStartAgain
  609.  
  610. ::Asks if the user wants to enter another puzzle.
  611.  
  612. echo Would you like to start again? [Y/N]
  613.  
  614. set /p Response=
  615. if /i "%Response:~0,1%"=="Y" goto ClearPreviousPuzzle
  616. if /i "%Response:~0,1%"=="N" exit /b
  617. goto RequestStartAgain
  618.  
  619.  
  620.  
  621. :ClearPreviousPuzzle
  622.  
  623. ::Clears all variables that may interfere with the next puzzle entered.
  624.  
  625. for /l %%I in (1,1,9) do (
  626. for /l %%J in (1,1,9) do (
  627. set S%%J-%%I=
  628. set P%%J-%%I=
  629. set R%%J-%%I=123456789
  630. set C%%J-%%I=123456789
  631. set B%%J-%%I=123456789
  632. set InputCount=0
  633. )
  634. )
  635.  
  636.  
  637. goto SetUpAllSolutionSpaces
  638.  
  639. Save As .cmd or .bat
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement