-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cpp
109 lines (99 loc) · 2.87 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
/**
*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){
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;
}
//----------------------------------------
// 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;
}