Skip to content

Commit

Permalink
better
Browse files Browse the repository at this point in the history
  • Loading branch information
wertzuz committed Dec 7, 2023
1 parent 9f16c3f commit 0e4886e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
3 changes: 1 addition & 2 deletions include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ std::shared_ptr <Piece> getMatchingPiece(glm::vec2 field, std::vector <std::shar

glm::vec2 getMousePosition(bool whiteDown, int squareSize);

std::string FenExport(const std::vector <std::shared_ptr<Piece>>& piecesVector,bool whiteTurn,int halfMoveNumber);
std::string FenExport(const std::vector <std::shared_ptr<Piece>>& pieces,bool whiteTurn,int halfMoveNumber);

std::vector<std::shared_ptr<Piece>> FenImport(const std::string& FenString);
28 changes: 14 additions & 14 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ void Game::selectPiece() {
* @return std::vector <std::shared_ptr<Piece>>
*/
std::vector <std::shared_ptr<Piece>> Game::FenImport(const std::string &FenString) {
std::vector <std::shared_ptr<Piece>> piecesVector;
std::vector <std::shared_ptr<Piece>> pieces;
int countx = 0;
int county = 0;
std::string delimiter = " ";
Expand All @@ -491,27 +491,27 @@ std::vector <std::shared_ptr<Piece>> Game::FenImport(const std::string &FenStrin
} else if (std::isalpha(c)) {
switch (tolower(c)) {
case 'k':
piecesVector.push_back(std::make_shared<King>(glm::vec2{countx, county}, isupper(c)));
pieces.push_back(std::make_shared<King>(glm::vec2{countx, county}, isupper(c)));
countx += 1;
break;
case 'n':
piecesVector.push_back(std::make_shared<Knight>(glm::vec2{countx, county}, isupper(c)));
pieces.push_back(std::make_shared<Knight>(glm::vec2{countx, county}, isupper(c)));
countx += 1;
break;
case 'p':
piecesVector.push_back(std::make_shared<Pawn>(glm::vec2{countx, county}, isupper(c)));
pieces.push_back(std::make_shared<Pawn>(glm::vec2{countx, county}, isupper(c)));
countx += 1;
break;
case 'r':
piecesVector.push_back(std::make_shared<Rook>(glm::vec2{countx, county}, isupper(c)));
pieces.push_back(std::make_shared<Rook>(glm::vec2{countx, county}, isupper(c)));
countx += 1;
break;
case 'b':
piecesVector.push_back(std::make_shared<Bishop>(glm::vec2{countx, county}, isupper(c)));
pieces.push_back(std::make_shared<Bishop>(glm::vec2{countx, county}, isupper(c)));
countx += 1;
break;
case 'q':
piecesVector.push_back(std::make_shared<Queen>(glm::vec2{countx, county}, isupper(c)));
pieces.push_back(std::make_shared<Queen>(glm::vec2{countx, county}, isupper(c)));
countx += 1;
break;
}
Expand All @@ -532,7 +532,7 @@ std::vector <std::shared_ptr<Piece>> Game::FenImport(const std::string &FenStrin

// Check if castling is possible and update the pieces
if (metadataFen[count] == '-') {
for (const auto& i: piecesVector) {
for (const auto& i: pieces) {
std::shared_ptr <King> Kings = std::dynamic_pointer_cast<King>(i);
if (Kings != nullptr) {
Kings->hasMoved = true;
Expand All @@ -541,7 +541,7 @@ std::vector <std::shared_ptr<Piece>> Game::FenImport(const std::string &FenStrin
count++;
} else {
if (metadataFen[count] == 'K') {
std::shared_ptr <Piece> pieceTemp = getMatchingPiece(glm::vec2{7, 7}, piecesVector);
std::shared_ptr <Piece> pieceTemp = getMatchingPiece(glm::vec2{7, 7}, pieces);
std::shared_ptr <Rook> derivedPtr = std::dynamic_pointer_cast<Rook>(pieceTemp);
if (derivedPtr != nullptr) {
if (derivedPtr->white) {
Expand All @@ -552,7 +552,7 @@ std::vector <std::shared_ptr<Piece>> Game::FenImport(const std::string &FenStrin
}

if (metadataFen[count] == 'Q') {
std::shared_ptr <Piece> pieceTemp = getMatchingPiece(glm::vec2{0, 7}, piecesVector);
std::shared_ptr <Piece> pieceTemp = getMatchingPiece(glm::vec2{0, 7}, pieces);
std::shared_ptr <Rook> derivedPtr = std::dynamic_pointer_cast<Rook>(pieceTemp);
if (derivedPtr != nullptr) {
if (derivedPtr->white) {
Expand All @@ -563,7 +563,7 @@ std::vector <std::shared_ptr<Piece>> Game::FenImport(const std::string &FenStrin
}

if (metadataFen[count] == 'k') {
std::shared_ptr <Piece> pieceTemp = getMatchingPiece(glm::vec2{7, 0}, piecesVector);
std::shared_ptr <Piece> pieceTemp = getMatchingPiece(glm::vec2{7, 0}, pieces);
std::shared_ptr <Rook> derivedPtr = std::dynamic_pointer_cast<Rook>(pieceTemp);
if (derivedPtr != nullptr) {
if (!derivedPtr->white) {
Expand All @@ -573,7 +573,7 @@ std::vector <std::shared_ptr<Piece>> Game::FenImport(const std::string &FenStrin
count++;
}
if (metadataFen[count] == 'q') {
std::shared_ptr <Piece> pieceTemp = getMatchingPiece(glm::vec2{0, 0}, piecesVector);
std::shared_ptr <Piece> pieceTemp = getMatchingPiece(glm::vec2{0, 0}, pieces);
std::shared_ptr <Rook> derivedPtr = std::dynamic_pointer_cast<Rook>(pieceTemp);
if (derivedPtr != nullptr) {
if (!derivedPtr->white) {
Expand All @@ -591,7 +591,7 @@ std::vector <std::shared_ptr<Piece>> Game::FenImport(const std::string &FenStrin
std::find(abc.begin(), abc.end(), metadataFen[count + 1]) != abc.end()) {
u_long pos_x = abc.find(metadataFen[count]);
u_long pos_y = 8 - (metadataFen[count + 1] - '0');
std::shared_ptr <Piece> derivedPtr = getMatchingPiece({pos_x, whiteTurn ? pos_y + 1 : pos_y - 1}, piecesVector);
std::shared_ptr <Piece> derivedPtr = getMatchingPiece({pos_x, whiteTurn ? pos_y + 1 : pos_y - 1}, pieces);
std::shared_ptr <Pawn> enPassant = std::dynamic_pointer_cast<Pawn>(derivedPtr);
if (enPassant != nullptr) {
enPassant->isEnPassantVulnerable = true;
Expand All @@ -618,5 +618,5 @@ std::vector <std::shared_ptr<Piece>> Game::FenImport(const std::string &FenStrin
if (metadataFen[count]) {
fullMoveNumber = stoi(metadataFen.substr(count));
}
return piecesVector;
return pieces;
}
6 changes: 3 additions & 3 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ glm::vec2 getMousePosition(bool whiteDown, int squareSize) {
/**
* Converts board position into valid FEN-string
* https://en.wikipedia.org/wiki/Forsyth-Edwards_Notation
* @param piecesVector
* @param pieces
* @param whiteTurn
* @param halfMoveNumber
* @return std::string FEN-string
*/
std::string FenExport(const std::vector <std::shared_ptr<Piece>>& piecesVector,bool whiteTurn,int halfMoveNumber) {
std::string FenExport(const std::vector <std::shared_ptr<Piece>>& pieces,bool whiteTurn,int halfMoveNumber) {
std::map <std::string, std::shared_ptr<Piece>> posMap; //en.wikipedia.org/wiki/Hash_table
std::string FenExportString; // string to be returned
std::string enPassantSquare = "-";
// fill map with all pieces
for (const auto& i: piecesVector) {
for (const auto& i: pieces) {
posMap[glm::to_string(i->getPos())] = i;
}
int count = 0;
Expand Down

0 comments on commit 0e4886e

Please sign in to comment.