-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cpp
288 lines (253 loc) · 6.61 KB
/
Board.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/***************************************************************************************************
* Name: Jessica Albert
* Date: 3/6/19
* Project: Final Project - Board.cpp
* Description: The Board class initializes the linked list of Space objects for the game.
* ************************************************************************************************/
#include "random.hpp"
#include "Board.hpp"
#include "Space.hpp"
#include "Study.hpp"
#include "Project.hpp"
#include "Apply.hpp"
#include <iostream>
/***************************************************************************************************
* The Board class constructor takes the number of each type of space you would like added to the
* (assumg 3 different types of spaces) and the umber of rows and columns of the board. The
* constructor automatically builds the board when it is called.
* ************************************************************************************************/
Board::Board(int numStudy, int numProject, int numApply, int row, int col)
{
this->numSpace1 = numStudy;
this->numSpace2 = numProject;
this->numSpace3 = numApply;
this->numRows = row;
this->numCols = col;
this->head = NULL;
this->study = 0;
this->project = 0;
this->apply = 0;
buildBoard();
}
/***************************************************************************************************
* The destructor traverses the linked spaces in the board and deletes them one at a time. The head
* of the list is moved down to the next row while a pair of Space pointers deleted the spaces
* in the previous row until all the spaces are gone
* ************************************************************************************************/
Board::~Board()
{
Space *ptr = this->head;
Space *garbage = ptr;
while(head != NULL)
{
ptr = head;
garbage = ptr;
if(head->getDown() != NULL)
{
head = head->getDown();
}
else
{
head = NULL;
}
while(ptr != NULL)
{
ptr = ptr->getNext();
delete garbage;
garbage = ptr;
}
}
}
/***************************************************************************************************
* The following are a list of setters and getters for the Board class data members
* ************************************************************************************************/
int Board::getNumSpace1()
{
return this->numSpace1;
}
void Board::setNumSpace1(int numStudy)
{
this->numSpace1 = numStudy;
}
int Board::getNumSpace2()
{
return this->numSpace2;
}
void Board::setNumSpace2(int numProject)
{
this->numSpace2 = numProject;
}
int Board::getNumSpace3()
{
return this->numSpace3;
}
void Board::setNumSpace3(int numApply)
{
this->numSpace3 = numApply;
}
int Board::getNumRows()
{
return this->numRows;
}
void Board::setNumRows(int row)
{
this->numRows = row;
}
int Board::getNumCols()
{
return this->numCols;
}
void Board::setNumCols(int col)
{
this->numCols = col;
}
Space* Board::getHeadPtr()
{
return this->head;
}
/***************************************************************************************************
* This function builds the board of Space objects row by row.
* ************************************************************************************************/
void Board::buildBoard()
{
Space *newHead = NULL;
Space *ptr = NULL;
addBack(newHead);
this->head = newHead;
for(int i = 0; i < (this->numRows - 1); i++)
{
ptr = NULL;
addBack(ptr);
addDown(newHead, ptr);
newHead = ptr;
}
std::cout << "The starting Board is: " <<std::endl;
printBoard();
}
/***************************************************************************************************
* This function adds a new Space object to the end of a list of Space objects. The function
* stops adding Spaces to the end once the number of spaces are equal to the numCols member variable
* ************************************************************************************************/
void Board::addBack(Space *&newHead)
{
Space *ptr = NULL;
Space *subHead = newHead;
bool success = false;
int type = 0;
for(int i = 0; i < this->numCols; i++)
{
type = randomNumber(1,3);
//if the list is empty
if(subHead == NULL)
{
while(success == false)
{
if(this->study < this->numSpace1 && type == 1)
{
subHead = new Study();
this->study++;
success = true;
}
else if(this->project < this->numSpace2 && type == 2 )
{
subHead = new Project();
this->project++;
success = true;
}
else if(this->apply < this->numSpace3 && type == 3)
{
subHead = new Apply();
this->apply++;
success = true;
}
else
{
type = randomNumber(1,3);
}
if(success)
{
newHead = subHead;
}
}
}
else
{
while(success == false)
{
if(this->study < this->numSpace1 && type == 1)
{
ptr = new Study();
this->study++;
success = true;
}
else if(this->project < this->numSpace2 && type == 2)
{
ptr = new Project();
this->project++;
success = true;
}
else if(this->apply < this->numSpace3 && type == 3)
{
ptr = new Apply();
this->apply++;
success = true;
}
else
{
type = randomNumber(1,3);
}
}
subHead->setNext(ptr);
ptr->setPrev(subHead);
subHead = subHead->getNext();
}
success = false;
}
}
/****************************************************************************************************
* The add down function knits two rows of linked Space objects together.
* ************************************************************************************************/
void Board::addDown(Space *top, Space *bottom)
{
Space *ptr1 = top;
Space *ptr2 = bottom;
if(ptr1 != NULL && ptr2 != NULL)
{
ptr1->setDown(ptr2);
ptr2->setUp(ptr1);
for(int i = 0; i < (this->numCols - 1); i++)
{
ptr1 = ptr1->getNext();
ptr2 = ptr2->getNext();
ptr1->setDown(ptr2);
ptr2->setUp(ptr1);
}
}
}
/***************************************************************************************************
* This funciton calls the Space member function getSymbol() to get and print the board of spaces
* ************************************************************************************************/
void Board::printBoard()
{
Space *ptr = this->head;
Space *subHead = this->head;
bool done = false;
do{
for(int c = 0; c < this->numCols; c++)
{
if(ptr != NULL)
{
std::cout << ptr->getSymbol() << " ";
ptr = ptr->getNext();
}
}
if(subHead != NULL)
{
subHead = subHead->getDown();
ptr = subHead;
std::cout<< std::endl;
}
else
done = true;
}while(done == false);
}