Distance to King Formula - J-Value

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

jhaglund
Posts: 173
Joined: Sun May 11, 2008 7:43 am

Distance to King Formula - J-Value

Post by jhaglund »

Here's my program I made that could be helpful to some people. It calculates the distance to king, j-value, from an attacking square.

I used ICCF Numeric Chess Notation.


// test example
//83 - 21

//str[0] = 8;
//str[1] = 3;
//strk[0] = 2;
//strk[1] = 1;

//ga = 8
//gb = 3
//la = 2
//lb = 1

83-21 (h3 - b1) = 6.0408


Comments?

Joshua D. Haglund

/*
J-Value v1.0
by Joshua D. Haglund
January 16th 2010

Joshua-Value is the distance to king, in # of squares, from attacker piece.

Joshua was the king of kings.

*/

#include <stdio.h>
#include <string>
#include <iostream>

using namespace std;


int main()
{


cout << " " << endl;

string str;
string strk;

float jvalue;
float a,b,c;
float la,ga,lb,gb,va,vb;


cout << " Enter Square of Attacker Piece: ";
cin >> str;
cout << " Enter Square of Enemy King: ";
cin >> strk;

ga = str[0];
gb = str[1];

la = strk[0];
lb = strk[1];

a = gb - lb;
b = lb;
if(lb > gb)
a = lb - gb;
c = a / b;
va = ga - la;
vb = gb - lb;

if(ga < la)
va = la - ga;
if(ga > la)
va = ga - la;

if(gb < lb)
vb = lb - gb;
if(gb > lb)
vb = gb - lb;

if(va > 1 || vb >= 1)
jvalue = va;
if(vb > 1 || va >= 1)
jvalue = vb;
if(va > 1 || vb == 1)
jvalue = va;
if(va > 1 || vb == 0)
jvalue = va;
if(vb > 1 || va == 1)
jvalue = va;
if(vb > 1 || va == 0)
jvalue = vb;

if(va == 0 || vb > 0)
jvalue = vb;

if(va == 1 || vb > 1)
jvalue = vb+c;
if(vb == 1 || va > 1)
jvalue = va+c;

if(va == vb)
jvalue = va;
if(vb == va)
jvalue = vb;

if(va == 0)
jvalue = vb;
if(vb == 0)
jvalue = va;

cout.precision(5);
cout.width(5);
cout << " J-Value to King: " << jvalue << endl;

main();

system("pause");
return 0;
}
jhaglund
Posts: 173
Joined: Sun May 11, 2008 7:43 am

Re: Distance to King Formula - J-Value

Post by jhaglund »

Bob, you should try this in Crafty...

Add a bonus based on distance to opposing king. It can be used for other pieces as well.

Smaller distance, = bigger bonus.

bonus[7] = {-7, -6, -5, -4, -3 ,-2 ,-1 , 0};
scale = 8 - jvalue;
if(alpha_bonus >= beta_bonus)
eval =+ bonus[scale]; // 8 squares - distance
return beta_bonus;
else
if(alpha_bonus <= beta_bonus)
eval =+ bonus[scale];
return alpha_bonus;

It can used to order moves, attacking the king & other pieces, & scoring for eval piece tables. Well you get the idea...
vladstamate
Posts: 161
Joined: Thu Jan 08, 2009 9:06 pm
Location: San Francisco, USA

Re: Distance to King Formula - J-Value

Post by vladstamate »

Hi,

I am confused. Why is the value 6.0408 and not 6? The squares distance between b1 and h3 is 6 squares.

Regards,
Vlad.
jhaglund
Posts: 173
Joined: Sun May 11, 2008 7:43 am

Re: Distance to King Formula - J-Value

Post by jhaglund »

Hi,

I am confused. Why is the value 6.0408 and not 6? The squares distance between b1 and h3 is 6 squares.

Regards,
Vlad.
It's not on the same rank or file, making it a bit longer because you travel diagonally to the next rank. Think of it kinda like a^2 + b^2 = c^2 but counting in squares

abcdefgh
00000000 8
00000000 7
00000000 6
00000000 5
00000000 4
00000001 3
00000000 2
01000000 1
jhaglund
Posts: 173
Joined: Sun May 11, 2008 7:43 am

Re: Distance to King Formula - J-Value

Post by jhaglund »

bonus[7] = {-7, -6, -5, -4, -3 ,-2 ,-1 , 0};
backwards....

more like:

bonus[7] = { 21, 18, 15, 12, 9, 6, 3, 0};
vladstamate
Posts: 161
Joined: Thu Jan 08, 2009 9:06 pm
Location: San Francisco, USA

Re: Distance to King Formula - J-Value

Post by vladstamate »

Hi Joshua,
It's not on the same rank or file, making it a bit longer because you travel diagonally to the next rank. Think of it kinda like a^2 + b^2 = c^2 but counting in squares
I understand now, thanks.

Regards,
Vlad.
User avatar
Kempelen
Posts: 620
Joined: Fri Feb 08, 2008 10:44 am
Location: Madrid - Spain

Re: Distance to King Formula - J-Value

Post by Kempelen »

jhaglund wrote:Here's my program I made that could be helpful to some people. It calculates the distance to king, j-value, from an attacking square.

I used ICCF Numeric Chess Notation.


// test example
//83 - 21

//str[0] = 8;
//str[1] = 3;
//strk[0] = 2;
//strk[1] = 1;

//ga = 8
//gb = 3
//la = 2
//lb = 1

83-21 (h3 - b1) = 6.0408


Comments?

Joshua D. Haglund

Not sure, but maybe work with decimal numbers need more CPU time and could be counterproductive. Your idea need testing in order to know if it is worthy
Fermin Serrano
Author of 'Rodin' engine
http://sites.google.com/site/clonfsp/
jhaglund
Posts: 173
Joined: Sun May 11, 2008 7:43 am

Re: Distance to King Formula - J-Value

Post by jhaglund »

Your idea need testing in order to know if it is worthy
I guess since no one else likes to take the initiative, I'll have to do this too...