0% found this document useful (0 votes)
51 views6 pages

Maze (C++)

This document contains C++ code defining methods for a Maze class. The Maze class contains methods for: - Setting the size of the maze and initializing empty vectors to represent it - Populating the maze with values from a input vector - Setting start and destination points - Finding a route through the maze using breadth-first search - Marking the route taken on a separate "maze of step counters" grid - Setting bonus points and markings on the solved maze grid The class contains various getter/setter methods and methods for displaying the maze and route grids.

Uploaded by

asdf w
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views6 pages

Maze (C++)

This document contains C++ code defining methods for a Maze class. The Maze class contains methods for: - Setting the size of the maze and initializing empty vectors to represent it - Populating the maze with values from a input vector - Setting start and destination points - Finding a route through the maze using breadth-first search - Marking the route taken on a separate "maze of step counters" grid - Setting bonus points and markings on the solved maze grid The class contains various getter/setter methods and methods for displaying the maze and route grids.

Uploaded by

asdf w
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

#include "Maze.

h"
#include"read_file.h"
#include <iostream>
#include <vector>
#include <queue>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <stdlib.h> /* 亂數相關函數 */
#include <time.h>
using namespace std;
Maze::Maze()
{

}
void Maze::set_r(int row)
{
r = row;
}
void Maze::set_c(int column)
{
r = column;
}
void Maze::set_MAZE_size(int row,int column)
{
r = row;
c = column;
for (int i = 0; i < r; ++i)
{
vector<int> Row;
for (int j = 0; j < c; ++j)
{
Row.push_back(0);
}
maze.push_back(Row);
}
}
void Maze::set_maze(vector<vector<int>>v,int row,int col)
{
for (int ca = 0; ca < row; ca++)
{
for (int cal = 0; cal < col; cal++)
{

maze[ca][cal] = v[ca][cal];
}
}
}
void Maze::set_solved_maze_size(int row, int column)
{
for (int i = 0; i < row; ++i)
{
vector<char> Row;
for (int j = 0; j < column; ++j)
{
Row.push_back(0);
}
solved_maze.push_back(Row);
}
}
void Maze::set_solved_maze(vector<vector<int>>v, int row, int col)
{
for (int ca = 0; ca < row; ca++)
{
for (int cal = 0; cal < col; cal++)
{
if (v[ca][cal] == -1)
{
solved_maze[ca][cal] = 'x';
}
if (v[ca][cal] == 1 && solved_maze[ca][cal] != 'x')
{
solved_maze[ca][cal] = '1';
}
if (v[ca][cal] == 0 && solved_maze[ca][cal] != 'x')
{
solved_maze[ca][cal] = '0';
}

}
}
}
vector<std::vector<int>> Maze::get_maze_of_stepcounter()const
{
return maze_of_stepcounter;
}
int Maze::get_maze(int r, int c)
{
return maze[r][c];
}
void Maze::show_maze()
{
for (int ca = 0; ca < r; ca++)//examination for maze
{
for (int cal = 0; cal < c; cal++)
{
cout << maze[ca][cal] << " ";
}
cout << endl;
}
}
void Maze::show_solved_maze()
{
for (int ca = 0; ca < r; ca++)//examination for maze
{
for (int cal = 0; cal < c; cal++)
{
cout << solved_maze[ca][cal] << " ";
}
cout << endl;
}
}
void Maze::get_r()const
{
cout << r << endl;
}
void Maze::set_gate(int a)
{
gate = a;
}
int Maze::gate = 0;
void Maze::startdestination()
{
for (int ca = 0; ca < r; ca++)
{
for (int cal = 0; cal < c; cal++)
{
if (maze[ca][cal] == 200)
{
start[0] = ca;
start[1] = cal;

}
if (maze[ca][cal] == 201)
{
destination[0] = ca;
destination[1] = cal;
}
}
}
maze[start[0]][start[1]] = 2;
}

int Maze::counter = 1;
void Maze::find_route(int Row,int Col)
{
cout << maze_of_stepcounter[destination[0]][destination[1]] << endl;
int R = Row;
int C = Col;
q1.pop();
q2.pop();
maze[R][C] = 2;
{
if (maze[R][C + 1] == 0)
{
q1.push(R);
q2.push(C + 1);
maze_of_stepcounter[R][C + 1] = maze_of_stepcounter[R][C] + 1;
maze_of_stepcounter[destination[0]][destination[1]] =
maze_of_stepcounter[R][C] + 2;
}
if (maze[R - 1][C] == 0)
{
q1.push(R - 1);
q2.push(C);
maze_of_stepcounter[R - 1][C] = maze_of_stepcounter[R][C] + 1;
maze_of_stepcounter[destination[0]][destination[1]] =
maze_of_stepcounter[R][C] + 2;
}
if (maze[R][C - 1] == 0)
{
q1.push(R);
q2.push(C - 1);
maze_of_stepcounter[R][C - 1] = maze_of_stepcounter[R][C] + 1;
maze_of_stepcounter[destination[0]][destination[1]] =
maze_of_stepcounter[R][C] + 2;
}
if (maze[R + 1][C] == 0)
{
q1.push(R + 1);
q2.push(C);
maze_of_stepcounter[R + 1][C] = maze_of_stepcounter[R][C] + 1;
maze_of_stepcounter[destination[0]][destination[1]] =
maze_of_stepcounter[R][C] + 2;
}
if (!q1.empty())
{
find_route(q1.front(), q2.front());
}
}
}
void Maze::set_q1(int a)
{
q1.push(a);
}
void Maze::set_q2(int a)
{
q2.push(a);
}
int Maze::get_q1()
{
return q1.front();
q1.pop();
}
int Maze::get_q2()
{
return q2.front();
q2.pop();
}
int Maze::get_start(int a)const
{
return start[a];
}
int Maze::get_destination(int a)const
{
return destination[a];
}
void Maze::set_maze_of_stepcounter_size(int row,int column)
{
for (int i = 0; i < row; ++i)
{
vector<int> Row;
for (int j = 0; j < column; ++j)
{
Row.push_back(0);
}
maze_of_stepcounter.push_back(Row);
}
}
void Maze::set_maze_of_stepcounter(vector<vector<int>>v, int row, int col)
{
for (int ca = 0; ca < row; ca++)
{
for (int cal = 0; cal < col; cal++)
{
maze[ca][cal] = v[ca][cal];
}
}
}
void Maze::set_start_step(int a, int b)
{
maze_of_stepcounter[a][b] = 1;
}
void Maze::show_maze_of_stepcounter()
{
for (int ca = 0; ca < r; ca++)//examination for maze
{
for (int cal = 0; cal < c; cal++)
{
cout << maze_of_stepcounter[ca][cal] << " ";
}
cout << endl;
}
}
void Maze::set_road(int a,int b)
{
int R = a;
int C = b;
if (maze_of_stepcounter[a][b] >= 2)
{
if (maze_of_stepcounter[R][C + 1] == maze_of_stepcounter[R][C] - 1)
{
set_road(R, C + 1);
}
else if (maze_of_stepcounter[R - 1][C] == maze_of_stepcounter[R][C] - 1)
{
set_road(R - 1, C);
}
else if (maze_of_stepcounter[R][C - 1] == maze_of_stepcounter[R][C] - 1)
{
set_road(R, C - 1);
}
else if (maze_of_stepcounter[R + 1][C] == maze_of_stepcounter[R][C] - 1)
{
set_road(R + 1, C);
}
}
maze_of_stepcounter[a][b] = -1;
}
void Maze::set_bonus_and_SD(int s1,int s2,int d1, int d2)
{
solved_maze[s1][s2] = 'S';
solved_maze[d1][d2] = 'D';
srand(time(NULL));
for (int ca = 1; ca <= 3; ca++)
{
for (;;)
{
int x = rand() % r;
int y = rand() % c;
if (maze[x][y] == 0 || maze[x][y] == 2 && solved_maze[x][y] !=
'B'&&solved_maze[x][y] != 's'&&solved_maze[x][y] != 'D')
{
solved_maze[x][y] = 'B';
break;
}
}
}
}

You might also like