Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

****C++**** For this project we are going to play a little game or run a little simulation, depending on how you look at it. Our

****C++****

For this project we are going to play a little game or run a little simulation, depending on how you look at it. Our world will have a rabbit in it. The rabbit is trying to get to its hole. There will also be foxes in our world. The foxes are trying to get the rabbit before the rabbit can get to its hole. For a little bit of distraction there will be bushes in our world that may help our rabbit escape.

To store this information we will use a 2D array that will represent the game world. We will place the hole, rabbit, foxes, and bushes on the grid. Once they been placed we will move the rabbit 1 space closer to the hole. Then the foxes will move 1 space closer to the rabbit.

Input

The input to our program will be a text file that will contain information about our world. The first two lines will tell us how large our world is. The rest of the input will be a series of the actors from our world, that is, the rabbit, foxes, the hole, and the bushes. The bushes are optional and may not appear in the input. We must have at least 1 rabbit, fox, and hole.

An actor is any of the items we are putting on the board. Its a generic term to mean all of them as a group.

Example

ROWSRows 10 COLSCols 10 RABBIT 3 4 FOX 10 10 HOLE 1 10 

This example would be a minimal example. The rows will always come first followed by the columns. The numbers after the word ROWS and COLS is how much of the array we are using.

Our array will be fixed size of 50x50.

It is possible, as in the example, that we might use less. In that case the other rows and columns are off limits, but still created. We simply use only what we are allowed to use. If the given row and/or column in the input is negative or bigger than 50 it is an error.

The rest of the lines are for where the rabbit, fox, and hole is located. In this example there are no bushes. That is not an error. If a bush was present it would look like this:

BUSH 3 7 

The numbers after all of the actors in the input file indicate where the actor is trying to go. If the numbers after the actor are invalid, i.e. less than 1 or greater than the given row and column sizes, the actor can not be placed on the board. Some of the actors being placed in an incorrect row or column will be an error and others will not. Ill specify the errors down below. There are about 8 or 9 of them altogether.

In general an actor can only be placed on the grid if the given row and column are valid and if the space is empty, i.e. there is nothing already on the board at the row and column.

Notice, the given row and columns are 1 based, that is to say, the smallest number Ill give you is 1, and that is the smallest valid number. The largest number Ill give you is 50, and assuming the row or column size is 50, then that is a valid number. Arrays in C++ are 0 based, meaning you must adjust the given row and column to be able to place the actor. The rabbit in the example is actually placed in to row 2 column 3 in the array.

Output

For this project, assuming the board is loaded correctly, then you will start the simulation. For the given input, this would be the output:

Step: 1 1 2 3 4 5 6 7 8 9 10 1 . . . . . . . . . o 2 . . . . . . . . . . 3 . . . R . . . . . . 4 . . . . . . . . . . 5 . . . . . . . . . . 6 . . . . . . . . . . 7 . . . . . . . . . . 8 . . . . . . . . . . 9 . . . . . . . . . . 10 . . . . . . . . . F Step: 2 1 2 3 4 5 6 7 8 9 10 1 . . . . . . . . . o 2 . . . . R . . . . . 3 . . . . . . . . . . 4 . . . . . . . . . . 5 . . . . . . . . . . 6 . . . . . . . . . . 7 . . . . . . . . . . 8 . . . . . . . . . . 9 . . . . . . . . F . 10 . . . . . . . . . . Step: 3 1 2 3 4 5 6 7 8 9 10 1 . . . . . R . . . o 2 . . . . . . . . . . 3 . . . . . . . . . . 4 . . . . . . . . . . 5 . . . . . . . . . . 6 . . . . . . . . . . 7 . . . . . . . . . . 8 . . . . . . . F . . 9 . . . . . . . . . . 10 . . . . . . . . . . Step: 4 1 2 3 4 5 6 7 8 9 10 1 . . . . . . R . . o 2 . . . . . . . . . . 3 . . . . . . . . . . 4 . . . . . . . . . . 5 . . . . . . . . . . 6 . . . . . . . . . . 7 . . . . . . F . . . 8 . . . . . . . . . . 9 . . . . . . . . . . 10 . . . . . . . . . . Step: 5 1 2 3 4 5 6 7 8 9 10 1 . . . . . . . R . o 2 . . . . . . . . . . 3 . . . . . . . . . . 4 . . . . . . . . . . 5 . . . . . . . . . . 6 . . . . . . . F . . 7 . . . . . . . . . . 8 . . . . . . . . . . 9 . . . . . . . . . . 10 . . . . . . . . . . Step: 6 1 2 3 4 5 6 7 8 9 10 1 . . . . . . . . R o 2 . . . . . . . . . . 3 . . . . . . . . . . 4 . . . . . . . . . . 5 . . . . . . . . F . 6 . . . . . . . . . . 7 . . . . . . . . . . 8 . . . . . . . . . . 9 . . . . . . . . . . 10 . . . . . . . . . . Step: 7 1 2 3 4 5 6 7 8 9 10 1 . . . . . . . . . # 2 . . . . . . . . . . 3 . . . . . . . . . . 4 . . . . . . . . . . 5 . . . . . . . . F . 6 . . . . . . . . . . 7 . . . . . . . . . . 8 . . . . . . . . . . 9 . . . . . . . . . . 10 . . . . . . . . . . Rabbit got away 

So once the board is loaded you start running the game or simulation. You see that first you tell me what step you are on. Then you output the board with the actors on it. Despite the fact that C++ arrays are 0 based, we know that must humans are 1 based. So we adjust our output to make it look like the actors are at their 1 based locations.

Each step the rabbit moves 1 cell closer to the hole. Each step the fox, or foxes, will move 1 cell closer to the rabbit.

If the rabbit gets to the hole we will say that and if the foxes get to the rabbit we will say that.

Moving

I say the rabbit moves towards the hole and the foxes move towards the rabbit. What we are really doing is looking at each of the adjacent cells and deciding which cell is closer to the desired actor. The rabbit is trying to get close to the hole and the foxes are trying to get closer to the rabbit.

So we can use the distance formula to determine which cell is closer. If we treat each cell as an x, y coordinate then we can compute how far each cell is to the desired location. Then we can pick the smallest distance. The moving comes in by putting a blank back over the actor in its current cell and moving them into their new cell.

We will consider all 8 directions as possibilities for moving. We will not move off the board, so you have to check for that. We will not moving into an occupied cell, except the rabbit will move into the cell with the hole and the fox will move into the cell with the rabbit.

Once the rabbit has reached the hole, no fox will move. If 1 fox reaches a rabbit, and there are other foxes to move, they will not move.

You must move the foxes in the order they were in the input file. So the first fox in the file moves first. The second fox in the input moves second. etc. This is why we need a second array to store where the foxes are.

Errors

There are 8 errors. They are listed here and if the error occurs the error message listed should be displayed. Once 1 error occurs and its message is output, no other errors or messages are displayed, nor is the game played.

No RABBIT in the input file. Message: ERROR: Rabbit was missing from input file.

RABBIT has bad row or column. Message: ERROR: Rabbit was placed off of board during loading.

RABBIT placed on non empty space. Message: ERROR: Board was not empty where rabbit was placed.

No HOLE in the input file. Message: ERROR: Hole was missing from input file.

Hole has bad row or column. Message: ERROR: Hole was placed off of board during loading.

HOLE placed on non empty space. Message: ERROR: Board was not empty where hole was placed.

No FOX at all in the input file. Message: ERROR: No fox was in input file.

Initial row or column in the input file was bad. Message: ERROR: Row or Column in input file was illegal.

All of these errors occur when you are reading, or parsing, the input file. If any of these errors occur, only report the first error and nothing else is done.

Do not use exit() to end your program or that will end the entire testing program and you will not get a grade, i.e. your grade will be 0.

Constants

Global constants are my best friend. Here are some global constants you are welcome to use. Also this shows the symbols to use for the various actors.

const int WIDTH = 50; const int HEIGHT = 50; const int NUM_FOXES = 25; const char BLANK = '.'; const char RABBIT = 'R'; const char HOLE = 'o'; const char OUT = '#'; const char EATEN = '@'; const char FOX = 'F'; const char BUSH = '*'; 

The first two constants are the maximum size for our rows and columns. Rows is the same as height, and columns is the same as width. You are welcome to rename those if you wish.

The 3rd constants is the maximum number of foxes we will track. You need this because we have to know how large to make the array for the foxes.

The rest of the constants I used to initialize my board, BLANK, and for the actors. The EATEN is when the fox catches the rabbit. The OUT is when the rabbit gets to the hole. The HOLE is the hole before the rabbit gets to it. The other constants are self explanatory, I think, but ask questions if you have any.

Arrays

As I mentioned the arrays are our main focus. I may come back and pop in a video about arrays, but I want to put a few things here.

Our main 2D array, or at least I feel the main array, is the board. Its a square 50x50 array. The top left corner is 0, 0 and the bottom right corner is 49, 49. To when we move down the board, the row numbers increase. When move up the board the row numbers decrease. When we move right the column numbers increase and when we move left the column numbers decrease.

Its important that we all use the same orientation for our arrays or your board may come out rotated.

For the array for the foxes, I recommend either a 25x2, or a 2x25. In C++ the rows always come first. So in the first orientation its like a long column. In the second orientation its a long row.

Row, Column <-- these are the index in the second brackets +-+-+ | | | Fox # <-- this is the index in the first bracket +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+ 

This is orientation 1. So the row is the fox number. Fox 1 would go in row 1. The first column, or column index 0, is the foxs row and the second column, or column index 1, is the foxs column. In this way, foxes[0][0] is the first foxs row and foxes[0][1] is the first foxs column. If we keep track of the number of foxes, we can write a nice for loop to loop over each fox and move them.

Fox # <-- this is the index in the second bracket +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | | | | | | | | | | | | | | | | | | | | | | | | | Row +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | | | | | | | | | | | | | | | | | | | | | | | | | Column +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ^ | | These are the indices in the first bracket 

This is the second orientation. In this orientation the top row would be the foxs row and the bottom row would be the foxes column. So foxes[0][0] would be the foxs row and foxes[1][0] would be the foxes column. Note the difference in the order of indices in the two uses.

It doesnt really matter how you think about the orientation of the foxes array, but this array will help you move the foxes in the right order.

Simulation

In order to make sure we have the same output, its essential that you move in the same order as I do.

After loading if there are no errors while the rabbit is still on the board move the rabbit towards the hole if the rabbit is still on the board move all the foxes towards the rabbit if any of the foxes get the rabbit don't move any more foxes show message about who got who if there are errors show the error message and stop 

So a few things, the rabbit moves first. If the rabbit is still there move the foxes. If a fox gets the rabbit, dont move any more of the foxes.

When deciding on moving, you will choose the cell that is closest to the desired actor. If there is a tie, i.e. two or more cells with the same distance, then you choose the cell that comes first in a clock wise rotation starting with up.

 8 1 2 ^ ^ ^ \|/ 7 <- a -> 3 /|\ v v v 6 5 4 

The drawing above shows the order you choose. So for example

If up and right have the same distance, you would choose up.

If down left and down right have the same distance you would choose down right.

If up right and up left have the same distance you would choose up right.

If the rabbit gets out, heres what you say:

Rabbit got away 

If the foxes get the rabbit, heres what you say:

Foxes got the rabbit 

Requirements

You must declare a function void rabbit( string input, string ouput); in a file named rabbit.h

You must implement your function(s) in a cpp file.

You must use a 50x50 2D array for the board or grid.

You must use a 2D array for the foxes.

You must use functions for the following things:

Loading the board

Moving the rabbit

Moving the foxes

Displaying the board.

You may write other functions as you want.

You may not use classes or structs.

You may not use pointers.

You may not use global variables.

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

Database Systems On GPUs In Databases

Authors: Johns Paul ,Shengliang Lu ,Bingsheng He

1st Edition

1680838482, 978-1680838480

More Books

Students also viewed these Databases questions