Skip to content
Snippets Groups Projects
Commit f5140860 authored by tnbeats's avatar tnbeats
Browse files

22211572

parent bd5691ee
No related branches found
No related tags found
No related merge requests found
......@@ -62,7 +62,29 @@ def sudoku_allowed(state):
def puzzle_successor(state):
return []
y, x = -1, -1
for i in range(len(state)):
for j in range(len(state[i])):
if state[i][j] == 0:
y, x = i, j
break
possible_moves = [(y + 1, x), (y - 1, x), (y, x + 1), (y, x - 1)]
next_states = []
for move in possible_moves:
new_y, new_x = move
if 0 <= new_y < len(state) and 0 <= new_x < len(state[0]):
# make a copy of the state
new_state = [list(row) for row in state]
# swap tiles
new_state[y][x], new_state[new_y][new_x] = new_state[new_y][new_x], new_state[y][x]
# Convert state to a tuple of tuples
new_state_tuple = tuple(tuple(row) for row in new_state)
if puzzle_allowed(new_state_tuple):
next_states.append(new_state_tuple)
return next_states
def queens_successor(state):
......@@ -82,7 +104,7 @@ def queens_successor(state):
def puzzle_allowed(state):
return False
return True
def queens_allowed(state):
......
File added
File added
File added
......@@ -18,5 +18,12 @@ def is_goal_queens(state):
return sum(map(sum, state)) == len(state)
PUZZLE_GOAL = (
(0, 1, 2),
(3, 4, 5),
(6, 7, 8)
)
def is_goal_puzzle(state):
pass
return PUZZLE_GOAL == state
......@@ -19,7 +19,8 @@ grid = ((1, 0, 0, 0, 0, 0, 0, 0, 0),
# sln = (Search_Algorithms.BreadthFirstSearch(grid, Sucessors.sudoku_successor, is_goal.is_goal_sudoku)[-1])
sln = (Search_Algorithms.DepthFirstSearch(grid, Sucessors.sudoku_successor, is_goal.is_goal_sudoku)[-1])
sln = (Search_Algorithms.DepthFirstSearch(
grid, Sucessors.sudoku_successor, is_goal.is_goal_sudoku)[-1])
for rows in sln:
print(rows)
......@@ -38,14 +39,27 @@ board = ((False, False, False, False, False, False, False, False, False),
# sln = (Search_Algorithms.BreadthFirstSearch(board, Sucessors.queens_successor, is_goal.is_goal_queens)[-1])
sln = (Search_Algorithms.DepthFirstSearch(board, Sucessors.queens_successor, is_goal.is_goal_queens)[-1])
sln = (Search_Algorithms.DepthFirstSearch(
board, Sucessors.queens_successor, is_goal.is_goal_queens)[-1])
output_tuple = tuple(tuple("Q" if value else "." for value in sln) for sln in sln)
output_tuple = tuple(tuple("Q" if value else "." for value in sln)
for sln in sln)
for rows in output_tuple:
print(rows)
#
# puzzle = ()
#
#
# print(Search_Algorithms.BreadthFirstSearch(puzzle, Sucessors.puzzle_successor, is_goal.is_goal_puzzle)[-1])
# Sliding Puzzle
initial_puzzle = (
(7, 2, 4),
(5, 0, 6),
(8, 3, 1)
)
sln = (Search_Algorithms.BreadthFirstSearch(initial_puzzle, Sucessors.puzzle_successor, is_goal.is_goal_puzzle)[-1])
if sln:
for rows in sln:
print(rows)
else:
print("No solution found")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment