-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.cpp
136 lines (112 loc) · 3.46 KB
/
Grid.cpp
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
131
132
133
134
135
136
// a. Michael Bertagna
// b. 2353491
// c. bertagna@chapman.edu
// d. CPSC 350-01
// e. Assignment 3
/* Grid.cpp contains the implementation of the Grid class. It implements a default constructor, two overloaded constructors, a destructor, and four functions:
1) printGrid: prints grid in user friendly format
2) getHeight: gets grid height
3) getWidth: gets grid width
4) isSpecifiedCellOccupied: checks if cell is occupied
5) setCellOccupied: sets grid cell occupied
6) setCellEmpty: sets grid cell empty
7) getGridString: returns string of grid elements
8) getSpecifiedCellData: returns specified cell's data
*/
#include "Grid.h"
//default constructor
Grid::Grid(){}
// overloaded constructor for map file string input: creates grid with specified occupancy
Grid::Grid(int height, int width, string gridStr){
m_height = height;
m_width = width;
//create 2D pointer array of Cells
m_grid = new Cell*[m_height];
for(int i = 0; i < m_height; ++i)
m_grid[i] = new Cell[m_width];
int charCount = 0;
// set specified cells occupied based on map file string
for(int i = 0; i < m_height; ++i){
for(int j = 0; j < m_width; ++j){
if(gridStr[charCount] == 'X')
(m_grid[i][j]).setOccupied();
++charCount;
}
}
}
//overloaded constructor for density input: creates grid with random occupancy based on density
Grid::Grid(int height, int width, double density){
m_height = height;
m_width = width;
//create 2D pointer array of Cells
m_grid = new Cell*[m_height];
for(int i = 0; i < m_height; ++i)
m_grid[i] = new Cell[m_width];
srand(time(0));//use current time as random seed
int numOccupiedCells = density * (m_height * m_width);//calculate number of cells to occupy
int currNumOccupiedCells = 0;
while(currNumOccupiedCells < numOccupiedCells){
int randomHeight = rand() % m_height;//pick random height index
int randomWidth = rand() % m_width;//pick random width index
if(!((m_grid[randomHeight][randomWidth]).isOccupied())){//if cell not occupied, set occupied and increment number of occupied cells
(m_grid[randomHeight][randomWidth]).setOccupied();
++currNumOccupiedCells;
}
}
}
// destructor
Grid::~Grid(){
// delete 2D pointer array of cells
for (int i = 0; i < m_height; ++i){
delete [] m_grid[i];
}
delete [] m_grid;
}
// prints grid in user friendly format
void Grid::printGrid(){
for(int i = 0; i < m_height; ++i){
if(i != 0)
cout << endl;
for(int j = 0; j < m_width; ++j){
cout << (m_grid[i][j]).getValue();
}
}
cout << endl;
}
// gets grid height
int Grid::getHeight(){
return m_height;
}
// gets grid width
int Grid::getWidth(){
return m_width;
}
// checks if cell is occupied
bool Grid::isSpecifiedCellOccupied(int height, int width){
return ((m_grid[height-1][width-1]).isOccupied());
}
// sets grid cell occupied
void Grid::setCellOccupied(int height, int width){
m_grid[height-1][width-1].setOccupied();
}
// sets grid cell empty
void Grid::setCellEmpty(int height, int width){
m_grid[height-1][width-1].setEmpty();
}
// returns string of grid elements
string Grid::getGridString(){
string gridString = "";
for(int i = 0; i < m_height; ++i){
if(i != 0)
gridString += "\n";
for(int j = 0; j < m_width; ++j){
gridString += (m_grid[i][j]).getValue();
}
}
gridString += "\n";
return gridString;
}
// returns specified cell's data
char Grid::getSpecifiedCellData(int height, int width){
return m_grid[height][width].getValue();
}