added checks for possible moves horizontal and vertical; changed some var types
This commit is contained in:
+63
-22
@@ -34,60 +34,101 @@ void FCC::Chess::startupGame()
|
||||
// preparing board: colors
|
||||
for (int8_t i = 0; i < 8; 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++) {
|
||||
posColor[i][j] = (uint8_t)(PiecesColors::WHITE);
|
||||
posColor[i][j] = PiecesColors::WHITE;
|
||||
}
|
||||
for (int j = 2; i < 7; i++) {
|
||||
posColor[i][j] = (uint8_t)(PiecesColors::NONE);
|
||||
posColor[i][j] = PiecesColors::NONE;
|
||||
}
|
||||
}
|
||||
// preparing board: pieces
|
||||
for (int8_t i = 0; i < 8; i++) {
|
||||
posPieces[1][i] = (uint8_t)(Pieces::PAWN);
|
||||
posPieces[6][i] = (uint8_t)(Pieces::PAWN);
|
||||
posPieces[1][i] = Pieces::PAWN;
|
||||
posPieces[6][i] = Pieces::PAWN;
|
||||
}
|
||||
for (int8_t i = 0; i < 2; i++) {
|
||||
posPieces[i][0] = (uint8_t)(Pieces::ROOK);
|
||||
posPieces[i][7] = (uint8_t)(Pieces::ROOK);
|
||||
posPieces[i][1] = (uint8_t)(Pieces::KNIGHT);
|
||||
posPieces[i][6] = (uint8_t)(Pieces::KNIGHT);
|
||||
posPieces[i][2] = (uint8_t)(Pieces::BISHOP);
|
||||
posPieces[i][5] = (uint8_t)(Pieces::BISHOP);
|
||||
posPieces[i][3] = (uint8_t)(Pieces::KING);
|
||||
posPieces[i][4] = (uint8_t)(Pieces::QUEEN);
|
||||
posPieces[i][0] = Pieces::ROOK;
|
||||
posPieces[i][7] = Pieces::ROOK;
|
||||
posPieces[i][1] = Pieces::KNIGHT;
|
||||
posPieces[i][6] = Pieces::KNIGHT;
|
||||
posPieces[i][2] = Pieces::BISHOP;
|
||||
posPieces[i][5] = Pieces::BISHOP;
|
||||
posPieces[i][3] = Pieces::KING;
|
||||
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
|
||||
if (posColor[selX][selY] != posColor[tarX][tarY])
|
||||
if (posColor[*x][*y] != posColor[selX][selY])
|
||||
{
|
||||
return true;
|
||||
}
|
||||
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
|
||||
if (selX <= 7 && selX <= 7)
|
||||
if (*x <= 7 && *y <= 7)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
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
|
||||
if (tarX <= 7 && tarX <= 7)
|
||||
// reset all allowed positions
|
||||
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) {
|
||||
|
||||
+19
-7
@@ -37,6 +37,7 @@ namespace FCC
|
||||
void startup();
|
||||
void mainLoop();
|
||||
void testLoop();
|
||||
void errOutput(std::string* errMessage);
|
||||
};
|
||||
|
||||
class Chess
|
||||
@@ -47,15 +48,18 @@ namespace FCC
|
||||
virtual ~Chess() = default;
|
||||
|
||||
// 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
|
||||
void mainMenu();
|
||||
void startupGame();
|
||||
void gameLoop();
|
||||
void checkHorizontalMovementAllowed();
|
||||
void checkVerticalMovementAllowed();
|
||||
void checkDiagonalMovementAllowed();
|
||||
// void checkHorizontalMovementAllowed();
|
||||
// void checkVerticalMovementAllowed();
|
||||
// void checkDiagonalMovementAllowed();
|
||||
//void set_lang(FCC::Languages lang);
|
||||
|
||||
private:
|
||||
@@ -70,9 +74,17 @@ namespace FCC
|
||||
|
||||
// funcs
|
||||
void input();
|
||||
bool checkIsEnemyOnField();
|
||||
bool checkSelectedPosInBounds();
|
||||
bool checkTargetPosInBounds();
|
||||
bool checkIsEnemyOnField(uint8_t* x, uint8_t* y);
|
||||
bool checkPosInBounds(uint8_t* x, uint8_t* y);
|
||||
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 resetAllowedMovement();
|
||||
};
|
||||
}
|
||||
|
||||
@@ -16,5 +16,8 @@ namespace FCC
|
||||
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 backButton = "Back <--";
|
||||
|
||||
// error texts
|
||||
std::string errNoPieceOnPosition = "Error: Empty position.";
|
||||
};
|
||||
}
|
||||
@@ -17,3 +17,8 @@ void FCC::TUI::testLoop()
|
||||
std::cout << "♔" << std::endl;
|
||||
//std::cout << L"♔";
|
||||
}
|
||||
|
||||
void FCC::TUI::errOutput(std::string* errMessage)
|
||||
{
|
||||
std::cout << *errMessage;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user