-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
131 lines (119 loc) · 3.32 KB
/
Copy pathBoard.cpp
File metadata and controls
131 lines (119 loc) · 3.32 KB
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
#include "Board.h"
Board::Board(const std::string& matrixStr) : _matrixStr(matrixStr)
{
// This function is also responsible for creating the Players with their kings.
createPieces();
}
Board::~Board()
{
for (int i = 0; i < SIDE_LEN; i++)
{
for (int j = 0; j < SIDE_LEN; j++)
{
delete _pieces[i][j];
}
}
delete _black;
delete _white;
}
void Board::printState() const
{
for (int i = 0; i < SIDE_LEN; i++)
{
for (int j = 0; j < SIDE_LEN; j++)
{
std::cout << _matrixStr[i * SIDE_LEN + j];
}
std::cout << std::endl;
}
}
void Board::move(const Position& src, const Position& dst)
{
_pieces[src.getNumber()][src.getIntLetter()]->move(dst);
if (_pieces[dst.getNumber()][dst.getIntLetter()] != nullptr)
delete _pieces[dst.getNumber()][dst.getIntLetter()];
_pieces[dst.getNumber()][dst.getIntLetter()] = _pieces[src.getNumber()][src.getIntLetter()];
_pieces[src.getNumber()][src.getIntLetter()] = nullptr;
updateMatrixStr(src.translate(), dst.translate());
}
void Board::createPieces()
{
for (unsigned int i = 0; i < _matrixStr.size(); i++)
{
Position p(i);
King* newKing;
switch (tolower(_matrixStr[i]))
{
case 'q':
_pieces[p.getNumber()][p.getIntLetter()] = new Queen(p, _matrixStr[i], this);
break;
case 'k':
newKing = new King(p, _matrixStr[i], this);
_pieces[p.getNumber()][p.getIntLetter()] = newKing;
if (_matrixStr[i] == 'k')
_black = new Player(newKing, "Black");
else
_white = new Player(newKing, "White");
break;
case 'r':
_pieces[p.getNumber()][p.getIntLetter()] = new Rook(p, _matrixStr[i], this);
break;
case 'n':
_pieces[p.getNumber()][p.getIntLetter()] = new Knight(p, _matrixStr[i], this);
break;
case 'p':
_pieces[p.getNumber()][p.getIntLetter()] = new Pawn(p, _matrixStr[i], this);
break;
case 'b':
_pieces[p.getNumber()][p.getIntLetter()] = new Bishop(p, _matrixStr[i], this);
break;
default:
_pieces[p.getNumber()][p.getIntLetter()] = nullptr;
break;
}
}
}
std::string Board::getMatrixStr() const
{
return _matrixStr;
}
Piece* Board::operator[](int index) const
{
Position pos(index);
return (*this)[pos];
}
Player* Board::getPlayer(bool player) const
{
if (player)
return _white;
return _black;
}
void Board::updateMatrixStr(int oldIndex, int newIndex)
{
//Updating the matrix string once a move has been made.
_matrixStr[newIndex] = _matrixStr[oldIndex];
_matrixStr[oldIndex] = '#';
}
Piece* Board::getPiece(const Position& src)
{
Piece* p = _pieces[src.getNumber()][src.getIntLetter()];
_pieces[src.getNumber()][src.getIntLetter()] = nullptr;
return p;
}
Piece* Board::operator[](Position pos) const
{
return _pieces[pos.getNumber()][pos.getIntLetter()];
}
void Board::changePiece(Piece* newPiece, const Position& pos)
{
_pieces[pos.getIntLetter()][pos.getNumber()] = newPiece;
_matrixStr[pos.translate()] = newPiece->getType();
}
void Board::promote(Piece* promotion, const Position& old)
{
_pieces[promotion->getPos().getIntLetter()][promotion->getPos().getNumber()] = promotion;
_pieces[old.getIntLetter()][old.getNumber()] = nullptr;
_matrixStr[promotion->getPos().translate()] = promotion->getType();
_matrixStr[old.translate()] = '#';
delete _pieces[old.getIntLetter()][old.getNumber()];
}