|
@@ -188,30 +188,46 @@ void CheckerBoard::generateMovesForPiece(CheckerPosition pos, std::vector<Checke
|
|
}
|
|
}
|
|
|
|
|
|
bool CheckerBoard::isValidMove(CheckerPosition oldPos, CheckerPosition newPos, CheckerPiece piece) const {
|
|
bool CheckerBoard::isValidMove(CheckerPosition oldPos, CheckerPosition newPos, CheckerPiece piece) const {
|
|
- //wcout << "Old: " << oldPos.x << ", " << oldPos.y << "; New: " << newPos.x << ", " << newPos.y << "; Piece: " << piece << "; NPiece: " << getCheckerAt(newPos) << endl;
|
|
|
|
- //wcout << "New Pos: " << newPos.to_num_string().c_str() << "; MoveOldPos: " << moveOldPos.to_num_string().c_str() << endl;
|
|
|
|
|
|
+ if (newPos.x < 0 || newPos.x >= BOARD_SIZE) return false;
|
|
|
|
+ if (newPos.y < 0 || newPos.y >= BOARD_SIZE) return false;
|
|
|
|
+
|
|
|
|
+ CheckerPiece at = getCheckerAt(oldPos);
|
|
|
|
+ CheckerPiece atnew = getCheckerAt(newPos);
|
|
|
|
+
|
|
|
|
+ if (at == CheckerPiece::NONE) return false;
|
|
|
|
+ if (atnew != CheckerPiece::NONE) return false;
|
|
|
|
+
|
|
|
|
+ int dx = newPos.x - oldPos.x, dy = newPos.y - oldPos.y;
|
|
|
|
+ int cx = dx > 0 ? 1 : -1, cy = dy > 0 ? 1 : -1;
|
|
|
|
+ bool isWhite = at == CheckerPiece::WHITE || at == CheckerPiece::WHITE_KING;
|
|
|
|
+
|
|
|
|
+ if (abs(dx) != abs(dy) || dx == 0) return false;
|
|
|
|
+
|
|
if (piece == CheckerPiece::WHITE_KING || piece == CheckerPiece::BLACK_KING)
|
|
if (piece == CheckerPiece::WHITE_KING || piece == CheckerPiece::BLACK_KING)
|
|
{
|
|
{
|
|
- if (oldPos.x == newPos.x || oldPos.y == newPos.y) return false;
|
|
|
|
- bool isWhite = piece == CheckerPiece::WHITE_KING;
|
|
|
|
|
|
+ int sx = oldPos.x + cx, sy = oldPos.y + cy;
|
|
|
|
|
|
- int cx = newPos.x > oldPos.x ? 1 : -1;
|
|
|
|
- int cy = newPos.y > oldPos.y ? 1 : -1;
|
|
|
|
- for (int x = oldPos.x + cx; x != newPos.x; x += cx)
|
|
|
|
|
|
+ while (sx != newPos.x || sy != newPos.y)
|
|
{
|
|
{
|
|
- for (int y = oldPos.y + cy; y != newPos.y; y += cy)
|
|
|
|
- {
|
|
|
|
- CheckerPiece other = getCheckerAt(CheckerPosition(x, y));
|
|
|
|
- if (isWhite && (other == CheckerPiece::WHITE || other == CheckerPiece::WHITE_KING)) return false;
|
|
|
|
- else if (!isWhite && (other == CheckerPiece::BLACK || other == CheckerPiece::BLACK_KING)) return false;
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
|
|
+ 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;
|
|
|
|
+
|
|
|
|
+ sx += cx;
|
|
|
|
+ sy += cy;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ if (abs(dx) == 2)
|
|
|
|
+ {
|
|
|
|
+ CheckerPiece between = getCheckerAt(CheckerPosition(oldPos.x + cx, oldPos.y + cy));
|
|
|
|
+ if (isWhite && !(between == CheckerPiece::BLACK || between == CheckerPiece::BLACK_KING)) return false;
|
|
|
|
+ if (!isWhite && !(between == CheckerPiece::WHITE || between == CheckerPiece::WHITE_KING)) return true;
|
|
}
|
|
}
|
|
- return true;
|
|
|
|
|
|
+ else if (abs(dx) > 2) return false;
|
|
}
|
|
}
|
|
- if (!newPos.isValid()) return false;
|
|
|
|
- if (getCheckerAt(newPos) != CheckerPiece::NONE) return false;
|
|
|
|
- return true;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
void CheckerBoard::generateAdditionalCaptures(CheckerPosition pos, CheckerBoard& currentBoard, std::vector<CheckerBoard>& moves) const {
|
|
void CheckerBoard::generateAdditionalCaptures(CheckerPosition pos, CheckerBoard& currentBoard, std::vector<CheckerBoard>& moves) const {
|