removed pointers and updated checkMovesVertical()
This commit is contained in:
+79
-83
@@ -33,16 +33,17 @@ void FCC::Chess::startupGame()
|
||||
{
|
||||
// preparing board: colors
|
||||
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;
|
||||
}
|
||||
for (int j = 6; i < 8; i++) {
|
||||
for (uint8_t j = 6; i < 8; i++) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// preparing board: pieces
|
||||
for (int8_t i = 0; i < 8; i++) {
|
||||
posPieces[1][i] = Pieces::PAWN;
|
||||
@@ -60,18 +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 color of the selected piece/field is the opposite of the target piece/field => is true then
|
||||
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;
|
||||
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 false;
|
||||
}
|
||||
|
||||
bool FCC::Chess::checkIsPosInBounds(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
|
||||
if (*x <= 7 && *y <= 7)
|
||||
if (x <= 7 && y <= 7)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -103,79 +104,74 @@ void FCC::Chess::checkAllowedMoves()
|
||||
|
||||
void FCC::Chess::checkMovesHorizontal()
|
||||
{
|
||||
// checking positions to the right
|
||||
for (uint8_t i = selX; i <= 7; i++)
|
||||
{
|
||||
if (checkIsFriendlyOnField(&i, &selY)) break;
|
||||
if (checkIsFriendlyOnField(i, selY)) break;
|
||||
posPossible[i][selY] = true;
|
||||
if (checkIsEnemyOnField(&i, &selY)) break;
|
||||
if (checkIsEnemyOnField(i, selY)) break;
|
||||
}
|
||||
|
||||
// checking positions to the left
|
||||
for (uint8_t i = selX; i >= 0; i--)
|
||||
{
|
||||
if (checkIsFriendlyOnField(&i, &selY)) break;
|
||||
if (checkIsFriendlyOnField(i, selY)) break;
|
||||
posPossible[i][selY] = true;
|
||||
if (checkIsEnemyOnField(&i, &selY)) break;
|
||||
if (checkIsEnemyOnField(i, selY)) break;
|
||||
}
|
||||
}
|
||||
|
||||
void FCC::Chess::checkMovesVertical()
|
||||
{
|
||||
// checking positions above
|
||||
for (uint8_t i = selY; i <= 7; i++)
|
||||
{
|
||||
if (checkIsFriendlyOnField(&selX, &i)) break;
|
||||
if (checkIsFriendlyOnField(selX, i)) break;
|
||||
posPossible[selX][i] = true;
|
||||
if (checkIsEnemyOnField(&selX, &i)) break;
|
||||
if (checkIsEnemyOnField(selX, i)) break;
|
||||
}
|
||||
|
||||
// checking positions below
|
||||
for (uint8_t i = selY; i >= 0; i--)
|
||||
{
|
||||
if (checkIsFriendlyOnField(&selX, &i)) break;
|
||||
if (checkIsFriendlyOnField(selX, i)) break;
|
||||
posPossible[selX][i] = true;
|
||||
if (checkIsEnemyOnField(&selX, &i)) break;
|
||||
if (checkIsEnemyOnField(selX, i)) break;
|
||||
}
|
||||
}
|
||||
|
||||
void FCC::Chess::checkMovesDiagonal()
|
||||
{
|
||||
// creating variables for x and y
|
||||
uint8_t i = selX, j = selY;
|
||||
|
||||
// checking possible moves
|
||||
for (; i <= 7 && j <= 7;)
|
||||
// checking positions up-right
|
||||
for (uint8_t i = 1; checkIsPosInBounds(selX + i, selY + i); i++)
|
||||
{
|
||||
if (checkIsFriendlyOnField(&i, &j)) break;
|
||||
posPossible[i][j] = true;
|
||||
if (checkIsEnemyOnField(&i, &j)) break;
|
||||
i++;
|
||||
j++;
|
||||
if (checkIsFriendlyOnField(selX + i, selY + i)) break;
|
||||
posPossible[selX + i][selY + i] = true;
|
||||
if (checkIsEnemyOnField(selX + i, selY + i)) break;
|
||||
}
|
||||
|
||||
i = selX, j = selY; // resetting variables
|
||||
for (; i <= 7 && j >= 0;)
|
||||
// checking positions below-right
|
||||
for (uint8_t i = 1; checkIsPosInBounds(selX + i, selY - i); i++)
|
||||
{
|
||||
if (checkIsFriendlyOnField(&i, &j)) break;
|
||||
posPossible[i][j] = true;
|
||||
if (checkIsEnemyOnField(&i, &j)) break;
|
||||
i++;
|
||||
j--;
|
||||
if (checkIsFriendlyOnField(selX + i, selY - i)) break;
|
||||
posPossible[selX + i][selY - i] = true;
|
||||
if (checkIsEnemyOnField(selX + i, selY - i)) break;
|
||||
}
|
||||
|
||||
i = selX, j = selY; // resetting variables
|
||||
for (; i >= 0 && j <= 7;)
|
||||
// checking positions up-left
|
||||
for (uint8_t i = 1; checkIsPosInBounds(selX - i, selY + i); i++)
|
||||
{
|
||||
if (checkIsFriendlyOnField(&i, &j)) break;
|
||||
posPossible[i][j] = true;
|
||||
if (checkIsEnemyOnField(&i, &j)) break;
|
||||
i--;
|
||||
j++;
|
||||
if (checkIsFriendlyOnField(selX - i, selY + i)) break;
|
||||
posPossible[selX - i][selY + i] = true;
|
||||
if (checkIsEnemyOnField(selX - i, selY + i)) break;
|
||||
}
|
||||
|
||||
i = selX, j = selY; // resetting variables
|
||||
for (; i >= 0 && j >= 0;)
|
||||
// checking positions below-left
|
||||
for (uint8_t i = 1; checkIsPosInBounds(selX - i, selY - i); i++)
|
||||
{
|
||||
if (checkIsFriendlyOnField(&i, &j)) break;
|
||||
posPossible[i][j] = true;
|
||||
if (checkIsEnemyOnField(&i, &j)) break;
|
||||
i--;
|
||||
j--;
|
||||
if (checkIsFriendlyOnField(selX - i, selY - i)) break;
|
||||
posPossible[selX - i][selY - i] = true;
|
||||
if (checkIsEnemyOnField(selX - i, selY - i)) break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,58 +182,58 @@ void FCC::Chess::checkMovesKnight()
|
||||
|
||||
// checking possible moves
|
||||
i += 1, j += 3;
|
||||
if (checkIsPosInBounds(&i, &j))
|
||||
if (checkIsPosInBounds(i, j))
|
||||
{
|
||||
if (!checkIsFriendlyOnField(&i, &j)) posPossible[i][j] = true;
|
||||
if (!checkIsFriendlyOnField(i, j)) posPossible[i][j] = true;
|
||||
}
|
||||
|
||||
i = selX, j = selY; // resetting variables
|
||||
i += 1, j -= 3;
|
||||
if (checkIsPosInBounds(&i, &j))
|
||||
if (checkIsPosInBounds(i, j))
|
||||
{
|
||||
if (!checkIsFriendlyOnField(&i, &j)) posPossible[i][j] = true;
|
||||
if (!checkIsFriendlyOnField(i, j)) posPossible[i][j] = true;
|
||||
}
|
||||
|
||||
i = selX, j = selY; // resetting variables
|
||||
i -= 1, j += 3;
|
||||
if (checkIsPosInBounds(&i, &j))
|
||||
if (checkIsPosInBounds(i, j))
|
||||
{
|
||||
if (!checkIsFriendlyOnField(&i, &j)) posPossible[i][j] = true;
|
||||
if (!checkIsFriendlyOnField(i, j)) posPossible[i][j] = true;
|
||||
}
|
||||
|
||||
i = selX, j = selY; // resetting variables
|
||||
i -= 1, j -= 3;
|
||||
if (checkIsPosInBounds(&i, &j))
|
||||
if (checkIsPosInBounds(i, j))
|
||||
{
|
||||
if (!checkIsFriendlyOnField(&i, &j)) posPossible[i][j] = true;
|
||||
if (!checkIsFriendlyOnField(i, j)) posPossible[i][j] = true;
|
||||
}
|
||||
|
||||
i = selX, j = selY; // resetting variables
|
||||
i += 3, j += 1;
|
||||
if (checkIsPosInBounds(&i, &j))
|
||||
if (checkIsPosInBounds(i, j))
|
||||
{
|
||||
if (!checkIsFriendlyOnField(&i, &j)) posPossible[i][j] = true;
|
||||
if (!checkIsFriendlyOnField(i, j)) posPossible[i][j] = true;
|
||||
}
|
||||
|
||||
i = selX, j = selY; // resetting variables
|
||||
i += 3, j -= 1;
|
||||
if (checkIsPosInBounds(&i, &j))
|
||||
if (checkIsPosInBounds(i, j))
|
||||
{
|
||||
if (!checkIsFriendlyOnField(&i, &j)) posPossible[i][j] = true;
|
||||
if (!checkIsFriendlyOnField(i, j)) posPossible[i][j] = true;
|
||||
}
|
||||
|
||||
i = selX, j = selY; // resetting variables
|
||||
i -= 3, j += 1;
|
||||
if (checkIsPosInBounds(&i, &j))
|
||||
if (checkIsPosInBounds(i, j))
|
||||
{
|
||||
if (!checkIsFriendlyOnField(&i, &j)) posPossible[i][j] = true;
|
||||
if (!checkIsFriendlyOnField(i, j)) posPossible[i][j] = true;
|
||||
}
|
||||
|
||||
i = selX, j = selY; // resetting variables
|
||||
i -= 3, j -= 1;
|
||||
if (checkIsPosInBounds(&i, &j))
|
||||
if (checkIsPosInBounds(i, j))
|
||||
{
|
||||
if (!checkIsFriendlyOnField(&i, &j)) posPossible[i][j] = true;
|
||||
if (!checkIsFriendlyOnField(i, j)) posPossible[i][j] = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,31 +246,31 @@ void FCC::Chess::checkMovesKing()
|
||||
i = selX - 1, j = selY + 1;
|
||||
for (; i <= selX + 1; i++)
|
||||
{
|
||||
if (checkIsPosInBounds(&i, &j))
|
||||
if (checkIsPosInBounds(i, j))
|
||||
{
|
||||
if (!checkIsFriendlyOnField(&i, &j)) posPossible[i][j] = true;
|
||||
if (!checkIsFriendlyOnField(i, j)) posPossible[i][j] = true;
|
||||
}
|
||||
}
|
||||
|
||||
i = selX - 1, j = selY - 1;
|
||||
for (; i <= selX + 1; i++)
|
||||
{
|
||||
if (checkIsPosInBounds(&i, &j))
|
||||
if (checkIsPosInBounds(i, j))
|
||||
{
|
||||
if (!checkIsFriendlyOnField(&i, &j)) posPossible[i][j] = true;
|
||||
if (!checkIsFriendlyOnField(i, j)) posPossible[i][j] = true;
|
||||
}
|
||||
}
|
||||
|
||||
i = selX - 1, j = selY;
|
||||
if (checkIsPosInBounds(&i, &j))
|
||||
if (checkIsPosInBounds(i, j))
|
||||
{
|
||||
if (!checkIsFriendlyOnField(&i, &j)) posPossible[i][j] = true;
|
||||
if (!checkIsFriendlyOnField(i, j)) posPossible[i][j] = true;
|
||||
}
|
||||
|
||||
i = selX + 1, j = selY;
|
||||
if (checkIsPosInBounds(&i, &j))
|
||||
if (checkIsPosInBounds(i, j))
|
||||
{
|
||||
if (!checkIsFriendlyOnField(&i, &j)) posPossible[i][j] = true;
|
||||
if (!checkIsFriendlyOnField(i, j)) posPossible[i][j] = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -290,17 +286,17 @@ void FCC::Chess::checkMovesPawn()
|
||||
i = selX - 1, j = selY - 1;
|
||||
for (; i <= selX + 1;)
|
||||
{
|
||||
if (checkIsPosInBounds(&i, &j))
|
||||
if (checkIsPosInBounds(i, j))
|
||||
{
|
||||
if (checkIsEnemyOnField(&i, &j)) posPossible[i][j] = true;
|
||||
if (checkIsEnemyOnField(i, j)) posPossible[i][j] = true;
|
||||
}
|
||||
i += 2;
|
||||
}
|
||||
|
||||
i = selX, j = selY - 1;
|
||||
if (checkIsPosInBounds(&i, &j))
|
||||
if (checkIsPosInBounds(i, j))
|
||||
{
|
||||
if (checkIsPositionClear(&i, &j)) posPossible[i][j] = true;
|
||||
if (checkIsPositionClear(i, j)) posPossible[i][j] = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -314,17 +310,17 @@ void FCC::Chess::checkMovesPawn()
|
||||
i = selX - 1, j = selY + 1;
|
||||
for (; i <= selX + 1;)
|
||||
{
|
||||
if (checkIsPosInBounds(&i, &j))
|
||||
if (checkIsPosInBounds(i, j))
|
||||
{
|
||||
if (checkIsEnemyOnField(&i, &j)) posPossible[i][j] = true;
|
||||
if (checkIsEnemyOnField(i, j)) posPossible[i][j] = true;
|
||||
}
|
||||
i += 2;
|
||||
}
|
||||
|
||||
i = selX, j = selY + 1;
|
||||
if (checkIsPosInBounds(&i, &j))
|
||||
if (checkIsPosInBounds(i, j))
|
||||
{
|
||||
if (checkIsPositionClear(&i, &j)) posPossible[i][j] = true;
|
||||
if (checkIsPositionClear(i, j)) posPossible[i][j] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -360,15 +356,15 @@ void FCC::Chess::resetAllowedMovement()
|
||||
}
|
||||
}
|
||||
|
||||
bool FCC::Chess::checkIsPositionClear(uint8_t* x, uint8_t* y)
|
||||
bool FCC::Chess::checkIsPositionClear(uint8_t x, uint8_t y)
|
||||
{
|
||||
if (posColor[*x][*y] == FCC::PiecesColors::NONE) return true;
|
||||
if (posColor[x][y] == FCC::PiecesColors::NONE) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FCC::Chess::checkIsFriendlyOnField(uint8_t* x, uint8_t* y)
|
||||
bool FCC::Chess::checkIsFriendlyOnField(uint8_t x, uint8_t y)
|
||||
{
|
||||
if (posColor[*x][*y] == posColor[selX][selY]) return true;
|
||||
if (posColor[x][y] == posColor[selX][selY]) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
+5
-5
@@ -76,10 +76,10 @@ namespace FCC
|
||||
void input();
|
||||
|
||||
// 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);
|
||||
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 checkMovesHorizontal();
|
||||
void checkMovesVertical();
|
||||
@@ -88,7 +88,7 @@ namespace FCC
|
||||
void checkMovesKing();
|
||||
void checkMovesPawn();
|
||||
bool checkIsPawnChangingToQueen();
|
||||
bool checkIsPosEqualsSelPos(uint8_t* x, uint8_t* y);
|
||||
bool checkIsPosEqualsSelPos(uint8_t x, uint8_t y);
|
||||
|
||||
// reset funcs
|
||||
void resetFieldColors();
|
||||
|
||||
Reference in New Issue
Block a user