Ultimate Sudoku Suite
I first built a Sudoku game using Python and Pygame. Sudoku is a number-based logic puzzle played on a 9x9 grid where the goal is to fill every row, column, and 3x3 section with numbers 1 through 9 without repeating any digit.
The Rules of Sudoku
- The Grid: The board has 81 total squares divided into nine 3x3 blocks (or boxes).
- No Repeats: Each number from 1 to 9 can only appear once in every individual row, column, and 3x3 block.
- Givens: Puzzles start with some numbers already filled in as clues. Harder puzzles give you fewer starting numbers.
- No Math Needed: You do not use arithmetic or math skills; success relies entirely on logic and pattern recognition.
How My Solver Bot Works
To make the computer solve puzzles automatically, I built a backtracking solver.
Think of the bot like a person walking through a maze. Since it can’t see the whole maze at once, it has to remember every step it takes. My two stacks (stack_empty and stack_av) act like a trail of breadcrumbs and a list of backup choices at every fork in the road.
Here is how the logic works step-by-step:
-
Taking a Step Forward:
The bot looks for an empty spot, checks what numbers are allowed using thevalidfunction, and picks the first legal number.-
Stack action: It drops a breadcrumb (
stack_empty) with the spot’s location, and saves its leftover backup numbers in a pocket (stack_av).
-
Stack action: It drops a breadcrumb (
-
Hitting a Dead End:
Sometimes, the bot fills up the board, but a later spot gets completely stuck because no numbers are legal.-
Stack action: The bot says, “Oops, this path is wrong,” and erases the failed spot back to
0.
-
Stack action: The bot says, “Oops, this path is wrong,” and erases the failed spot back to
-
Backtracking (Rewinding):
It looks at its last breadcrumb (stack_empty) and checks its backup pocket (stack_av) to see if it has any untried numbers left for that spot. If that spot is also out of options, it deletes it, erases it back to0, and steps back even further up the stack.
Once it finds a spot that still has a backup number waiting, it plays that new number and starts moving forward again. In short, the stacks let the bot undo its mistakes one step at a time instead of starting over from scratch!
Comments 0
No comments yet. Be the first!
Sign in to join the conversation.