Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Show me the steps to solve 1 . Create a recursive, multithreaded program that finds the exit of each maze. import math import threading from

Show me the steps to solve 1. Create a recursive, multithreaded program that finds the exit of each maze.
import math
import threading
from screen import Screen
from maze import Maze
import sys
import cv2
SCREEN_SIZE =800
COLOR =(0,0,255)
COLORS =(
(0,0,255),
(0,255,0),
(255,0,0),
(255,255,0),
(0,255,255),
(255,0,255),
(128,0,0),
(128,128,0),
(0,128,0),
(128,0,128),
(0,128,128),
(0,0,128),
(72,61,139),
(143,143,188),
(226,138,43),
(128,114,250)
)
# Globals
current_color_index =0
thread_count =0
stop = False
def get_color():
""" Returns a different color when called """
global current_color_index
if current_color_index >= len(COLORS):
current_color_index =0
color = COLORS[current_color_index]
current_color_index +=1
return color
def solve_find_end(maze):
""" finds the end position using threads. Nothing is returned """
# When one of the threads finds the end position, stop all of them
# TODO - add code here
pass
def find_end(filename, delay):
global thread_count
# create a Screen Object that will contain all of the drawing commands
screen = Screen(SCREEN_SIZE, SCREEN_SIZE)
screen.background((255,255,0))
maze = Maze(screen, SCREEN_SIZE, SCREEN_SIZE, filename, delay=delay)
solve_find_end(maze)
print(f'Number of drawing commands ={screen.get_command_count()}')
print(f'Number of threads created ={thread_count}')
done = False
speed =1
while not done:
if screen.play_commands(speed):
key = cv2.waitKey(0)
if key == ord('+'):
speed = max(0, speed -1)
elif key == ord('-'):
speed +=1
elif key != ord('p'):
done = True
else:
done = True
def find_ends():
files =(
('verysmall.bmp', True),
('verysmall-loops.bmp', True),
('small.bmp', True),
('small-loops.bmp', True),
('small-odd.bmp', True),
('small-open.bmp', False),
('large.bmp', False),
('large-loops.bmp', False)
)
print('*'*40)
print('Part 2')
for filename, delay in files:
print()
print(f'File: {filename}')
find_end(filename, delay)
print('*'*40)
def main():
# prevent crashing in case of infinite recursion
sys.setrecursionlimit(5000)
find_ends()
if __name__=="__main__":
main()
using this for its thread
if maze.at_end(x, y):
solution_path.append((x, y))
return True
if not maze.can_move_here(x, y):
return False
maze.move(x, y, COLOR)
solution_path.append((x, y))
for move in maze.get_possible_moves(x, y):
if solve(maze, move[0], move[1], solution_path):
return True
solution_path.pop()
maze.restore(x, y)
return False
each thread needs to be a different color

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What are the current HRM challenges in the textile industry?

Answered: 1 week ago