Detect draw code from crafty

Programming Topics (Computer Chess) and technical aspects as test techniques, book building, program tuning etc

Moderator: Andres Valverde

Detect draw code from crafty

Postby Dann Corbit » 11 Jan 2006, 21:33

Code: Select all
/*
 *******************************************************************************
 *                                                                             *
 *   EvaluateDraws() detects completely blocked positions as draws (hopefully).*
 *                                                                             *
 *******************************************************************************
 */

#define NODRAW       0
#define DRAW         1

int             EvaluateDraws(TREE * RESTRICT tree, int wtm)
{
    register int    square,
                    fdist,
                    rdist,
                    open = 0,
                    rank = 0;
    int             i,
                    sq,
                    blocked,
                    defenders,
                    attackers,
                    kingpath = 0,
                    rookpath = 0;
    int             noblockw[2] =
    {0, 0},         noblockb[2] =
    {
    0, 0};
    BITBOARD        temp;
    int             wp,
                    bp;

    for (i = FILEA; i <= FILEH; i++) {
        temp = file_mask[i];
        if (WhitePawns & temp) {
            square = LastOne(WhitePawns & temp);
            wp = square;
            blocked = 1;
            do {
                for (sq = square; sq < Min(square + 32, A8); sq += 8) {
                    if (SetMask(sq + 8) & BlackPieces)
                        break;
                    defenders = PopCnt(b_pawn_attacks[sq] & WhitePawns);
                    attackers = PopCnt(w_pawn_attacks[sq] & BlackPawns);
                    if (attackers > defenders)
                        break;
                    else if (attackers) {
                        blocked = 0;
                        break;
                    }
                }
                if (sq >= Min(square + 32, A8))
                    blocked = 0;
            }
            while (0);
            if (blocked) {
                if (!(plus8dir[square] & BlackPieces)) {
                    if (!TotalBlackPieces) {
                        if (Rank(square) >= RANK4)
                            blocked = 0;
                    }
                }
            }
            if (!blocked) {
                if (noblockw[0])
                    noblockw[1] = square;
                else
                    noblockw[0] = square;
            }
        } else
            wp = 0;
        if (BlackPawns & temp) {
            square = FirstOne(BlackPawns & temp);
            bp = square;
            blocked = 1;
            do {
                for (sq = square; sq > Max(square - 32, H1); sq -= 8) {
                    if (SetMask(sq - 8) & WhitePieces)
                        break;
                    attackers = PopCnt(b_pawn_attacks[sq] & WhitePawns);
                    defenders = PopCnt(w_pawn_attacks[sq] & BlackPawns);
                    if (attackers > defenders)
                        break;
                    else if (attackers) {
                        blocked = 0;
                        break;
                    }
                }
                if (sq <= Max(square - 32, H1))
                    blocked = 0;
            }
            while (0);
            if (blocked) {
                if (!(minus8dir[square] & WhitePieces)) {
                    if (!TotalWhitePieces) {
                        if (Rank(square) <= RANK5)
                            blocked = 0;
                    }
                }
            }
            if (!blocked) {
                if (noblockb[0])
                    noblockb[1] = square;
                else
                    noblockb[0] = square;
            }
        } else
            bp = 0;
        if (!kingpath) {
            if (wp && wp == bp - 8) {
                if (rank)
                    rdist = RankDistance(wp, rank);
                else
                    rdist = 0;
                rank = wp;
                if (open) {
                    if (open == 1 && rdist > 1)
                        kingpath = 1;
                    else if (open == 2 && rdist)
                        kingpath = 1;
                }
                open = 0;
            } else if (wp && SetMask(wp) & (BlackPieces << 8)) {
                if (rank)
                    rdist = RankDistance(wp, rank);
                else
                    rdist = 0;
                rank = wp;
                if (open) {
                    if (open == 1 && rdist > 1)
                        kingpath = 1;
                    else if (open == 2 && rdist)
                        kingpath = 1;
                }
                open = 0;
            } else if (bp && SetMask(bp) & (WhitePieces >> 8)) {
                if (rank)
                    rdist = RankDistance(bp - 8, rank);
                else
                    rdist = 0;
                rank = bp - 8;
                if (open) {
                    if (open == 1 && rdist > 1)
                        kingpath = 1;
                    else if (open == 2 && rdist)
                        kingpath = 1;
                }
                open = 0;
            } else
                open++;
            if (open > 2)
                kingpath = 1;
            else if (i == FILEH && open == 2)
                kingpath = 1;
        }
        if (kingpath) {
            if (WhiteMajors && BlackMajors)
                return (NODRAW);
            else if (WhiteMinors > 1 && BlackMinors > 1)
                return (NODRAW);
        }
        if (open)
            rookpath = 1;
    }
    if (!rookpath)
        return (DRAW);
    else if (!kingpath && !noblockw[0] && !noblockb[0])
        return (DRAW);
    if (noblockw[1]) {
        fdist = FileDistance(noblockw[1], noblockw[0]);
        if (WhiteMajors || WhiteMinors > 1)
            return (NODRAW);
        else if (!TotalBlackPieces) {
            if (fdist > 1)
                return (NODRAW);
            else {
                square = noblockw[1];
                if (Rank(square) >= RANK4) {
                    rdist = 7 - Rank(square);
                    fdist = FileDistance(square, BlackKingSQ);
                    if (rdist < Max(fdist, 7 - Rank(BlackKingSQ)) - (1 - wtm) ||
                        (Rank(WhiteKingSQ) >= Rank(square) && kingpath))
                        return (NODRAW);
                }
            }
        } else if (!noblockb[0] && !kingpath && !BlackMajors && BlackMinors <= 1)
            return (DRAW);
    }
    if (noblockw[0]) {
        if (WhiteMajors || WhiteMinors > 1)
            return (NODRAW);
        else if (!TotalBlackPieces) {
            square = noblockw[0];
            if (Rank(square) >= RANK4) {
                rdist = 7 - Rank(square);
                fdist = FileDistance(square, BlackKingSQ);
                if (rdist < Max(fdist, 7 - Rank(BlackKingSQ)) - (1 - wtm) ||
                    (Rank(WhiteKingSQ) >= Rank(square) && kingpath))
                    return (NODRAW);
                else if (!noblockb[0] && !kingpath && !WhiteMajors && WhiteMinors <= 1)
                    return (DRAW);
            }
        } else if (!noblockb[0] && !kingpath && !BlackMajors && BlackMinors <= 1)
            return (DRAW);
    }
    if (noblockb[1]) {
        fdist = FileDistance(noblockb[1], noblockb[0]);
        if (BlackMajors || BlackMinors > 1)
            return (NODRAW);
        else if (!TotalWhitePieces) {
            if (fdist > 1)
                return (NODRAW);
            else {
                square = noblockb[1];
                if (Rank(square) <= RANK5) {
                    fdist = FileDistance(square, WhiteKingSQ);
                    if (Rank(square) < Max(fdist, Rank(WhiteKingSQ)) - wtm ||
                        (Rank(BlackKingSQ) <= Rank(square) && kingpath))
                        return (NODRAW);
                }
            }
        } else if (!noblockw[0] && !kingpath && !WhiteMajors && WhiteMinors <= 1)
            return (DRAW);
    }
    if (noblockb[0]) {
        if (BlackMajors || BlackMinors > 1)
            return (NODRAW);
        else if (!TotalWhitePieces) {
            square = noblockb[0];
            if (Rank(square) <= RANK5) {
                fdist = FileDistance(square, WhiteKingSQ);
                if (Rank(square) < Max(fdist, Rank(WhiteKingSQ)) - wtm ||
                    (Rank(BlackKingSQ) <= Rank(square) && kingpath))
                    return (NODRAW);
                else if (!noblockw[0] && !kingpath && !BlackMajors && BlackMinors <= 1)
                    return (DRAW);
            }
        } else if (!noblockw[0] && !kingpath && !WhiteMajors && WhiteMinors <= 1)
            return (DRAW);
    } else if (!(noblockb[0] || noblockw[0])) {
        if (!TotalWhitePieces && !TotalBlackPieces) {
            if (!kingpath)
                return (DRAW);
            else if (HasOpposition(wtm, WhiteKingSQ, BlackKingSQ) &&
                     Rank(WhiteKingSQ) >= Rank(rank))
                return (NODRAW);
            else if (HasOpposition(Flip(wtm), BlackKingSQ, WhiteKingSQ) &&
                     Rank(BlackKingSQ) <= Rank(rank))
                return (NODRAW);
            else
                return (DRAW);
        } else if (!TotalWhitePieces) {
            if (kingpath && BlackMinors | BlackMajors)
                return (NODRAW);
            else if (!kingpath && (BlackMinors > 1 || BlackMajors))
                return (NODRAW);
            else
                return (DRAW);
        } else if (!TotalBlackPieces) {
            if (kingpath && WhiteMinors | WhiteMajors)
                return (NODRAW);
            else if (!kingpath && (WhiteMinors > 1 || WhiteMajors))
                return (NODRAW);
            else
                return (DRAW);
        } else if (!kingpath &&
                   WhiteMinors + PopCnt(WhiteRooks | WhiteQueens) ==
                   BlackMinors + PopCnt(BlackRooks | BlackQueens))
            return (DRAW);
        else
            return (NODRAW);
    }
    return (NODRAW);
}
Dann Corbit
 

Re: Detect draw code from crafty

Postby Pradu » 12 Jan 2006, 01:33

Can anyone make sense out of that code? It looks interesting but I can't understand more than 5% of it!
User avatar
Pradu
 
Posts: 343
Joined: 12 Jan 2005, 19:17
Location: Chandler, Arizona, USA

Re: Detect draw code from crafty

Postby Dann Corbit » 12 Jan 2006, 02:56

Pradu wrote:Can anyone make sense out of that code? It looks interesting but I can't understand more than 5% of it!


The idea is pretty simple.

First, you figure out if your pawn formation is locked.

Then you look for holes.

Worth a mention is that you don't even call this routine if you have knights, because you can always punch a hole in the pawn wall if you have knights.

Get the crafty code for 19.20 or so, compile it with DETECTDRAW defined, and then trace through the EvaluateDraw() code for these positions:
r3k3/1b4r1/2p1p3/1pPpPp1p/pP1P1PpP/P5P1/5K2/2B5 w - -
r3k3/1b4r1/2p1p3/1pP1Pp1p/pP1P1PpP/B5P1/5K2/8 w - -
3k2r1/8/3p1b2/2pPp3/1pP1Pp2/pP3Pp1/P5P1/6KR w - -
3k3r/7r/3p1b2/2pPp3/1pP1PpB1/pP3PpP/P5P1/6K1 w - -
b3k3/2r5/8/3p4/p1pPp1pP/PpP1PpP1/1B3P1K/8 w - -
b3k2b/2r3q1/6r1/3p4/p1pPp1pP/PpP1PpP1/1B3P1K/8 w - -
Dann Corbit
 

Re: Detect draw code from crafty

Postby Dann Corbit » 12 Jan 2006, 04:38

Here's a famous one:
6kr/5b1p/2p3pP/rpPp1pP1/pP1PpP2/P3P3/1K6/8 w - - am bxa5; id "Bill Hartston";
Dann Corbit
 

Re: Detect draw code from crafty

Postby eric_oldre » 12 Jan 2006, 06:24

Dann Corbit wrote:Here's a famous one:
6kr/5b1p/2p3pP/rpPp1pP1/pP1PpP2/P3P3/1K6/8 w - - am bxa5; id "Bill Hartston";


Dann, that is a fun position, I like how the bishop is positioned to push black's "breakthrough" as far past the horizon as possible. Who thinks these things up?

Eric
eric_oldre
 
Posts: 28
Joined: 14 Dec 2004, 20:42
Location: Minnetonka, Minnesota

Re: Detect draw code from crafty

Postby Dann Corbit » 12 Jan 2006, 08:09

eric_oldre wrote:
Dann Corbit wrote:Here's a famous one:
6kr/5b1p/2p3pP/rpPp1pP1/pP1PpP2/P3P3/1K6/8 w - - am bxa5; id "Bill Hartston";


Dann, that is a fun position, I like how the bishop is positioned to push black's "breakthrough" as far past the horizon as possible. Who thinks these things up?

Eric


http://www.chessbase.com/newsdetail.asp?newsid=1319
Dann Corbit
 

Re: Detect draw code from crafty

Postby Leen Ammeraal » 14 Jan 2006, 18:56

Dann Corbit wrote:
Pradu wrote:Can anyone make sense out of that code? It looks interesting but I can't understand more than 5% of it!


The idea is pretty simple.

First, you figure out if your pawn formation is locked.

Then you look for holes.

Worth a mention is that you don't even call this routine if you have knights, because you can always punch a hole in the pawn wall if you have knights.

Get the crafty code for 19.20 or so, compile it with DETECTDRAW defined, and then trace through the EvaluateDraw() code for these positions:
r3k3/1b4r1/2p1p3/1pPpPp1p/pP1P1PpP/P5P1/5K2/2B5 w - -
r3k3/1b4r1/2p1p3/1pP1Pp1p/pP1P1PpP/B5P1/5K2/8 w - -
3k2r1/8/3p1b2/2pPp3/1pP1Pp2/pP3Pp1/P5P1/6KR w - -
3k3r/7r/3p1b2/2pPp3/1pP1PpB1/pP3PpP/P5P1/6K1 w - -
b3k3/2r5/8/3p4/p1pPp1pP/PpP1PpP1/1B3P1K/8 w - -
b3k2b/2r3q1/6r1/3p4/p1pPp1pP/PpP1PpP1/1B3P1K/8 w - -


Is the second of these positions, that is,
r3k3/1b4r1/2p1p3/1pP1Pp1p/pP1P1PpP/B5P1/5K2/8 w - -
really a drawish position? My engine lets Black win as follows:
1.Ke3 Rd7
2.Kd3 Rad8
3.Bb2 a3
4.Ba1 Rxd4
5.Bxd4 Rxd4
Leen Ammeraal
User avatar
Leen Ammeraal
 
Posts: 63
Joined: 14 Oct 2004, 19:46

Re: Detect draw code from crafty

Postby Pradu » 14 Jan 2006, 21:18

Yes some of them are not drawn. But they are just some test positions.
User avatar
Pradu
 
Posts: 343
Joined: 12 Jan 2005, 19:17
Location: Chandler, Arizona, USA

Re: Detect draw code from crafty

Postby smcracraft » 25 Feb 2006, 07:28

Dann Corbit wrote:Here's a famous one:
6kr/5b1p/2p3pP/rpPp1pP1/pP1PpP2/P3P3/1K6/8 w - - am bxa5; id "Bill Hartston";


I fed this to teedee but have nothing conclusive.

.st 999999
maxdepth = 9999999 maxtime = 99999900 timed = 1
.ts
position file? [wac.epd] tests.epd
# of test positions to test? 1
maxtime = 99999900
Interrupt current ply and return move at timeout
Testsuite: Tests/tests.epd 1 positions
*** Problem Solution(s): bxa5 (am)
-- ** -- ** -- ** BK BR
** -- ** -- ** BB ** BP
-- ** BP ** -- ** BP WP
BR BP WP BP ** BP WP --
BP WP -- WP BP WP -- **
WP -- ** -- WP -- ** --
-- WK -- ** -- ** -- **
** -- ** -- ** -- ** --
mv 1 stage 0, white to move, computer plays white
hash=ed811e622451814d
pawnhash=2d1f22df240b0fa4
0 0 0 0 0 0
Alpha=-13225 Beta=-12424 Maxdepth=9999999 MaxTime=99999900 xboard=0
Itr/Max Mv Score Time Nodes PV
1/ 1> b4a5 -12424 0.00 2 b4a5
1/ 1 b4a5 -8863 0.00 10 b4a5
2/ 4 b4a5 -9021 0.00 40 b4a5 g8f8
3/ 4> b4a5 -8620 0.00 51 b4a5 g8f8 b2c3
3/ 6 b4a5 -6319 0.01 141 b4a5 f7e6 b2a1
4/ 6 b4a5 -5939 0.01 318 b4a5 b5b4 b2a1
5/ 6< b4a5 -6339 0.01 377 b4a5 b5b4 b2a1 f7e6 a3b4
5/ 7 b4a5 -6339 0.01 424 b4a5 b5b4 b2a1 b4b3
6/ 7> b4a5 -5938 0.02 464 b4a5 b5b4 b2a1 b4a3 mtmt
6/ 7 b4a5 -5938 0.02 553 b4a5 b5b4 b2a1 b4a3 mtmt
7/12< b4a5 -6338 0.02 944 b4a5 b5b4 b2a1 b4a3 mtmt
7/12 b2c3 -6338 0.03 1428 b2c3 f7e6 mtmt
8/18> b2c3 -5937 0.04 2761 b2c3 f7e6 b4a5 g8f7 c3d2 f7e7 d2c3
8/18 b4a5 -5936 0.04 3231 b4a5 f7e6 b2a1 g8f7 a5a6 f7e7
9/18> b4a5 -5535 0.05 3467 b4a5 f7e6 b2a1 g8f7 a5a6 f7e7 a1b2 h8b8 b2c3
9/18 b4a5 -5535 0.06 4467 b4a5 f7e6 b2a1 b5b4 a5a6 b4b3 a1b2 mtmt
10/18< b4a5 -5935 0.06 5040 b4a5 f7e6 b2a1 b5b4 a5a6 b4b3 a1b2 g8f7 b2c3 f7e7
10/18 b4a5 -5935 0.07 5359 b4a5 f7e6 b2a1 b5b4 a5a6 b4b3 a1b2 g8f8 b2c3
11/18> b4a5 -5534 0.09 9169 b4a5 f7e6 b2c3 b5b4 c3b4 g8f7 mtmt
11/18 b4a5 -5534 0.13 13584 b4a5 f7e6 b2c3 b5b4 c3b4 g8f7 mtmt
12/26 b2c3 -5933 0.29 32279 b2c3 g8f8 b4a5 f8e7 c3b4 mtmt
13/26 b4a5 -6166 0.61 75037 b4a5 f7e6 a5a6 g8f7 b2c1 h8a8 c1c2 mtmt
14/26> b4a5 -5765 0.81 102141 b4a5 f7e6 a5a6 g8f7 b2c1 h8a8 c1b2 a8a7 b2c3 f7e7 c3d2 a7a6 d2e1 mtmt
14/37 b4a5 -5764 3.03 363348 b4a5 b5b4 a3b4 f7e6 b2c1 g8f7 c1b2 h8a8
15/37< b4a5 -6164 4.87 580215 b4a5 b5b4 a3b4 f7e6 b2c3 a4a3 b4b5 c6b5 mtmt
15/37 b2c3 -6164 5.85 702822 b2c3 g8f8 b4a5 b5b4 a3b4 a4a3 a5a6 a3a2 c3d2 a2a1Q a6a7 mtmt
16/37> b2c3 -5763 10.58 1297126 b2c3 g8f8 b4a5 b5b4 a3b4 a4a3 c3b3 f7e6 a5a6 e6c8 b3a3 c8d7
16/37 b4a5 -5763 11.47 1404016 b4a5 f7e6 b2c1 b5b4 a3b4 g8f7 a5a6 a4a3 mtmt
17/47 b2c1 -6161 33.06 3701956 b2c1 f7e6 c1d1 mtmt
18/54 b4a5 -6559 72.83 7573572 b4a5 f7e6 b2a1 b5b4 a3b4 g8f7 a5a6
19/54 b2c2 -6559 102.82 10711163 b2c2 f7e6 c2c3 mtmt
20/
smcracraft
 
Posts: 65
Joined: 15 Jan 2006, 05:38


Return to Programming and Technical Discussions

Who is online

Users browsing this forum: No registered users and 18 guests