Question: Having a rough time getting started with the constructors and the display function of this class, any advice/assitance is appreciated! Task You will write a
Having a rough time getting started with the constructors and the display function of this class, any advice/assitance is appreciated!
Task
You will write a class called Grid, and test it with a couple of programs. A Grid object will be made up of a grid of positions, numbered with rows and columns. Row and column numbering start at 0, at the top left corner of the grid. A grid object also has a "mover", which can move around to different locations on the grid. Obstacles (which block the mover) and other objects (that can be placed or picked up) are also available. Here is a sample picture of a Grid object in its display format:
0 . . . This is a Grid object with 4 rows and 4 columns (numbered 0 - 3). . . > . The mover '>' is at row 1, column 2, facing east. . . . . The obstacle '#' is at row 3, column 1 . # . . The other item '0' is at row 0, column 0
The @ character indicates that the mover and an item (0) currently occupy the same position on the grid.
Program Details and Requirements
1) Grid class
You must store the grid itself as a two-dimensional array. Maximum grid size will be 40 rows and 40 columns. (Note that this means dynamic allocation will NOT be needed for this assignment).
Meaning of Grid symbols
. empty spot on the grid 0 an item that can be placed, or picked up # a block (an obstacle). Mover cannot move through it < mover facing WEST > mover facing EAST ^ mover facing NORTH v mover facing SOUTH @ mover occupying same place as item (0)
A printed space ' ' in a grid position represents a spot that the mover has already moved through when the path display is toggled on
Member function descriptions
- Grid() The default constructor should create a 1 x 1 grid, with the mover in the only position, facing EAST
- Grid(int r, int c) The two-parameter constructor will accept two integers, representing rows and columns. Create a grid with r rows and c columns. If either of these is less than 3, default to 3. If either is greater than the max number of rows or columns, default to the max. This grid should be built such that blocks are placed all around the edge, with one random exit (i.e. with no block). The mover should be in a random position and facing a random direction within the grid. When setting up the randomness, make sure each possibility has an equal chance of happening. For example, the random direction has 4 possibilities, so each one should happen about 25% of the time. For the random exit, it will be sufficient to pick a random wall first, then pick a random location on that wall (note that the exit cannot be in a corner spot).
You'll need the library cstdlib for the srand and rand functions. While it's not normally the best place to do it, you can go ahead and seed the random number generator here in the constructor in some appropriate way so that it's different for seperate program runs.
- Grid(int r, int c, int mr, int mc, int d) This constructor (5 parameters) takes in the following information, in this order:
- number of starting rows for the grid (if out of range, adjust like in the 2-parameter constructor, although minimum in this case is 1)
- number of starting columns for the grid (if out of range, adjust like in the 2-parameter constructor, although minimum in this case is 1)
- The starting row position of the mover (if out of range, adjust to the first or last row, whichever is closer)
- The starting column position of the mover (if out of range, adjust to the first or last column, whichever is closer)
- The starting direction of the mover
- Display() This function should print out "The Grid:" on one line, then output the full grid below it -- place spaces between columns so that outputs line up more evenly. End with a newline (so that any next output will be on a separate line). If the path setting is toggled to ON, then any position the mover has already moved through should show up blank. If the path setting is toggled to OFF, then all empty grid spaces show up as dots '.
The header file included is this:
// Grid class class Grid { public: // public static class constants, for direction indicators static const int NORTH = 0; static const int WEST = 1; static const int SOUTH = 2; static const int EAST = 3; // public member funcitons Grid(); // build 1 x 1 grid with mover in only // square, facing east Grid(int r, int c); // build grid with r rows, c cols, // blocks around edge with random exit // and random mover position and direction Grid(int r, int c, int mr, int mc, int d); // build empty grid with r rows, c cols, and mover // at row mr, col mc, and facing direction d bool Move(int s); // move forward s spaces, if possible void TogglePath(); // toggle whether or not moved path is shown void TurnLeft(); // turn the mover to the left void PutDown(); // put down an item at the mover's position bool PutDown(int r, int c); // put down an item at (r,c), if possible bool PickUp(); // pick up item at current position bool PlaceBlock(int r, int c); // put a block at (r,c), if possible void Grow(int gr, int gc); // grow the grid by gr rows, gc columns void Display() const; // display the grid on screen // Accessors bool FrontIsClear() const; // check to see if space in front of // mover is clear bool RightIsClear() const; // check to see if space to right of // mover is clear int GetRow() const; // return row of the mover int GetCol() const; // return column of the mover int GetNumRows() const; // return number of rows in the grid int GetNumCols() const; // return number of columns in the grid private: int rows, columns, rowmover, columnmover, direction; char mover, fill; char grid [40][40]; };
Step by Step Solution
There are 3 Steps involved in it
To implement the Grid class with its constructors and Display function lets break down the task step by step StepbyStep Solution 1 Default Constructor Grid Purpose Create a 1x1 grid with the mover in ... View full answer
Get step-by-step solutions from verified subject matter experts
