From 73e116b23b1b3012b235a7f1e4ba8f7501663d02 Mon Sep 17 00:00:00 2001
From: Michael Mutote <130656746+mr1Michael@users.noreply.github.com>
Date: Wed, 18 Oct 2023 19:09:57 +0200
Subject: [PATCH] 22202956 breadth first algorithm untouched. Sudoku Successors
 and allowed moves completed. Tested with solution testing.py. Sudoku is_goal
 also completed

---
 Breath-First Search/Sucessors.py        | 80 ++++++++++++++++++++++++-
 Breath-First Search/is_goal.py          | 15 ++++-
 Breath-First Search/solution testing.py | 18 ++++++
 3 files changed, 109 insertions(+), 4 deletions(-)
 create mode 100644 Breath-First Search/solution testing.py

diff --git a/Breath-First Search/Sucessors.py b/Breath-First Search/Sucessors.py
index f65ac2e..ed090b8 100644
--- a/Breath-First Search/Sucessors.py	
+++ b/Breath-First Search/Sucessors.py	
@@ -1,3 +1,79 @@
+def maze_successor(state):
+    next_states = [(state[0] + 1, state[1]),
+                   (state[0], state[1] + 1),
+                   (state[0] - 1, state[1]),
+                   (state[0], state[1] - 1)]
+    next_states = [s for s in next_states if maze_allowed(s)]
+    return next_states
 
-def sucessor(state):
-    return []
\ No newline at end of file
+
+def sudoku_successor(state):
+    y, x = 10, 10
+    for i, s in enumerate(state):
+        if 0 in s:
+            (y, x) = (i, s.index(0))
+            break
+    next_states = []
+    for j in range(1, 10):
+        new_state = list(map(list, state))  # Convert state to a list of lists
+        new_state[y][x] = j
+        if sudoku_allowed((y, x, new_state)):
+            # print(j)
+            # print(state)
+            next_states.append(tuple(map(tuple, new_state)))
+    return next_states
+
+
+def puzzle_successor(state):
+    return []
+
+
+def queens_successor(state):
+    return []
+
+
+def maze_allowed(state):
+    maze = [[' ', 'W', ' ', ' ', 'G'],
+            [' ', 'W', ' ', 'W', ' '],
+            [' ', 'W', ' ', ' ', ' '],
+            [' ', ' ', 'W', 'W', ' '],
+            [' ', ' ', ' ', ' ', ' ']]
+    if state[0] >= len(maze[0]) or state[0] < 0:
+        return False
+    if state[1] >= len(maze) or state[1] < 0:
+        return False
+    return maze[state[0]][state[1]] == ' ' or maze[state[0]][state[1]] == 'G'
+
+
+def sudoku_allowed(state):
+    (y, x, grid) = state
+    """I'm making the state a triple on this one"""
+    # (y, x, z): x,y is the coordinate and z is the value in the square
+
+    # check if still inside the box
+    if y < 0 or y >= len(grid[0]):
+        return False
+    if x < 0 or x >= len(grid):
+        return False
+
+    # inspect columns and rows
+    if ([row[x] for row in grid].count(grid[y][x]) > 1) or (grid[y].count(grid[y][x]) > 1):
+        return False
+
+    # inspect minor square in teh sudoku
+    inner_x = ((x - (x % 3)), (x - (x % 3) + 3))
+    inner_y = (y - (y % 3))
+    inner_square = grid[inner_y][inner_x[0]:inner_x[1]] + grid[inner_y + 1][inner_x[0]:inner_x[1]] + \
+                   grid[inner_y + 2][inner_x[0]:inner_x[1]]
+    if inner_square.count(grid[y][x]) > 1:
+        return False
+
+    return True
+
+
+def queens_allowed(state):
+    return False
+
+
+def puzzle_allowed(state):
+    return False
diff --git a/Breath-First Search/is_goal.py b/Breath-First Search/is_goal.py
index e07ac1c..7afb23d 100644
--- a/Breath-First Search/is_goal.py	
+++ b/Breath-First Search/is_goal.py	
@@ -1,7 +1,18 @@
+MAZE_GOAL = (0, 4)
 
-def isgoal(state):
 
-    return False
+def is_goal_maze(state):
+    return MAZE_GOAL == state
+
+
+def is_goal_sudoku(state):
+    y, x = 10, 10
+    for i, s in enumerate(state):
+        if 0 in s:
+            (y, x) = (i, s.index(0))
+            break
+    return ((y, x) == (10, 10))
+
 
 
 
diff --git a/Breath-First Search/solution testing.py b/Breath-First Search/solution testing.py
new file mode 100644
index 0000000..84d051e
--- /dev/null
+++ b/Breath-First Search/solution testing.py	
@@ -0,0 +1,18 @@
+import breadth_first_search
+import Sucessors
+import is_goal
+
+print(breadth_first_search.BreadthFirstSearch((4, 0), Sucessors.maze_successor, is_goal.is_goal_maze))
+
+
+grid = ((5, 3, 0, 0, 7, 0, 0, 0, 0),
+        (6, 0, 0, 1, 9, 5, 0, 0, 0),
+        (0, 9, 8, 0, 0, 0, 0, 6, 0),
+        (8, 0, 0, 0, 6, 0, 0, 0, 3),
+        (4, 0, 0, 8, 0, 3, 0, 0, 1),
+        (7, 0, 0, 0, 2, 0, 0, 0, 6),
+        (0, 6, 0, 0, 0, 0, 2, 8, 0),
+        (0, 0, 0, 4, 1, 9, 0, 0, 5),
+        (0, 0, 0, 0, 8, 0, 0, 0, 0))
+
+print(breadth_first_search.BreadthFirstSearch(grid, Sucessors.sudoku_successor, is_goal.is_goal_sudoku))
-- 
GitLab