-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
291 lines (272 loc) · 5.94 KB
/
Board.cpp
File metadata and controls
291 lines (272 loc) · 5.94 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
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
287
288
289
290
291
#include "Board.h"
#include "Coordinate.h"
using namespace std;
bool Board::checkValidIndex(int index) const
{
return (index >= 0) && (index < m_size);
}
inline bool Board::exists_test(const std::string & name)
{
struct stat buffer;
return (stat(name.c_str(), &buffer) == 0);
}
Board::Board():m_size(0), m_a(nullptr)
{
}
/**
* Constructor
* */
Board::Board(size_t size) : m_size(size)
{
this->m_a = new char[size * size];
for (size_t i = 0; i < m_size; i++)
{
for (size_t j = 0; j < m_size; j++)
{
m_a[i*m_size +j] = Board::param.empty;
}
}
}
Board::Board(const Board & other):m_size(other.m_size)
{
size_t size = other.m_size;
this->m_a = new char[size * size];
for (size_t i = 0; i < m_size*m_size; i++)
{
//copy array
m_a[i] = other.m_a[i];
}
}
/**
* Overloaded assignment operator;
* const return avoid (b1 = b2) = b3
*/
const Board& Board::operator=(const Board& other)
{
if (this != &other) // Avoid self-assignment
{
//for Arrays of different sizes, deallocate original
//left-side Array, then allocate new left-side Array
if (this->m_size != other.m_size)
{
delete[] m_a;// realse space
m_size = other.m_size;//resize this object
m_a = new char[m_size * m_size];//create spcae for array
}
for (size_t i = 0; i < m_size*m_size; i++)
{
//copy array
m_a[i] = other.m_a[i];
}
}
return *this;
}
/**
* Assignment operator overloading
*/
const Board & Board::operator=(const char & c)
{
if (!this->m_a)
{
throw invalid_argument("Board is not initialized");
}
if (c != '.' && c != 'X' && c != 'O')
{
throw IllegalCharException(c, "Invalid Charcter");
}
for (size_t i = 0; i < m_size * m_size; i++)
{
m_a[i] = c;
}
return *this;
}
/**
* subscripting operator overloading
*/
const char& Board::operator[](std::pair<size_t, size_t> index) const
{
size_t indx = index.first * m_size + index.second;
size_t size = m_size * m_size;
// check for subscript out-of-range error
if (indx > size)
{
throw IllegalCoordinateException(index, "Out of range");
}
return this->m_a[indx];
}
Proxy Board::operator[](std::pair<size_t, size_t> index)
{
size_t indx = index.first * m_size + index.second;
// check for subscript out-of-range error
if (!Board::checkValidIndex(index.first) || ! Board::checkValidIndex(index.second))
{
throw IllegalCoordinateException(index, "Out of range");
}
return Proxy(this->m_a[indx]);
}
const size_t Board::size() const
{
return this->m_size;
}
const bool Board::isFull() const
{
for (size_t i = 0; i < m_size; i++)
{
for (size_t j = 0; j < m_size; j++)
{
if ((*this)[{i,j}] == '.')
{
return false;
}
}
}
return true;
}
const bool Board::isEmpty() const
{
for (size_t i = 0; i < m_size; i++)
{
for (size_t j = 0; j < m_size; j++)
{
if ((*this)[{i, j}] != '.')
{
return false;
}
}
}
return true;
}
const bool Board::clear()
{
for (size_t i = 0; i < m_size; i++)
{
for (size_t j = 0; j < m_size; j++)
{
m_a[i*m_size + j] = Board::param.empty;
}
}
return true;
}
/*
* Destructor
*/
Board::~Board()
{
delete[] m_a;
}
ostream & operator<<(ostream & os, const Board & b)
{
string st = "";
for (size_t i = 0; i < b.m_size; i++)
{
for (size_t j = 0; j < b.m_size; j++)
{
os << b.m_a[i*b.m_size + j];
}
os << endl;
}
return os;
}
istream & operator>>(istream & is, Board & b)
{
string st("");
//stringstream ss("");
is >> st;
b = Board(st.size());
for (size_t i = 0; i < b.m_size; i++)
{
for (size_t j = 0; j < b.m_size; j++)
{
char* location = &b.m_a[i * b.m_size + j];
*location = st[j];
if (*location != '.' && *location != 'X' && *location != 'O')
{
throw IllegalCharException(*location, "You explict the faith I gave you :-<");
}
}
if (i != b.size() - 1)
{
is >> st;
}
}
return is;
}
const string Board::draw(const int n) const
{
// Taken from Erel Segal-Halevi git
const int dimx = n, dimy = n;
size_t i = 0;
ostringstream st;
st << "image" << i << ".ppm";
while (exists_test(st.str()))
{
i++;
st.str(std::string());
st << "image" << i << ".ppm";
}
ofstream imageFile(st.str(), ios::out | ios::binary);
imageFile << "P6" << endl << dimx << " " << dimy << endl << 255 << endl;
RGB* image = new RGB[dimx*dimy];
if (image == nullptr)
{
cerr << "error";
}
size_t size = n / m_size;
for (size_t i = 0; i < m_size; i++)
{
for (size_t j = 0; j < m_size; j++)
{
switch (m_a[m_size* i + j])
{
case 'X':
for (size_t k = 0; k < size; k++)
{
for (size_t l = 0; l < size; l++)
{
// The index of the {i,j} in the image is equal to
// {n * size * i , j * size}
// Since each entry has size colums and we pass i entries
// and each entry has size colums and we pass j entries
// so lets note the new location using (i',j')
// we ran from (i' ... i' + size) in each row
// When we go to the next row we pass n entries
// the claim is follows
image[n * size * i + j * size + k * n + l].red = 100; //******//
image[n * size * i + j * size + k * n + l].green = 248;// Green//
image[n * size * i + j * size + k * n + l].blue = 173; //******//
}
}
break;
case 'O':
for (size_t k = 0; k < size; k++)
{
for (size_t l = 0; l < size; l++)
{
image[n * size * i + j * size + k * n + l].red = 200; // ****** //
image[n * size * i + j * size + k * n + l].green = 162;// Purple //
image[n * size * i + j * size + k * n + l].blue = 236; // ****** //
}
}
break;
case '.':
for (size_t k = 0; k < size; k++)
{
for (size_t l = 0; l < size; l++)
{
image[n * size * i + j * size + k * n + l].red = 255; // ******* //
image[n * size * i + j * size + k * n + l].green = 255;// White //
image[n * size * i + j * size + k * n + l].blue = 255; // ******* //
}
}
break;
}
}
}
///
///image processing
///
imageFile.write(reinterpret_cast<char*>(image), 3 * n * n);
imageFile.close();
delete[] image;
return st.str();
}