* Massachusetts.
*
* Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
- * 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
+ * 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Free Software Foundation, Inc.
*
* Enhancements Copyright 2005 Alessandro Scotti
*
#define SameColor(piece1, piece2) (piece1 < EmptySquare && piece2 < EmptySquare && (piece1 < BlackPawn) == (piece2 < BlackPawn) || piece1 == DarkSquare || piece2 == DarkSquare)
#endif
-char pieceToChar[] = {
+unsigned char pieceToChar[EmptySquare+1] = {
'P', 'N', 'B', 'R', 'Q', 'F', 'E', 'A', 'C', 'W', 'M',
'O', 'H', 'I', 'J', 'G', 'D', 'V', 'L', 'S', 'U', 'K',
'p', 'n', 'b', 'r', 'q', 'f', 'e', 'a', 'c', 'w', 'm',
'o', 'h', 'i', 'j', 'g', 'd', 'v', 'l', 's', 'u', 'k',
'x' };
-char pieceNickName[EmptySquare];
+unsigned char pieceNickName[EmptySquare];
char
PieceToChar (ChessSquare p)
{
+ int c;
if((int)p < 0 || (int)p >= (int)EmptySquare) return('x'); /* [HGM] for safety */
- return pieceToChar[(int) p];
+ c = pieceToChar[(int) p];
+ if(c & 128) c = c & 63 | 64;
+ return c;
+}
+
+char
+PieceSuffix (ChessSquare p)
+{
+ int c;
+ if((int)p < 0 || (int)p >= (int)EmptySquare) return 0; /* [HGM] for safety */
+ c = pieceToChar[(int) p];
+ if(c < 128) return 0;
+ return SUFFIXES[c - 128 >> 6];
}
int
for(dir=0, bit=1; dir<8; dir++, bit += bit) { // loop over directions
int i = expo, j = skip, hop = mode, vx, vy, loop = 0;
if(!(bit & dirSet)) continue; // does not move in this direction
- if(dy != 1) j = 0; //
+ if(dy != 1 || mode & 1024) j = 0; //
vx = dx*rot[dir][0] + dy*rot[dir][1]; // rotate step vector
vy = dx*rot[dir][2] + dy*rot[dir][3];
if(tx < 0) x = f, y = r; // start square
if(y < 0 || y >= BOARD_HEIGHT) break; // vertically off-board: always done
if(x < BOARD_LEFT) { if(mode & 128) x += BOARD_RGHT - BOARD_LEFT, loop++; else break; }
if(x >= BOARD_RGHT) { if(mode & 128) x -= BOARD_RGHT - BOARD_LEFT, loop++; else break; }
- if(board[y][x] == DarkSquare) break; // black squares are supposed to be off board
if(j) { j--; continue; } // skip irrespective of occupation
+ if(board[y][x] == DarkSquare) break; // black squares are supposed to be off board
if(!jump && board[y - vy + vy/2][x - vx + vx/2] != EmptySquare) break; // blocked
if(jump > 1 && board[y - vy + vy/2][x - vx + vx/2] == EmptySquare) break; // no hop
if(x == f && y == r && !loop) occup = 4; else // start square counts as empty (if not around cylinder!)
if(mode & 1024) { // castling
i = 2; // kludge to elongate move indefinitely
if(occup == 4) continue; // skip empty squares
- if((x == BOARD_LEFT || vx < 0 && board[y][x-1] == DarkSquare) && board[y][x] == initialPosition[y][x]) // reached initial corner piece
+ if((x == BOARD_LEFT + skip || x > BOARD_LEFT + skip && vx < 0 && board[y][x-1-skip] == DarkSquare)
+ && board[y][x] == initialPosition[y][x]) { // reached initial corner piece
+ if(pc != WhiteKing && pc != BlackKing) { // non-royal castling (to be entered as two-leg move via 'Rook')
+ if(killX < 0) cb(board, flags, FirstLeg, r, f, y, x, cl); if(killX < f)
+ legNr <<= 1, cb(board, flags, NormalMove, r, f, y, f - expo, cl), legNr >>= 1;
+ } else
cb(board, flags, mine == 1 ? WhiteQueenSideCastle : BlackQueenSideCastle, r, f, y, f - expo, cl);
- if((x == BOARD_RGHT-1 || vx > 0 && board[y][x+1] == DarkSquare) && board[y][x] == initialPosition[y][x])
+ }
+ if((x == BOARD_RGHT-1-skip || x < BOARD_RGHT-1-skip && vx > 0 && board[y][x+1+skip] == DarkSquare)
+ && board[y][x] == initialPosition[y][x]) {
+ if(pc != WhiteKing && pc != BlackKing) {
+ if(killX < 0) cb(board, flags, FirstLeg, r, f, y, x, cl); if(killX > f)
+ legNr <<= 1, cb(board, flags, NormalMove, r, f, y, f + expo, cl), legNr >>= 1;
+ } else
cb(board, flags, mine == 1 ? WhiteKingSideCastle : BlackKingSideCastle, r, f, y, f + expo, cl);
+ }
break;
}
if(mode & 16 && (board[y][x] == WhiteKing || board[y][x] == BlackKing)) break; // tame piece, cannot capture royal
}
ep = board[EP_STATUS];
if( captured == WhiteLion || captured == BlackLion ) { // [HGM] lion: Chu Lion-capture rules
- ChessSquare victim = killX < 0 ? EmptySquare : trampled;
+ ChessSquare victim = saveKill < 0 ? EmptySquare : trampled;
if( (board[rt][ft] == WhiteLion || board[rt][ft] == BlackLion) && // capturer is Lion
(ff - ft > 1 || ft - ff > 1 || rf - rt > 1 || rt - rf > 1) && // captures from a distance
- (victim == EmptySquare || victim == WhitePawn || victim == BlackPawn) ) // no or worthless 'bridge'
+ (victim == EmptySquare || victim == WhitePawn || victim == BlackPawn // no or worthless 'bridge'
+ || victim == WhiteCobra || victim == BlackCobra) ) // (Pawn or Go Between)
board[EP_STATUS] = EP_ROYAL_LION; // on distant Lion x Lion victim must not be pseudo-legally protected
}
}
board[rf][ft] = captured;
board[rt][ft] = EmptySquare;
} else {
- if(saveKill >= 0) board[killY][killX] = trampled, killX = saveKill;
+ if(saveKill >= 0) board[killY][killX = saveKill] = trampled;
board[rt][ft] = captured;
}
board[EP_STATUS] = ep;
if(cl.kind != NormalMove || promoChar == NULLCHAR || promoChar == '=') return cl.kind;
if(promoChar != '+')
return CharToPiece(promoChar) == EmptySquare ? ImpossibleMove : IllegalMove;
- if(PieceToChar(CHUPROMOTED board[rf][ff]) != '+') return ImpossibleMove;
+ if(PieceToChar(CHUPROMOTED board[rf][ff]) != '+') {
+ if(PieceToChar(CHUPROMOTED (board[rf][ff] < BlackPawn ? WhitePawn : BlackPawn)) != '.')
+ return ImpossibleMove;
+ }
return flags & F_WHITE_ON_MOVE ? WhitePromotion : BlackPromotion;
} else
if(gameInfo.variant == VariantShogi) {
/* [HGM] Shogi promotions. '=' means defer */
if(rf != DROP_RANK && cl.kind == NormalMove) {
ChessSquare piece = board[rf][ff];
+ int zone = BOARD_HEIGHT/3 + (BOARD_HEIGHT == 8);
if(promoChar == PieceToChar(BlackQueen)) promoChar = NULLCHAR; /* [HGM] Kludge */
if(promoChar == 'd' && (piece == WhiteRook || piece == BlackRook) ||
return CharToPiece(promoChar) == EmptySquare ? ImpossibleMove : IllegalMove;
else if(flags & F_WHITE_ON_MOVE) {
if( (int) piece < (int) WhiteWazir &&
- (rf >= BOARD_HEIGHT - BOARD_HEIGHT/3 || rt >= BOARD_HEIGHT - BOARD_HEIGHT/3) ) {
+ (rf >= BOARD_HEIGHT - zone || rt >= BOARD_HEIGHT - zone) ) {
if( (piece == WhitePawn || piece == WhiteQueen) && rt > BOARD_HEIGHT-2 ||
piece == WhiteKnight && rt > BOARD_HEIGHT-3) /* promotion mandatory */
cl.kind = promoChar == '=' ? IllegalMove : WhitePromotion;
cl.kind = promoChar == '+' ? WhitePromotion : WhiteNonPromotion;
} else cl.kind = promoChar == '+' ? IllegalMove : NormalMove;
} else {
- if( (int) piece < (int) BlackWazir && (rf < BOARD_HEIGHT/3 || rt < BOARD_HEIGHT/3) ) {
+ if( (int) piece < (int) BlackWazir && (rf < zone || rt < zone) ) {
if( (piece == BlackPawn || piece == BlackQueen) && rt < 1 ||
piece == BlackKnight && rt < 2 ) /* promotion obligatory */
cl.kind = promoChar == '=' ? IllegalMove : BlackPromotion;
{
register DisambiguateClosure *cl = (DisambiguateClosure *) closure;
int wildCard = FALSE; ChessSquare piece = board[rf][ff];
+ extern int kifu; // in parser.c
// [HGM] wild: for wild-card pieces rt and rf are dummies
if(piece == WhiteFalcon || piece == BlackFalcon ||
if(cl->count && rf == cl->rf && ff == cl->ff) return; // duplicate move
+ if(cl->count == 1 && kifu & 0x7E && cl->rfIn == -1 && cl->ffIn == -1) { // traditional Shogi disambiguation required
+ int this = 1, other = 1;
+ if(kifu & 2) this &= (flags & 1 ? rt > rf : rt < rf), other &= (flags & 1 ? cl->rt > cl->rf : cl->rt < cl->rf);
+ if(kifu & 4) this &= (flags & 1 ? rt < rf : rt > rf), other &= (flags & 1 ? cl->rt < cl->rf : cl->rt > cl->rf);
+ if(kifu & 8) this &= (rf == rt), other &= (cl->rt == cl->rf);
+ if(kifu & 0x10) this &= (flags & 1 ? ft <= ff : ft >= ff), other &= (flags & 1 ? cl->ft <= cl->ff : cl->ft >= cl->ff);
+ if(kifu & 0x20) this &= (flags & 1 ? ft >= ff : ft <= ff), other &= (flags & 1 ? cl->ft >= cl->ff : cl->ft <= cl->ff);
+ if(kifu & 0x40) this &= (ft == ff), other &= (cl->ft == cl->ff); // should never be used
+ if(!other) cl->count--; // the old move did not satisfy the requested relative position, erase it
+ if(!this) return; // the current move does not satisfy the requested relative position, ignore it
+ }
+
cl->count++;
if(cl->count == 1 || board[rt][ft] != EmptySquare) {
// [HGM] oneclick: if multiple moves, be sure we remember capture
GenLegal(board, flags|F_IGNORE_CHECK, DisambiguateCallback, (VOIDSTAR) closure, closure->pieceIn);
if (closure->count == 0) {
/* No, it's not even that */
- if(!appData.testLegality && closure->pieceIn != EmptySquare) {
+ if(!appData.testLegality && !pieceDefs && closure->pieceIn != EmptySquare) {
int f, r; // if there is only a single piece of the requested type on the board, use that
closure->rt = closure->rtIn, closure->ft = closure->ftIn;
for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++)
/* [HGM] Shogi promotions. On input, '=' means defer, '+' promote. Afterwards, c is set to '+' for promotions, NULL other */
if(closure->rfIn != DROP_RANK && closure->kind == NormalMove) {
ChessSquare piece = closure->piece;
+ int zone = BOARD_HEIGHT/3 + (BOARD_HEIGHT == 8);
if (c == 'd' && (piece == WhiteRook || piece == BlackRook) ||
c == 'h' && (piece == WhiteBishop || piece == BlackBishop) ||
c == 'g' && (piece <= WhiteFerz || piece <= BlackFerz && piece >= BlackPawn) )
if(c != NULLCHAR && c != '+' && c != '=') closure->kind = IllegalMove; // otherwise specifying a piece is illegal
else if(flags & F_WHITE_ON_MOVE) {
if( (int) piece < (int) WhiteWazir &&
- (closure->rf >= BOARD_HEIGHT-(BOARD_HEIGHT/3) || closure->rt >= BOARD_HEIGHT-(BOARD_HEIGHT/3)) ) {
+ (closure->rf >= BOARD_HEIGHT-zone || closure->rt >= BOARD_HEIGHT-zone) ) {
if( (piece == WhitePawn || piece == WhiteQueen) && closure->rt > BOARD_HEIGHT-2 ||
piece == WhiteKnight && closure->rt > BOARD_HEIGHT-3) /* promotion mandatory */
closure->kind = c == '=' ? IllegalMove : WhitePromotion;
closure->kind = c == '+' ? WhitePromotion : WhiteNonPromotion;
} else closure->kind = c == '+' ? IllegalMove : NormalMove;
} else {
- if( (int) piece < (int) BlackWazir && (closure->rf < BOARD_HEIGHT/3 || closure->rt < BOARD_HEIGHT/3) ) {
+ if( (int) piece < (int) BlackWazir && (closure->rf < zone || closure->rt < zone) ) {
if( (piece == BlackPawn || piece == BlackQueen) && closure->rt < 1 ||
piece == BlackKnight && closure->rt < 2 ) /* promotion obligatory */
closure->kind = c == '=' ? IllegalMove : BlackPromotion;
if (closure->kind == WhitePromotion || closure->kind == BlackPromotion) {
if(c == NULLCHAR) { // missing promoChar on mandatory promotion; use default for variant
if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier ||
- gameInfo.variant == VariantMakruk || gameInfo.variant == VariantASEAN)
+ gameInfo.variant == VariantMakruk)
c = PieceToChar(BlackFerz);
+ else if(gameInfo.variant == VariantASEAN)
+ c = PieceToChar(BlackRook);
else if(gameInfo.variant == VariantGreat)
c = PieceToChar(BlackMan);
else if(gameInfo.variant == VariantGrand)
}
if(c=='+') *outp++ = c;
*outp++ = ToUpper(PieceToChar(piece));
+ if(*outp = PieceSuffix(piece)) outp++;
if (cl.file || (cl.either && !cl.rank)) {
*outp++ = ff + AAA;