mirror of
https://gitlab.com/manzerbredes/chessarbiter.git
synced 2025-04-05 17:46:26 +02:00
Cleaning code
This commit is contained in:
parent
159d533412
commit
f42a6ac20b
4 changed files with 20 additions and 21 deletions
|
@ -72,8 +72,8 @@ std::string Board::GetKingLocation(bool isBlack) {
|
|||
}
|
||||
|
||||
void Board::Move(std::string move) {
|
||||
std::string src(move.substr(0, 2));
|
||||
std::string dst(move.substr(2, 2));
|
||||
std::string src = move.substr(0, 2);
|
||||
std::string dst = move.substr(2, 2);
|
||||
for (Piece &p : pieces) {
|
||||
if (p.coord == src) {
|
||||
RemovePiece(dst); // Remove piece on dst if exists
|
||||
|
@ -99,8 +99,8 @@ std::string Board::Serialize() {
|
|||
}
|
||||
|
||||
bool Board::IsMovePossible(std::string move) {
|
||||
std::string src(move.substr(0, 2));
|
||||
std::string dst(move.substr(2, 2));
|
||||
std::string src = move.substr(0, 2);
|
||||
std::string dst = move.substr(2, 2);
|
||||
if (src == dst) {
|
||||
return (false);
|
||||
}
|
||||
|
|
|
@ -133,7 +133,6 @@ bool ChessArbiter::IsAttacked(std::string square, bool by) {
|
|||
for (std::string &m : moves) {
|
||||
std::string src = m.substr(0, 2);
|
||||
std::string dst = m.substr(2, 2);
|
||||
|
||||
if (dst == square) {
|
||||
// Pawn do not attack forward
|
||||
Piece p = board.GetPieceAt(src);
|
||||
|
|
26
src/Fen.cpp
26
src/Fen.cpp
|
@ -5,7 +5,7 @@ namespace chessarbiter {
|
|||
std::string FENParser::normalize_rank(std::string fen_rank) {
|
||||
std::string normalized;
|
||||
for (char &c : fen_rank) {
|
||||
if (FEN__IS_DIGIT(c)) {
|
||||
if (IS_DIGIT(c)) {
|
||||
for (char i = 0; i < (c - '0'); i++) {
|
||||
normalized += ' ';
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ std::string FENParser::normalize_rank(std::string fen_rank) {
|
|||
}
|
||||
|
||||
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++;
|
||||
}
|
||||
return (loc);
|
||||
|
@ -110,7 +110,7 @@ FEN FENParser::Parse(std::string fen) {
|
|||
// Parse board
|
||||
char loc = 0;
|
||||
for (char rank = 0; rank < 8; rank++) {
|
||||
FEN__CHECK_LOC();
|
||||
CHECK_LOC();
|
||||
char newloc = NextRank(fen, loc);
|
||||
parsed.board += normalize_rank(fen.substr(loc, newloc - loc));
|
||||
loc = newloc + 1;
|
||||
|
@ -122,16 +122,16 @@ FEN FENParser::Parse(std::string fen) {
|
|||
throw InvalidFEN();
|
||||
}
|
||||
parsed.player = fen[loc] == 'b';
|
||||
FEN__CHECK_LOC();
|
||||
CHECK_LOC();
|
||||
|
||||
// Parse castling
|
||||
loc = NextToken(fen, loc + 1);
|
||||
char length = 0;
|
||||
char cur_loc = loc;
|
||||
while (!FEN__IS_BLANK(fen[cur_loc])) {
|
||||
while (!IS_BLANK(fen[cur_loc])) {
|
||||
length++;
|
||||
cur_loc++;
|
||||
FEN__CHECK_LOC();
|
||||
CHECK_LOC();
|
||||
}
|
||||
parsed.white_castle_short = false;
|
||||
parsed.white_castle_long = false;
|
||||
|
@ -172,29 +172,29 @@ FEN FENParser::Parse(std::string fen) {
|
|||
}
|
||||
}
|
||||
loc++;
|
||||
FEN__CHECK_LOC();
|
||||
CHECK_LOC();
|
||||
|
||||
// Parse half move counter
|
||||
loc = NextToken(fen, loc);
|
||||
std::string halfmove;
|
||||
while (!FEN__IS_BLANK(fen[loc])) {
|
||||
if (!FEN__IS_DIGIT(fen[loc])) {
|
||||
while (!IS_BLANK(fen[loc])) {
|
||||
if (!IS_DIGIT(fen[loc])) {
|
||||
throw InvalidFEN();
|
||||
}
|
||||
halfmove += fen[loc];
|
||||
loc++;
|
||||
FEN__CHECK_LOC();
|
||||
CHECK_LOC();
|
||||
}
|
||||
parsed.halfmove = stoi(halfmove);
|
||||
|
||||
// Parse move counter
|
||||
loc = NextToken(fen, loc);
|
||||
std::string move;
|
||||
while (loc < fen.size() && !FEN__IS_BLANK(fen[loc])) {
|
||||
if (!FEN__IS_DIGIT(fen[loc])) {
|
||||
while (loc < fen.size() && !IS_BLANK(fen[loc])) {
|
||||
if (!IS_DIGIT(fen[loc])) {
|
||||
throw InvalidFEN();
|
||||
}
|
||||
FEN__CHECK_LOC();
|
||||
CHECK_LOC();
|
||||
move += fen[loc];
|
||||
loc++;
|
||||
}
|
||||
|
|
|
@ -2,11 +2,11 @@
|
|||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#define FEN__IS_DIGIT(c) \
|
||||
#define IS_DIGIT(c) \
|
||||
(c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || \
|
||||
c == '6' || c == '7' || c == '8' || c == '9')
|
||||
#define FEN__IS_BLANK(c) (c == ' ' || c == '\n' || c == '\t' || c == '\r')
|
||||
#define FEN__CHECK_LOC() {if(loc>=fen.size()){throw InvalidFEN();}}
|
||||
#define IS_BLANK(c) (c == ' ' || c == '\n' || c == '\t' || c == '\r')
|
||||
#define CHECK_LOC() {if(loc>=fen.size()){throw InvalidFEN();}}
|
||||
|
||||
namespace chessarbiter {
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue