-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cpp
199 lines (174 loc) · 4.5 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
#include <iostream>
#include <vector>
#include <cctype>
#include "Board.h"
#include "moves.h"
// returns success in storing piece location
Board::Board(const Board& source)
{
for (int i = 0; i < source.numPieces(); ++i)
{
this->pieces.push_back(source.copyPiece(i));
}
this->moveName = source.getMoveName();
}
Piece Board::copyPiece(int i) const
{
return pieces[i];
}
Piece* Board::refByPosition(int pos)
{
for (int i = 0; i < pieces.size(); ++i)
{
if (pieces[i].getPos() == pos)
{
return &pieces[i];
}
}
return 0;
}
std::vector<Piece *> Board::getPieces(int player)
{
std::vector<Piece *> output;
for (int i = 0; i < pieces.size(); ++i)
{
if (pieces[i].getPlayer() == player)
{
//Piece * temp = &pieces[i];
output.push_back(&pieces[i]);
}
}
return output;
}
int Board::numPieces() const
{
return pieces.size();
}
bool Board::move(Piece* moving, int newPos)
{
if (moving->getPos() == newPos) {
// moving to same location is not valid.
return false;
}
bool noFriendlies = checkNoFriendlies(*moving, newPos);
if (!noFriendlies)
{
return false;
}
// move the piece.
moving->setPos(newPos);
deleteEnemyAtPosition(*moving);
return true;
}
/*
tries to delete any pieces at this location loc that
belong to any other players than pl
*/
bool Board::deleteEnemyAtPosition(Piece attacker)
{
for (int i = 0; i < pieces.size(); ++i)
{
if (pieces[i].getPlayer() != attacker.getPlayer())
{
if (pieces[i].getPos() == attacker.getPos())
{
pieces.erase(pieces.begin() + i);
return true;
}
}
}
return false;
}
bool Board::checkNoFriendlies(Piece thisPiece, int newPos)
{
// std::cout << "checkNoFriendlies(): \n";
for (int i = 0; i < pieces.size(); ++i)
{
// std::cout << "i: " << i << ", pieces.size(): " << pieces.size() << "\n";
if (pieces[i].getPlayer() == thisPiece.getPlayer())
{
// std::cout << "found piece of same team\n";
// std::cout << "pieces[i].getPos(): " << pieces[i].getPos() << ", newPos: " << newPos << "\n";
if (pieces[i].getPos() == newPos)
{
return false;
}
}
}
return true;
}
// poorly implemented. next version should find more
// ways to be certain you are getting the correct piece.
// but, this will work for the only thing i need it for, which
// is finding the king.
Piece* Board::findPiece(int player, TYPE type)
{
for (int i = 0; i < pieces.size(); ++i)
{
if (
pieces[i].getPlayer() == player &&
pieces[i].getType() == type
)
return &pieces[i];
}
return 0;
}
void Board::debugListPieces() {
std::cout << "*****locations occupied on board*****\n";
for (int i = 0; i < pieces.size(); ++i)
{
std::cout << "P" << pieces[i].getPlayer() << "-" << (int) pieces[i].getType() << "@" << pieces[i].getPos() << " ";
}
std::cout << "\n\n";
}
std::string Board::displayBoardPieces() {
std::string output;
for (int i = 0; i < pieces.size(); ++i)
{
if (pieces[i].getPlayer() == 0)
output += "W:";
else
output += "B:";
output += getChessPosition(
pieces[i].getType(),
pieces[i].getPos() % 8,
pieces[i].getPos() / 8
);
output += " ";
}
return output;
}
std::string Board::getChessPosition(TYPE t, int x, int y) {
std::string output;
output.push_back(getPieceChar(t));
output.push_back(97 + x);
output.push_back(49 + y);
return output;
}
void Board::printBoard()
{
std::cout << "\n";
for (int y = 7; y >= 0; --y)
{
for (int x = 0; x < 8; ++x)
{
int thisPos = (y*8)+x;
Piece* thisPiece = refByPosition(thisPos);
if (thisPiece != 0)
{
char c = getPieceChar(thisPiece->getType());
if (thisPiece->getPlayer() == 0)
std::cout << (char) tolower(c);
else
std::cout << c;
}
else if (((x % 2) != (y % 2)) == 0)
std::cout << "_";
else
std::cout << "x";
std::cout << " ";
}
std::cout << "\n";
}
std::cout << "\n";
}