CECS 277 – Lab 4 – File IO

$30.00

Category: You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (1 vote)

Maze Solver

Create a program that allows the user to solve a maze that is read in from a file. The user will
begin at the starting point (‘s’) of the maze and will be able to move up, down, left, or right to
move through the maze. When the user reaches the finish (‘f’), they have solved the maze.

Create the following functions for your program:
1. read_maze() – read in the contents of the file and store the contents in a 2D list. Each
character in the maze (make sure to keep all the spaces, but you can strip out the new
lines if necessary) should be stored in a separate element in the 2D list (note: this function
should work for any size maze, not just the 9×15 provided). Return the filled 2D list.

2. find_start(maze) – passes in the filled maze (of any size). Search through the
elements in the maze using a set of nested for loops to find an ‘s’. Return the location as
a two item 1D list where the first item is the row, and the second item is the column.

3. display_maze(maze, loc) – passes in the filled maze (of any size) and the user’s
location. Iterate through the contents of the maze. Display each character in the maze in
a matrix format. When you reach the user’s location, display an ‘X’ instead so that it
shows where the user is in the maze (do not actually place the ‘X’ in the 2D list, just
display it).

In the main function, you should have a loop with a menu that repeatedly prompts the user to
move in a direction until the user finds the finish. The user will have a two item 1D list that
stores the row and column of their location in the maze that is initialized to the start position of
the map.

Move the user by updating the row and column values in the location list by adding or
subtracting 1 to the row or column (depending on the direction they moved). Do not allow the
user to move through any walls (‘*’), check the maze at the location the user is moving to see if
it is a wall, if it is, display a message that they cannot move there and do not update the user’s
location. Display the maze each time the user moves.

When the user finds the finish, display a
congratulatory message and end the program.
Example Output:
-Maze Solver-
***************
* * *
*** ******* * *
* * * *
* * ** *** * *
* * * * *
* ***** * * * *
* X* *f *
***************
1. Go North
2. Go South
3. Go East
4. Go West
Enter choice: f
Invalid input – should be an
integer.
Enter choice: 2
You cannot move there.
***************
* * *
*** ******* * *
* * * *
* * ** *** * *
* * * * *
* ***** * * * *
* X* *f *
***************

1. Go North
2. Go South
3. Go East
4. Go West
Enter choice: 1
***************
* * *
*** ******* * *
* * * *
* * ** *** * *
* * * * *
* *****X* * * *
* s* *f *
***************

***************
* * *
*** ******* * *
* * * *
* * ** *** * *
* * * * *
* ***** * * * *
* s* *fX*
***************
1. Go North
2. Go South
3. Go East
4. Go West
Enter choice: 4
***************
* * *
*** ******* * *
* * * *
* * ** *** * *
* * * * *
* ***** * * * *
* s* *X *
***************
Congratulations! You solved
the maze.

Notes:
1. Please place your name, date, and a brief description in a comment block at the top of
your program.
2. Use the check_input module provided on Canvas to check the user’s input for invalid
values. Add the .py file to your project folder to use the functions. You may modify it as
needed. Examples using the module is provided in a reference document on Canvas.

3. Do not create any extra functions or add any extra parameters.
4. Please do not create any global variables, instead, pass variables as arguments to the
functions and return values back when needed.
5. Please read through the Coding Standards reference document on Canvas for guidelines
on how to name your variables and to format your program.

6. Use docstrings to document each of your functions. Document all parameters and return
values. Add brief comments in your program to describe sections of code.
7. Thoroughly test your program before submitting:
a. Make sure that the file is read in correctly and each character is stored in a
separate element in the 2D list.
b. Make sure that you don’t mix up the rows and columns of the 2D list.
c. Make sure that your maze is displayed correctly (ie. properly oriented).
d. Make sure that the user begins at the start position.
e. Make sure that all user input is checked for invalid values.
f. Make sure that the user correctly moves in the direction specified.
g. Make sure that the user cannot move through walls in any direction.
h. Make sure that the program ends when the user finds the finish.