From b1319c76201e06aec44e4af14eb141ac5e26d36f Mon Sep 17 00:00:00 2001 From: AdmiralEmser Date: Sun, 9 Oct 2022 21:36:07 +0200 Subject: [PATCH] added checks for possible moves horizontal and vertical; changed some var types --- FC-Chess/FC-Chess.cpp | 85 ++++++++++++++++++++++++++++++++----------- FC-Chess/FC-Chess.h | 26 +++++++++---- FC-Chess/lang.h | 3 ++ FC-Chess/tui.cpp | 7 +++- 4 files changed, 91 insertions(+), 30 deletions(-) diff --git a/FC-Chess/FC-Chess.cpp b/FC-Chess/FC-Chess.cpp index c0ca0d5..b93c987 100644 --- a/FC-Chess/FC-Chess.cpp +++ b/FC-Chess/FC-Chess.cpp @@ -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) { diff --git a/FC-Chess/FC-Chess.h b/FC-Chess/FC-Chess.h index 8441648..206d31a 100644 --- a/FC-Chess/FC-Chess.h +++ b/FC-Chess/FC-Chess.h @@ -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(); }; } diff --git a/FC-Chess/lang.h b/FC-Chess/lang.h index 26945a9..970dc01 100644 --- a/FC-Chess/lang.h +++ b/FC-Chess/lang.h @@ -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."; }; } \ No newline at end of file diff --git a/FC-Chess/tui.cpp b/FC-Chess/tui.cpp index 1c78e81..a548192 100644 --- a/FC-Chess/tui.cpp +++ b/FC-Chess/tui.cpp @@ -16,4 +16,9 @@ void FCC::TUI::testLoop() std::cout << "\u2654" << std::endl; std::cout << "♔" << std::endl; //std::cout << L"♔"; -} \ No newline at end of file +} + +void FCC::TUI::errOutput(std::string* errMessage) +{ + std::cout << *errMessage; +}