### Fall 2017 :: ECE 264 :: Purdue University

This is for Fall 2017 (2 years ago) only.
Due 10/20

# 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:

1. Determine the dimensions of a given maze.
2. Find the column location of the openings at the top/bottom of a given maze.
3. Count the number of path locations in a given maze.
4. Determine whether a given location in the maze is a wall or a path.
5. 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:

./amaze r c X > filename

…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

1. 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: void
Determine the dimensions of the maze contain in file.
1. fp is an open file pointer for a file containing a maze in the correct format.
2. Write the number of rows to the location at a_num_rows, and likewise for a_num_cols.
3. You may assume a_num_rows and a_num_cols are safe to write to.
find opening location(FILE ✶ fp)
return type: int
Determine the column location of the top opening.
1. The top opening is the sole PATH character in the first row.
2. There will be exactly one opening in the top and one in the bottom (at the same column).
3. fp is an open file pointer for a file containing a maze in the correct format.
count path locations(FILE ✶ fp)
return type: int
Count the number of locations that are PATH (defined in maze.h).
1. fp is an open file pointer for a file containing a maze in the correct format.
2. Include the openings at the top and bottom of the maze.
3. A location is PATH if it equal to the PATH variable (currently defined as ' ' in maze.h).
4. 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: char
Return the type of location: WALL or PATH.
1. row and col are valid coordinates for the location of interest.
2. fp is an open file pointer for a file containing a maze in the correct format.
3. 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.
4. 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: int
Write a copy of the maze, excluding newlines ('\n'), to a new file.
1. fp is an open file pointer for a file containing a maze in the correct format.
2. filename is a valid filename (i.e., not "", etc.). You will open this file and write to it.
3. 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
4. If opening the new file or writing to it fails, return MAZE_WRITE_FAILURE (defined in maze.h).
5. Return the number of characters written into the the new file.
6. The new file must not contain any newline characters.
2. For functions that take a file pointer fp as an argument:
1. Assume fp is already open.
2. Do not assume fp is currently at the beginning of the file.
3. Do not close fp in the function that received it as an argument.
3. 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.
4. Any file pointers opened within any of the above functions must be closed before the function returns.
5. Do not print error messages or anything else on stdout (i.e., with printf(…)).
6. Do not modify maze.h.
7. Refer to PATH and WALL only by those names, and not by the character constants ' ' and 'X'.
8. Do not include a main(…) in maze.c. (→ zero credit, since we won't be able to compile.)
9. 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

1. I get Permission Denied when I try to run ./amaze. How do I make it work?
From bash, run chmod u+x amaze to adjust the permissions so that it can be executed.
2. Can we use fseek(…)/feof(…)/EOF/…?
Yes. You may use anything in the C99 standard library (subject to the base requirements in the Syllabus).
3. Do I need to submit test_maze.c or Makefile?
No.
4. Can I have a function that returns a CodeDeck?
Yes. A function may return a struct object.
5. 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.
6. 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.