Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Programming! Assignment: Your task is to implement a mystery carpet, which applies pattern matching as shown in the figure below. The program receives a

C++ Programming!

Assignment:

Your task is to implement a mystery carpet, which applies pattern matching as shown in the figure below.

The program receives a carpet and a pattern as its input. As its output it tells, in which positions the pattern was found and the number of pattern instances. However, the program only has a textual user interface (not a graphical one), so the input and output uses only ASCII characters.

Your task is to implement a mystery carpet applying pattern matching in a way described below.

The program uses five colors: R (red), G (green), B (blue), Y (yellow), and W (white). The user can give a pattern consisting of four colors e.g. in a form as BRYG, or bryg, or Bryg. (So upper and lower case letters can be used equally and mixed.) The colors of the carpet can be given in the same way, but as a longer string.

For declaring colors, you must use an enum type, for example, like this:

enum Color {RED, GREEN, BLUE, YELLOW, WHITE, NUMBER_OF_COLORS}; 

The program starts by asking for the width and height of the carpet:

Enter carpet's width and height: 10 5 

It should be asked by giving two numbers separated by an empty space. Width (the input number given first) is a horizontal measurement, and height (the latter one) is a vertical one, which can be seen unlogical in the case of a carpet. (To save space, it is better to draw a carpet horizontally, and typically a horizontal measurement is called width, and a vertical one is called height. Moreover, instead of a carpet, we could use a more general term as grid or rectangle.) Anyway, the carpet in the figure shown at the beginning has 7 as its width and 3 as its height.

Since the size of the pattern is 2 x 2, the size of the carpet cannot be smaller than that. If one of the given numbers is smaller than two, the program prints the following error message and terminates with the return value EXIT_FAILURE:

Enter carpet's width and height: 4 1 Error: Carpet cannot be smaller than pattern. 

If the user gave an acceptable size for the carpet, the program continues by asking a starting way (drawing color squares randomly or reading them from the user input):

Enter carpet's width and height: 10 5 Select start (R for random, I for input): x Select start (R for random, I for input): yyy Select start (R for random, I for input): r Enter a seed value: 1 R R Y B B G R Y Y W G B W R R B Y R G R B Y B W W B R Y B Y W Y G R Y G Y Y W G G W Y Y Y R Y W G B Enter 4 colors, or q to quit: 

The question is repeated until the user gives either string R or I. Note that both upper-case and lower-case letters are accepted equally.

If the user selects a way, where the carpet is created with randomly drawn colors, the program asks next a seed value for the random number generator. After giving a seed value the program prints the carpet of desired size, filled with random colors in a way shown above. (Each color, also the first in line, is preceded by an empty space.)

If the user choses a non-random creation way, the program asks for the colors of the carpet and checks if the correct amount of acceptable colors was given:

Enter carpet's width and height: 10 5 Select start (R for random, I for input): i Input: GGGYGGYYRWGBWWRRRRYBBGBWRGBYBYWBWWRRBWWRWYYWWGBRB Error: Wrong amount of colors. Select start (R for random, I for input): i Input: GGGYGGYYRWGBWWRRRRYBBGBWRGBYBYWBWWRRBWWRWYYWWGBRBX Error: Unknown color. Select start (R for random, I for input): i Input: XYZ Error: Wrong amount of colors. Select start (R for random, I for input): i Input: GGGYGGYYRWGBWWRRRRYBBGBWRGBYBYWBWWRRBWWRWYYWWGBRBY G G G Y G G Y Y R W G B W W R R R R Y B B G B W R G B Y B Y W B W W R R B W W R W Y Y W W G B R B Y Enter 4 colors, or q to quit: 

If the user gave an incorrect number of colors, the program prints the error message shown above: Error: Wrong amount of colors. If one of the colors the user gave is not a code for an acceptable color, the program prints the error message shown above: Error: Unknown color.

If the user input is incorrect in two ways: it has an incorrect number of colors and it contains an unknown color, the program informs only of the first mentioned error, as can be seen in the example above (input XYZ).

The example above shows also that after an erroneuos input, the user has the possibility to choose the starting way again.

After giving an acceptable input, the program prints the carpet in the same way as in the starting way with random colors: as many colors are taken from the input as is the width of the carpet, and these colors are printed in the first line, then the same amount of colors are taken and printed in the second line and so on.

Now, the program proceeding does not depend on the creation way any more.

Next the program asks for a pattern consisting of four colors. If the input is something else than a color series consisting of four acceptable colors, the program prints the same error messages as before:

Enter 4 colors, or q to quit: GGG Error: Wrong amount of colors. Enter 4 colors, or q to quit: GGGYY Error: Wrong amount of colors. Enter 4 colors, or q to quit: GGGX Error: Unknown color. Enter 4 colors, or q to quit: XYZ Error: Wrong amount of colors. Enter 4 colors, or q to quit: 

Besides the acceptable color series, the user can always give the quitting command "Q" or "q", which makes the program terminate without any prints with the return value EXIT_SUCCESS.

Let us next consider the actual going of the game:

Enter carpet's width and height: 10 5 Select start (R for random, I for input): i Input: RRYBBGRYYWWYWRRRYRGRWYBWWWRYBYWYGRYGYYWGGWYYYRYWGB R R Y B B G R Y Y W W Y W R R R Y R G R W Y B W W W R Y B Y W Y G R Y G Y Y W G G W Y Y Y R Y W G B Enter 4 colors, or q to quit: wrwb = Matches found: 0 Enter 4 colors, or q to quit: rywr - Found at (6, 2) = Matches found: 1 Enter 4 colors, or q to quit: ryyy - Found at (7, 3) - Found at (4, 4) = Matches found: 2 Enter 4 colors, or q to quit: wywy - Found at (1, 2) - Found at (1, 3) = Matches found: 2 Enter 4 colors, or q to quit: rrww - Found at (4, 2) - Found at (5, 2) = Matches found: 2 Enter 4 colors, or q to quit: 

The program prints first the coordinates, where the pattern was found (the left upper corner of the pattern), and after that the amount of occurrences. Occurrences can be overlapping, as wywy and rrww above.

When searching for a pattern, the carpet is gone through line by line, whereupon the program prints first the occurrence, the line number or y-coordinate of which is the smallest (see the input ryyy). If there are several occurrences in the same line (the same y-coordinate), the program program prints first the occurrence, the column number or x-coordinate of which is the smallest (see the input rrww).

The shape of the pattern is 2 x 2, but a pattern is given by listing four consecutive colors. For example, the input BRYG means the pattern:

B R Y G 

i.e. the first two characters are the colors of the upper line, and the latter two characters are the colors of the lower line of the pattern. The same thing is shown in the figure below, where the pattern can be seen on the left, and the corresponding input can be seen on the right.

Assignment

Your task is to implement a mystery carpet applying pattern matching in a way described below.

The program uses five colors: R (red), G (green), B (blue), Y (yellow), and W (white). The user can give a pattern consisting of four colors e.g. in a form as BRYG, or bryg, or Bryg. (So upper and lower case letters can be used equally and mixed.) The colors of the carpet can be given in the same way, but as a longer string.

For declaring colors, you must use an enum type, for example, like this:

enum Color {RED, GREEN, BLUE, YELLOW, WHITE, NUMBER_OF_COLORS}; 

The program starts by asking for the width and height of the carpet:

Enter carpet's width and height: 10 5 

It should be asked by giving two numbers separated by an empty space. Width (the input number given first) is a horizontal measurement, and height (the latter one) is a vertical one, which can be seen unlogical in the case of a carpet. (To save space, it is better to draw a carpet horizontally, and typically a horizontal measurement is called width, and a vertical one is called height. Moreover, instead of a carpet, we could use a more general term as grid or rectangle.) Anyway, the carpet in the figure shown at the beginning has 7 as its width and 3 as its height.

Since the size of the pattern is 2 x 2, the size of the carpet cannot be smaller than that. If one of the given numbers is smaller than two, the program prints the following error message and terminates with the return value EXIT_FAILURE:

Enter carpet's width and height: 4 1 Error: Carpet cannot be smaller than pattern. 

If the user gave an acceptable size for the carpet, the program continues by asking a starting way (drawing color squares randomly or reading them from the user input):

Enter carpet's width and height: 10 5 Select start (R for random, I for input): x Select start (R for random, I for input): yyy Select start (R for random, I for input): r Enter a seed value: 1 R R Y B B G R Y Y W G B W R R B Y R G R B Y B W W B R Y B Y W Y G R Y G Y Y W G G W Y Y Y R Y W G B Enter 4 colors, or q to quit: 

The question is repeated until the user gives either string R or I. Note that both upper-case and lower-case letters are accepted equally.

If the user selects a way, where the carpet is created with randomly drawn colors, the program asks next a seed value for the random number generator. After giving a seed value the program prints the carpet of desired size, filled with random colors in a way shown above. (Each color, also the first in line, is preceded by an empty space.)

If the user choses a non-random creation way, the program asks for the colors of the carpet and checks if the correct amount of acceptable colors was given:

Enter carpet's width and height: 10 5 Select start (R for random, I for input): i Input: GGGYGGYYRWGBWWRRRRYBBGBWRGBYBYWBWWRRBWWRWYYWWGBRB Error: Wrong amount of colors. Select start (R for random, I for input): i Input: GGGYGGYYRWGBWWRRRRYBBGBWRGBYBYWBWWRRBWWRWYYWWGBRBX Error: Unknown color. Select start (R for random, I for input): i Input: XYZ Error: Wrong amount of colors. Select start (R for random, I for input): i Input: GGGYGGYYRWGBWWRRRRYBBGBWRGBYBYWBWWRRBWWRWYYWWGBRBY G G G Y G G Y Y R W G B W W R R R R Y B B G B W R G B Y B Y W B W W R R B W W R W Y Y W W G B R B Y Enter 4 colors, or q to quit: 

If the user gave an incorrect number of colors, the program prints the error message shown above: Error: Wrong amount of colors. If one of the colors the user gave is not a code for an acceptable color, the program prints the error message shown above: Error: Unknown color.

If the user input is incorrect in two ways: it has an incorrect number of colors and it contains an unknown color, the program informs only of the first mentioned error, as can be seen in the example above (input XYZ).

The example above shows also that after an erroneuos input, the user has the possibility to choose the starting way again.

After giving an acceptable input, the program prints the carpet in the same way as in the starting way with random colors: as many colors are taken from the input as is the width of the carpet, and these colors are printed in the first line, then the same amount of colors are taken and printed in the second line and so on.

Now, the program proceeding does not depend on the creation way any more.

Next the program asks for a pattern consisting of four colors. If the input is something else than a color series consisting of four acceptable colors, the program prints the same error messages as before:

Enter 4 colors, or q to quit: GGG Error: Wrong amount of colors. Enter 4 colors, or q to quit: GGGYY Error: Wrong amount of colors. Enter 4 colors, or q to quit: GGGX Error: Unknown color. Enter 4 colors, or q to quit: XYZ Error: Wrong amount of colors. Enter 4 colors, or q to quit: 

Besides the acceptable color series, the user can always give the quitting command "Q" or "q", which makes the program terminate without any prints with the return value EXIT_SUCCESS.

Let us next consider the actual going of the game:

Enter carpet's width and height: 10 5 Select start (R for random, I for input): i Input: RRYBBGRYYWWYWRRRYRGRWYBWWWRYBYWYGRYGYYWGGWYYYRYWGB R R Y B B G R Y Y W W Y W R R R Y R G R W Y B W W W R Y B Y W Y G R Y G Y Y W G G W Y Y Y R Y W G B Enter 4 colors, or q to quit: wrwb = Matches found: 0 Enter 4 colors, or q to quit: rywr - Found at (6, 2) = Matches found: 1 Enter 4 colors, or q to quit: ryyy - Found at (7, 3) - Found at (4, 4) = Matches found: 2 Enter 4 colors, or q to quit: wywy - Found at (1, 2) - Found at (1, 3) = Matches found: 2 Enter 4 colors, or q to quit: rrww - Found at (4, 2) - Found at (5, 2) = Matches found: 2 Enter 4 colors, or q to quit: 

The program prints first the coordinates, where the pattern was found (the left upper corner of the pattern), and after that the amount of occurrences. Occurrences can be overlapping, as wywy and rrww above.

When searching for a pattern, the carpet is gone through line by line, whereupon the program prints first the occurrence, the line number or y-coordinate of which is the smallest (see the input ryyy). If there are several occurrences in the same line (the same y-coordinate), the program program prints first the occurrence, the column number or x-coordinate of which is the smallest (see the input rrww).

The shape of the pattern is 2 x 2, but a pattern is given by listing four consecutive colors. For example, the input BRYG means the pattern:

B R Y G 

i.e. the first two characters are the colors of the upper line, and the latter two characters are the colors of the lower line of the pattern. The same thing is shown in the figure below, where the pattern can be seen on the left, and the corresponding input can be seen on the right.

Special requirements

You must an enum type for declaring colors, for example, a type like this:

enum Color {RED, GREEN, BLUE, YELLOW, WHITE, NUMBER_OF_COLORS}; 

The above type can be used as an element of a vector.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M Ricardo, Susan D Urban

3rd Edition

1284056945, 9781284056945

More Books

Students also viewed these Databases questions