I have read that some computer chess engines will use the distance to king as a variable in the Evaluation Function.
Does anyone know an efficient way of calculating this distance?
Distance to King
Moderators: hgm, Rebel, chrisw
-
- Posts: 27796
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: Distance to King
Look it up in a table indexed by (square - kingSquare).
Re: Distance to King
I am not sure that works for me. I have the board indexed like so:
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31
32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63
So by simply subtracting the values 24 woud be right next to 23 which is not true.
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31
32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63
So by simply subtracting the values 24 woud be right next to 23 which is not true.
-
- Posts: 317
- Joined: Mon Jun 26, 2006 9:44 am
Re: Distance to King
For whatever reason, HG was assuming a 0x88 representation where the square difference index works. For more information, see
http://chessprogramming.wikispaces.com/0x88
http://chessprogramming.wikispaces.com/0x88
-
- Posts: 27796
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: Distance to King
This is why it is better to use 0x88 numbering:
You can convert one into the other by nr0x88 = nrPlain + (nrPlain & 070); or of course use another table lookup for that, nr0x88 = table[nrPlain].
Code: Select all
00 01 02 03 04 05 06 07
16 17 18 19 20 21 22 23
32 33 34 35 ....
-
- Posts: 568
- Joined: Tue Dec 12, 2006 10:10 am
- Full name: Gary Linscott
Re: Distance to King
Well, it depends on what distance you want as well . The dumb method is still pretty fast for king walk distance:
distance = abs(file(king) - file(piece)) + abs(rank(king) - rank(piece));
Calculating file/rank is trivial:
file = index & 7;
rank = index >> 3;
distance = abs(file(king) - file(piece)) + abs(rank(king) - rank(piece));
Calculating file/rank is trivial:
file = index & 7;
rank = index >> 3;
-
- Posts: 27796
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: Distance to King
Your King cannot walk diagonally?
-
- Posts: 792
- Joined: Wed Jul 19, 2006 9:58 am
Re: Distance to King
True, but you can precalculate the table at engine startup using the long way round (shown below) and reference the table when eval is called.
Richard
Richard
-
- Posts: 5228
- Joined: Thu Mar 09, 2006 9:40 am
- Full name: Vincent Lejeune
Re: Distance to King
LOL !hgm wrote:Your King cannot walk diagonally?
I'd say : Max(Abs(FileKingX-FileTargetX) , Abs(FileKingY-FileTargetY)) .
So it's the maximum distance in vertical/horizontal
-
- Posts: 20943
- Joined: Mon Feb 27, 2006 7:30 pm
- Location: Birmingham, AL
Re: Distance to King
orhgm wrote:Look it up in a table indexed by (square - kingSquare).
distance = Max( abs(Rank(king) - rank(square)), abs(file(king) - file(square)));
That computes the so-called "taxicab distance" You take the largest of the vertical and horizontal distances.