int is_move(const char *mstr)
{
int len = strlen(mstr);
- if ((len > 3) && (mstr[len - 2] == '='))
- len -= 2;
-
/* remove the 'mates' marker */
if (mstr[len - 1] == '#')
len--;
+ if ((len > 3) && (mstr[len - 2] == '=' || mstr[len - 2] == '/'))
+ len -= 2;
+
if (len == 4) { /* Test for e2e4 */
if (isfile(mstr[0]) && isrank(mstr[1]) &&
isfile(mstr[2]) && isrank(mstr[3])) {
if (gs->onMove == WHITE) {
/* King side castling */
- if ((fr == 0) && (tr == 0) && (ff == gs->files/2) && (tf == gs->files-2) && (gs->wkmoved >= 0)
- && (gs->wkrmoved >= 0) && (gs->board[gs->files-3][0] == NOPIECE) &&
+ if ((fr == 0) && (tr == 0) && ((ff == gs->files/2) && (tf == gs->files-2) ||
+ gs->drops == 2 && (tf == gs->files/2) && (ff == gs->files-1)) // [HGM] reverse Seirawan gating
+ && (gs->wkmoved >= 0) && (gs->wkrmoved >= 0) && (gs->board[gs->files-3][0] == NOPIECE) &&
(gs->board[gs->files-2][0] == NOPIECE) && (gs->board[gs->files-1][0] == W_ROOK) &&
(gs->board[gs->files/2+1][0] == NOPIECE) && (!is_square_attacked(gs, gs->files/2+1, 0)) &&
(!is_square_attacked(gs, gs->files/2, 0)) && (!is_square_attacked(gs, gs->files-3, 0))) {
return 2;
}
/* Queen side castling */
- if ((fr == 0) && (tr == 0) && (ff == gs->files/2) && (tf == 2) && (gs->wkmoved >= 0)
- && (gs->wqrmoved >= 0) && (gs->board[3][0] == NOPIECE) &&
+ if ((fr == 0) && (tr == 0) && ((ff == gs->files/2) && (tf == 2) ||
+ gs->drops == 2 && (tf == gs->files/2) && (ff == 0)) // [HGM] reverse Seirawan gating
+ && (gs->wkmoved >= 0) && (gs->wqrmoved >= 0) && (gs->board[3][0] == NOPIECE) &&
(gs->board[2][0] == NOPIECE) && (gs->board[1][0] == NOPIECE) &&
(gs->board[0][0] == W_ROOK) &&
(gs->board[gs->files/2-1][0] == NOPIECE) && (!is_square_attacked(gs, gs->files/2-1, 0)) &&
}
} else { /* Black */
/* King side castling */
- if ((fr == gs->ranks-1) && (tr == gs->ranks-1) && (ff == gs->files/2) && (tf == gs->files-2) && (gs->bkmoved >= 0)
- && (gs->bkrmoved >= 0) && (gs->board[gs->files-3][7] == NOPIECE) &&
+ if ((fr == gs->ranks-1) && (tr == gs->ranks-1) && ((ff == gs->files/2) && (tf == gs->files-2) ||
+ gs->drops == 2 && (tf == gs->files/2) && (ff == gs->files-1)) // [HGM] reverse Seirawan gating
+ && (gs->bkmoved >= 0) && (gs->bkrmoved >= 0) && (gs->board[gs->files-3][7] == NOPIECE) &&
(gs->board[gs->files-2][gs->ranks-1] == NOPIECE) && (gs->board[gs->files-1][gs->ranks-1] == B_ROOK) &&
(gs->board[gs->files/2+1][gs->ranks-1] == NOPIECE) && (!is_square_attacked(gs, gs->files/2+1, gs->ranks-1)) &&
(!is_square_attacked(gs, gs->files/2, gs->ranks-1)) && (!is_square_attacked(gs, gs->files-3, gs->ranks-1))) {
return 2;
}
/* Queen side castling */
- if ((fr == gs->ranks-1) && (tr == gs->ranks-1) && (ff == gs->files/2) && (tf == 2) && (gs->bkmoved >= 0)
- && (gs->bqrmoved >= 0) && (gs->board[3][gs->ranks-1] == NOPIECE) &&
+ if ((fr == gs->ranks-1) && (tr == gs->ranks-1) && ((ff == gs->files/2) && (tf == 2) ||
+ gs->drops == 2 && (tf == gs->files/2) && (ff == 0)) // [HGM] reverse Seirawan gating
+ && (gs->bkmoved >= 0) && (gs->bqrmoved >= 0) && (gs->board[3][gs->ranks-1] == NOPIECE) &&
(gs->board[2][gs->ranks-1] == NOPIECE) && (gs->board[1][gs->ranks-1] == NOPIECE) &&
(gs->board[0][gs->ranks-1] == B_ROOK) &&
(gs->board[gs->files/2-1][gs->ranks-1] == NOPIECE) && (!is_square_attacked(gs, gs->files/2-1, gs->ranks-1)) &&
int fFile, int fRank,
int tFile, int tRank)
{
- int move_piece;
+ int move_piece, victim;
int legal;
if (fFile == ALG_DROP) {
move_piece = fRank;
- if(!gs->drops) return 0; // [HGM] variants: no drops in this variant
+ if(gs->drops != 1) return 0; // [HGM] variants: no drops in this variant
if (move_piece == KING)
return 0;
if (gs->holding[gs->onMove==WHITE ? 0 : 1][move_piece-1] == 0)
return 0;
if (!iscolor(gs->board[fFile][fRank], gs->onMove)) /* Wrong color */
return 0;
- if ((gs->board[tFile][tRank] != NOPIECE) &&
- iscolor(gs->board[tFile][tRank], gs->onMove)) /* Can't capture own */
- return 0;
+ if (((victim = gs->board[tFile][tRank]) != NOPIECE) &&
+ iscolor(gs->board[tFile][tRank], gs->onMove)) {
+ if(piecetype(move_piece) == KING && piecetype(victim) == ROOK) { // [HGM] could be FRC castling
+ }
+ if(gs->drops== 2 && piecetype(move_piece) == ROOK && piecetype(victim) == KING) { // [HGM] could be Seirawan reverse gating
+ return legal_king_move(gs, fFile, fRank, tFile, tRank);
+ }
+ return 0; /* Can't capture own */
+ }
if ((fFile == tFile) && (fRank == tRank)) /* Same square */
return 0;
switch (move_piece) {
case ROOK:
legal = legal_rook_move(gs, fFile, fRank, tFile, tRank);
break;
+ case HAWK:
case CARDINAL:
case PRINCESS:
legal = legal_cardinal_move(gs, fFile, fRank, tFile, tRank);
break;
+ case SELEPHANT:
case MARSHALL:
case EMPRESS:
legal = legal_marshall_move(gs, fFile, fRank, tFile, tRank);
static int move_calculate(struct game_state_t * gs, struct move_t * mt, int promote)
{
struct game_state_t fakeMove;
+ int gating = 0, stm;
mt->pieceCaptured = gs->board[mt->toFile][mt->toRank];
mt->enPassant = 0; /* Don't know yet, let execute move take care
} else if(mt->fromFile == ALG_CASTLE) {
// [HGM] castle: generalized castling, fr and tr give from and to file of Rook.
sprintf(mt->moveString, mt->toRank > mt->toFile ? "o-o-o" : "o-o");
+ if(gs->drops == 2 && promote && gs->holding[gs->onMove == BLACK][abs(promote)-1]) { // promote can be flipped (reverse gating kludge)
+ int c = gs->onMove == WHITE ? 0 : gs->ranks-1;
+ mt->piecePromotionTo = promote; gating = 1;
+ if(promote < 0) sprintf(mt->moveString, "R/%c%d-e%d", mt->fromRank + 'a', c, c); // use RxK notation for Rook-square gatings
+ }
} else {
+ stm = colorval(gs->board[mt->fromFile][mt->fromRank]);
if(gs->promoType == 3) { // Shogi-style promotions: not just Pawns, but many pieces can promote
int piece = gs->board[mt->fromFile][mt->fromRank];
mt->piecePromotionTo = NOPIECE;
if ((piecetype(gs->board[mt->fromFile][mt->fromRank]) == PAWN) &&
!gs->palace && // [HGM] XQ: no promotions in xiangqi
((mt->toRank < gs->promoZone) || (mt->toRank >= gs->ranks - gs->promoZone))) {
- int stm = colorval(gs->board[mt->fromFile][mt->fromRank]);
if(!promote && (mt->toRank == 0 || mt->toRank == gs->ranks-1)) { // promotion obligatory, but not specified
if(gs->promoType != 2) promote = QUEEN; else { // choose a default
- for(promote=KING-1; promote>PAWN; promote--) if(gs->holding[stm == BLACK][promote-1]) break;
+ for(promote=PIECES-1; promote>PAWN; promote--) if(gs->holding[stm == BLACK][promote-1]) break;
if(promote == PAWN) return MOVE_ILLEGAL; // nothing available
}
- } // if not obligatory, we defer unless promoton was explicitly specified!
+ } // if not obligatory, we defer unless promotion was explicitly specified!
if(!gs->pawnDblStep && promote == PRINCESS) promote = MAN2;
if(!gs->pawnDblStep && promote != FERZ2 && promote != MAN2) promote = FERZ; // [HGM] kludge to recognize shatranj
// non-promotion can still be an option for deeper promotion zones
mt->piecePromotionTo = promote ? (promote | stm) : NOPIECE;
if(promote && gs->promoType == 2 && !gs->holding[stm == BLACK][promote-1]) return MOVE_ILLEGAL; // unavailable piece specified
+ } else if(gs->drops == 2 && promote && mt->fromRank == (stm == WHITE ? 0 : gs->ranks-1)) { // [HGM] Seirawan-style gating
+ int i; struct game *g = &game_globals.garray[gs->gameNum];
+ if(!gs->holding[stm == BLACK][promote-1]) return MOVE_ILLEGAL; // unavailable piece specified
+ // now we must test virginity of the moved piece. Yegh!
+ for (i = g->numHalfMoves-1; i >= 0; i--) {
+ if (g->moveList[i].fromFile == mt->fromFile && g->moveList[i].fromRank == mt->fromRank ||
+ g->moveList[i].toFile == mt->fromFile && g->moveList[i].toRank == mt->fromRank ||
+ g->moveList[i].fromFile == ALG_CASTLE && (gs->onMove == WHITE ? 0 : gs->ranks-1) == mt->fromRank &&
+ (g->moveList[i].fromRank == mt->fromFile || gs->files>>1 == mt->fromFile )) return MOVE_ILLEGAL;
+ }
+ mt->piecePromotionTo = promote; // gating OK
+ gating = 1; // remember we did it for check test
} else {
mt->piecePromotionTo = NOPIECE;
}
fakeMove = *gs;
/* Calculates enPassant also */
execute_move(&fakeMove, mt, 0);
+ if(gating) fakeMove.board[mt->fromFile][mt->fromRank] = NOPIECE; // [HGM] gating is only legal if non-gating move was (weird, but true)
/* Does making this move leave ME in check? */
if (in_check(&fakeMove))
case ROOK:
possible_rook_moves(gs, f, r, possiblef, possibler, &numpossible);
break;
+ case HAWK:
case CARDINAL:
case PRINCESS:
possible_cardinal_moves(gs, f, r, possiblef, possibler, &numpossible);
break;
+ case SELEPHANT:
case MARSHALL:
case EMPRESS:
possible_marshall_moves(gs, f, r, possiblef, possibler, &numpossible);
int parse_move(char *mstr, struct game_state_t * gs, struct move_t * mt, int promote)
{
int type = is_move(mstr);
- int result;
+ int result, flipflag = 1;
mt->piecePromotionTo = NOPIECE;
mt->color = gs->onMove;
return MOVE_ILLEGAL;
if(result == 2) { // [HGM] castle: orthodox castling was given as King move; convert it to new format
+ int ff=mt->fromFile, tf=mt->toFile;
+ if(piecetype(gs->board[tf][mt->toRank]) == KING) { // [HGM] RxK notation
+ mt->fromFile = tf; mt->toFile = ff > tf ? gs->files-2 : 2; // correct to coventional
+ flipflag = -1; // kludge: flip gated piece
+ }
if(mt->fromFile - mt->toFile > 1) { // Q-side
mt->fromRank = 0;
mt->toRank = mt->toFile+1;
if (mt->piecePromotionTo != NOPIECE) {
promote = piecetype(mt->piecePromotionTo);
- } else if(gs->promoType == 3 && promote == MASTODON) promote = GOLD;
+ } else if (promote != NOPIECE) { // [HGM] promotion on long algebraic move; correct ambiguous types for variant
+ if(gs->promoType == 3 && promote == MASTODON) promote = GOLD;
+ if(gs->drops == 2 && promote == EMPRESS) promote = SELEPHANT;
+ if(gs->drops == 2 && promote == DRAGONHORSE) promote = HAWK;
+ }
- return move_calculate(gs, mt, promote);
+ return move_calculate(gs, mt, promote*flipflag);
}
/* Returns MOVE_OK, MOVE_NOMATERIAL, MOVE_CHECKMATE, or MOVE_STALEMATE */
gs->board[fKing][backRank] = NOPIECE;
gs->board[mt->toRank][backRank] = rook; // then put back
gs->board[mt->toFile][backRank] = king;
+ if(gs->drops == 2 && mt->piecePromotionTo != NOPIECE) { // [HGM] Seirawan-style gating
+ if(mt->piecePromotionTo > 0)
+ gs->board[fKing][backRank] = mt->piecePromotionTo | gs->onMove; // gate on King square
+ else
+ gs->board[mt->fromRank][backRank] = -mt->piecePromotionTo | gs->onMove; // gate on Rook square
+ gs->holding[gs->onMove==WHITE ? 0 : 1][abs(mt->piecePromotionTo)-1]--; // remove gated piece from holdings
+ }
} else {
movedPiece = gs->board[mt->fromFile][mt->fromRank];
tookPiece = gs->board[mt->toFile][mt->toRank];
- if (mt->piecePromotionTo == NOPIECE) {
+ if(gs->drops == 2 && mt->piecePromotionTo != NOPIECE && piecetype(movedPiece) != PAWN) { // [HGM] Seirawan-style gating
gs->board[mt->toFile][mt->toRank] = gs->board[mt->fromFile][mt->fromRank];
+ gs->board[mt->fromFile][mt->fromRank] = mt->piecePromotionTo | gs->onMove;;
+ gs->holding[gs->onMove==WHITE ? 0 : 1][mt->piecePromotionTo-1]--; // remove gated piece from holdings
} else {
- gs->board[mt->toFile][mt->toRank] = mt->piecePromotionTo | gs->onMove;
- if(gs->promoType == 2) gs->holding[gs->onMove][mt->piecePromotionTo-1]--;
+ if (mt->piecePromotionTo == NOPIECE) {
+ gs->board[mt->toFile][mt->toRank] = gs->board[mt->fromFile][mt->fromRank];
+ } else {
+ gs->board[mt->toFile][mt->toRank] = mt->piecePromotionTo | gs->onMove;
+ if(gs->promoType == 2) gs->holding[gs->onMove][mt->piecePromotionTo-1]--;
+ }
+ gs->board[mt->fromFile][mt->fromRank] = NOPIECE;
}
- gs->board[mt->fromFile][mt->fromRank] = NOPIECE;
/* Check if irreversable */
if ((piecetype(movedPiece) == PAWN) && (mt->fromRank != mt->toRank) // [HGM] XQ: sideway Pawn move reversible!
|| (tookPiece != NOPIECE)) {
// remove first, as one might come back to a square the other left
gs->board[m->toFile ][rank] = NOPIECE; // King toSqr
gs->board[m->toRank ][rank] = NOPIECE; // Rook toSqr
+ if(gs->board[m->fromRank][rank] != NOPIECE)
+ gs->holding[gs->onMove==WHITE ? 1 : 0][piecetype(gs->board[m->fromRank][rank])-1]++; // put back in holdings (onMove not flipped yet!)
+ if(gs->board[kingFromFile][rank] != NOPIECE)
+ gs->holding[gs->onMove==WHITE ? 1 : 0][piecetype(gs->board[kingFromFile][rank])-1]++; // put back in holdings (onMove not flipped yet!)
gs->board[m->fromRank][rank] = ROOK | m->color; // Rook fromSqr
gs->board[kingFromFile][rank] = KING | m->color; // King fromSquare
goto cleanupMove;
}
+ if(gs->board[m->fromFile][m->fromRank] != NOPIECE) { // [HGM] from-square occupied; move must have been Seirawan-style gating
+ gs->holding[gs->onMove==WHITE ? 1 : 0][piecetype(gs->board[m->fromFile][m->fromRank])-1]++; // put back in holdings (onMove not flipped yet!)
+ }
gs->board[m->fromFile][m->fromRank] = gs->board[m->toFile][m->toRank];
if (m->piecePromotionTo != NOPIECE) {
gs->board[m->fromFile][m->fromRank] = PAWN |