Skip to content

Commit 9801bf3

Browse files
committed
Fix VLB parsing/printing and move storage
1 parent c2c355f commit 9801bf3

File tree

4 files changed

+98
-25
lines changed

4 files changed

+98
-25
lines changed

src/apiutil.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,12 @@ inline Validation check_for_valid_characters(const std::string& firstFenPart, co
569569
if (isdigit(c) || contains(validSpecialCharactersFirstField, c))
570570
continue;
571571
if (c == '+')
572-
continue;
572+
{
573+
if (v && v->shogiStylePromotions)
574+
continue;
575+
std::cerr << "Invalid piece character: '+'." << std::endl;
576+
return NOK;
577+
}
573578
if (Variant::is_piece_id_start(c))
574579
{
575580
std::string token(1, c);

src/parser.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,52 @@ Variant* VariantParser<DoCheck>::parse() {
297297

298298
template <bool DoCheck>
299299
Variant* VariantParser<DoCheck>::parse(Variant* v) {
300+
auto parse_rank_value = [](const std::string& value, int& out) {
301+
std::stringstream ss(value);
302+
int i;
303+
ss >> i;
304+
if (ss.fail())
305+
return false;
306+
out = i;
307+
return true;
308+
};
309+
auto parse_file_value = [](const std::string& value, int& out) {
310+
std::string trimmed = trim(value);
311+
if (trimmed.empty())
312+
return false;
313+
std::stringstream ss(trimmed);
314+
if (isdigit(ss.peek()))
315+
{
316+
int i;
317+
ss >> i;
318+
if (ss.fail())
319+
return false;
320+
out = i - 1;
321+
return true;
322+
}
323+
char c;
324+
ss >> c;
325+
if (ss.fail())
326+
return false;
327+
out = tolower(c) - 'a';
328+
return true;
329+
};
330+
int cfgMaxRank = -1;
331+
int cfgMaxFile = -1;
332+
auto itRank = config.find("maxRank");
333+
if (itRank != config.end())
334+
parse_rank_value(itRank->second, cfgMaxRank);
335+
auto itFile = config.find("maxFile");
336+
if (itFile != config.end())
337+
parse_file_value(itFile->second, cfgMaxFile);
338+
if ( (cfgMaxRank > 0 && cfgMaxRank - 1 > RANK_MAX)
339+
|| (cfgMaxFile >= 0 && cfgMaxFile > FILE_MAX))
340+
{
341+
v->maxRank = static_cast<Rank>(RANK_MAX + 1);
342+
v->maxFile = static_cast<File>(FILE_MAX + 1);
343+
return v;
344+
}
345+
300346
parse_attribute("maxRank", v->maxRank);
301347
parse_attribute("maxFile", v->maxFile);
302348
// piece types

src/position.cpp

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ Position& Position::set(const Variant* v, const string& fenStr, bool isChess960,
347347
}
348348

349349
// Promoted shogi pieces
350-
else if (token == '+')
350+
else if (token == '+' && var->shogiStylePromotions)
351351
{
352352
if (Variant::is_piece_id_start(ss.peek()))
353353
{
@@ -467,33 +467,55 @@ Position& Position::set(const Variant* v, const string& fenStr, bool isChess960,
467467
// 4. En passant square.
468468
// Ignore if square is invalid or not on side to move relative rank 6.
469469
else
470-
while ( ((ss >> col) && (col >= 'a' && col <= 'a' + max_file()))
471-
&& ((ss >> row) && (row >= '1' && row <= '1' + max_rank())))
470+
{
471+
std::string epInfo;
472+
ss >> std::skipws >> epInfo;
473+
ss >> std::noskipws;
474+
if (!epInfo.empty() && epInfo != "-")
472475
{
473-
Square epSquare = make_square(File(col - 'a'), Rank(row - '1'));
476+
size_t i = 0;
477+
while (i < epInfo.size())
478+
{
479+
char fileChar = epInfo[i];
480+
if (fileChar < 'a' || fileChar > 'a' + max_file())
481+
break;
482+
++i;
483+
if (i >= epInfo.size() || !isdigit(epInfo[i]))
484+
break;
485+
int rankNum = 0;
486+
while (i < epInfo.size() && isdigit(epInfo[i]))
487+
{
488+
rankNum = rankNum * 10 + (epInfo[i] - '0');
489+
++i;
490+
}
491+
if (rankNum < 1 || rankNum > int(max_rank()) + 1)
492+
continue;
493+
Square epSquare = make_square(File(fileChar - 'a'), Rank(rankNum - 1));
474494
#ifdef LARGEBOARDS
475-
// Consider different rank numbering in CECP
476-
if (max_rank() == RANK_10 && CurrentProtocol == XBOARD)
477-
epSquare += NORTH;
495+
// Consider different rank numbering in CECP
496+
if (max_rank() == RANK_10 && CurrentProtocol == XBOARD)
497+
epSquare += NORTH;
478498
#endif
479499

480-
// En passant square will be considered only if
481-
// epSquare is within enPassantRegion and
482-
// 1) variant has non-standard rules
483-
// or
484-
// 2)
485-
// a) side to move have a pawn threatening epSquare
486-
// b) there is an enemy pawn one or two (for triple steps) squares in front of epSquare
487-
// c) there is no (non-wall) piece on epSquare or behind epSquare
488-
if ( (var->enPassantRegion[sideToMove] & epSquare)
489-
&& ( !var->fastAttacks
490-
|| (var->enPassantTypes[sideToMove] & ~piece_set(PAWN))
491-
|| ( pawn_attacks_bb(~sideToMove, epSquare) & pieces(sideToMove, PAWN)
492-
&& ( (pieces(~sideToMove, PAWN) & (epSquare + pawn_push(~sideToMove)))
493-
|| (pieces(~sideToMove, PAWN) & (epSquare + 2 * pawn_push(~sideToMove))))
494-
&& !((pieces(WHITE) | pieces(BLACK)) & (epSquare | (epSquare + pawn_push(sideToMove)))))))
495-
st->epSquares |= epSquare;
500+
// En passant square will be considered only if
501+
// epSquare is within enPassantRegion and
502+
// 1) variant has non-standard rules
503+
// or
504+
// 2)
505+
// a) side to move have a pawn threatening epSquare
506+
// b) there is an enemy pawn one or two (for triple steps) squares in front of epSquare
507+
// c) there is no (non-wall) piece on epSquare or behind epSquare
508+
if ( (var->enPassantRegion[sideToMove] & epSquare)
509+
&& ( !var->fastAttacks
510+
|| (var->enPassantTypes[sideToMove] & ~piece_set(PAWN))
511+
|| ( pawn_attacks_bb(~sideToMove, epSquare) & pieces(sideToMove, PAWN)
512+
&& ( (pieces(~sideToMove, PAWN) & (epSquare + pawn_push(~sideToMove)))
513+
|| (pieces(~sideToMove, PAWN) & (epSquare + 2 * pawn_push(~sideToMove))))
514+
&& !((pieces(WHITE) | pieces(BLACK)) & (epSquare | (epSquare + pawn_push(sideToMove)))))))
515+
st->epSquares |= epSquare;
516+
}
496517
}
518+
}
497519
}
498520

499521
// Check counter for nCheck

src/types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ struct Bitboard {
414414
}
415415

416416
inline Bitboard operator * (const Bitboard x) const {
417-
#if defined(__GNUC__) || defined(__clang__)
417+
#if (defined(__GNUC__) || defined(__clang__)) && defined(__SIZEOF_INT128__)
418418
unsigned __int128 lo = (unsigned __int128)b64[1] * x.b64[1];
419419
unsigned __int128 cross1 = (unsigned __int128)b64[1] * x.b64[0];
420420
unsigned __int128 cross2 = (unsigned __int128)b64[0] * x.b64[1];

0 commit comments

Comments
 (0)