Skip to content

Commit

Permalink
Added 'solve.py' and implemented a solving system and CLI. Updated RE…
Browse files Browse the repository at this point in the history
…ADME.md accordingly.
  • Loading branch information
atomicsorcerer committed Aug 7, 2022
1 parent 95db6c5 commit a9bbccd
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 5 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,30 @@ The wave function collapse algorithm is a newer idea generally implemented in ga
### Sudoku Generation
For this implementation, each tile on the board is initialized with all possible values--the values 1 through 9. In quantum mechanics, the inspiration for this algorithm, one data point can contain multiple values. The algorithm goes through and randomly picks tiles to collapse to their lowest entropy (1). Every time this happens, all other tiles in the same group, column, and row loose that new value from their own array of values. This implements the restrictions that make sudoku work. This sudoku generator is a perfect way to represent the capabilities of the wave function collapse algorithm, and to make a few sudoku boards on the side.
### Sudoku Solver
By collapsing specific tiles before random tile collapse takes over, the sudoku generator becomes a sudoku solver. Depending on the tiles "pre-collapsed," the solver may provide a different solution every run. There is no current runtime for the solver, so, for now, you will have to manually implement it using the provided functions in `lib.py` and `board.py`.
By collapsing specific tiles before random tile collapse takes over, the sudoku generator becomes a sudoku solver. Depending on the tiles "pre-collapsed," the solver may provide a different solution every run.
## Dependencies
None! This project is built in 100% vanilla python to add transparency to the algorithm. The only included dependency is the `random` module which is built in to the language.
## Usage
For now, there is not much customization possible at run time, so usage is quite simple. The `show_process` option will print out each iteration of the board while it is generating. This is helpful for studying the algorithm and debugging.
### Generation
Usage is quite simple. The `--show_process` flag will print out each iteration of the board while it is generating. This is helpful for studying the algorithm and debugging.
```commandline
python main.py <show_process>
python main.py <--show_process>
```
### Solving
The solving system is straightforward, but has more steps. The `--show_process` flag is the same, while the `--do_highlight` flag will mark the starting tiles after solving. To start, run the following command:
```commandline
python solve.py <--show_process> <--do_highlight>
```
After running the code, you will be prompted to select the tiles you want to collapse.
```
---
Enter the 'x' coordinate: ...
Enter the 'y' coordinate: ...
Enter value to collapse tile to: ...
Do you want to collapse more tiles (y, N): ...
```
If you answer `y`, the menu will reopen until you are finished. The board will be solved once you answer 'N'.
### Testing
You can also run the following command to test the program. The first parameter sets how many boards you want generated. It is set to 1000 automatically. None of the boards will be printed to the console.
```commandline
python test.py <iterations_to_run: int>
Expand Down
Binary file added __pycache__/solve.cpython-310.pyc
Binary file not shown.
Binary file modified board/__pycache__/board.cpython-310.pyc
Binary file not shown.
4 changes: 3 additions & 1 deletion board/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ def collapse_random_tile(self) -> tuple[int, int] | None:

return rand_x_coord, rand_y_coord

def collapse_specific_tile(self, x, y, value_to_set=None, do_highlight=False) -> tuple[int, int] | None:
def collapse_specific_tile(
self, x, y, value_to_set=None, do_highlight=False
) -> tuple[int, int] | None:
# make sure that there are still tiles to collapse
if len(self.collapsed_tiles) >= self.rows * self.columns:
self.completed = True
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
show_build_process = False

try:
if sys.argv[1] == "show_process":
if sys.argv.count("--show_process") > 0:
show_build_process = True
except IndexError:
pass
Expand Down
57 changes: 57 additions & 0 deletions solve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
Sudoku Generator
© Atomic Sorcerer 2022
"""

from lib.utils import complete_board, print_board
from board.board import Board

import sys


show_build_process = False

do_highlight = False

try:
if sys.argv.count("--show_process") > 0:
show_build_process = True
except IndexError:
pass

try:
if sys.argv.count("--do_highlight") > 0:
do_highlight = True
except IndexError:
pass

indexes_to_collapse = []


def add_a_coord():
print("---")
x = int(input("Enter the 'x' coordinate: "))
y = int(input("Enter the 'y' coordinate: "))
value = int(input("Enter value to collapse tile to: "))
coords = (x, y)

indexes_to_collapse.append((coords, value))

add_more = input("Do you want to collapse more tiles (y, N): ")

if add_more == "y":
add_a_coord()


if __name__ == "__main__":
add_a_coord()

board = Board()

for i in indexes_to_collapse:
board.collapse_specific_tile(i[0][0], i[0][1], i[1], do_highlight=do_highlight)

new_board = complete_board(board, show_process=show_build_process)

print(f"Iteration Amount: {str(new_board[1])}")
print(f"Attempts to create board: {str(new_board[2])}")

0 comments on commit a9bbccd

Please sign in to comment.