Ask Computer Engineering Expert

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 > 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. (i.e, if we declare Grid g; then g[n] is the n-th row.) Because g[n] is itself a vector, 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. 

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M9815182

Have any Question?


Related Questions in Computer Engineering

Does bmw have a guided missile corporate culture and

Does BMW have a guided missile corporate culture, and incubator corporate culture, a family corporate culture, or an Eiffel tower corporate culture?

Rebecca borrows 10000 at 18 compounded annually she pays

Rebecca borrows $10,000 at 18% compounded annually. She pays off the loan over a 5-year period with annual payments, starting at year 1. Each successive payment is $700 greater than the previous payment. (a) How much was ...

Jeff decides to start saving some money from this upcoming

Jeff decides to start saving some money from this upcoming month onwards. He decides to save only $500 at first, but each month he will increase the amount invested by $100. He will do it for 60 months (including the fir ...

Suppose you make 30 annual investments in a fund that pays

Suppose you make 30 annual investments in a fund that pays 6% compounded annually. If your first deposit is $7,500 and each successive deposit is 6% greater than the preceding deposit, how much will be in the fund immedi ...

Question -under what circumstances is it ethical if ever to

Question :- Under what circumstances is it ethical, if ever, to use consumer information in marketing research? Explain why you consider it ethical or unethical.

What are the differences between four types of economics

What are the differences between four types of economics evaluations and their differences with other two (budget impact analysis (BIA) and cost of illness (COI) studies)?

What type of economic system does norway have explain some

What type of economic system does Norway have? Explain some of the benefits of this system to the country and some of the drawbacks,

Among the who imf and wto which of these governmental

Among the WHO, IMF, and WTO, which of these governmental institutions do you feel has most profoundly shaped healthcare outcomes in low-income countries and why? Please support your reasons with examples and research/doc ...

A real estate developer will build two different types of

A real estate developer will build two different types of apartments in a residential area: one- bedroom apartments and two-bedroom apartments. In addition, the developer will build either a swimming pool or a tennis cou ...

Question what some of the reasons that evolutionary models

Question : What some of the reasons that evolutionary models are considered by many to be the best approach to software development. The response must be typed, single spaced, must be in times new roman font (size 12) an ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As