-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.cpp
More file actions
206 lines (183 loc) · 5.1 KB
/
grid.cpp
File metadata and controls
206 lines (183 loc) · 5.1 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "grid.h"
#include "grid_given.cpp"
// PA1 functions
/**
* Destroys the current Grid. This function should ensure that
* memory does not leak on destruction of a grid.
*/
Grid::~Grid(){ /*your code here*/
clear();
}
/**
* Rotate row r (row 0 is the first) by count positions.
* If row r is ABCDE (left to right) in grid g then row r
* in grid g should be DEABC after the call g.rotateR(r, 2).
* Rotate headOfCol_ if necessary.
*/
void Grid::rotateR(int r, int count) { /* your code here */
r = r % numRows();
Node* currHeadR = headOfRow_[r];
Node* temp = headOfRow_[r];
Node* headUp = headOfRow_[r] -> up;
Node* currheadU = headOfRow_[r]->up;
Node* headDown = headOfRow_[r] -> down;
Node* currheadD = headOfRow_[r]->down;
if (count < 0 || r < 0) return;
for(int countRow = 0; countRow < count; countRow++){
for(int countCol = 0; countCol < numCols(); countCol++){
if(countCol == numCols() - 1){
currheadD = currheadD->right;
currheadU = currheadU->right;
temp->up = headUp;
temp->down = headDown;
currheadD->up = temp;
currheadU->down = temp;
}else{
currheadD = currheadD->right;
currheadU = currheadU->right;
temp->up = currheadU;
temp->down = currheadD;
currheadD->up = temp;
currheadU->down = temp;
temp = temp->right;
}
}
}
for (int numberRotate = 0; numberRotate < count; numberRotate++){
currHeadR = currHeadR->left;
if (r == 0) {
for (int i = 0; i < numCols(); i++) {
headOfCol_[i] = headOfCol_[i]->left;
}
}
}
headOfRow_[r] = currHeadR;
}
/**
* Rotate column c (column 0 is the first) by count positions.
* If column c is ABCDE (top to bottom) in grid g then column c
* in grid g should be DEABC after the call g.rotateC(c, 2).
* Rotate headOfRow_ if necessary.
*/
void Grid::rotateC(int c, int count) { /* your code here */
c = c % numCols();
Node* currHeadC = headOfCol_[c];
Node* tempC = headOfCol_[c];
Node* headLeft = headOfCol_[c] -> left;
Node* currHeadL = headOfCol_[c]->left;
Node* headRight = headOfCol_[c] -> right;
Node* currHeadR = headOfCol_[c]->right;
if (count < 0 || c < 0) return;
for(int countCol = 0; countCol < count; countCol++){
for(int countRow = 0; countRow < numRows(); countRow++){
if(countRow == numRows() - 1){
currHeadL = currHeadL->down;
currHeadR = currHeadR->down;
tempC->left = headLeft;
tempC->right = headRight;
currHeadL->right = tempC;
currHeadR->left = tempC;
}else{
currHeadL = currHeadL->down;
currHeadR = currHeadR->down;
tempC->left = currHeadL;
tempC->right = currHeadR;
currHeadL->right = tempC;
currHeadR->left = tempC;
tempC = tempC->down;
}
}
}
for (int numberRotate = 0; numberRotate < count; numberRotate++){
currHeadC = currHeadC-> up;
if (c == 0) {
for (int i = 0; i < numRows(); i++) {
headOfRow_[i] = headOfRow_[i]->up;
}
}
}
headOfCol_[c] = currHeadC;
}
/**
* Destroys all dynamically allocated memory associated with the
* current Grid class. Clears headOfRow_ and headOfCol_ vectors.
* Sets bwidth_, bheight_ to zero.
* After clear() the grid represents an empty grid.
*/
void Grid::clear() { /*your code here*/
//Node* tempNext;
Node* temp = headOfRow_[0];
Node* tempNext = headOfRow_[0];
for(int countRow = 0; countRow < numRows(); countRow++){
/* Node* temp = headOfRow_[countRow];
Node* tempNext = headOfRow_[countRow];*/
for(int countCol = 0; countCol < numCols(); countCol++){
if (countCol == numCols() - 1 && countRow == numRows() - 1) {
tempNext = NULL;
delete temp;
temp = NULL;
} else
if(countCol == numCols() - 1){
tempNext = temp -> down -> right;
delete temp;
temp = tempNext;
} else{
tempNext = temp->right;
delete temp;
temp = tempNext;
}
}
}
headOfRow_.clear();
headOfCol_.clear();
headOfRow_.resize(0);
headOfCol_.resize(0);
bwidth_ = 0;
bheight_ = 0;
}
/**
* Makes the current Grid a copy of the "other" Grid.
* The dimensions should be the same. The Nodes should
* contain the same blocks, but the Nodes should be newly
* allocated. This function is used in both the copy
* constructor and the assignment operator for Grids.
*/
void Grid::copy(Grid const& other) { /*your code here*/
bwidth_ = other.bwidth_;
bheight_ = other.bheight_;
int nRows = other.numRows();
int nCols = other.numCols();
Node* currTemp = NULL;
vector < vector < Node* > > A;
for (int j = 0; j < nRows; j++) {
vector < Node* > temp;
currTemp = other.headOfRow_[j];
for (int i = 0; i < nCols; i++) {
if (i == 0) {
Node* p = new Node(currTemp->block);
temp.push_back(p);
}
else {
currTemp = currTemp->right;
Node* p = new Node(currTemp->block);
temp.push_back(p);
}
}
A.push_back(temp);
}
for (int j = 0; j < nRows; j++) {
for (int i = 0; i < nCols; i++) {
Node* p = A[j][i];
p->up = A[(j == 0) ? nRows - 1 : j - 1][i];
p->down = A[(j == nRows - 1) ? 0 : j + 1][i];
p->left = A[j][(i == 0) ? nCols - 1 : i - 1];
p->right = A[j][(i == nCols - 1) ? 0 : i + 1];
}
}
for (int j = 0; j < nRows; j++) {
headOfRow_.push_back(A[j][0]);
}
for (int i = 0; i < nCols; i++) {
headOfCol_.push_back(A[0][i]);
}
}