October 17, 2021, 11:30:45

Author Topic: [bb] Sudoku solver by bytecode77 [ 1+ years ago ]  (Read 567 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Sudoku solver by bytecode77 [ 1+ years ago ]
« on: June 29, 2017, 00:28:39 »
Title : Sudoku solver
Author : bytecode77
Posted : 1+ years ago

Description : This is a sudoku solver. I've written it in ~45 minutes and now here it is. have fun :)

replace
Code: [Select]
Select 1with
Code: [Select]
Select 2to see slow motion!

Please use this level file:
Level1.txt:
Code: [Select]
53..7....
6..195...
.98....6.
8...6...3
4..8.3..1
7...2...6
.6....28.
...419..5
....8..79


Code :
Code: BlitzBasic
  1. Graphics 900, 900, 32, 2
  2. SetBuffer BackBuffer()
  3. AppTitle "Sudoku solver"
  4. SetFont LoadFont("Arial Black", 90, True)
  5.  
  6. Dim Grid(9, 9), Pos(9, 9, 9)
  7. LoadLevel("Level1.txt")
  8.  
  9. Select 1 ;<========== set to '2' to see slow motion!
  10.         Case 1
  11.                 ms = MilliSecs()
  12.                 Repeat
  13.                         UpdatePos()
  14.                         ApplyPos()
  15.                 Until IsSolved()
  16.                 ms = MilliSecs() - ms
  17.                 AppTitle "SOLVED in " + ms + " ms."
  18.                 DrawLevel()
  19.         Case 2
  20.                 Repeat
  21.                         Cls
  22.                         DrawLevel()
  23.                         UpdatePos()
  24.                         ApplyPos()
  25.                         Delay 300
  26.                 Until IsSolved()
  27.                 AppTitle "SOLVED."
  28.                 DrawLevel()
  29. End Select
  30.  
  31. WaitKey()
  32. End
  33.  
  34. Function LoadLevel(path$)
  35. file = ReadFile(path$)
  36. For y = 1 To 9
  37.         l$ = ReadLine(file)
  38.         For x = 1 To 9
  39.                 Grid(x, y) = Mid(l$, x, 1)
  40.                 For i = 1 To 9
  41.                         Pos(x, y, i) = True
  42.                 Next
  43.         Next
  44. Next
  45. CloseFile file
  46. End Function
  47.  
  48. Function DrawLevel()
  49. Cls
  50. Color 255, 255, 255
  51. For x = 1 To 9
  52.         For y = 1 To 9
  53.                 ch$ = Grid(x, y)
  54.                 If ch$ = 0 Then ch$ = " "
  55.                 Text x * 90 - 30, y * 90 - 30, ch$
  56.         Next
  57. Next
  58. Color 127, 127, 127
  59. For x = 0 To 2
  60.         For y = 0 To 2
  61.                 Rect 40 + x * 270, 55 + y * 270, 271, 271, False
  62.         Next
  63. Next
  64. Flip 0
  65. End Function
  66.  
  67. Function UpdatePos()
  68. ;Rows
  69. For y = 1 To 9
  70.         For x = 1 To 9
  71.                 For i = 1 To 9
  72.                         If Grid(i, y) Then
  73.                                 Pos(x, y, Grid(i, y)) = False
  74.                         EndIf
  75.                 Next
  76.         Next
  77. Next
  78. ;Cols
  79. For x = 1 To 9
  80.         For y = 1 To 9
  81.                 For i = 1 To 9
  82.                         If Grid(x, i) Then
  83.                                 Pos(x, y, Grid(x, i)) = False
  84.                         EndIf
  85.                 Next
  86.         Next
  87. Next
  88. ;Fields
  89. For y = 1 To 9
  90.         Select y
  91.                 Case 1, 2, 3: fy = 1
  92.                 Case 4, 5, 6: fy = 4
  93.                 Case 7, 8, 9: fy = 7
  94.         End Select
  95.         For x = 1 To 9
  96.                 Select x
  97.                         Case 1, 2, 3: fx = 1
  98.                         Case 4, 5, 6: fx = 4
  99.                         Case 7, 8, 9: fx = 7
  100.                 End Select
  101.                 For i = fx To fx + 2
  102.                         For a = fy To fy + 2
  103.                                 If Grid(i, a) Then
  104.                                         Pos(x, y, Grid(i, a)) = False
  105.                                 EndIf
  106.                         Next
  107.                 Next
  108.         Next
  109. Next
  110. End Function
  111.  
  112. Function ApplyPos()
  113. For x = 1 To 9
  114.         For y = 1 To 9
  115.                 cnt = 0
  116.                 For i = 1 To 9
  117.                         If Pos(x, y, i) Then
  118.                                 cnt = cnt + 1
  119.                                 res = i
  120.                         EndIf
  121.                 Next
  122.                 If cnt = 1 And Grid(x, y) = 0 Then
  123.                         Grid(x, y) = res
  124.                 EndIf
  125.         Next
  126. Next
  127. End Function
  128.  
  129. Function IsSolved()
  130. For x = 1 To 9
  131.         For y = 1 To 9
  132.                 If Grid(x, y) = 0 Then Return False
  133.         Next
  134. Next
  135. Return True
  136. End Function


Comments :


GIB3D(Posted 1+ years ago)

 MWAHAHAHAI have crippled your Sudoku answerer with this Challenger diffuclty sudoku puzzle that I got out of "Dell Original Sudoku" book
Code: [Select]
...1..8.6
.8...2.1.
3.1.6....
8.39...5.
4...7...8
.2...81.9
....1.4.2
.9.3...7.
1.5..4...
) I guess I'm not the only one completely confused at how to complete some of the harder sudoku puzzles


bytecode77(Posted 1+ years ago)

 i guess your one is where i have to guess in some cases. my program is simple and just makes an entry if its clearly


Nate the Great(Posted 1+ years ago)

 lol I think it should only count as a sudoku if it can be solved by pure logic no guessing involved


Subirenihil(Posted 1+ years ago)

 My solver solved it (yes, it is a logical sudoku solver)I'm considering whether to post my solver or not.My program even writes out a *.txt file telling the order it filled in the spaces. (It leaves it up to you to figure out how it could tell that that is the correct number)<div class="quote"> lol I think it should only count as a sudoku if it can be solved by pure logic no guessing involved  </div>Very true. If you look at the star ratings for the puzzles 1-5 star puzzles are solvable by logic, 6 star puzzles have guesswork.P.S. I refuse to do anything less than a 4 star (and I don't do 4 stars much either) - they're too easy :P


Warpy(Posted 1+ years ago)

 Guesswork would either mean the problem is undecidable (unlikely), or there are multiple solutions, which I think would make a puzzle easier...


TaskMaster(Posted 1+ years ago)

 I made a brute force solver a while back.  It just starts at the first empty square checks if a 1 fits, if so, moves to the next square, checks if a one fits, if not place a 2, etc.  If it gets to nine and it doesn't fit, it backs up one square and increments it.It was actually quite fun to watch it work, as it would get nearly done, find a out that the entire combination didn't work and go all the way back to one of the first few empty squares and start over.  Almost felt sorry for it when that happened.  :)


Nate the Great(Posted 1+ years ago)

 @ taskmaster - sometimes you just have to feel sorry for poor programs like that even though they really dont have feelings, it seems like such a downer for it to start over on something it worked so hard on.


Chroma(Posted 1+ years ago)

 I'm pretty sure a true Sudoku only has one solution. [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal