|
| 1 | +""" |
| 2 | +
|
| 3 | +""" |
| 4 | + |
| 5 | +from node import Node |
| 6 | +from generator import generate_till_full |
| 7 | +from export import save_grid |
| 8 | +from solver import solve |
| 9 | +from cathegorise import determine_difficulty |
| 10 | + |
| 11 | +# DIRECTORIES |
| 12 | +import os |
| 13 | +""" --STRUCTURE-- |
| 14 | +project/ |
| 15 | + hashi/ |
| 16 | + production.py |
| 17 | + ... |
| 18 | + database/ |
| 19 | + easy/ |
| 20 | + intermediate/ |
| 21 | + hard/ |
| 22 | +""" |
| 23 | +DATABASE_DIR: str = os.path.abspath(os.path.join(os.path.dirname(__file__), '../database')) |
| 24 | +EASY_DIR: str = os.path.join(DATABASE_DIR, "easy") |
| 25 | +INTERMEDIATE_DIR: str = os.path.join(DATABASE_DIR, "intermediate") |
| 26 | +HARD_DIR: str = os.path.join(DATABASE_DIR, "hard") |
| 27 | +assert os.path.isdir(DATABASE_DIR) |
| 28 | +assert os.path.isdir(EASY_DIR) |
| 29 | +assert os.path.isdir(INTERMEDIATE_DIR) |
| 30 | +assert os.path.isdir(HARD_DIR) |
| 31 | +easy_puzzle_count: int = len(os.listdir(EASY_DIR)) |
| 32 | +intermediate_puzzle_count: int = len(os.listdir(INTERMEDIATE_DIR)) |
| 33 | +hard_puzzle_count: int = len(os.listdir(HARD_DIR)) |
| 34 | + |
| 35 | + |
| 36 | +def is_completed(grid: list[list[Node]]) -> bool: |
| 37 | + """ |
| 38 | + Checks if the grid is completed. |
| 39 | + """ |
| 40 | + for row in grid: |
| 41 | + for node in row: |
| 42 | + if node.needed != 0: |
| 43 | + return False |
| 44 | + return True |
| 45 | + |
| 46 | + |
| 47 | +def save_according_to_difficulty(grid: list[list[Node]], difficulty: int) -> None: |
| 48 | + """ |
| 49 | + Saves the grid according to its difficulty. |
| 50 | + """ |
| 51 | + global easy_puzzle_count, intermediate_puzzle_count, hard_puzzle_count |
| 52 | + puzzle_path = None |
| 53 | + if difficulty < 0.3: |
| 54 | + puzzle_path = os.path.join(EASY_DIR, f"puzzle_{easy_puzzle_count}.csv") |
| 55 | + easy_puzzle_count += 1 |
| 56 | + elif difficulty < 0.6: |
| 57 | + puzzle_path = os.path.join(INTERMEDIATE_DIR, f"puzzle_{intermediate_puzzle_count}.csv") |
| 58 | + intermediate_puzzle_count += 1 |
| 59 | + else: |
| 60 | + puzzle_path = os.path.join(HARD_DIR, f"puzzle_{hard_puzzle_count}.csv") |
| 61 | + hard_puzzle_count += 1 |
| 62 | + assert puzzle_path is not None |
| 63 | + save_grid(grid, puzzle_path) |
| 64 | + |
| 65 | + |
| 66 | +def produce(amount: int) -> None: |
| 67 | + """ |
| 68 | + Steps: |
| 69 | + 1. Generate a full grid |
| 70 | + 2. Solve it and determine the difficulty |
| 71 | + 3. Save it according to the difficulty |
| 72 | + """ |
| 73 | + |
| 74 | + while amount > 0: |
| 75 | + grid = generate_till_full() |
| 76 | + solved_grid, step_count = solve(grid) |
| 77 | + if not is_completed(solved_grid): return # if the grid is not solvable, don't save it |
| 78 | + difficulty = determine_difficulty(solved_grid, step_count) |
| 79 | + save_according_to_difficulty(solved_grid, difficulty) |
| 80 | + amount -= 1 |
| 81 | + |
| 82 | + |
| 83 | +def main() -> None: |
| 84 | + pass |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + main() |
0 commit comments