Merge pull request #4 from AdmiralEmser/checking-moves
added more movement checks and minor fixes
This commit is contained in:
+219
-15
@@ -33,16 +33,17 @@ void FCC::Chess::startupGame()
|
|||||||
{
|
{
|
||||||
// preparing board: colors
|
// preparing board: colors
|
||||||
for (int8_t i = 0; i < 8; i++) {
|
for (int8_t i = 0; i < 8; i++) {
|
||||||
for (int j = 0; i < 2; i++) {
|
for (uint8_t j = 0; i < 2; i++) {
|
||||||
posColor[i][j] = PiecesColors::BLACK;
|
posColor[i][j] = PiecesColors::BLACK;
|
||||||
}
|
}
|
||||||
for (int j = 6; i < 8; i++) {
|
for (uint8_t j = 6; i < 8; i++) {
|
||||||
posColor[i][j] = PiecesColors::WHITE;
|
posColor[i][j] = PiecesColors::WHITE;
|
||||||
}
|
}
|
||||||
for (int j = 2; i < 7; i++) {
|
for (uint8_t j = 2; i < 7; i++) {
|
||||||
posColor[i][j] = PiecesColors::NONE;
|
posColor[i][j] = PiecesColors::NONE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// preparing board: pieces
|
// preparing board: pieces
|
||||||
for (int8_t i = 0; i < 8; i++) {
|
for (int8_t i = 0; i < 8; i++) {
|
||||||
posPieces[1][i] = Pieces::PAWN;
|
posPieces[1][i] = Pieces::PAWN;
|
||||||
@@ -60,20 +61,18 @@ void FCC::Chess::startupGame()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FCC::Chess::checkIsEnemyOnField(uint8_t* x, uint8_t* y)
|
bool FCC::Chess::checkIsEnemyOnField(uint8_t x, uint8_t y)
|
||||||
{
|
{
|
||||||
// Check if the selected piece/field unequals the target piece/field => is true then
|
// Check if the color of the selected piece/field is the opposite of the target piece/field => is true then
|
||||||
if (posColor[*x][*y] != posColor[selX][selY])
|
if (posColor[x][y] == FCC::PiecesColors::BLACK && posColor[selX][selY] == FCC::PiecesColors::WHITE) return true;
|
||||||
{
|
if (posColor[x][y] == FCC::PiecesColors::WHITE && posColor[selX][selY] == FCC::PiecesColors::BLACK) return true;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FCC::Chess::checkPosInBounds(uint8_t* x, uint8_t* y)
|
bool FCC::Chess::checkIsPosInBounds(uint8_t x, uint8_t y)
|
||||||
{
|
{
|
||||||
// check if X and Y values are below 8. No negative values have to be checked since the var type is unsigned int
|
// check if X and Y values are below 8. No negative values have to be checked since the var type is unsigned int
|
||||||
if (*x <= 7 && *y <= 7)
|
if (x <= 7 && y <= 7)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -105,32 +104,237 @@ void FCC::Chess::checkAllowedMoves()
|
|||||||
|
|
||||||
void FCC::Chess::checkMovesHorizontal()
|
void FCC::Chess::checkMovesHorizontal()
|
||||||
{
|
{
|
||||||
|
// checking positions to the right
|
||||||
for (uint8_t i = selX; i <= 7; i++)
|
for (uint8_t i = selX; i <= 7; i++)
|
||||||
{
|
{
|
||||||
if (checkIsEnemyOnField(&i, &selY)) break;
|
if (checkIsFriendlyOnField(i, selY)) break;
|
||||||
posPossible[i][selY] = true;
|
posPossible[i][selY] = true;
|
||||||
|
if (checkIsEnemyOnField(i, selY)) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checking positions to the left
|
||||||
for (uint8_t i = selX; i >= 0; i--)
|
for (uint8_t i = selX; i >= 0; i--)
|
||||||
{
|
{
|
||||||
if (checkIsEnemyOnField(&i, &selY)) break;
|
if (checkIsFriendlyOnField(i, selY)) break;
|
||||||
posPossible[i][selY] = true;
|
posPossible[i][selY] = true;
|
||||||
|
if (checkIsEnemyOnField(i, selY)) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void FCC::Chess::checkMovesVertical()
|
void FCC::Chess::checkMovesVertical()
|
||||||
{
|
{
|
||||||
|
// checking positions above
|
||||||
for (uint8_t i = selY; i <= 7; i++)
|
for (uint8_t i = selY; i <= 7; i++)
|
||||||
{
|
{
|
||||||
if (checkIsEnemyOnField(&selX, &i)) break;
|
if (checkIsFriendlyOnField(selX, i)) break;
|
||||||
posPossible[selX][i] = true;
|
posPossible[selX][i] = true;
|
||||||
|
if (checkIsEnemyOnField(selX, i)) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checking positions below
|
||||||
for (uint8_t i = selY; i >= 0; i--)
|
for (uint8_t i = selY; i >= 0; i--)
|
||||||
{
|
{
|
||||||
if (checkIsEnemyOnField(&selX, &i)) break;
|
if (checkIsFriendlyOnField(selX, i)) break;
|
||||||
posPossible[selX][i] = true;
|
posPossible[selX][i] = true;
|
||||||
|
if (checkIsEnemyOnField(selX, i)) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FCC::Chess::checkMovesDiagonal()
|
||||||
|
{
|
||||||
|
// checking positions up-right
|
||||||
|
for (uint8_t i = 1; checkIsPosInBounds(selX + i, selY + i); i++)
|
||||||
|
{
|
||||||
|
if (checkIsFriendlyOnField(selX + i, selY + i)) break;
|
||||||
|
posPossible[selX + i][selY + i] = true;
|
||||||
|
if (checkIsEnemyOnField(selX + i, selY + i)) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// checking positions below-right
|
||||||
|
for (uint8_t i = 1; checkIsPosInBounds(selX + i, selY - i); i++)
|
||||||
|
{
|
||||||
|
if (checkIsFriendlyOnField(selX + i, selY - i)) break;
|
||||||
|
posPossible[selX + i][selY - i] = true;
|
||||||
|
if (checkIsEnemyOnField(selX + i, selY - i)) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// checking positions up-left
|
||||||
|
for (uint8_t i = 1; checkIsPosInBounds(selX - i, selY + i); i++)
|
||||||
|
{
|
||||||
|
if (checkIsFriendlyOnField(selX - i, selY + i)) break;
|
||||||
|
posPossible[selX - i][selY + i] = true;
|
||||||
|
if (checkIsEnemyOnField(selX - i, selY + i)) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// checking positions below-left
|
||||||
|
for (uint8_t i = 1; checkIsPosInBounds(selX - i, selY - i); i++)
|
||||||
|
{
|
||||||
|
if (checkIsFriendlyOnField(selX - i, selY - i)) break;
|
||||||
|
posPossible[selX - i][selY - i] = true;
|
||||||
|
if (checkIsEnemyOnField(selX - i, selY - i)) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FCC::Chess::checkMovesKnight()
|
||||||
|
{
|
||||||
|
// checking possible moves
|
||||||
|
if (checkIsPosInBounds(selX + 1, selY + 3))
|
||||||
|
{
|
||||||
|
if (!checkIsFriendlyOnField(selX + 1, selY + 3)) posPossible[selX + 1][selY + 3] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkIsPosInBounds(selX + 1, selY - 3))
|
||||||
|
{
|
||||||
|
if (!checkIsFriendlyOnField(selX + 1, selY - 3)) posPossible[selX + 1][selY - 3] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkIsPosInBounds(selX - 1, selY + 3))
|
||||||
|
{
|
||||||
|
if (!checkIsFriendlyOnField(selX - 1, selY + 3)) posPossible[selX - 1][selY + 3] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkIsPosInBounds(selX - 1, selY - 3))
|
||||||
|
{
|
||||||
|
if (!checkIsFriendlyOnField(selX - 1, selY - 3)) posPossible[selX - 1][selY - 3] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkIsPosInBounds(selX + 3, selY + 1))
|
||||||
|
{
|
||||||
|
if (!checkIsFriendlyOnField(selX + 3, selY + 1)) posPossible[selX + 3][selY + 1] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkIsPosInBounds(selX + 3, selY - 1))
|
||||||
|
{
|
||||||
|
if (!checkIsFriendlyOnField(selX + 3, selY - 1)) posPossible[selX + 3][selY - 1] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkIsPosInBounds(selX - 3, selY + 1))
|
||||||
|
{
|
||||||
|
if (!checkIsFriendlyOnField(selX - 3, selY + 1)) posPossible[selX - 3][selY + 1] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkIsPosInBounds(selX - 3, selY - 1))
|
||||||
|
{
|
||||||
|
if (!checkIsFriendlyOnField(selX - 3, selY - 1)) posPossible[selX - 3][selY - 1] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FCC::Chess::checkMovesKing()
|
||||||
|
{
|
||||||
|
// checking possible moves
|
||||||
|
// rows above and below
|
||||||
|
for (int8_t i = selX - 1; i <= selX + 1; i++)
|
||||||
|
{
|
||||||
|
// row above
|
||||||
|
if (checkIsPosInBounds(i, selY + 1))
|
||||||
|
{
|
||||||
|
if (!checkIsFriendlyOnField(i, selY + 1)) posPossible[i][selY + 1] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// row below
|
||||||
|
if (checkIsPosInBounds(i, selY - 1))
|
||||||
|
{
|
||||||
|
if (!checkIsFriendlyOnField(i, selY - 1)) posPossible[i][selY - 1] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// left
|
||||||
|
if (checkIsPosInBounds(selX - 1, selY))
|
||||||
|
{
|
||||||
|
if (!checkIsFriendlyOnField(selX - 1, selY)) posPossible[selX - 1][selY] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// right
|
||||||
|
if (checkIsPosInBounds(selX + 1, selY))
|
||||||
|
{
|
||||||
|
if (!checkIsFriendlyOnField(selX + 1, selY)) posPossible[selX + 1][selY] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FCC::Chess::checkMovesPawn()
|
||||||
|
{
|
||||||
|
// black pieces
|
||||||
|
if (posColor[selX][selY] == FCC::PiecesColors::BLACK)
|
||||||
|
{
|
||||||
|
// checking possible moves
|
||||||
|
// left and right for enemy pieces
|
||||||
|
for (uint8_t i = selX - 1; i <= selX + 1; i += 2)
|
||||||
|
{
|
||||||
|
if (checkIsPosInBounds(i, selY - 1))
|
||||||
|
{
|
||||||
|
if (checkIsEnemyOnField(i, selY - 1)) posPossible[i][selY - 1] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// standard move to the front
|
||||||
|
if (checkIsPosInBounds(selX, selY - 1))
|
||||||
|
{
|
||||||
|
if (checkIsPositionClear(selX, selY - 1)) posPossible[selX][selY - 1] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// white pieces
|
||||||
|
if (posColor[selX][selY] == FCC::PiecesColors::WHITE)
|
||||||
|
{
|
||||||
|
// checking possible moves
|
||||||
|
// left and right for enemy pieces
|
||||||
|
for (uint8_t i = selX - 1; i <= selX + 1; i += 2)
|
||||||
|
{
|
||||||
|
if (checkIsPosInBounds(i, selY + 1))
|
||||||
|
{
|
||||||
|
if (checkIsEnemyOnField(i, selY + 1)) posPossible[i][selY + 1] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// standard move to the front
|
||||||
|
if (checkIsPosInBounds(selX, selY + 1))
|
||||||
|
{
|
||||||
|
if (checkIsPositionClear(selX, selY + 1)) posPossible[selX][selY + 1] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FCC::Chess::checkIsPawnChangingToQueen()
|
||||||
|
{
|
||||||
|
//black pieces
|
||||||
|
if (posColor[selX][selY] == FCC::PiecesColors::BLACK)
|
||||||
|
{
|
||||||
|
if (selY == 0) return true;
|
||||||
|
} else {
|
||||||
|
// white pieces
|
||||||
|
if (posColor[selX][selY] == FCC::PiecesColors::WHITE)
|
||||||
|
{
|
||||||
|
if (selY == 7) return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// no piece at all
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FCC::Chess::resetAllowedMovement()
|
||||||
|
{
|
||||||
|
for (uint8_t i = 0; i <= 7; i++)
|
||||||
|
{
|
||||||
|
for (uint8_t j = 0; j <= 7; j++)
|
||||||
|
{
|
||||||
|
posPossible[i][j] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FCC::Chess::checkIsPositionClear(uint8_t x, uint8_t y)
|
||||||
|
{
|
||||||
|
if (posColor[x][y] == FCC::PiecesColors::NONE) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FCC::Chess::checkIsFriendlyOnField(uint8_t x, uint8_t y)
|
||||||
|
{
|
||||||
|
if (posColor[x][y] == posColor[selX][selY]) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
//void FCC::Chess::set_lang(FCC::Languages lang) {
|
//void FCC::Chess::set_lang(FCC::Languages lang) {
|
||||||
// m_lang = lang;
|
// m_lang = lang;
|
||||||
//}
|
//}
|
||||||
|
|||||||
+11
-4
@@ -72,10 +72,14 @@ namespace FCC
|
|||||||
//FCC::ChessLang chessLangObj;
|
//FCC::ChessLang chessLangObj;
|
||||||
//FCC::Languages m_lang{ FCC::Languages::ENGLISH };
|
//FCC::Languages m_lang{ FCC::Languages::ENGLISH };
|
||||||
|
|
||||||
// funcs
|
// main funcs
|
||||||
void input();
|
void input();
|
||||||
bool checkIsEnemyOnField(uint8_t* x, uint8_t* y);
|
|
||||||
bool checkPosInBounds(uint8_t* x, uint8_t* y);
|
// check funcs
|
||||||
|
bool checkIsEnemyOnField(uint8_t x, uint8_t y);
|
||||||
|
bool checkIsFriendlyOnField(uint8_t x, uint8_t y);
|
||||||
|
bool checkIsPositionClear(uint8_t x, uint8_t y);
|
||||||
|
bool checkIsPosInBounds(uint8_t x, uint8_t y);
|
||||||
void checkAllowedMoves();
|
void checkAllowedMoves();
|
||||||
void checkMovesHorizontal();
|
void checkMovesHorizontal();
|
||||||
void checkMovesVertical();
|
void checkMovesVertical();
|
||||||
@@ -83,7 +87,10 @@ namespace FCC
|
|||||||
void checkMovesKnight();
|
void checkMovesKnight();
|
||||||
void checkMovesKing();
|
void checkMovesKing();
|
||||||
void checkMovesPawn();
|
void checkMovesPawn();
|
||||||
void checkPosEqualsSelPos(uint8_t* x, uint8_t* y);
|
bool checkIsPawnChangingToQueen();
|
||||||
|
bool checkIsPosEqualsSelPos(uint8_t x, uint8_t y);
|
||||||
|
|
||||||
|
// reset funcs
|
||||||
void resetFieldColors();
|
void resetFieldColors();
|
||||||
void resetAllowedMovement();
|
void resetAllowedMovement();
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user