Answer To: Do the task 2 of this assignment :https://www.cse.iitd.ac.in/~rijurekha/cop290_2021.html
Swapnil answered on May 11 2021
83540/Code/maze_back.cpp
#include "maze_back.h"
maze_back::maze_back(int si):size(si)
{
srand(time(NULL));
counter = 0;
tab = new int*[size];
for (int i = 0; i < size; i++)tab[i] = new int[size];
}
maze_back::~maze_back()
{
}
void maze_back::fill_the_table()
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
tab[i][j] = 0;
}
}
}
void maze_back::show_the_table()
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
cout << tab[i][j] << " ";
}
cout << endl << endl;
}
}
void maze_back::show_queue()
{
for (int i = 0; i < queue.size(); i++)
{
cout << queue[i] << " ";
if (i % 10 == 0)cout << endl;
}
}
int maze_back::make_maze(int x, int y)
{
history.push_back(cv::Point3i(x, y,0));
int p = rand() % 4;
for (int i = 0; i < 4; i++)
{
p++;
p = p % 4;
if (((x - 1 < 0) && (p == 0)) || ((x + 1 > size - 1) && (p == 1)) || ((y - 1 < 0) && (p == 2)) || ((y + 1>size - 1) && (p == 3)))continue;
if (((p == 0) && (tab[x - 1][y] != 0)) || ((p == 1) && (tab[x + 1][y] != 0)) || ((p == 2) && (tab[x][y - 1] != 0)) || ((p == 3) && (tab[x][y + 1] != 0)))continue;
switch (p)
{
case 0:
{
tab[x][y] = -1;
make_maze(x - 1, y) + 1;
}
break;
case 1:
{
tab[x][y] = -1;
make_maze(x + 1, y) + 1;
}
break;
case 2:
{
tab[x][y] = -1;
make_maze(x, y - 1) + 1;
}
break;
case 3:
{
tab[x][y] = -1;
make_maze(x, y + 1) + 1;
}
break;
default:
{
cout << "something gone wrong" << endl;
}
break;
}
}
tab[x][y] = counter++;
history.push_back(cv::Point3i(x, y, 1));
queue.push_back(cv::Point(x, y));
if (x == 0 && y == 0)
{
cout << "finish" << endl;
return 1;
}
}
int ** maze_back::return_tab()
{
return tab;
}
vector maze_back::return_queue()
{
return queue;
}
vector maze_back::return_history()
{
return history;
}
int maze_back::return_size()
{
return size;
}
83540/Code/maze_back.h
#pragma once
#include
#include
#include
#include
#include
#include "opencv2/imgproc/imgproc.hpp"
#include
using namespace std;
class maze_back
{
public:
maze_back(int si);
~maze_back();
void fill_the_table();
void show_the_table();
void show_queue();
int make_maze(int x, int y);
int** return_tab();
vector return_queue();
vector return_history();
int return_size();
private:
int **tab;
vector queue;
vector history;
int size;
int counter;
};
83540/Code/maze_front.cpp
#include "maze_front.h"
maze_front::maze_front(int **tabbb, vector que, int sizeee, vector his, int marg, bool mod=false):tab(tabbb), queue(que),history(his), size(sizeee),margin(marg),mode(mod)
{
cell_size = (GetDesktopResolution() - 2 * margin-50) / sizeee;
}
maze_front::~maze_front()
{
}
void maze_front::make_maze()
{
make_board();
make_walls();
if (mode) make_walls_step_by_step2();
else make_walls();
imshow("image", board);
}
void maze_front::show_queue()
{
for (int i = 0; i < queue.size(); i++)
{
cout << queue[i] << " ";
if (i % 10 == 0)cout << endl;
}
}
int maze_front::is_neighbour(Point one, Point two)
{
if (one.x - two.x == 0 && one.y - two.y == 1)return 0;
if (one.x - two.x == 0 && one.y - two.y == -1)return 1;
if (one.x - two.x == 1 && one.y - two.y == 0)return 2;
if (one.x - two.x == -1 && one.y - two.y == 0)return 3;
return -1;
}
Mat maze_front::return_board()
{
return board;
}
int maze_front::GetDesktopResolution()
{
RECT desktop;
const HWND hDesktop = GetDesktopWindow();
GetWindowRect(hDesktop, &desktop);
int vertical = desktop.bottom;
return vertical;
}
int maze_front::return_cell_size()
{
return cell_size;
}
void maze_front::make_board()
{
const Scalar white = Scalar(255, 255, 255);
const Scalar black = Scalar(0, 0, 0);
board = Mat(Size(margin * 2 + size*cell_size+1, margin * 2 + size * cell_size+1), CV_8UC3, white);
rectangle(board, Point(margin, margin), Point(board.cols - margin, board.rows - margin), black);
}
void maze_front::make_walls()
{
int i = 1;
Point start;
while (i <= size*size)
{
start = find_position(i);
rectangle(board, start, Point(start.x + cell_size, start.y + cell_size), 0,2);
i++;
}
int j = size*size;
Point present;
while (j > 0)
{
present =find_position(j);
int temp= is_neighbour(queue[j], queue[j - 1]);
if(temp!=-1)
destroy_wall(board,temp, find_position(tab[queue[j].x][queue[j].y]));
else
{
int k = j;
int temp2 = is_neighbour(queue[k], queue[j - 1]);
while (temp2 == -1)
{
k++;
temp2 = is_neighbour(queue[k], queue[j - 1]);
}
destroy_wall(board,temp2, find_position(tab[queue[k].x][queue[k].y]));
}
j--;
}
destroy_wall(board,3, Point(margin + size / 2 * cell_size, margin + (size-1) * cell_size));
destroy_wall(board,2, Point(margin + size/2*cell_size,margin+ 0));
}
void maze_front::make_walls_step_by_step()
{
Mat temp_board = Mat::zeros(board.size(), CV_8UC3);
board.copyTo(temp_board);
namedWindow("Step_by_step");
int i = 1;
Point start;
int j = size*size;
while (j > 0)
{
int temp = is_neighbour(queue[j], queue[j - 1]);
if (temp != -1)
{
destroy_wall(temp_board, temp, find_position(tab[queue[j].x][queue[j].y]));
rectangle(temp_board, Point(queue[j].y * cell_size+1, (queue[j].x*cell_size) +1), Point((queue[j].y + 1) *cell_size - 1, (queue[j].x + 1)* cell_size - 1), Scalar(138, 43, 226), -1);
}
else
{
int k = j;
int temp2 = is_neighbour(queue[k], queue[j - 1]);
while (temp2 == -1)
{
k++;
temp2 = is_neighbour(queue[k], queue[j - 1]);
rectangle(temp_board, Point(queue[k].y * cell_size + 1, (queue[k].x*cell_size + 1)), Point((queue[k].y + 1) *cell_size - 1, (queue[k].x + 1)* cell_size - 1), Scalar(200, 214,48), -1);
imshow("Step_by_step", temp_board);
waitKey(10);
}
rectangle(temp_board, Point(queue[j].y * cell_size + 1, (queue[j].x*cell_size + 1)), Point((queue[j].y + 1) *cell_size - 1, (queue[j].x + 1)* cell_size - 1), Scalar(170, 43, 226), -1);
destroy_wall(temp_board,temp2, find_position(tab[queue[k].x][queue[k].y]));
}
imshow("Step_by_step", temp_board);
waitKey(10);
j--;
}
destroy_wall(temp_board,3, Point(margin + size / 2 * cell_size, margin + (size - 1) * cell_size));
destroy_wall(temp_board,2, Point(margin + size / 2 * cell_size, margin + 0));
}
void maze_front::make_walls_step_by_step2()
{
Mat temp_board = Mat::zeros(board.size(), CV_8UC3);
board.copyTo(temp_board);
int j = 0;
namedWindow("Step_by_step");
while (j < history.size())
{
if (history[j].z == 0)
{
rectangle(temp_board, Point(history[j].y * cell_size + 2, (history[j].x*cell_size) + 2), Point((history[j].y + 1) *cell_size - 2, (history[j].x + 1)* cell_size - 2), Scalar(200, 214, 48), -1);
history[j].z++;
}
else
rectangle(temp_board, Point(history[j].y * cell_size + 2, (history[j].x*cell_size) + 2), Point((history[j].y + 1) *cell_size - 2, (history[j].x + 1)* cell_size - 2), Scalar(138, 43, 226), -1);
imshow("Step_by_step", temp_board);
waitKey(200);
j++;
}
}
void maze_front::destroy_wall(Mat img ,int j,Point present,Scalar color)
{
int thickness = 2;
int wall_to_destroy = j;
switch (wall_to_destroy)
{
case 0:
{
line(img, Point(present.x,present.y+ thickness), Point(present.x, present.y + cell_size- thickness), color, thickness);
}
break;
case 1:
{
line(img, Point(present.x + cell_size, present.y+ thickness), Point(present.x + cell_size, present.y + cell_size- thickness), color, thickness);
}
break;
case 2:
{
line(img,Point(present.x+ thickness,present.y), Point(present.x + cell_size- thickness, present.y), color, thickness);
}
break;
case 3:
{
line(img, Point(present.x- thickness + cell_size, present.y + cell_size), Point(present.x+ thickness, present.y + cell_size), color, thickness);
}
break;
}
}
Point maze_front::find_position(int x)
{
Point max;
for (int i = 0; i < size; i++)
{
for...