added checks for possible moves horizontal and vertical; changed some var types

This commit is contained in:
AdmiralEmser
2022-10-09 21:36:07 +02:00
parent 3b565e4d8c
commit b1319c7620
4 changed files with 91 additions and 30 deletions
+63 -22
View File
@@ -34,60 +34,101 @@ 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 (int j = 0; i < 2; i++) {
posColor[i][j] = (uint8_t)(PiecesColors::BLACK); posColor[i][j] = PiecesColors::BLACK;
} }
for (int j = 6; i < 8; i++) { for (int j = 6; i < 8; i++) {
posColor[i][j] = (uint8_t)(PiecesColors::WHITE); posColor[i][j] = PiecesColors::WHITE;
} }
for (int j = 2; i < 7; i++) { for (int j = 2; i < 7; i++) {
posColor[i][j] = (uint8_t)(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] = (uint8_t)(Pieces::PAWN); posPieces[1][i] = Pieces::PAWN;
posPieces[6][i] = (uint8_t)(Pieces::PAWN); posPieces[6][i] = Pieces::PAWN;
} }
for (int8_t i = 0; i < 2; i++) { for (int8_t i = 0; i < 2; i++) {
posPieces[i][0] = (uint8_t)(Pieces::ROOK); posPieces[i][0] = Pieces::ROOK;
posPieces[i][7] = (uint8_t)(Pieces::ROOK); posPieces[i][7] = Pieces::ROOK;
posPieces[i][1] = (uint8_t)(Pieces::KNIGHT); posPieces[i][1] = Pieces::KNIGHT;
posPieces[i][6] = (uint8_t)(Pieces::KNIGHT); posPieces[i][6] = Pieces::KNIGHT;
posPieces[i][2] = (uint8_t)(Pieces::BISHOP); posPieces[i][2] = Pieces::BISHOP;
posPieces[i][5] = (uint8_t)(Pieces::BISHOP); posPieces[i][5] = Pieces::BISHOP;
posPieces[i][3] = (uint8_t)(Pieces::KING); posPieces[i][3] = Pieces::KING;
posPieces[i][4] = (uint8_t)(Pieces::QUEEN); posPieces[i][4] = Pieces::QUEEN;
} }
} }
bool FCC::Chess::checkIsEnemyOnField() 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 selected piece/field unequals the target piece/field => is true then
if (posColor[selX][selY] != posColor[tarX][tarY]) if (posColor[*x][*y] != posColor[selX][selY])
{ {
return true; return true;
} }
return false; return false;
} }
bool FCC::Chess::checkSelectedPosInBounds() bool FCC::Chess::checkPosInBounds(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 (selX <= 7 && selX <= 7) if (*x <= 7 && *y <= 7)
{ {
return true; return true;
} }
return false; return false;
} }
bool FCC::Chess::checkTargetPosInBounds() void FCC::Chess::checkAllowedMoves()
{ {
// check if X and Y values are below 8. No negative values have to be checked since the var type is unsigned int // reset all allowed positions
if (tarX <= 7 && tarX <= 7) for (uint8_t i = 0; i <= 7; i++)
{ {
return true; for (uint8_t j = 0; j <= 7; j++)
{
posPossible[i][j] = false;
}
}
// check piece and then check possible moves accordingly
switch (FCC::Chess::posPieces[FCC::Chess::selX][FCC::Chess::selY]) {
case FCC::Pieces::PAWN:
break;
case FCC::Pieces::ROOK:
break;
default:
tui.errOutput(&lang.errNoPieceOnPosition);
break;
}
}
void FCC::Chess::checkMovesHorizontal()
{
for (uint8_t i = selX; i <= 7; i++)
{
if (checkIsEnemyOnField(&i, &selY)) break;
posPossible[i][selY] = true;
}
for (uint8_t i = selX; i >= 0; i--)
{
if (checkIsEnemyOnField(&i, &selY)) break;
posPossible[i][selY] = true;
}
}
void FCC::Chess::checkMovesVertical()
{
for (uint8_t i = selY; i <= 7; i++)
{
if (checkIsEnemyOnField(&selX, &i)) break;
posPossible[selX][i] = true;
}
for (uint8_t i = selY; i >= 0; i--)
{
if (checkIsEnemyOnField(&selX, &i)) break;
posPossible[selX][i] = true;
} }
return false;
} }
//void FCC::Chess::set_lang(FCC::Languages lang) { //void FCC::Chess::set_lang(FCC::Languages lang) {
+19 -7
View File
@@ -37,6 +37,7 @@ namespace FCC
void startup(); void startup();
void mainLoop(); void mainLoop();
void testLoop(); void testLoop();
void errOutput(std::string* errMessage);
}; };
class Chess class Chess
@@ -47,15 +48,18 @@ namespace FCC
virtual ~Chess() = default; virtual ~Chess() = default;
// vars // vars
uint8_t posPieces[8][8], posColor[8][8], posPossible[8][8], selX, selY, tarX, tarY; //tar = target, sel = selected, pos = possible FCC::Pieces posPieces[8][8];
FCC::PiecesColors posColor[8][8];
bool posPossible[8][8];
uint8_t selX, selY, tarX, tarY; //tar = target, sel = selected, pos = possible
// funcs // funcs
void mainMenu(); void mainMenu();
void startupGame(); void startupGame();
void gameLoop(); void gameLoop();
void checkHorizontalMovementAllowed(); // void checkHorizontalMovementAllowed();
void checkVerticalMovementAllowed(); // void checkVerticalMovementAllowed();
void checkDiagonalMovementAllowed(); // void checkDiagonalMovementAllowed();
//void set_lang(FCC::Languages lang); //void set_lang(FCC::Languages lang);
private: private:
@@ -70,9 +74,17 @@ namespace FCC
// funcs // funcs
void input(); void input();
bool checkIsEnemyOnField(); bool checkIsEnemyOnField(uint8_t* x, uint8_t* y);
bool checkSelectedPosInBounds(); bool checkPosInBounds(uint8_t* x, uint8_t* y);
bool checkTargetPosInBounds(); void checkAllowedMoves();
void checkMovesHorizontal();
void checkMovesVertical();
void checkMovesDiagonal();
void checkMovesKnight();
void checkMovesKing();
void checkMovesPawn();
void checkPosEqualsSelPos(uint8_t* x, uint8_t* y);
void resetFieldColors(); void resetFieldColors();
void resetAllowedMovement();
}; };
} }
+3
View File
@@ -16,5 +16,8 @@ namespace FCC
std::string gameForbiddenMove = "This move is not allowed."; std::string gameForbiddenMove = "This move is not allowed.";
std::string credits = "FoxCreation Chess by SinusFox 2022.\nContact: admiralemser@gmail.com\nhttps://github.com/AdmiralEmser/FC-Chess"; std::string credits = "FoxCreation Chess by SinusFox 2022.\nContact: admiralemser@gmail.com\nhttps://github.com/AdmiralEmser/FC-Chess";
std::string backButton = "Back <--"; std::string backButton = "Back <--";
// error texts
std::string errNoPieceOnPosition = "Error: Empty position.";
}; };
} }
+5
View File
@@ -17,3 +17,8 @@ void FCC::TUI::testLoop()
std::cout << "" << std::endl; std::cout << "" << std::endl;
//std::cout << L"♔"; //std::cout << L"♔";
} }
void FCC::TUI::errOutput(std::string* errMessage)
{
std::cout << *errMessage;
}