|
@@ -2,12 +2,10 @@
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
-void CheckerBoard::placeChecker(CheckerPosition position, CheckerPiece checker) {
|
|
|
- board[position.x][position.y] = checker;
|
|
|
+CheckerBoard::CheckerBoard() {
|
|
|
+ lastMove = "NONE";
|
|
|
}
|
|
|
|
|
|
-CheckerBoard::CheckerBoard() {}
|
|
|
-
|
|
|
CheckerBoard CheckerBoard::loadFromFile(string filename) {
|
|
|
CheckerBoard board = CheckerBoard();
|
|
|
|
|
@@ -59,7 +57,21 @@ void CheckerBoard::setCheckerAt(CheckerPosition pos, CheckerPiece checker)
|
|
|
void CheckerBoard::moveChecker(CheckerPosition from, CheckerPosition to) {
|
|
|
CheckerPiece checker = getCheckerAt(from);
|
|
|
setCheckerAt(from, CheckerPiece::NONE);
|
|
|
+ int cx = to.x - from.x > 0 ? 1 : -1;
|
|
|
+ int cy = to.y - from.y > 0 ? 1 : -1;
|
|
|
+ int sx = from.x + cx;
|
|
|
+ int sy = from.y + cy;
|
|
|
+
|
|
|
+ while (sx != to.x)
|
|
|
+ {
|
|
|
+ setCheckerAt(CheckerPosition(sx, sy), CheckerPiece::NONE);
|
|
|
+ sx += cx;
|
|
|
+ sy += cy;
|
|
|
+ }
|
|
|
setCheckerAt(to, checker);
|
|
|
+
|
|
|
+ if (checker == CheckerPiece::WHITE && to.y == BOARD_SIZE - 1) setCheckerAt(to, CheckerPiece::WHITE_KING);
|
|
|
+ else if (checker == CheckerPiece::BLACK && to.y == 0) setCheckerAt(to, CheckerPiece::BLACK_KING);
|
|
|
}
|
|
|
|
|
|
void CheckerBoard::print() const
|
|
@@ -148,11 +160,12 @@ void CheckerBoard::generateMovesForPiece(CheckerPosition pos, std::vector<Checke
|
|
|
|
|
|
for (auto& dir : directions) {
|
|
|
CheckerPosition newpos(pos.x + dir[0], pos.y + dir[1]);
|
|
|
+ CheckerPosition cpos(pos.x + 2 * dir[0], pos.y + 2 * dir[1]);
|
|
|
|
|
|
if (isValidMove(pos, newpos, piece)) {
|
|
|
CheckerBoard newBoard = *this;
|
|
|
newBoard.moveChecker(pos, newpos);
|
|
|
- newBoard.move = pos.to_num_string() + " -> " + newpos.to_num_string();
|
|
|
+ newBoard.lastMove = pos.to_num_string() + " -> " + newpos.to_num_string();
|
|
|
newBoard.moveOldPos = pos;
|
|
|
newBoard.moveNewPos = newpos;
|
|
|
|
|
@@ -161,17 +174,41 @@ void CheckerBoard::generateMovesForPiece(CheckerPosition pos, std::vector<Checke
|
|
|
}
|
|
|
|
|
|
moves.push_back(newBoard);
|
|
|
- }
|
|
|
|
|
|
- CheckerPosition cpos(pos.x + 2 * dir[0], pos.y + 2 * dir[1]);
|
|
|
- if (isValidCapture(pos, newpos, cpos, piece)) {
|
|
|
+ CheckerPosition cpos(pos.x + 2 * dir[0], pos.y + 2 * dir[1]);
|
|
|
+ if (isValidCapture(pos, newpos, cpos, piece)) {
|
|
|
+ CheckerBoard newBoard = *this;
|
|
|
+ CheckerPiece checker = newBoard.getCheckerAt(pos);
|
|
|
+
|
|
|
+ newBoard.lastMove = pos.to_num_string() + " -> " + newpos.to_num_string();
|
|
|
+ newBoard.moveOldPos = pos;
|
|
|
+ newBoard.moveNewPos = newpos;
|
|
|
+
|
|
|
+ newBoard.setCheckerAt(newpos, CheckerPiece::NONE);
|
|
|
+ newBoard.setCheckerAt(pos, CheckerPiece::NONE);
|
|
|
+ newBoard.setCheckerAt(cpos, checker);
|
|
|
+
|
|
|
+ if ((piece == WHITE && newpos.y == BOARD_SIZE - 1) || (piece == BLACK && cpos.y == 0)) {
|
|
|
+ newBoard.setCheckerAt(cpos, (piece == WHITE) ? WHITE_KING : BLACK_KING);
|
|
|
+ }
|
|
|
+
|
|
|
+ moves.push_back(newBoard);
|
|
|
+ //generateAdditionalCaptures(cpos, newBoard, moves);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (isValidCapture(pos, newpos, cpos, piece)) {
|
|
|
CheckerBoard newBoard = *this;
|
|
|
CheckerPiece checker = newBoard.getCheckerAt(pos);
|
|
|
+
|
|
|
+ newBoard.lastMove = pos.to_num_string() + " -> " + newpos.to_num_string();
|
|
|
+ newBoard.moveOldPos = pos;
|
|
|
+ newBoard.moveNewPos = newpos;
|
|
|
+
|
|
|
newBoard.setCheckerAt(newpos, CheckerPiece::NONE);
|
|
|
newBoard.setCheckerAt(pos, CheckerPiece::NONE);
|
|
|
newBoard.setCheckerAt(cpos, checker);
|
|
|
|
|
|
- if ((piece == WHITE && newpos.y == BOARD_SIZE-1) || (piece == BLACK && cpos.y == 0)) {
|
|
|
+ if ((piece == WHITE && newpos.y == BOARD_SIZE - 1) || (piece == BLACK && cpos.y == 0)) {
|
|
|
newBoard.setCheckerAt(cpos, (piece == WHITE) ? WHITE_KING : BLACK_KING);
|
|
|
}
|
|
|
|
|
@@ -211,8 +248,8 @@ bool CheckerBoard::isValidMove(CheckerPosition oldPos, CheckerPosition newPos, C
|
|
|
{
|
|
|
CheckerPiece between = getCheckerAt(CheckerPosition(sx, sy));
|
|
|
|
|
|
- if (isWhite && !(between == CheckerPiece::BLACK || between == CheckerPiece::BLACK_KING)) return false;
|
|
|
- if (!isWhite && !(between == CheckerPiece::WHITE || between == CheckerPiece::WHITE_KING)) return true;
|
|
|
+ if (isWhite && !(between == CheckerPiece::BLACK || between == CheckerPiece::BLACK_KING || between == CheckerPiece::NONE)) return false;
|
|
|
+ if (!isWhite && !(between == CheckerPiece::WHITE || between == CheckerPiece::WHITE_KING || between == CheckerPiece::NONE)) return false;
|
|
|
|
|
|
sx += cx;
|
|
|
sy += cy;
|
|
@@ -220,14 +257,20 @@ bool CheckerBoard::isValidMove(CheckerPosition oldPos, CheckerPosition newPos, C
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- if (abs(dx) == 2)
|
|
|
+ if (isWhite && dy == -1) return false;
|
|
|
+ else if (!isWhite && dy == 1) return false;
|
|
|
+ else if (abs(dx) == 2)
|
|
|
{
|
|
|
CheckerPiece between = getCheckerAt(CheckerPosition(oldPos.x + cx, oldPos.y + cy));
|
|
|
+ if (between == CheckerPiece::NONE) return false;
|
|
|
+ if (getCheckerAt(newPos) != CheckerPiece::NONE) return false;
|
|
|
if (isWhite && !(between == CheckerPiece::BLACK || between == CheckerPiece::BLACK_KING)) return false;
|
|
|
- if (!isWhite && !(between == CheckerPiece::WHITE || between == CheckerPiece::WHITE_KING)) return true;
|
|
|
+ if (!isWhite && !(between == CheckerPiece::WHITE || between == CheckerPiece::WHITE_KING)) return false;
|
|
|
}
|
|
|
else if (abs(dx) > 2) return false;
|
|
|
}
|
|
|
+
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
void CheckerBoard::generateAdditionalCaptures(CheckerPosition pos, CheckerBoard& currentBoard, std::vector<CheckerBoard>& moves) const {
|
|
@@ -240,9 +283,6 @@ void CheckerBoard::generateAdditionalCaptures(CheckerPosition pos, CheckerBoard&
|
|
|
if (isValidCapture(pos, npos, cpos, piece)) {
|
|
|
CheckerBoard newBoard = currentBoard;
|
|
|
|
|
|
- wcout << "OldBoard: " << endl;
|
|
|
- newBoard.print();
|
|
|
-
|
|
|
newBoard.setCheckerAt(cpos, newBoard.getCheckerAt(pos));
|
|
|
newBoard.setCheckerAt(pos, CheckerPiece::NONE);
|
|
|
newBoard.setCheckerAt(npos, CheckerPiece::NONE);
|
|
@@ -251,9 +291,6 @@ void CheckerBoard::generateAdditionalCaptures(CheckerPosition pos, CheckerBoard&
|
|
|
newBoard.setCheckerAt(cpos, (piece == WHITE) ? WHITE_KING : BLACK_KING);
|
|
|
}
|
|
|
|
|
|
- wcout << "NewBoard: " << endl;
|
|
|
- newBoard.print();
|
|
|
-
|
|
|
moves.push_back(newBoard);
|
|
|
generateAdditionalCaptures(cpos, newBoard, moves);
|
|
|
}
|
|
@@ -265,6 +302,7 @@ bool CheckerBoard::isValidCapture(CheckerPosition pos, CheckerPosition newPos, C
|
|
|
CheckerPiece newC = getCheckerAt(newPos);
|
|
|
CheckerPiece cC = getCheckerAt(cpos);
|
|
|
if (!cpos.isValid()) return false;
|
|
|
+ if (cC != CheckerPiece::NONE) return false;
|
|
|
if (newC == CheckerPiece::NONE) return false;
|
|
|
|
|
|
if (piece == WHITE && (newC == BLACK || newC == BLACK_KING)) return true;
|