代做cave system作业、c/c++编程作业调试

Assignment 1 Due September 22, 2019 In this assignment, you will load in and process a map of a cave system. When processing the map, you will calculate which positions are tunnels and which positions are solid rock. For the tunnel position...



Assignment 1

Due September 22, 2019

In this assignment, you will load in and process a map of a cave system. When processing the map, you

will calculate which positions are tunnels and which positions are solid rock. For the tunnel positions,

you will also calculate the shape of the tunnel (e.g. straight, corner, T-junction, etc.). First, the program

will load the map and print it in its raw (unprocessed) form. Second, it will process the map and print it

again in its processed form. Third, the program will ask the user for a position and print a description for

that position. Finally, the program will terminate.

The purpose of this assignment is to ensure that you understand how to use two-dimensional arrays,

including passing two-dimensional arrays to functions. For Part A, you will write functions to handle the

map in its raw form. For Part B, you will create a list of constants for tunnel shapes and some functions

to process them. For Part C, you will write functions to handle the map in its processed form. For Part

D, you will divide the program into several files for separate compilation.

The Map

There will be two maps, both represented as 2-dimensional arrays. The raw map is a 2-dimensional

array of chars and stores values from an input file. The processed map is a 2-dimensional array of

unsigned ints representing the tunnel shapes. All maps are 20 horizontal rows by 40 vertical

columns in size.

Both arrays use the same coordinate system: the top of the screen is north, the bottom is south, the

right is east and the left is west. The northwest (top-left) corner is position (0, 0). Other positions are

identified by how far south (down) and east (right) they are from that position. For example, the "A" is

2 south and 9 east, putting it at position (2, 9).

The raw map used in this assignment is map1.txt:

In this assignment, the symbols on the raw map are divided into '#'s, representing solid rock, and

everything else, representing tunnels. The other symbols will be used in later assignments. Any position

outside the map is treated as if it is solid rock.

The processed map corresponding to the above raw map is map1-processed.txt:

Part A: The Raw Map [10 / 50 marks]

In Part A, you will handle the raw map. You will eventually put these functions in their own module, but

for now put all the code in a file named BigMain.cpp. Throughout this course, make sure that you

use exactly the specified file names, which are case sensitive. Here, B and M are upper case and the

remaining letters are lower case.

By the end of Part A, you will have created functions for handling the raw map with the following

prototypes:

• void rawMapLoad (char raw[][MAP_SIZE_EAST],

 const std::string& filename);

• void rawMapPrint (const char raw[][MAP_SIZE_EAST]);

• bool rawMapIsOpen (const char raw[][MAP_SIZE_EAST],

 int south, int east);

In Part B, you will also add a function with the following prototype:

• // Shape rawMapCalculateShape (const char raw[][MAP_SIZE_EAST],

// int south, int east);

Perform the following steps:

1. Use #include to include the string, iostream, and fstream libraries. Remember to put

using namespace std;

2. Define MAP_SIZE_SOUTH and MAP_SIZE_EAST as constants with the values 20, and 40,

respectively.

3. Copy in the function prototypes shown above.

4. Add an implementation for the rawMapLoad function. The filename string represents the

name of the file to load from. You should declare a variable of the ifstream type and then open

the file for reading using the filename. If the file cannot be opened, print an error message. (For

the rest of CS 115, you should always print an error message when a file cannot be opened.)

Otherwise read the values from the file into the raw map array.

• Hint: One way to read in the values from the file into the array is to use a double for

loop. You can use >> to take a value from the variable of type ifstream and place the

result in the proper location in the raw map array. You will need to call

ignore(1000, '\n') on the ifstream variable at the end of each line to avoid

reading the newline character into the array.

• Hint: A different way to read in the map is using getline to read a line from the file

into some string variable. You will need an outer for loop that is executed once for

each line of input. Then use an inner for loop to copy the characters, one by one, from

the string into the raw map array.

• Hint: Yet another way to read in the map is by using unformatted I/O (fin.get() ) to

copy the values directly into the array. You will need to call fin.ignore(1000,

'\n') at the end of each line to avoid reading the newline character into the array.

5. Add a main function. It should start by declaring a suitable variable to represent the raw map

array. Then call the rawMapLoad function with the array as the first parameter and

"map1.txt" as the second parameter.

• Hint: If you are using Visual Studio, the program window will usually close as soon as the

program ends. To prevent this, print a message saying "Press [ENTER] to

continue..." at the end of main and then read in a line of input into a temporary

variable with getline.

6. Add an implementation for the rawMapPrint function. It should print out all the values in the

array with a newline at the end of each line. Call this function from main after you load the

map.

7. Add an implementation for the rawMapIsOpen function. The function should first check if the

position is outside the map and, if so, return false. Otherwise, the function should return

false if the raw map has a value of '#' at the specified position and true otherwise.

• Hint: The position is outside the map if south is outside the interval [0,

MAP_SIZE_SOUTH) or east is outside the interval [0, MAP_SIZE_EAST).

• Debugging: In the main function, call the rawMapIsOpen function for several

positions and print out the result for each.

Part B: Tunnel Shapes [10 / 50 marks for code, 10 / 50 marks for output]

In Part B, you will create a list of constants to store in the processed map. Each constant will represent a

possible shape that the tunnel can have at a position. You will also write a number of functions that use

these shapes.

By the end of Part B, you will have functions with the following prototypes:

• bool shapeIsTunnel (Shape shape);

• char shapeGetMapChar (Shape shape);

• Shape shapeCalculate (bool is_open_north,

 bool is_open_south,

 bool is_open_east,

 bool is_open_west);

Perform the following steps:

1. Above the main function, use the typedef statement to define an alias (alternate name) to

the unsigned int type named Shape. Use the Shape type for values representing tunnel

shapes.

• Note: We are using the Shape type to make the program easier to read. The compiler

will treat it as unsigned int.

2. Define 17 constants of the Shape type with the names and values in the table below (ignore

the Map Char column for now). The constants must be placed somewhere after the typedef

mentioned in step B1.

Constant Name Value Map Char

The letters in the constant names after "SHAPE_" indicate which directions the player can

move (except in SHAPE_BUBBLE and SHAPE_SOLID because those have no directions in

which the player can move). Similarly, the Map Char values give graphic depictions of the

directions in which the player can move. For example, the pipe symbol ('|') is used to indicate

that the player’s character can move north or south from its current location.

3. Copy in the function prototypes shown above.

4. Add an implementation for the shapeIsTunnel function. It should return true if the shape

parameter is strictly less than SHAPE_SOLID and false otherwise.

• Note: Do not just return false if the value is SHAPE_SOLID and true otherwise. In

Assignment 2 and later there will be other values and the function needs to return

false for them too.

5. Add an implementation for the shapeGetMapChar function. It should return the value from

the "Map Char" column corresponding to the shape parameter.

• Hint: Store the possible return values as an array of chars and use the shape

parameter as the array index. Add a symbolic constant for the number of tunnel shapes

and use it as the array size. A symbolic constant should be given in all capital letters and

underscores, e.g., ALL_UPPERCASE_LETTERS_WITH_UNDERSCORES.

6. Add an implementation for the shapeCalculate function. This function should return one

of the first 16 constants (from the Constant Name column), according to the parameters that

say which directions are open.

• For example, if the values of all four parameters are false, this function should return

SHAPE_BUBBLE. If is_open_south is true and the other three parameters are

false, this function should return SHAPE_S_DEAD_END. Similarly, if

is_open_north and is_open_east are true and the other two parameters are

false, this function should return SHAPE_NE_CORNER. This function should never

return SHAPE_SOLID.

7. Add an implementation for the rawMapCalculateShape function from Part A. This

function should first check if the specified position in the raw map is open. If not, it should

return SHAPE_SOLID. Otherwise, it should determine if the positions to the north, south,

east, and west are open, pass those four values to the shapeCalculate function, and return

the result.

• Hint: You can use the rawMapIsOpen function to check if the specified position in the

raw map is open. You can store the result in a Boolean variable. You can call the

function five times for the current location and the four nearby locations and store each

result in a Boolean variable.

• Hint: If the current position is at [i][j], then the position to the north of it is at position [i-

1][j]. Other directions can be determined similarly.

• Debugging: In the main function, call the rawMapCalculateShape function for

several positions and print out the results of each. You may want to use the

shapeGetMapChar function to transform your output into a nicer format.

8. [10 marks] Test your Shape type with the Main1B.cpp file on the course website.

Part C: The Processed Map [15 / 50 marks]

In Part C, you will handle the processed map.

By the end of Part C, you will have written functions for the processed map with the following

prototypes:

• void mapInit (Shape map[][MAP_SIZE_EAST]);

• void mapPrintMap (const Shape map[][MAP_SIZE_EAST]);

• bool mapIsInMap (const Shape map[][MAP_SIZE_EAST],

 int south, int east);

• bool mapIsTunnel (const Shape map[][MAP_SIZE_EAST],

 int south, int east);

• void mapPrintDescription (const Shape map[][MAP_SIZE_EAST],

 int south, int east);

• void mapSetAt (Shape map[][MAP_SIZE_EAST],

 int south, int east,

 Shape shape);

• void mapSetRectangle (Shape map[][MAP_SIZE_EAST],

 int south_min, int east_min,

 int south_size, int east_size,

 Shape shape);

Perform the following steps:

1. Copy in the function prototypes shown above.

2. Add an implementation for the mapSetAt function. It should change the specified element of

the map array to the specified shape. For example, if south is 5, east is 3, and shape is

SHAPE_NE_CORNER, then position [5][3] should be assigned the value of

SHAPE_NE_CORNER.

3. Add an implementation for the mapSetRectangle function. It should set all elements of the

map array in the specified area to the specified shape.

4. Add an implementation for the mapInit function. It should set the value of every element in

the map to SHAPE_SOLID.

5. Add an implementation for the mapPrintMap function. It should print out one character for

each position in the map. Use the shapeGetMapChar function to determine which character

to print. Remember to print a newline character at the end of each line.

6. Add code to your main function after loading and printing the raw map. Declare an array to

store the processed map and call the mapInit and mapPrintMap functions on it. When you

run your program, you can expect it to print a big rectangle of spaces (blanks) since mapInit

sets all locations to SHAPE_SOLID and this shape should be printed as a space.

7. Add code to your main function to add tunnels to the processed map. Use two for loops to go

through every position on the raw map. If that position is not solid, use the

rawMapCalculateShape function to determine the tunnel shape there. Then store the

tunnel shape in the processed map. Do this before printing the processed map. You should

now see tunnels when you print the processed map.

8. Add an implementation for the mapIsInMap function. It should return true if the specified

position is inside the bounds of map and false otherwise.

9. Add an implementation for the mapIsTunnel function. This function should call the

shapeIsTunnel function.

10. Add an implementation for the mapPrintDescription function. It should check if the map

at the specified position is a tunnel and, if so, print "You are in a tunnel.". Otherwise,

it should print "You are in solid rock.".

• Debugging: Call the mapPrintDescription function for several positions on the

map.

11. Add code to your main function after printing the processed map to ask the user for a position.

Then read in two integers corresponding to south and east coordinates. Using one of the

functions, check if that position is inside the map. If it is, print a description for that position

using the mapPrintDescription function. Otherwise, print "You are somewhere

outside the map.".

• Hint: Add a call to ignore (or getline) after reading in the two integers from the

console input (cin). Otherwise, the newline will stay in cin and might be read next

time you try to read something else.

cin.ignore(1000, '\n');

Part D: Separate the Program into Modules [5 / 50 marks]

1. Save a copy of your existing file named BigMain.cpp somewhere safe.

2. Place the MAP_SIZE_SOUTH and MAP_SIZE_EAST constants in their own header file named

MapSize.h.

3. Place the function prototypes from Part A (including rawMapCalculateShape) in a header

file named RawMap.h. The RawMap.h file should use #include to include MapSize.h

and Shape.h. Also copy the #includes and using namespace std; from your original

file.

4. Place the implementations of the rawMap* functions in a separate file named RawMap.cpp

that #includes the header files MapSize.h, Shape.h, and RawMap.h.

5. Place the typedef for Shape and the function prototypes from Part B in a header file named

Shape.h. (rawMapCalculateShape is in RawMap.h.)

6. Place the implementations of the shape* functions in a separate file named Shape.cpp that

#includes the header file Shape.h.

7. Place the function prototypes from Part C in a header file named Map.h. The Map.h file

should include MapSize.h and Shape.h.

8. Place the implementations of the map* functions in a separate file named Map.cpp that

#includes the header files MapSize.h, Shape.h, and Map.h.

9. Change the name of your original file from BigMain.cpp to Main.cpp and #include the

MapSize.h, RawMap.h, Shape.h, and Map.h header files. Remove the constants, function

prototypes, and function implementations that you copied to other files. Main.cpp should

now just contain the main function (and possibly helper functions of your own if you added

any).

10. Upload your source and header files and all your data files to hercules.cs.uregina.ca

and compile them together as explained in the lab.

Formatting [ −5 marks if not done]

1. Neatly indent and format your program. Use a consistent indentation scheme and make sure to

put spaces around your arithmetic operators, e.g., x = x + 3;.

Submission

• Submit a complete copy of your source code. You should have the following files with exactly

these names:

1. MapSize.h

2. RawMap.h

3. RawMap.cpp

4. Shape.h

5. Shape.cpp

6. Map.h

7. Map.cpp

8. Main.cpp

o Note: A Visual Studio .sln file does NOT contain the source code, it is just a text file.

You do not need to submit it. Make sure you submit the .cpp files and .h files.

• If you had any problems with Part D, or if you have not tested the above files on

hercules.cs.uregina.ca, also submit a complete copy of:

o BigMain.cpp

• If possible, convert all your files to a single archive (.zip file) before handing them in

• Do NOT submit a compiled version

• Do NOT submit intermediate files, such as:

o Debug folder

o Release folder

o ipch folder

o *.ncb, *.sdf, or *.db files


因为专业,所以值得信赖。如有需要,请加QQ99515681 或邮箱:99515681@qq.com

微信:codehelp


相关文章