-
Notifications
You must be signed in to change notification settings - Fork 0
/
Square.cpp
141 lines (120 loc) · 2.94 KB
/
Square.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
/*
* Roni Fultheim, ID: 313465965
* Square.cpp
*/
#include <iostream>
#include <stdexcept>
#include "Square.h"
using namespace std;
Square::Square(Color c): status_(c) {}
void Square::switchStatus() {
//using switch case to switch color from black to white and vice versa
switch(status_) {
case BLACK:
status_ = WHITE;
break;
case WHITE:
status_ = BLACK;
break;
default:
// if square color is empty or unassigned, there is a problem
throw logic_error("Logic problem: tried switching an empty square's color\n");
}
}
bool Square::isBlack() const {
//compare, return value of comparison
return (status_ == BLACK);
}
bool Square::isWhite() const {
//compare, return value of comparison
return (status_ == WHITE);
}
bool Square::isEmpty() const {
//compare, return value of comparison
return (status_ == EMPTY);
}
void Square::makeBlack() {
//check that the square isn't colored yet and change color
if (status_ == EMPTY) {
status_ = BLACK;
} else {
// if function is invoked while square is not empty, there is a logical problem in the code
throw logic_error("Logic problem: tried to make a non-empty square black.\n");
}
}
void Square::makeWhite() {
//check that the square isn't colored yet and change color
if (status_ == EMPTY) {
status_ = WHITE;
} else {
// if function is invoked while square is not empty, there is a logical problem in the code
throw logic_error("Logic problem: tried to make a non-empty square white.\n");
}
}
bool Square::isSameColor(const Square &s) const {
switch (status_) {
//return true if squares' colors match, false otherwise
case BLACK:
if (s.isBlack()) {
return true;
}
return false;
case WHITE:
if (s.isWhite()) {
return true;
}
return false;
//an empty square has no color to be comared to
case EMPTY:
return false;
default:
//if this square has no color - this is a problem
cout << "problem comparing color of squares" << endl;
break;
//in case of default
return false;
}
}
bool Square::isSameColor(const Color& c) const {
switch (c) {
//return true if squares' colors match, false otherwise
case BLACK:
if (isBlack()) {
return true;
}
return false;
case WHITE:
if (isWhite()) {
return true;
}
return false;
//an empty square has no color to be comared to
case EMPTY:
return false;
default:
//if this square has no color - this is a problem
cout << "problem comparing color of squares" << endl;
break;
//in case of default
return false;
}
}
std::ostream &operator <<(std::ostream &out, const Square &s) {
//switch case of square's color
switch(s.status_) {
case EMPTY:
out << " ";
break;
case BLACK:
out << "X";
break;
case WHITE:
out << "O";
break;
default:
// if square color is empty or unassigned, there is a problem - print directly to console
cout << endl << "square is of unrecognized color: cannot print" << endl;
break;
}
return out;
}