![]() ![]() the sixth row with its 59, 89, 59 set, the middle one must be a 8.Let’s build a sudoku solver in Python today! Sudoku Puzzle is a very popular puzzle that appears in the daily newspaper that attracts the attention of a lot of people.the third row with its 29, 129, 29 set, the middle one must be a 1.Implementing the other rules would take care of, for example: That's enough to get it to a state where you can see the second rule would start to kick in (though I've modified the output slightly for formatting purposes): 1-9 1 5 4 7 1-9 2 6 9 If (row2 != row or col2 != col) and len(puzzle) = 1: If row2 != row and len(puzzle) = 1:įor row2 in range(base_row, base_row + 3):įor col2 in range(base_col, base_col + 3): # Allow for full cycle with no change to break, this means # Expand empty cells to full range of possible values. Here's the code you can start with, it implements only the first elimination rule shown above: import time Since the first two must be 1 and 2, the third must be 3 even though you don't know which of the others is 1 or 2.Īpplying just those two rules to every cell one by one, and repeating as many times as necessary, solves the first puzzle completely: sudoku1 = [ If a group has N cells which only allow the same N values, those values can be eliminated elsewhere, An example is a row with three empty cells with possibilities 12, 12, and 123. If the cell in question has a value not found anywhere else in the group, it must be that value. If a group already has a specific (discovered) value, that value can be eliminated from every other undiscovered cell in the group. The discovery strategies are basically self-contained, such as: You have to handle multiple branches in your search space but that's reasonably easy with recursion. In that final case, you just have to do the same thing again. You get the same situation (at least two possibilities in every empty cell). You finish the puzzle successfully in which case you made the right choice originally. You end up with a duplicate value in a group (in which case you go back and set the original cell to the other possibility). You'll eventually figure out that either: Just set one of the empty cells with two (or the smallest number of) possibilities to a specific value and then carry on. For example, let's say you followed all the basic discovery rules but you still have undiscovered cells. It fully solved simple Sudoku puzzles with relatively simple rules like those given above, but the more complicated ones had to use look-ahead to find impossible situations and discard them. I actually implemented such a beast that would actually do this (a). The second undiscovered cell in row one now has all but 8 eliminated because of the row (it's the one value remaining). That means the cell is now 3 and it is now discovered. The 3x3 eliminates 152486 of which only 8 has an effect. It then has 246 eliminated because of the column (irrelevant because the row already eliminated them). ![]() The first undiscovered cell in row one has all but 38 eliminated because all the other values in row one dictate that. You now have discovered cells (with one possible value) and undiscovered cells (with two or more possibilities).įor each undiscovered cell, apply discovery rules one by one. Start with all empty cells on the board containing the full range 123456789. You just get your code to do the same thing, along the lines of: Have a set of discovery rules that you apply to groups (rows, columns, or 3-by-3s) to eliminate possibilities, or select values where there is but one possibility.įor example, consider the first three rows in the first puzzle: ,Īs a human, I would realise that only 38 is allowed in the two empty cells in row one but 8 is disallowed in the first cell because it's already in the top left 3x3. You would solve it programmatically the same way you would solve it as an intelligent human. Print('Invalid Player Type!\nMake sure you use "Human" or "Computer"') If Player_Type = 'Human' or 'human' or 'HUMAN':Įlif Player_Type = 'Computer' or 'computer' or 'COMPUTER': Player_Type = input('"Human" or "Computer" solve?: ') While Player_Type != 'Human' or 'Computer': # Player Types | Computer or Player depending on the input below! # Don't change the layout of the following sudoku examples But when I would run the program it would be working but not printing the results each time it was solving it. I tried doing these using things such as for 'i in range'. I tried using rows and columns to solve and it just wouldn't print the results on the board every time it would work. I was wondering how to solve this Sudoku board in Python using Human input and Computer solving. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |