Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help in completing both parts for my homework assignment. (C++ Programming) Main.cpp: #include #include // For time() #include // For srand() and rand() #include

Need help in completing both parts for my homework assignment. (C++ Programming)

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Main.cpp:

#include  #include  // For time() #include  // For srand() and rand() #include  // std::flush #include  #include  #include  using namespace std; //generates random numbers 0 or 1 int coinToss() { return rand() % 2; } //generates random coordinates for the grid int randCoord(int ceiling, int floor) { return floor + rand() % (( ceiling + 1 ) - floor); } //initializes the grid 2D array void GridInit(char grid[25][25]) { for(int i=0; i fabs(h_distance)) { if(v_distance>0) //if distance is positive, move up return 'w'; else return 's'; //otherwise move down } else { if(h_distance>0) return 'a'; else return 'd'; } } //checks if coordinates are invalid or not bool InvalidPos(int x, int y){ if(x ==-1 || y ==-1) return true; else return false; } //checks if capture occurs //If rabbit is on an square next to the fox, or they are in the same square, capture happens bool Capture(int x, int y, char grid[25][25]){ if(grid[x][y] == 'X' || grid[x+1][y] == 'X' || grid[x-1][y] == 'X' || grid[x][y+1] == 'X' || grid[x][y-1] == 'X') return true; else return false; } void UpdateGrid(int rabbitsX[],int rabbitsY[], int foxesX[], int foxesY[], char grid[25][25], int &score, int numRabbits, int numFoxes) { //reset graphics for foxes for(int j=0; j>rabbitsX[i]; myfile>>rabbitsY[i]; } myfile.close(); } else cout 

Foxes.txt:

9 9 10 10 11 11 12 12 13 13

Rabbits.txt:

3 4 14 3 23 24 15 7 7 15 19 20 5 6 15 3 9 10 10 19 14 15 5 6 18 19 5 19 6 3

leaderboard.txt:

Stationary Rabbits Chase Fox Movement 15 
 

Sample Output:

image text in transcribed

image text in transcribed

General Instructions: Read the problem description below and reorganize this program in C++. The files for this assignment are provided under the folder for this assignment. You will be submitting two separate projects. See Part A and Part B for details. o All of the functions making up this program are complete and in working order except for functions marked with "/// FILL THIS FUNCTION". You will need to implement these functions. The major challenge in this assignment is to divide the program into separately compiled modules. You have just earned an internship position at a company developing simulations. You and a team of interns have been tasked with updating a prototype simulation program. The simulation involves five foxes and fifteen rabbits that roam around the grid. If a fox is next to a rabbit, the rabbit is captured and removed from the grid. In the visualization of the grid, the foxes are marked with an X' while rabbits are marked with an 'o'. You have been given unfinished code that includes all functionality within a single file (main.cpp), and severely hinders development as it prevents multiple interns from contributing at the same time. Thus, you have been asked to split the program into four separate modules: The Grid module must contain the files Grid h and Grid.cpp. It deals with code related to printing, updating, and querying characteristics related to grid positions. The Foxes module must contain the files Foxes.h and Foxes.cpp. It mainly deals with the foxes movement. The Rabbits module must contain the files Rabbith and Rabbit.cpp. It mainly deals with the rabbits movements. The Util module must contain the files Util.h and Util.cpp. It includes functions utilized by both the Rabbits and Foxes modules. Capturing A fox can capture a rabbit, if the rabbit is directly above, below, left, right, or in the same square with the fox. This is illustrated in the figure below which shows rabbits that can be captured. RF Random Movement While foxes have already included functionality to chase the rabbits, there is an additional mode f completely random movement that will be used as a base case to evaluate the chase algorithm. Ir this mode, a fox will move at a random direction based on a random number generator, without taking into consideration whether a rabbit is in the vicinity. Simulation Modes 1. Positions from file 1. Positions from file In this mode, the rabbits' and foxes' coordinates will be loaded from the files rabbits.txt and foxes.txt. 2. Random Positions In this mode, the coordinates of the foxes and the rabbits will be randomly generated. 3. Stationary Rabbits In this mode, the rabbits will not roam around the grid, but will instead remain in their initial position 4. Moving Rabbits In this mode, the rabbits move randomly. 5. Random Fox Movement In this mode, the foxes move randomly. 6. Chase Fox Movement In this mode, the foxes move based on a simple chase algorithm, based on which, the fox moves towards the direction of the closest rabbit. Part A: Create a project containing the file 'main.cpp'. Implement the empty functions marked with "///Fill This Function" comments. You will need understand the purpose of these functions in order to implement them. Do this by looking where the functions are being called and how the functions are being used in the program. Also, there may be other functions that perform similar task; these functions may give you a clue how to implement the empty functions. Part B: Create a project containing the files 'main.cpp', 'Rabbits.cpp', 'Rabbits.h', 'Foxes.cpp', 'Foxes.h', 'Grid.cpp', 'Grid.h', 'Util.cpp', 'Util.h'. Split the functions from Part A into the appropriate modules as outlined in the general instructions. The program should compile and have the same input and output as Part A. In order to determine where a function belongs, look to see what functions it calls, and which functions it is called by. Functions which rely heavily on each other are good candidates for a module. Main Menu Launch Import Positions from file Random Positions stationary Rabbits Random Fox Movement Chase Fox Movement ------- ------- ---------------- stationary Pieces :1 Random Fox Movement:1 Random Positions :1 Enter Action: Random Positions File Positions 0 0 Score:5 Turns:9 bcore:1 Turns:0 General Instructions: Read the problem description below and reorganize this program in C++. The files for this assignment are provided under the folder for this assignment. You will be submitting two separate projects. See Part A and Part B for details. o All of the functions making up this program are complete and in working order except for functions marked with "/// FILL THIS FUNCTION". You will need to implement these functions. The major challenge in this assignment is to divide the program into separately compiled modules. You have just earned an internship position at a company developing simulations. You and a team of interns have been tasked with updating a prototype simulation program. The simulation involves five foxes and fifteen rabbits that roam around the grid. If a fox is next to a rabbit, the rabbit is captured and removed from the grid. In the visualization of the grid, the foxes are marked with an X' while rabbits are marked with an 'o'. You have been given unfinished code that includes all functionality within a single file (main.cpp), and severely hinders development as it prevents multiple interns from contributing at the same time. Thus, you have been asked to split the program into four separate modules: The Grid module must contain the files Grid h and Grid.cpp. It deals with code related to printing, updating, and querying characteristics related to grid positions. The Foxes module must contain the files Foxes.h and Foxes.cpp. It mainly deals with the foxes movement. The Rabbits module must contain the files Rabbith and Rabbit.cpp. It mainly deals with the rabbits movements. The Util module must contain the files Util.h and Util.cpp. It includes functions utilized by both the Rabbits and Foxes modules. Capturing A fox can capture a rabbit, if the rabbit is directly above, below, left, right, or in the same square with the fox. This is illustrated in the figure below which shows rabbits that can be captured. RF Random Movement While foxes have already included functionality to chase the rabbits, there is an additional mode f completely random movement that will be used as a base case to evaluate the chase algorithm. Ir this mode, a fox will move at a random direction based on a random number generator, without taking into consideration whether a rabbit is in the vicinity. Simulation Modes 1. Positions from file 1. Positions from file In this mode, the rabbits' and foxes' coordinates will be loaded from the files rabbits.txt and foxes.txt. 2. Random Positions In this mode, the coordinates of the foxes and the rabbits will be randomly generated. 3. Stationary Rabbits In this mode, the rabbits will not roam around the grid, but will instead remain in their initial position 4. Moving Rabbits In this mode, the rabbits move randomly. 5. Random Fox Movement In this mode, the foxes move randomly. 6. Chase Fox Movement In this mode, the foxes move based on a simple chase algorithm, based on which, the fox moves towards the direction of the closest rabbit. Part A: Create a project containing the file 'main.cpp'. Implement the empty functions marked with "///Fill This Function" comments. You will need understand the purpose of these functions in order to implement them. Do this by looking where the functions are being called and how the functions are being used in the program. Also, there may be other functions that perform similar task; these functions may give you a clue how to implement the empty functions. Part B: Create a project containing the files 'main.cpp', 'Rabbits.cpp', 'Rabbits.h', 'Foxes.cpp', 'Foxes.h', 'Grid.cpp', 'Grid.h', 'Util.cpp', 'Util.h'. Split the functions from Part A into the appropriate modules as outlined in the general instructions. The program should compile and have the same input and output as Part A. In order to determine where a function belongs, look to see what functions it calls, and which functions it is called by. Functions which rely heavily on each other are good candidates for a module. Main Menu Launch Import Positions from file Random Positions stationary Rabbits Random Fox Movement Chase Fox Movement ------- ------- ---------------- stationary Pieces :1 Random Fox Movement:1 Random Positions :1 Enter Action: Random Positions File Positions 0 0 Score:5 Turns:9 bcore:1 Turns:0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions