-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cpp
215 lines (206 loc) · 7.36 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
/**
*cpp file of class Board
*Authors Alexey Titov and Shir Bentabou
*Version 1.0
**/
//library
#include "Board.h"
//Constructor that receives an int that defines the board size wanted
Board::Board(int b){
this->bound=b;
this->board=new Cell*[b];
for (int i=0; i<b; i++){
this->board[i]=new Cell[b];
}
for (int i=0; i<this->bound; i++){
for (int j=0; j<this->bound;j++){
this->board[i][j].setValue('.');
}
}
}
//A copy constructor that receives a Board object and makes a deep copy to another Board object.
Board::Board(Board& copy){
this->bound=copy.bound;
this->board=new Cell*[this->bound];
for (int i=0; i<this->bound; i++){
this->board[i]=new Cell[this->bound];
}
for (int i=0; i<this->bound; i++){
for (int j=0; j<this->bound;j++){
this->board[i][j]=copy.board[i][j];
}
}
}
//This function helps to delete a Board class object (used in distructor and '=' operator function).
void Board::delBoard(){
for (int i=0; i<this->bound; i++){
delete[] this->board[i];
}
delete[] this->board;
}
//Distructor for Board object - deletes everything we allocated memory for.
Board::~Board(){
delBoard();
}
//Operator [] overloading for Board class - for a specific cell inside the board.
Cell& Board::operator[](list<int> lst){
int r=lst.front(), c=lst.back();
if (r<this->bound && c<this->bound){
return this->board[r][c];
}
else{
IllegalCoordinateException ce(r,c);
throw ce;
}
}
//Operator '=' overloading for Board class. Inserts value to whole board if '.'. Else throws exception.
Board& Board::operator=(char c){
if (c=='.'){
for (int i=0; i<this->bound; i++){
for (int j=0; j<this->bound;j++){
this->board[i][j].setValue(c);
}
}
return *this;
}
else{
IllegalCharException ce;
ce.setCh(c);
throw ce;
}
}
//Operator '=' overloading for Board class. Copies another Board object.
Board& Board::operator=(Board& copy){
if (this->bound!=0)
delBoard();
this->bound=copy.bound;
this->board=new Cell*[this->bound];
for (int i=0; i<this->bound; i++){
this->board[i]=new Cell[this->bound];
}
for (int i=0; i<this->bound; i++){
for (int j=0; j<this->bound;j++){
this->board[i][j]=copy.board[i][j];
}
}
return *this;
}
//Draw function for Board class. Draws the game board.
const string Board::draw(int n){
//dimentions for picture
const int dimx = n, dimy = n;
int cell_size = n / this->bound; //defining cell size in board
int grid_width = cell_size / 10; //defining grid width in board
int r_c,g_c,b_c,r_cS,g_cS,b_cS; //variables for RGB
//name for text file
string filename="board"+to_string(this->bound)+".ppm";
ofstream imageFile(filename, ios::out | ios::binary);
imageFile << "P6" << endl << dimx <<" " << dimy << endl << 255 << endl;
RGB image[dimx*dimy];
//Background for grid
for (int j = 0; j < dimy; ++j) { // row
for (int i = 0; i < dimx; ++i) { // column
//0, 172, 230 - light blue for grid
image[dimx*j+i].red = (0);
image[dimx*j+i].green = (172);
image[dimx*j+i].blue = (230);
}
}
//Painitng the board acoording to '.' or 'X' or 'O' in the original game board
for (int m=0; m<this->bound; m++)
for (int k=0; k<this->bound; k++){
char c = this->board[m][k].getValue();
if (c=='O'){
r_c=51; g_c=255; b_c=51;
double centre_y=(m*cell_size+grid_width)/2+(m*cell_size+cell_size-grid_width-1)/2,
centre_x=(k*cell_size+grid_width)/2+(k*cell_size+cell_size-grid_width-1)/2,
radius=cell_size/2-2*grid_width,
d;
for (int j = m*cell_size+grid_width; j < m*cell_size+cell_size-grid_width; ++j) { // row
for (int i = k*cell_size+grid_width; i < k*cell_size+cell_size-grid_width; ++i) { // column
d=pow(centre_x-i,2)+pow(centre_y-j,2);
d=sqrt (d);
d=abs(d-radius);
if(d<10){
image[dimx*j+i].red = (255);
image[dimx*j+i].green = (255);
image[dimx*j+i].blue = (153);
}else{
image[dimx*j+i].red = (r_c);
image[dimx*j+i].green = (g_c);
image[dimx*j+i].blue = (b_c);
}
}
}
}else{
if (c=='.'){
r_c=g_c=b_c=242;
r_cS=g_cS=b_cS=242;
}else{
r_c=255; g_c=102; b_c=102;
r_cS=184; g_cS=0; b_cS=230;
}
int left=k*cell_size+grid_width,right=k*cell_size+cell_size-grid_width-1;
int row=0;
//Coloring everything at once
for (int j = m*cell_size+grid_width; j < m*cell_size+cell_size-grid_width; ++j) { // row
for (int i = k*cell_size+grid_width; i < k*cell_size+cell_size-grid_width; ++i) { // column
image[dimx*j+i].red = (r_c);
image[dimx*j+i].green = (g_c);
image[dimx*j+i].blue = (b_c);
}
//left
image[dimx*j+left+row].red = (r_cS);
image[dimx*j+left+row].green = (g_cS);
image[dimx*j+left+row].blue = (230);
//right
image[dimx*j+right-row].red = (r_cS);
image[dimx*j+right-row].green = (g_cS);
image[dimx*j+right-row].blue = (b_cS);
row++;
}
}
}
//image processing
imageFile.write(reinterpret_cast<char*>(&image), 3*dimx*dimy);
imageFile.close();
return filename;
}
//----------------------------------------
// friend global IO operators
//----------------------------------------
//Operator '<<' overlaoding for board class.
ostream& operator<< (ostream& os, const Board& b){
for (int i=0; i<b.bound; i++){
for (int j=0; j<b.bound;j++){
os<<b.board[i][j].getValue();
}
os <<endl;
}
return os;
}
//Operator '>>' overlaoding for board class.
istream& operator>> (istream& is,Board& b) {
string line;
int size;
is>>line;
size=(int)line.length();
Board tmp(size);
b=tmp;
for (int i=0; i<size; i++){
//if (line[i]=='X' || line[i]=='O' || line[i]=='.')
b[{0, i}] = line[i]; //needs to be checked
//else
//size--;
}
//tmp.bound=size;
int cur_line = 0;
while(is>>line){
cur_line++;
for (int i=0; i<size && cur_line<size; i++){
//if (line[i]=='X' || line[i]=='O' || line[i]=='.')
b[{cur_line, i}] = line[i]; //needs to be checked
}
}
return is;
}