-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextdisplay.cc
176 lines (160 loc) · 5.09 KB
/
textdisplay.cc
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
#include "textdisplay.h"
#include <iostream>
using namespace std;
// abs(int) - Returns the absolute value of a number
int abs(int a)
{
return (a >= 0) ? a : -a;
}
// squareCharDisplay(int, int) - Gets the character to print for an empty square
std::string squareCharDisplay(int c, int r)
{
int sum = c + abs(r - 7);
char ans = (sum % 2 == 0) ? '_' : ' ';
std::string ans1 = "";
return ans1 + ans;
}
// getPieceChar(Piece *) - Gets the character to print in order to represent a piece
std::string getPieceChar(Piece *p, const bool useUnicode) {
if (useUnicode) {
std::string ans = "";
if (p->getColour() == Colour::White) {
// Get the correct character for the type
switch (p->getType())
{
case PieceType::King:
ans = "♔";
break;
case PieceType::Queen:
ans = "♕";
break;
case PieceType::Bishop:
ans = "♗";
break;
case PieceType::Rook:
ans = "♖";
break;
case PieceType::Knight:
ans = "♘";
break;
default:
ans = "♙";
break;
}
}
else {
// Get the correct character for the type
switch (p->getType())
{
case PieceType::King:
ans = "♚";
break;
case PieceType::Queen:
ans = "♛";
break;
case PieceType::Bishop:
ans = "♝";
break;
case PieceType::Rook:
ans = "♜";
break;
case PieceType::Knight:
ans = "♞";
break;
default:
ans = "♟";
break;
}
}
return ans;
}
else {
char ans = ' ';
// Get the correct character for the type
switch (p->getType())
{
case PieceType::King:
ans = 'K';
break;
case PieceType::Queen:
ans = 'Q';
break;
case PieceType::Bishop:
ans = 'B';
break;
case PieceType::Rook:
ans = 'R';
break;
case PieceType::Knight:
ans = 'N';
break;
default:
ans = 'P';
break;
}
// Convert to lowercase if the piece is Black
ans = p->getColour() == Colour::White ? ans : tolower(ans);
std::string ans1 = "";
return ans1 + ans;
}
}
// Constructor for TextDisplay
TextDisplay::TextDisplay(const Board *subject, const bool useUnicode): useUnicode{useUnicode}
{
// Call doRefresh to populate theDisplay and print out the board
doRefresh(subject);
}
// doNotify(pair, Piece *) - Overriden function to update and print the grid
bool TextDisplay::doNotify(Position pos, Piece *p)
{
// Call getPieceChar() to get the appropriate character and update the grid
//! [fixed] make sure you convert row for display purposes
std::string tmp = (p == nullptr ? squareCharDisplay(pos.col, getRowForOutput(pos.row)) : getPieceChar(p, useUnicode));
// If there is a change
if (tmp != theDisplay[pos.col][getRowForOutput(pos.row)]) {
// Update the grid
theDisplay[pos.col][getRowForOutput(pos.row)] = tmp;
return true;
}
return false;
}
void TextDisplay::doDisplay() {
// Print out the updated TextDisplay
cout << *this << endl;
}
// doRefresh - Force refresh everything
void TextDisplay::doRefresh(const Board *subject) {
// Iterate through columns
for (int c = 0; c < NUM_COLS; ++c) {
// Initialize the column to a vector (the row)
theDisplay.push_back(vector<std::string>(NUM_COLS, " "));
// Iterate through the row
for (int r = 0; r < NUM_ROWS; ++r) {
// Call getPieceChar() to get the appropriate character and update the grid
Piece *p = subject->getPieceByCoords(c, r);
theDisplay[c][getRowForOutput(r)] = p == nullptr ? squareCharDisplay(c, getRowForOutput(r)) : getPieceChar(p, useUnicode);
}
}
// Print out the game board
doDisplay();
}
// Destructor for TextDisplay - Doesn't need to do anything
TextDisplay::~TextDisplay() {}
// Output operator for TextDisplay
ostream &operator<<(ostream &out, const TextDisplay &td)
{
// Iterate through columns
char rowDigit = '8';
for (int r = 0; r < NUM_ROWS; ++r)
{
// Print out row
out << rowDigit-- << " ";
for (const auto &col : td.theDisplay) out << col[r];
out << endl;
}
// Print out characters to represent columns
out << endl << " ";
for (char c = 'a'; c <= 'h'; ++c) out << c;
out << endl;
return out; // For chaining
}