-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
166 lines (146 loc) · 4.17 KB
/
Board.cpp
File metadata and controls
166 lines (146 loc) · 4.17 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
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
#include "Board.h"
void Board::PrintGameState() const {
PrintBoardInfo();
PrintBoard();
}
void Board::Clear() {
for (auto& i : board)
i.clear();
board.clear();
boardExpectedValues.rowWidth.clear();
isValid = false;
}
void Board::PrintBoard() const {
int toggle = 1;
int numOfSpace = boardInfo.dimension;
for (int row = 1; row < board.size() - 1; row++) {
numOfSpace -= 1 * toggle;
for (int i = 0; i < numOfSpace; i++)
std::cout << ' ';
if (!numOfSpace)
toggle *= -1;
for (auto& field : board[row])
if (field && field != '+')
std::cout << field << ' ';
std::cout << std::endl;
}
}
void Board::PrintBoardInfo() const {
std::cout << boardInfo.dimension << " ";
std::cout << boardInfo.pawnsToCapture << " ";
std::cout << boardInfo.pawns[WHITE] << " ";
std::cout << boardInfo.pawns[BLACK] << std::endl;
std::cout << boardInfo.reserve[WHITE] << " ";
std::cout << boardInfo.reserve[BLACK] << " ";
std::cout << boardInfo.activePlayer << std::endl;
}
void Board::PrintBoardDev() const {
for (auto& row : board) {
for (auto& field : row)
{
if (field)
std::cout << field << ' ';
else std::cout << 0 << ' ';
}
std::cout << std::endl;
}
}
Board::Position Board::FieldDecoder(const std::string& field) const{
Board::Position position;
int rowOffset = 0;
position.col = field[0] - 'a';
if (position.col > boardInfo.dimension) {
rowOffset = position.col - (boardInfo.dimension);
}
position.row = (field[1] - '0') - boardInfo.dimension - 1 - (field[0] - 'a');
position.row += rowOffset;
position.row *= -1;
return position;
}
void Board::MoveRow(int rowOffset, int colOffset, bool isCorner) {
char pawnToPlace = boardInfo.activePlayer;
for (int i = 0; i < boardInfo.dimension * 2 - (isCorner ? 1 : 2); i++) {
int row = move.tPosition.row + i * rowOffset;
int col = move.tPosition.col + i * colOffset;
char nextPawn = board[row][col];
board[row][col] = pawnToPlace;
if (nextPawn == '_') break;
pawnToPlace = nextPawn;
}
}
void Board::MakeMove() {
--boardInfo.reserve[boardInfo.activePlayer == 'W' ? WHITE : BLACK];
if (move.fPosition.row == 0) {
if (move.fPosition.col == 0)
MoveRow(1, 1, true);
else if (move.fPosition.col == boardInfo.dimension)
MoveRow(1, 0, true);
else {
if (move.tPosition.col == move.fPosition.col)
MoveRow(1, 0, false);
else
MoveRow(1, 1, false);
}
}
else if (move.fPosition.row == boardInfo.dimension) {
if (move.fPosition.col == 0)
MoveRow(0, 1, true);
else if (move.fPosition.col == boardInfo.dimension * 2)
MoveRow(0, -1, true);
}
else if (move.fPosition.row == boardInfo.dimension * 2) {
if (move.fPosition.col == boardInfo.dimension)
MoveRow(-1, 0, true);
else if (move.fPosition.col == boardInfo.dimension * 2)
MoveRow(-1, -1, true);
else {
if (move.tPosition.col == move.fPosition.col)
MoveRow(-1, 0, false);
else
MoveRow(-1, -1, false);
}
}
else {
if (move.fPosition.col < boardInfo.dimension) {
if (move.fPosition.row == move.tPosition.row)
MoveRow(0, 1, false);
else
MoveRow(-1, 0, false);
}
else {
if (move.fPosition.row == move.tPosition.row)
MoveRow(0, -1, false);
else
MoveRow(1, 0, false);
}
}
}
void Board::SetMove(const std::string& from, const std::string& to) {
move.from = from;
move.to = to;
move.fPosition = FieldDecoder(from);
move.tPosition = FieldDecoder(to);
}
void Board::NextPlayer() {
boardInfo.activePlayer = boardInfo.activePlayer == 'W' ? 'B' : 'W';
}
void Board::SetPawnCollectInfo(char color, const std::string& from, const std::string& to) {
if (color == 'w')
move.pawnCollectInfo.color = 'W';
else move.pawnCollectInfo.color = 'B';
move.pawnCollectInfo.from = from;
move.pawnCollectInfo.to = to;
move.pawnCollectInfo.fPosition = FieldDecoder(from);
move.pawnCollectInfo.tPosition = FieldDecoder(to);
}
bool Board::operator==(const Board& other) const {
if (boardInfo.dimension != other.boardInfo.dimension) return false;
for (int row = 1; row < boardInfo.dimension * 2; row++)
for (int col = 1; col < boardInfo.dimension * 2; col++)
if (board[row][col] != other.board[row][col])
return false;
return true;
}
bool Board::operator!=(const Board& other) const {
return !(*this==other);
}