Cleaning code

This commit is contained in:
Loic Guegan 2022-01-30 13:14:58 +01:00
parent 159d533412
commit f42a6ac20b
4 changed files with 20 additions and 21 deletions

View file

@ -72,8 +72,8 @@ std::string Board::GetKingLocation(bool isBlack) {
} }
void Board::Move(std::string move) { void Board::Move(std::string move) {
std::string src(move.substr(0, 2)); std::string src = move.substr(0, 2);
std::string dst(move.substr(2, 2)); std::string dst = move.substr(2, 2);
for (Piece &p : pieces) { for (Piece &p : pieces) {
if (p.coord == src) { if (p.coord == src) {
RemovePiece(dst); // Remove piece on dst if exists RemovePiece(dst); // Remove piece on dst if exists
@ -99,8 +99,8 @@ std::string Board::Serialize() {
} }
bool Board::IsMovePossible(std::string move) { bool Board::IsMovePossible(std::string move) {
std::string src(move.substr(0, 2)); std::string src = move.substr(0, 2);
std::string dst(move.substr(2, 2)); std::string dst = move.substr(2, 2);
if (src == dst) { if (src == dst) {
return (false); return (false);
} }

View file

@ -133,7 +133,6 @@ bool ChessArbiter::IsAttacked(std::string square, bool by) {
for (std::string &m : moves) { for (std::string &m : moves) {
std::string src = m.substr(0, 2); std::string src = m.substr(0, 2);
std::string dst = m.substr(2, 2); std::string dst = m.substr(2, 2);
if (dst == square) { if (dst == square) {
// Pawn do not attack forward // Pawn do not attack forward
Piece p = board.GetPieceAt(src); Piece p = board.GetPieceAt(src);

View file

@ -5,7 +5,7 @@ namespace chessarbiter {
std::string FENParser::normalize_rank(std::string fen_rank) { std::string FENParser::normalize_rank(std::string fen_rank) {
std::string normalized; std::string normalized;
for (char &c : fen_rank) { for (char &c : fen_rank) {
if (FEN__IS_DIGIT(c)) { if (IS_DIGIT(c)) {
for (char i = 0; i < (c - '0'); i++) { for (char i = 0; i < (c - '0'); i++) {
normalized += ' '; normalized += ' ';
} }
@ -28,7 +28,7 @@ std::string FENParser::normalize_rank(std::string fen_rank) {
} }
char FENParser::NextToken(std::string fen, char loc) { char FENParser::NextToken(std::string fen, char loc) {
while (loc < fen.size() && FEN__IS_BLANK(fen[loc])) { while (loc < fen.size() && IS_BLANK(fen[loc])) {
loc++; loc++;
} }
return (loc); return (loc);
@ -110,7 +110,7 @@ FEN FENParser::Parse(std::string fen) {
// Parse board // Parse board
char loc = 0; char loc = 0;
for (char rank = 0; rank < 8; rank++) { for (char rank = 0; rank < 8; rank++) {
FEN__CHECK_LOC(); CHECK_LOC();
char newloc = NextRank(fen, loc); char newloc = NextRank(fen, loc);
parsed.board += normalize_rank(fen.substr(loc, newloc - loc)); parsed.board += normalize_rank(fen.substr(loc, newloc - loc));
loc = newloc + 1; loc = newloc + 1;
@ -122,16 +122,16 @@ FEN FENParser::Parse(std::string fen) {
throw InvalidFEN(); throw InvalidFEN();
} }
parsed.player = fen[loc] == 'b'; parsed.player = fen[loc] == 'b';
FEN__CHECK_LOC(); CHECK_LOC();
// Parse castling // Parse castling
loc = NextToken(fen, loc + 1); loc = NextToken(fen, loc + 1);
char length = 0; char length = 0;
char cur_loc = loc; char cur_loc = loc;
while (!FEN__IS_BLANK(fen[cur_loc])) { while (!IS_BLANK(fen[cur_loc])) {
length++; length++;
cur_loc++; cur_loc++;
FEN__CHECK_LOC(); CHECK_LOC();
} }
parsed.white_castle_short = false; parsed.white_castle_short = false;
parsed.white_castle_long = false; parsed.white_castle_long = false;
@ -172,29 +172,29 @@ FEN FENParser::Parse(std::string fen) {
} }
} }
loc++; loc++;
FEN__CHECK_LOC(); CHECK_LOC();
// Parse half move counter // Parse half move counter
loc = NextToken(fen, loc); loc = NextToken(fen, loc);
std::string halfmove; std::string halfmove;
while (!FEN__IS_BLANK(fen[loc])) { while (!IS_BLANK(fen[loc])) {
if (!FEN__IS_DIGIT(fen[loc])) { if (!IS_DIGIT(fen[loc])) {
throw InvalidFEN(); throw InvalidFEN();
} }
halfmove += fen[loc]; halfmove += fen[loc];
loc++; loc++;
FEN__CHECK_LOC(); CHECK_LOC();
} }
parsed.halfmove = stoi(halfmove); parsed.halfmove = stoi(halfmove);
// Parse move counter // Parse move counter
loc = NextToken(fen, loc); loc = NextToken(fen, loc);
std::string move; std::string move;
while (loc < fen.size() && !FEN__IS_BLANK(fen[loc])) { while (loc < fen.size() && !IS_BLANK(fen[loc])) {
if (!FEN__IS_DIGIT(fen[loc])) { if (!IS_DIGIT(fen[loc])) {
throw InvalidFEN(); throw InvalidFEN();
} }
FEN__CHECK_LOC(); CHECK_LOC();
move += fen[loc]; move += fen[loc];
loc++; loc++;
} }

View file

@ -2,11 +2,11 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#define FEN__IS_DIGIT(c) \ #define IS_DIGIT(c) \
(c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || \ (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || \
c == '6' || c == '7' || c == '8' || c == '9') c == '6' || c == '7' || c == '8' || c == '9')
#define FEN__IS_BLANK(c) (c == ' ' || c == '\n' || c == '\t' || c == '\r') #define IS_BLANK(c) (c == ' ' || c == '\n' || c == '\t' || c == '\r')
#define FEN__CHECK_LOC() {if(loc>=fen.size()){throw InvalidFEN();}} #define CHECK_LOC() {if(loc>=fen.size()){throw InvalidFEN();}}
namespace chessarbiter { namespace chessarbiter {