-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBoard.cpp
More file actions
205 lines (170 loc) · 5.61 KB
/
Board.cpp
File metadata and controls
205 lines (170 loc) · 5.61 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
#include "Board.h"
Board::Board(){
this->_size = 0;
this->board = NULL;
}
Board::Board(int size) : _size(size){
this->board = new DerivedChar*[_size];
int i;
for(i = 0; i < _size; i++)
this->board[i] = new DerivedChar[_size];
}
Board::Board(const Board& b){
this->_size = b._size;
this->board = new DerivedChar*[this->_size];
int i, j;
for(i = 0; i < this->_size; i++){
this->board[i] = new DerivedChar[this->_size];
for(j = 0; j < this->_size; j++){
this->board[i][j] = b.board[i][j];
}
}
}
int Board::size() const{
return this->_size;
}
string Board::draw(unsigned int pixels){
milliseconds ms = duration_cast< milliseconds >(
system_clock::now().time_since_epoch()
);
string c_time = to_string(ms.count());
string file_name = "GameXO_" + c_time + ".ppm";
ofstream imageFile(file_name , ios::out | ios::binary);
imageFile << "P6" << endl << pixels <<" " << pixels << endl << 255 << endl;
RGB image[pixels * pixels];
drawOriginalBoard(image, pixels);
drawBoard(image, pixels); // Segmentation fault here
//image processing
imageFile.write(reinterpret_cast<char*>(&image), 3 * pixels * pixels);
imageFile.close();
/* need to free RGB image array */
return file_name;
}
void Board::drawOriginalBoard(RGB image[], unsigned int pixels){
/* fill whole board with white */
unsigned int i, j;
for (i = 0; i < pixels; ++i) { // row
for (j = 0; j < pixels; ++j) { // column
image[i * pixels + j].red = 255;
image[i * pixels + j].green = 255;
image[i * pixels + j].blue = 255;
}
}
/* draw y lines */
for (i = 0; i < pixels; ++i) { // row
for (j = 0; j < pixels; j+=(pixels/_size)) { // column
image[i * pixels + j].red = 0;
image[i * pixels + j].green = 0;
image[i * pixels + j].blue = 0;
}
}
/* draw x lines */
for (i = 0; i < pixels; i+=(pixels/_size)) { // row
for (j = 0; j < pixels; ++j) { // column
image[i * pixels + j].red = 0;
image[i * pixels + j].green = 0;
image[i * pixels + j].blue = 0;
}
}
}
void Board::drawBoard(RGB image[], unsigned int pixels){
unsigned int i, j;
for(i = 0; i < _size; i++){
for(j = 0; j < _size; j++){
if(board[i][j] == Symbol::X)
drawX(image, pixels, i, j);
else if(board[i][j] == Symbol::O)
drawO(image, pixels, i, j);
}
}
}
void Board::drawX(RGB image[], unsigned int pixels, unsigned int index_i, unsigned int index_j){
unsigned int i, k, vertex, row, column, jumps=(pixels/_size);
row = index_i * jumps; //starting row of matrix
column = index_j * jumps; //starting column of matrix
for(k = 0; k < jumps; k++){
vertex = (/*row of the UPPER corners*/row * pixels) + /*column of the LEFT corners*/column + (/*DOWN*/k * pixels) + /*RIGHT*/k;
image[vertex].red = 0;
image[vertex].green = 0;
image[vertex].blue = 0;
}
for(k = 0; k < jumps; k++){
vertex = (/*row of the BOTTOM corners*/(row + jumps) * pixels) + (/*column of the LEFT corners*/column) - (/*UP*/k * pixels) + /*RIGHT*/k;
image[vertex].red = 0;
image[vertex].green = 0;
image[vertex].blue = 0;
}
}
void Board::drawO(RGB image[], unsigned int pixels, unsigned int index_i, unsigned int index_j){
unsigned int jumps=(pixels/_size), row, column, k, j, vertex;
row = index_i * jumps + jumps * 0.25;
column = index_j * jumps + jumps * 0.25;
for(k = 0; k < (jumps/2); k++){
for(j = 0; j < (jumps/2); j++){
vertex = row * pixels + column + j;
image[vertex].red = 205;
image[vertex].green = 92;
image[vertex].blue = 92;
}
row++;
}
}
Board& Board::operator= (const char c){
DerivedChar dc(c);
int i, j;
for(i = 0; i < this->_size; i++)
for(j = 0; j < this->_size; j++)
this->board[i][j] = dc;
return *this;
}
Board& Board::operator= (const Board& b){
this->~Board();
this->_size = b._size;
this->board = new DerivedChar*[this->_size];
int i, j;
for(i = 0; i < this->_size; i++){
this->board[i] = new DerivedChar[this->_size];
for(j = 0; j < this->_size; j++){
this->board[i][j] = b.board[i][j];
}
}
return *this;
}
DerivedChar& Board::operator[] (const Coordinate& c) const{
if(c.getX() < _size && c.getX() >= 0 && c.getY() < _size && c.getY() >= 0)
return board[c.getX()][c.getY()]; //&& board[c.getX()][c.getY()] == Symbol::P
throw IllegalCoordinateException(c);
}
ostream& operator<< (ostream& os, const Board& b){
int i, j;
for(i = 0; i < b._size; i++){
for(j = 0; j < b._size; j++)
os << b.board[i][j];
os << endl;
}
return os;
}
istream& operator>> (istream& is, Board& b){
int len, j, i;
string input;
is >> input;
len = input.length(); // get board's size
Board tmp(len);
for(j = 0; j < len; j++){
tmp.board[0][j] = input[j];
}
for(i = 1; i < len; i++){
is >> input; //get next line
for(j = 0; j < len; j++){
tmp.board[i][j] = input[j];
}
}
b = tmp;
return is;
}
Board::~Board(){
int i;
for(i = 0; i< this->_size; i++)
delete[] board[i]; //This part was allocated with new[], so we use delete[]
delete[] board;
}