Question: ant.cpp #include #include #include using namespace std; /****************************************************************************** * DO NOT TOUCH THIS CODE * ******************************************************************************/ class Graphics; bool isBlack(vector grid, int row, int col);

ant.cpp

#include  #include  #include  using namespace std; /****************************************************************************** * DO NOT TOUCH THIS CODE * ******************************************************************************/ class Graphics; bool isBlack(vector> grid, int row, int col); void step(vector> &grid, vector> &positions, vector &directions); class Graphics { public: Graphics() { SDL_Init(SDL_INIT_VIDEO); window = SDL_CreateWindow("Ant", 0, 0, 600, 600, SDL_WINDOW_SHOWN); renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); } ~Graphics() { SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); } void render(const vector> &grid, const vector> &positions, const vector &directions) { SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_RenderClear(renderer); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_Rect rect; // Draw black only cells int width = 600 / grid[0].size(); int height = 600 / grid.size(); //draw grid for (int r = 1; r < grid.size(); ++r) { SDL_RenderDrawLine(renderer, 0, r * height, 600, r * height); } for (int c = 1; c < grid[0].size(); ++c) { SDL_RenderDrawLine(renderer, c * width, 0, c * width, 600); } for (int r = 0; r < grid.size(); ++r) { for (int c = 0; c < grid[r].size(); ++c) { if (isBlack(grid, r, c)) { int x = c * width; int y = r * height; rect = {x, y, width, height}; SDL_RenderFillRect(renderer, &rect); } } } //draw ants int i = 0; for (const pair pos : positions) { SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); int x = pos.second * width; int y = pos.first * height; rect = {x + width / 4, y + height / 4, width / 2, height / 2}; SDL_RenderFillRect(renderer, &rect); //draw head SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); switch (directions[i]) { case 'N': rect = {x + width / 4, y + height / 4, width / 2, height / 4}; break; case 'S': rect = {x + width / 4, y + height / 2, width / 2, height / 4}; break; case 'E': rect = {x + width / 2, y + height / 4, width / 4, height / 2}; break; case 'W': rect = {x + width / 4, y + height / 4, width / 4, height / 2}; break; } SDL_RenderFillRect(renderer, &rect); ++i; } SDL_RenderPresent(renderer); } private: SDL_Window* window; SDL_Renderer* renderer; }; /****************************************************************************** * MODIFY CODE BELOW THIS LINE * ******************************************************************************/ int main(int argc, char *argv[]) { int T; //timesteps vector> grid; vector> positions; vector directions; //do input and setup here: //leave this Graphics graphics; graphics.render(grid, positions, directions); for (int t = 0; t < T; ++t) { step(grid, positions, directions); graphics.render(grid, positions, directions); SDL_Delay(100); //render every 100 ms } return 0; } //implement isBlack and step here:

The Seventh Task (5 MeowMeowBeenz)

If you have completed all of the previous tasks, download the file ant.cpp from Moodle and complete the code to create a visualisation of the automaton in action!

Do not modify the code except where instructed. You will need to implement input in the main function and do the initial setup, as well as implement two functions:

1. isBlack(vector

2. step(vector&),whichexecutes one step of the simulation. The 2D vector is the grid, the second parameter lists the posi- tions of the ants, and the final parameter lists the directions the ants are facing.

You will need to have the SDL2 libraries on your system and link your application to them. This will differ from system to system, so please Google how to do this.

Note that I hacked together the visualiser pretty quickly. If you try click or move the screen around, the program will freeze. Sorry.

10.1 Input

The first line of input consists of two integers T and A, separated by a single space. These are the number of steps to simulate, and the number of ants. The next line consists of two integersr and c, separated by a single space. These are the number of rows and columns of the grid. Every cell is initially white. The next A lines each consist of two integers m and n, separated by a single space, specifying the row and column location of a single ant (recall that the ants starts facing north).

10.2 Output

A visualisation of the ants in progress. Look how pretty!The First Task (20 marks)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!