Advertisement
Guest User

Untitled

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