Skip to content

Commit a9bbccd

Browse files
Added 'solve.py' and implemented a solving system and CLI. Updated README.md accordingly.
1 parent 95db6c5 commit a9bbccd

File tree

6 files changed

+80
-5
lines changed

6 files changed

+80
-5
lines changed

README.md

+19-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,30 @@ The wave function collapse algorithm is a newer idea generally implemented in ga
77
### Sudoku Generation
88
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.
99
### Sudoku Solver
10-
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`.
10+
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.
1111
## Dependencies
1212
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.
1313
## Usage
14-
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.
14+
### Generation
15+
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.
1516
```commandline
16-
python main.py <show_process>
17+
python main.py <--show_process>
1718
```
19+
### Solving
20+
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:
21+
```commandline
22+
python solve.py <--show_process> <--do_highlight>
23+
```
24+
After running the code, you will be prompted to select the tiles you want to collapse.
25+
```
26+
---
27+
Enter the 'x' coordinate: ...
28+
Enter the 'y' coordinate: ...
29+
Enter value to collapse tile to: ...
30+
Do you want to collapse more tiles (y, N): ...
31+
```
32+
If you answer `y`, the menu will reopen until you are finished. The board will be solved once you answer 'N'.
33+
### Testing
1834
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.
1935
```commandline
2036
python test.py <iterations_to_run: int>

__pycache__/solve.cpython-310.pyc

787 Bytes
Binary file not shown.
8 Bytes
Binary file not shown.

board/board.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,9 @@ def collapse_random_tile(self) -> tuple[int, int] | None:
218218

219219
return rand_x_coord, rand_y_coord
220220

221-
def collapse_specific_tile(self, x, y, value_to_set=None, do_highlight=False) -> tuple[int, int] | None:
221+
def collapse_specific_tile(
222+
self, x, y, value_to_set=None, do_highlight=False
223+
) -> tuple[int, int] | None:
222224
# make sure that there are still tiles to collapse
223225
if len(self.collapsed_tiles) >= self.rows * self.columns:
224226
self.completed = True

main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
show_build_process = False
1212

1313
try:
14-
if sys.argv[1] == "show_process":
14+
if sys.argv.count("--show_process") > 0:
1515
show_build_process = True
1616
except IndexError:
1717
pass

solve.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
Sudoku Generator
3+
© Atomic Sorcerer 2022
4+
"""
5+
6+
from lib.utils import complete_board, print_board
7+
from board.board import Board
8+
9+
import sys
10+
11+
12+
show_build_process = False
13+
14+
do_highlight = False
15+
16+
try:
17+
if sys.argv.count("--show_process") > 0:
18+
show_build_process = True
19+
except IndexError:
20+
pass
21+
22+
try:
23+
if sys.argv.count("--do_highlight") > 0:
24+
do_highlight = True
25+
except IndexError:
26+
pass
27+
28+
indexes_to_collapse = []
29+
30+
31+
def add_a_coord():
32+
print("---")
33+
x = int(input("Enter the 'x' coordinate: "))
34+
y = int(input("Enter the 'y' coordinate: "))
35+
value = int(input("Enter value to collapse tile to: "))
36+
coords = (x, y)
37+
38+
indexes_to_collapse.append((coords, value))
39+
40+
add_more = input("Do you want to collapse more tiles (y, N): ")
41+
42+
if add_more == "y":
43+
add_a_coord()
44+
45+
46+
if __name__ == "__main__":
47+
add_a_coord()
48+
49+
board = Board()
50+
51+
for i in indexes_to_collapse:
52+
board.collapse_specific_tile(i[0][0], i[0][1], i[1], do_highlight=do_highlight)
53+
54+
new_board = complete_board(board, show_process=show_build_process)
55+
56+
print(f"Iteration Amount: {str(new_board[1])}")
57+
print(f"Attempts to create board: {str(new_board[2])}")

0 commit comments

Comments
 (0)