Maze #1 (E)
Overview
This is the first step toward HW10 in which you will “solve” a maze problem. You will design a program that would allow an agent air-dropped into a rectangular maze to find the shortest path to reach some other destination in the maze. For this exercise, you will write functions to:
- Determine the dimensions of a given maze.
- Find the column location of the openings at the top/bottom of a given maze.
- Count the number of path locations in a given maze.
- Determine whether a given location in the maze is a wall or a path.
- Create a new file containing a single-line representation of the maze.
You will be learning to program with
files, including the
fopen(…)
,
fgetc(…)
,
fputc(…)
, and
fclose(…)
functions.
Input file format
A maze is a rectangle consisting of r rows and c columns. We number the rows from top to bottom as row 0 through row 6, and the columns from left to right as column 0 through column 8.
sample.2.7x9
X | X | X | X | X | X | X | X | \n | |
X | X | \n | |||||||
X | X | X | X | X | X | X | \n | ||
X | X | X | \n | ||||||
X | X | X | X | X | X | X | X | \n | |
X | X | \n | |||||||
X | X | X | X | X | X | X | X | \n |
Example
The maze at right can be found in the file called sample.2.7x9. It has seven
rows (r=7
) and nine columns (c=9
).
The file contains exactly 7 lines, with exactly 10 characters per line: 9
visible characters ('X' or ' ') plus a newline
('\n') at the end of each line. It has a total of 25
PATH characters and 38 WALL characters.
(PATH and WALL are constants defined in maze.h as
' '
and 'X'
, respectively.)
The character highlighted in orange (X)
is a WALL character, and would be identified as row 0
(r=0
) and column 2 (c=2
). The two openings are
highlighted in green ( ).
Creating new test mazes
An executable "amaze" is provided for you to generate other
test cases. To generate a maze with (2r + 1)
rows and
(2c+1)
columns, where r and c ≤ 69,
issue the following command:
…where filename is the name of the file to store the generated maze. Note that amaze outputs the maze to the stdout, but the redirection '>' redirects that output to a file with the name you specify.
Requirements
- Your submission must contain each of the following files, as specified:
file contents maze.c functions find maze dimensions(FILE ✶ fp, int ✶ a num rows, int ✶ a num cols)
→ return type: voidDetermine the dimensions of the maze contain in file.- fp is an open file pointer for a file containing a maze in the correct format.
- Write the number of rows to the location at a_num_rows, and likewise for a_num_cols.
- You may assume a_num_rows and a_num_cols are safe to write to.
find opening location(FILE ✶ fp)
→ return type: intDetermine the column location of the top opening.- The top opening is the sole PATH character in the first row.
- There will be exactly one opening in the top and one in the bottom (at the same column).
- fp is an open file pointer for a file containing a maze in the correct format.
count path locations(FILE ✶ fp)
→ return type: intCount the number of locations that are PATH (defined in maze.h).- fp is an open file pointer for a file containing a maze in the correct format.
- Include the openings at the top and bottom of the maze.
- A location is PATH if it equal to the PATH variable (currently defined as
' '
in maze.h). - Example: If fp refers to sample.2.7x9, then
count_path_locations(fp)
should return 25.
get location type(FILE ✶ fp, int row, int col)
→ return type: charReturn the type of location: WALL or PATH.- row and col are valid coordinates for the location of interest.
- fp is an open file pointer for a file containing a maze in the correct format.
- You simply return the character at the given location. For example,
get_location_type(fp, 0, 0) will always return the first character in the file.
- Example: If fp refers to sample.2.7x9, then
get_location_type(fp, 0, 2)
should return WALL ('X'
).
represent maze in one line(char ✶ filename, FILE ✶ fp)
→ return type: intWrite a copy of the maze, excluding newlines ('\n'
), to a new file.- fp is an open file pointer for a file containing a maze in the correct format.
- filename is a valid filename (i.e., not
""
, etc.). You will open this file and write to it. - Example: If fp refers to sample.2.7x9, then
represent_maze_in_one_line("newsample.2.7x9", fp)
should return write the following (below) to a new file called "newsample.2.7x9" and return 63.X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X - If opening the new file or writing to it fails, return MAZE_WRITE_FAILURE (defined in maze.h).
- Return the number of characters written into the the new file.
- ⚠ The new file must not contain any newline characters.
- ⚠ For functions that take a file pointer fp as an argument:
- Assume fp is already open.
- Do not assume fp is currently at the beginning of the file.
- Do not close fp in the function that received it as an argument.
- ⚠ Do not make any assumption about the order in which functions will be called. Each function must work as specified, even if we call them in some strange and unpredictable fashion.
- ⚠ Any file pointers opened within any of the above functions must be closed before the function returns.
-
⚠ Do not print error messages or anything else on
stdout
(i.e., withprintf(…)
). - ⚠ Do not modify maze.h.
- ⚠ Refer to PATH and WALL only by those names, and not by the character constants
' '
and'X'
. - ⚠ Do not include a
main(…)
in maze.c. (→ zero credit, since we won't be able to compile.) - Submissions must meet the code quality standards and the course policies on homework and academic integrity.
Submit
To submit HW09, type
264submit HW09 maze.c
from inside your hw09 directory.
Update: You do not need to submit test_maze.c or Makefile for this exercise.
Pre-tester ●
The pre-tester for HW09 has been released and is ready to use.
Q&A
-
I get Permission Denied when I try to run ./amaze. How do I make it work?
From bash, runchmod u+x amaze
to adjust the permissions so that it can be executed. -
Can we use
fseek(…)
/feof(…)
/EOF
/…?
Yes. You may use anything in the C99 standard library (subject to the base requirements in the Syllabus). -
Do I need to submit test_maze.c or Makefile?
No. -
Can I have a function that returns a
CodeDeck
?
Yes. A function may return a struct object. -
Does this line of code violate the code quality standards (multiple statements on line line)?
*a_num_rows = *a_num_cols = 0;
No. That is one statement. -
Can we add helper functions to maze.c?
Yes, as long as ⓐ the name begins with"_"
, and ⓑ they do not require any changes to maze.h.
Updates
10/17/2017 | You do not need to turn in test_maze.c or Makefile. Added Q&A with Q1-Q5. |
10/17/2017 | The contents of "newsample.2.7x9" given in the description for the function represent_maze_in_one_line are updated to reflect what are really stored in the file. To test your function, you should write to a different file and perform a "diff" between "newsample.2.7x9" and your output file. |
10/19/2017 | Pre-tester was updated. |