Answered step by step
Verified Expert Solution
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 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 cv
SCREENSIZE
COLOR
COLORS
# Globals
currentcolorindex
threadcount
stop False
def getcolor:
Returns a different color when called
global currentcolorindex
if currentcolorindex lenCOLORS:
currentcolorindex
color COLORScurrentcolorindex
currentcolorindex
return color
def solvefindendmaze:
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 findendfilename delay:
global threadcount
# create a Screen Object that will contain all of the drawing commands
screen ScreenSCREENSIZE, SCREENSIZE
screen.background
maze Mazescreen SCREENSIZE, SCREENSIZE, filename, delaydelay
solvefindendmaze
printfNumber of drawing commands screengetcommandcount
printfNumber of threads created threadcount
done False
speed
while not done:
if screen.playcommandsspeed:
key cvwaitKey
if key ord:
speed max speed
elif key ord:
speed
elif key ordp:
done True
else:
done True
def findends:
files
verysmallbmp True
verysmallloops.bmp True
smallbmp True
smallloops.bmp True
smallodd.bmp True
smallopen.bmp False
largebmp False
largeloops.bmp False
print
printPart
for filename, delay in files:
print
printfFile: filename
findendfilename delay
print
def main:
# prevent crashing in case of infinite recursion
syssetrecursionlimit
findends
if namemain:
main
using this for its thread
if maze.atendx y:
solutionpath.appendx y
return True
if not maze.canmoveherex y:
return False
maze.movex y COLOR
solutionpath.appendx y
for move in maze.getpossiblemovesx y:
if solvemaze move move solutionpath:
return True
solutionpath.pop
maze.restorex y
return False
each thread needs to be a different color
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started