A small robot explores a two-dimensional word with pits, gold, and other objects. The robot cannot escape the world or climb pits. Being a good raider, it does pick up any gold it encounters. You are to write a simulator for the raider. This assignment will give you experience with two-dimensional arrays, classes, and pointers.
The raider's world is a grid 20 columns wide, 10 rows high. The following shows a sample world followed by initial coordinates for the robot and a series of instructions.
+--------------------+ | # | | ** - | | # # | | | | O | | *# + # | | | | #### | | ## | | | +--------------------+ 1 1 eesThe dashes and vertical bars mark the edges of the world. Pound signs (occasionally known as hashtags) mark pits, asterisks mark gold, and other characters mark objects that have no consequence to the raider. (The raider will pass over them without picking them up. Note this means that you show the raider's R on the cell while the raider is in it.) If the raider attempts to pass through a wall, by robot magic the raider is transported to the same location on the other side of the world. So if the raider is in the lower right corner and goes south, it will move to the upper right corner. The two numbers give the starting location as (x, y) values, where 0 0 is the upper left corner of the world and 19 9 is the lower right corner. Note there are 10 rows and 20 columns within the walls of the world.
The remaining text (which may be on more than one line) captures the list of commands:
Initial state: Robot at 1, 1 (0 gold) +--------------------+ | # | | R** - | | # # | | | | O | | *# + # | | | | #### | | ## | | | +--------------------+ ======================================== Executing ees Robot at 3, 2 (2 gold) +--------------------+ | # | | - | | # R # | | | | O | | *# + # | | | | #### | | ## | | | +--------------------+ ======================================== Robot has completed its task.Note the robot picked the gold in row 1. Print an error message (and stop executing instructions) when the robot runs into a pit. The above world followed by the input
4 5 seeswwould mean it would print
Executing seesw Error: robot with current status of Robot at 6, 7 (1 gold) broke while executing seesw +--------------------+ | # | | ** - | | # # | | | | O | | # + # | | | | ####R | | ## | | | +--------------------+between the two lines of equal signs. (If you want to see more detail, this is test 2 in the tests given at the bottom of this writeup.) In this case, note the robot is shown at its last legal position.
main.cpp
along with a number of
skeleton files are being given to you. You can modify the other files,
but not main.cpp
. If you think you need to
change main
, talk to your instructor.
a.exe < in.1
you can type at the command prompt)
introduces challenges. main.cpp
is written so that if a file
name is
specified on the command-line (when running your program from the
command prompt), the input is read from that file instead of the
console. To use this feature, you will edit the run configurations for
the project and add the file to process to the command line arguments.
main
contains code to read the initial position of the
robot and
to read and process commands such as moving east or south. You will
implement reading the initial world state. The best way to do this is to
use getline
to read lines of map
data. Given string
input_line
, the code
getline(cin, input_line);reads a line of input (including spaces and newlines) into the string
input_line
. You can then process each of the
characters in input_line
using a simple for
loop (0 to input_line.length()
).
MapCell
, Map
,
and Robot
. Attempting to do
otherwise will cause difficult-to-debug problems, and using pointers will
give you important experience. However, writing destructors and
using delete
is not
required. See this version
of todo.cpp
for sample code using classes and pointers.
Another reason to use pointers is to avoid creating copies of objects
that must be unique like map cells. If you have
(say) two copies of the cell at (0, 0), then updating one copy will not
necessarily update the other copies.
MapCell *cells[WIDTH][HEIGHT];where
WIDTH
is 20 and HEIGHT
is 10. If you prefer, you
can reverse the indices (cells[HEIGHT][WIDTH]
), but you must use a
two-dimensional array with exactly 200 elements. To access the
upper left corner of the area within the map, you would access
cells[0][0]. In general, if the robot is at (x, y) and you
use cells[WIDTH][HEIGHT]
as shown above, you would access the
cell at that location with code like
MapCell *some_cell; some_cell = cells[x][y];If you flip the width and height, then you would flip x and y.
MapCell
will be
in cell.h
and cell.cpp
, class Map
will be
in map.h
and map.cpp
, and class Robot
will be
in robot.h
and robot.cpp
.
.h
files includes any of the other
files you write. (That is, you can include standard library files
like iostream, but do not include your header files
like cell.h
.)
.cpp
files will likely include
multiple .h
files; you will need all of the details for
each class in the .cpp
files since they will be calling
the methods.
.cpp
file, ensure the corresponding .h
file is the first included file. This helps identify errors
more quickly. You will include other .h
files as well, just
put them later in the include list.
.cpp
files.
#ifndef
"magic" to ensure all .h
files can
be included more than once. This is set up for you already.
using namespace std;
in your .h
files. You can use this in your .cpp
files.
|
to ensure the right thing happens
when the raider reaches a wall.
nullptr
. For example,
the Map
constructor should probably initialize all of the
cells to nullptr
since they will not be set to valid map
cell objects until after the input world is read.
switch
or series
of if/else
's to capture the logic for moving different
directions. (This might be in Robot::move
, but often you can
get a simpler answer by creating a routine that returns the cell in a
particular direction.) Fitting code like this into a fifteen-line method
is challenging, but note the coding standard makes an exception for
methods or functions that contain a switch
or
equivalent. However, do be careful of having a lot of repeated
code in the switch statement, because repeated code is also a problem.
Repeated code often results in buggy code!
cellAt
in Map
to retrieve a particular
map cell. Implementing this not only allows access to map cell data in
the robot, it is also a great place to add checks to ensure cell indices
are valid.
4raid
.
Submit all source (.h, .cpp)
files except main.cpp
. It does not matter
which file you submit as "main".