### Design a class for representing a rectangular grid

Assignment Help Basic Computer Science
##### Reference no: EM13235828

For this program, Design a class for representing a rectangular grid and use it to allow a user to query a grid interactively as part of a game.

Background:
Pegboard problems are single-player games played on a grid (or pegboard), in which moves are made by successively jumping and removing pegs from the pegboard. A peg can jump an adjacent peg if there is a slot adjacent to that peg in the opposite direction - horizontally, vertically, or diagonally. After a peg has been jumped, it is removed from the board (and possibly eaten). A typical objective of this problem is to begin with a full pegboard from which one peg has been removed, and determine a sequence of jumps which will result in one peg remaining, perhaps in the position from which the first peg was removed. A popular form of this problem involves a rectangular pegboard with 4 slots on a side (see below), which proves to be a challenging problem for a human being.

For instance, consider the example below, which demonstrates the first few moves of a session:

Pegboard Example
0 1 2 3
+---------+
0 | X X X X |
1 | X X X X |
2 | X X X X |
3 | X X X X |
+---------+

Which peg should be
removed to start? 3 1

Pegboard Example
0 1 2 3
+---------+
0 | X X X X |
1 | X X X X |
2 | X X X X |
3 | X O X X |
+---------+

Enter move: 1 3 3 1

Pegboard Example
0 1 2 3
+---------+
0 | X X X X |
1 | X X X O |
2 | X X O X |
3 | X X X X |
+---------+

Enter move: 2 0 2 2

Pegboard Example
0 1 2 3
+---------+
0 | X X X X |
1 | X X X O |
2 | O O X X |
3 | X X X X |
+---------+

Enter move: 1 1 1 3

A move can be characterized by the attributes (startPos, endPos), which respectively refer to the position of a peg that is about to jump (jumper) and the new position of the jumper. The position of the peg that is jumped and removed can be determined by averaging the respective row and column positions of startPos and endPos, provided those positions describe a legal move.

For instance, in the last panel of the example depicted above, the peg in position startPos=(3,3)can jump over the peg in position (2,3)and land in position endPos=(1,3). The "middle position" of the peg that gets jumped over can be found by averaging the coordinate values of startPos and endPos, i.e, ((1+3)/2,(3+3)/2) = (2,3).

The Assignment:

You will design and implement a class called Grid that represents a pegboard as described above. The underlying main data object in a Grid is a vector of vectors!!! Each row is itself a vector, where each element contains a value that designates either a full or empty cell. A Grid also knows how many rows and how many columns it has, and allows direct access to its grid elements via the [] operator -- that is, the element in row i, column j may be referred to as G[i][j]. A Grid also has an overloaded output operator so that it may be displayed on a console or saved in a file.

Your program should provide instructions for the user, print a copy of the board at each step, and allow the user to indicate the row and column numbers of the start position and end position for the next move. If the move is legal, the pegboard should be updated accordingly; otherwise the user should be prompted to enter a valid move. The user should also be permitted to quit at any time (especially when the game is over!)

NOTES:

you may NOT use the apmatrix class for your Grid definition.
You ARE allowed to use the DUPoint class, Pair class, or other classes developed in the text, labs, and/or lecture notes, as long as suitable credit is given.
You are allowed (and strongly encouraged) to use your Class Wizard to help generate your Grid code.
Details:

Your program should be designed for good usability.
Your Grid class should have a display method (for producing output with visible gridlines), as in the example above.
Notice that as an anomaly of C++ syntax, the declaration for the underlying vector in Grid class will be (assuming its name is "grid_");
vector< vector<int> > grid_ ;
which specifically has a space separating the two ">" symbols. (Can you figure out why the space is needed?)
The overloaded subscript [] operator for a Grid is actually not complicated. It simply returns a reference to the n-th row of the underlying vector structure. Thus, its return value is a vector<int>. (i.e, if we declare Grid g; then g[n] is the n-th row.) Because g[n] is itself a vector<int>, we are immediately able to retrieve g[n][k] as the element in the n-th row, k-th column, without need to write additional code. Furthermore, because it is a reference to the n-th row, we are able to use g[n] (and therefore g[n][k]) on either the left-hand or right-hand side of an assignment statement.
You are to write the following methods for your Grid class, and use them in a program that allows a user to attempt to solve a pegboard problem by specifying the position of the first removed peg, and subsequently specifying moves to make.
bool isLegalMove(startPosRow,startPosColumn,endPosRow,endPosColumn)
returns true if the given move is allowable, that is:
startPos and endPos are legal positions on the board;
startPos and endPos are the correct distance apart;
there is a peg in startPos=(startPosRow,startColumn)
there is a peg in the calculated middle position, and
there is no peg in endPos=(endPosRow,endPosColumn).
For instance, given the pegboard in the example above, the move given by (0,3,2,1)is legal (and true is returned) because slots(0,3),(1,2) and(2,1)are consecutive slots along a diagonal, and there are pegs in slot(0,3)and slot(1,2)and no peg in slot(2,1).

void applyMove(startPosRow, startPosColumn, endPosRow, endPosColumn)
updates the elements of the pegboard by applying the move.

#### Questions Cloud

 Why is innovative management important : Why is innovative management important and how have Walmart's leaders demonstrated innovation throughout the company's history? What level strategy is adopted with back orders or stockouts : The forecasted demand for fudge for the next four months is 140, 160, 90, and 70 pounds. What is the recommended production rate if the level strategy is adopted with no back orders or stockouts What was it capacity utilization rate for the month : In the month of July, the company operated the production line for a total of 175 hours and produced 16,900 units of output. What was it's capacity utilization rate for the month? State the net ionic equation to show the formation : Write the net ionic equation to show the formation of a precipitate (insoluble salt) when the following solutions are mixed. Write noreaction if no insoluble salt forms KCl(aq)+Pb(NO3)2(aq) Design a class for representing a rectangular grid : Design a class for representing a rectangular grid and use it to allow a user to query a grid interactively as part of a game. What is the capacity utilization rate in october : If the factory produces 10,925 units in October, what is the capacity utilization rate in October for this factory? Compute the freezing point of each of the solution : Calculate the freezing point of each of the solution 0.220 g C12H22011 of sucrose, , a nonelectrolyte, dissolved in 1.75 kg of water. What two policies could you use to reduce the total amount : Sometimes market activities (production, buying, and selling) have unintended positive or negative effects outside the market's scope. These are called externalities. As a policy maker concerned with correcting the effects of gases What are the magnitude and direction of the force of q2 : A charge Q1 = 6u is at the origin of an x y coordinate system, What are the magnitude and direction of the force of Q2 on Q1, What are the magnitude and direction of the force of Q3 on Q1

### Write a Review

#### Iscuss why or why not string and array of ints serializable

Discuss why or why not a string and an array of ints are serializable

#### Descriptions of data formats and to interpret raw data

The aim of this project is to exercise and test your ability to read and understand descriptions of data formats and to interpret raw data according to a particular format.  In this exercise you will produce and read the dump of a ZIP file.

#### Write relational algebra expression itinerary detailed dates

Write down the relational algebra expressions for given transactions. When viewing appointments have been made, itinerary has to be given detailing dates.

#### Is snort an hids or an nids

What kind of file is Snort output (logging info) stored in? 7. Can you use Snort with network switches? With hubs? 8. What operating system(s) does Snort run on?

#### Explain finding a feasible assignment as a shortest path

We say that a course assignment is a feasible assignment if every faculty member teaches either their first or second choice course. Formulate the problem of finding a feasible assignment as a shortest path, max flow, or min-cost flow problem.

#### Draw logic diagram of circuit having flip-flop equations

A sequential circuit has two D flip-flops A and B, two inputs x and y, and one output z. The flip-flop equations and circuit. Draw the logic diagram of the circuit.

#### Design and test using logic works

Design and test using Logic Works a dual-output function to implement a full-adder in Sum-of-Products form. Show the transistor count on your schematic.

#### Write recurrence relation corresponding to pseudo-code

Write the recurrence relation corresponding to the pseudo- code, don't forget the cost of the base case. iii. Write the solution of your recurrence, showing how you have solved the recurrence.

#### You are a systems development consultant

You are a systems development consultant who has been approached by a client, a law firm, to develop an automated case tracking system. The first step is to negotiate the project scope

#### Examine graph which compares segment sequence numbers

Why does Segment Sequence Number remain unchanged (indicated by the horizontal line in graphs) with every drop in congestion window?

#### Develop monthly claim status summary report

Dr. Jones has asked you to develop a monthly Claim Status Summary report. He wishes you to write the insurance company number, patient number and name.

#### Explaining quasi-legal activities over internet

Online gambling and buying of pharmaceutical drugs over the Internet are quasi-legal activities which can only take place as Internet allows the parties offering gambling.