-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.cpp
More file actions
136 lines (116 loc) · 2.88 KB
/
Matrix.cpp
File metadata and controls
136 lines (116 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
// Created by moran on 19/11/16.
//
#include <iostream>
#include "Matrix.h"
using namespace std;
/**
* constructor of matrix.
* @param x number of columes.
* @param y number of rows.
* @return nothing.
*/
Matrix::Matrix(int x, int y) {
sizeX = x;
sizeY = y;
createMap();
}
/**
* destructor.
*/
Matrix::~Matrix() {
for (int y = 0; y < sizeY; y++) {
for (int x = 0; x < sizeX; x++) {
delete map[x][y];
}
}
}
/**
* create 2d matrix pointer of Block - the map of the city.
* Initialized the matrix with blocks, Initialized each one location and
* neighbors in the scope of map.
*/
void Matrix::createMap() {
/*
* create 2d matrix by vector container.
* at first create one dimension, after create the 2d.
* note: the matrix Initialized by b variable, all the blocks have the
* same address.
*/
vector<Block *> v(sizeY, NULL);
vector<vector<Block *> > matrix(sizeX, v);
map = matrix;
/**
* Initialized the matrix with block roads and location.
* create block road in each block at matrix, the location if the [x][y]
* values of the matrix. we start with y in oreder to create rows > colums.
*/
for (int y = 0; y < sizeY; y++) {
for (int x = 0; x < sizeX; x++) {
map[x][y] = new Block(Point(x, y));
}
}
/**
* for each block add his neighbors-members (left, up, right, down) in
* the map/matrix scope.
*/
for (int y = 0; y < sizeY; y++) {
for (int x = 0; x < sizeX; x++) {
if (x - 1 >= 0) {
map[x][y]->setLeft(map[x - 1][y]);
}
if (y + 1 < sizeY) {
map[x][y]->setUp(map[x][y + 1]);
}
if (x + 1 < sizeX) {
map[x][y]->setRight(map[x + 1][y]);
}
if (y - 1 >= 0) {
map[x][y]->setDown(map[x][y - 1]);
}
}
}
}
/**
* get number of rows of matrix.
* @return number of rows of matrix.
*/
int Matrix::getRowsSize() {
return this->sizeX;
}
/**
* get number of column of matrix.
* @return number of column of matrix.
*/
int Matrix::getColumnsSize() {
return this->sizeY;
}
/**
* get the map of the city.
* @return map of the city.
*/
vector<vector<Block *> > Matrix::getMap() {
return this->map;
}
/**
* get pointer to block by giving loaction point.
* @param point point - location point.
* @return pointer to the block by givig loaction point.
*/
Block *Matrix::getBlock(Point point) {
return this->map[point.getX()][point.getY()]->getCurrBlock();
}
/**
* set obstacle in the map
* @param num the num of obstacle the user asked
*/
void Matrix::setObstacles(int num) {
int x, y;
char c;
while (num > 0) {
cin >> x >> c >> y;
// getting a point in the format x,y
map[x][y]->setIsObstacle(true);
num--;
}
}