辅导XJCO 2811、辅导C++语言程序
- 首页 >> Algorithm 算法 XJCO 2811:User Interfaces 2022-2023
COURSEWORK 0: Cave Plus Plus
Goals of this CW:
• Test your understanding of basic C++,
• Object-oriented programming in C++,
• Arrays, pointers, and references.
• Introduce a basic terminal user interface to a block-world
Getting started:
• Download the zipped project and open with Qt Creator.
• extract the project to a location on your computer
• go "File" → "Open File or Project" and select the .pro file in the zip
• select the Qt Kit you wish to use (the default is usually correct) and click
"Configure Project".
• click the run button to start the program. You may need to set the project to
"run in terminal" as detailed in lab 1.
• you should see something like this:
• Try out the program:
• the program will show you a world which is an array of characters
• you will be presented with a prompt. Type "move west" followed by
enter. observe the result.
• try these commands:
• "move north"
• "move east" - notice this fails!
• "place coin" - shown as an underscore _, or L if Tom is also at the location
• "place mushroom"
• "exit"
• Run the test script:
• If you run the main function with a single command line argument "test", it will run
the test function. This is an example of the kind of function I will use to grade your
code (but I will change the details). You can set the command line arguments in
Creator by clicking "Projects" → "Run" and then edit
"Command line arguments" on the right.
• The test function will also write out a .patch file that you must submit via Minerva
before the deadline.
• Read the code to understand how the program works. Use the debugger and breakpoints to
help you do this.
Your tasks:
Make the following changes to the program. Do not edit the files marked "Do not change
this file" in the comments at the top; you may have to click + to show all comments. You may
use the std library (but no others) to assist you if necessary. If you get stuck on one part,
continue to the others:
1. Fix the code so that the move command will accept "move east" and "move south" and
move tom appropriately.
(12/2 marks)
2. Currently the system only creates a small (8x8) Cave. Edit the constructor to cave so
that multiple sizes can be constructed. If you set the Command line arguments in your
IDE to a string such as "12 16" it will create a cave of a different size.
• remove the code which throws a logic_error if the size is not 8x8 in
the Cave constructor
• edit the constructor to create an array of locations based on the sizes given
• remember to add Rocks around the edge of the cave
(12/2 marks)
3. The Cave destructor is currently broken - it does not return all the memory allocated
by new statements in the Cave constructor. Fix this.
(12/2 marks)
4. The Cave class does not have a copy constructor or a copy assignment
operator defined. Implement them, creating a deep copy of the cave each time.
• A deep copy shares no variables or memory with the original
• All dynamically allocated variables will have to be deep copied
• If those variables contain dynamically allocated variables, they will also need to
be copied.
• To copy Cave you might write and use a copy constructor in Location.
• To deep copy a vector of pointers, you will have to deep copy each element of
the vector.
(12/2 marks)
5. Create the "throw
COURSEWORK 0: Cave Plus Plus
Goals of this CW:
• Test your understanding of basic C++,
• Object-oriented programming in C++,
• Arrays, pointers, and references.
• Introduce a basic terminal user interface to a block-world
Getting started:
• Download the zipped project and open with Qt Creator.
• extract the project to a location on your computer
• go "File" → "Open File or Project" and select the .pro file in the zip
• select the Qt Kit you wish to use (the default is usually correct) and click
"Configure Project".
• click the run button to start the program. You may need to set the project to
"run in terminal" as detailed in lab 1.
• you should see something like this:
• Try out the program:
• the program will show you a world which is an array of characters
• you will be presented with a prompt. Type "move west" followed by
enter. observe the result.
• try these commands:
• "move north"
• "move east" - notice this fails!
• "place coin" - shown as an underscore _, or L if Tom is also at the location
• "place mushroom"
• "exit"
• Run the test script:
• If you run the main function with a single command line argument "test", it will run
the test function. This is an example of the kind of function I will use to grade your
code (but I will change the details). You can set the command line arguments in
Creator by clicking "Projects" → "Run" and then edit
"Command line arguments" on the right.
• The test function will also write out a .patch file that you must submit via Minerva
before the deadline.
• Read the code to understand how the program works. Use the debugger and breakpoints to
help you do this.
Your tasks:
Make the following changes to the program. Do not edit the files marked "Do not change
this file" in the comments at the top; you may have to click + to show all comments. You may
use the std library (but no others) to assist you if necessary. If you get stuck on one part,
continue to the others:
1. Fix the code so that the move command will accept "move east" and "move south" and
move tom appropriately.
(12/2 marks)
2. Currently the system only creates a small (8x8) Cave. Edit the constructor to cave so
that multiple sizes can be constructed. If you set the Command line arguments in your
IDE to a string such as "12 16" it will create a cave of a different size.
• remove the code which throws a logic_error if the size is not 8x8 in
the Cave constructor
• edit the constructor to create an array of locations based on the sizes given
• remember to add Rocks around the edge of the cave
(12/2 marks)
3. The Cave destructor is currently broken - it does not return all the memory allocated
by new statements in the Cave constructor. Fix this.
(12/2 marks)
4. The Cave class does not have a copy constructor or a copy assignment
operator defined. Implement them, creating a deep copy of the cave each time.
• A deep copy shares no variables or memory with the original
• All dynamically allocated variables will have to be deep copied
• If those variables contain dynamically allocated variables, they will also need to
be copied.
• To copy Cave you might write and use a copy constructor in Location.
• To deep copy a vector of pointers, you will have to deep copy each element of
the vector.
(12/2 marks)
5. Create the "throw