Binary Puzzle Solver is a Python application with a Tkinter GUI that allows users to create, edit, and automatically solve binary puzzles. The solver uses advanced techniques from Constraint Satisfaction Problems (CSP) including backtracking, MRV, LCV, and forward checking to efficiently solve puzzles of various sizes.
- Create a square grid of any even size.
- Fill cells with
0,1or leave empty for the solver to fill. - Solve the puzzle using:
- MRV (Minimum Remaining Values) for selecting variables
- LCV (Least Constraining Value) for ordering values
- Forward Checking to prune inconsistent assignments early
- Light and Dark UI themes for better user experience.
- One-time “snake-style” animated message for fun.
- Shows status messages for invalid input, unsolvable puzzles, and success.
This section explains the key functions used in the BinaryPuzzleSolver and BinaryPuzzleUI classes, including their purpose, inputs, outputs, and behavior.
- Purpose: Initialize the solver with a grid.
- Inputs:
n: Grid size (number of rows/columns, must be even)grid: 2D list containing0,1, or'-'for empty cells
- Behavior:
- Sets up
self.domainsfor all empty cells with possible values{0, 1}. - Converts filled cells from string to integer.
- Sets up
- Output: None (initializes internal state)
- Purpose: Check if the puzzle is fully solved.
- Inputs: None
- Behavior: Returns
Trueifself.domainsis empty (no unassigned cells remain). - Output:
bool
- Purpose: Ensure a line (row or column) does not exceed allowed number of 0s or 1s.
- Inputs:
line: List of numbers or'-'for empty cells
- Behavior: Counts 0s and 1s; returns
Falseif either exceedsn/2. - Output:
bool
- Purpose: Check that no three identical numbers appear consecutively in a line.
- Inputs:
line: List of numbers or'-'
- Behavior: Iterates over the line and checks for consecutive triplets of 0s or 1s.
- Output:
bool
- Purpose: Ensure all completed rows and columns are unique.
- Inputs: None
- Behavior: Compares all filled rows and columns; returns
Falseif duplicates exist. - Output:
bool
- Purpose: Check whether the current assignment at
(r, c)is consistent. - Inputs:
r,c: Row and column indices
- Behavior:
- Checks
valid_countandvalid_tripletfor the row and column. - If row/column is complete, checks uniqueness.
- Checks
- Output:
bool
- Purpose: Choose the next cell to assign using MRV (Minimum Remaining Values).
- Inputs: None
- Behavior: Selects the variable with the fewest remaining possible values.
- Output: Tuple
(row, column)
- Purpose: Order possible values for a variable using LCV (Least Constraining Value) heuristic.
- Inputs:
var: Tuple(row, column)
- Behavior:
- Simulates each value and counts conflicts it causes in related cells.
- Returns values in ascending order of conflicts.
- Output: List of values
[0,1]sorted by least impact
- Purpose: Eliminate inconsistent values from neighboring variables.
- Inputs:
var: Tuple(row, column)just assigned
- Behavior:
- For each variable in the same row or column, removes values that violate constraints.
- Returns
Falseif any variable has no possible values left.
- Output:
bool
- Purpose: Solve the puzzle recursively using backtracking.
- Inputs: None
- Behavior:
- Checks if the puzzle is complete.
- Selects the next variable (MRV), orders values (LCV), assigns, forward-checks, and recurses.
- Backtracks if necessary.
- Output:
boolindicating whether a solution was found
- Purpose: Entry point to start solving the puzzle.
- Inputs: None
- Behavior: Calls
backtrack. - Output:
bool(solution found or not)
- Purpose: Initialize the Tkinter GUI.
- Inputs:
root(Tkinter root window) - Behavior:
- Sets up variables, builds the UI, applies theme, starts snake message.
- Output: None
- Purpose: Create widgets (buttons, entries, labels) for the interface.
- Inputs: None
- Behavior: Adds grid size input, buttons for grid creation, solving, theme toggle, and grid frame.
- Output: None
- Purpose: Generate a new empty grid of Entry widgets.
- Inputs: None
- Behavior:
- Reads
self.size_var, validates size. - Creates
n x nEntry widgets. - Stores them in
self.grid_cells.
- Reads
- Output: None
- Purpose: Read grid, solve it, and update UI.
- Inputs: None
- Behavior:
- Converts Entry values into solver input.
- Calls
BinaryPuzzleSolver.solve(). - Updates entries with solution or shows error.
- Output: None
- Purpose: Switch between Light and Dark mode.
- Inputs: None
- Behavior: Changes colors of root, buttons, labels, and entries.
- Output: None
- Purpose: Display temporary messages in the UI.
- Inputs:
main_text: Main message stringsub_text: Optional secondary textcolor: Text color
- Behavior: Displays a label, then destroys it after 2 seconds.
- Output: None
start_snake(self): Starts the one-time left-to-right message animation.move_snake_once(self): Moves the snake label by incrementingxuntil it exits the window.- Inputs: None
- Output: None
These explanations can be added as a “Functions Overview” section in your README to make it clear how your solver and UI work internally.
Clone the repository:
git clone https://github.com/yourusername/BinaryPuzzleSolver.git
cd BinaryPuzzleSolverby M. Mahdy Sobhany Poor 📟