Mobility

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

outAtime
Posts: 226
Joined: Sun Mar 08, 2009 3:08 pm
Location: Canada

Re: Mobility

Post by outAtime »

anyways, its 3.5 to 0.5 for the mobility version so far.
outAtime
outAtime
Posts: 226
Joined: Sun Mar 08, 2009 3:08 pm
Location: Canada

Re: Mobility

Post by outAtime »

Hmmmm...
outAtime
outAtime
Posts: 226
Joined: Sun Mar 08, 2009 3:08 pm
Location: Canada

Re: Mobility

Post by outAtime »

dang, no mobility won a game... 1.5 - 4.5
outAtime
outAtime
Posts: 226
Joined: Sun Mar 08, 2009 3:08 pm
Location: Canada

Re: Mobility

Post by outAtime »

1.5 - 5.5
outAtime
outAtime
Posts: 226
Joined: Sun Mar 08, 2009 3:08 pm
Location: Canada

Re: Mobility

Post by outAtime »

mcostalba wrote:
outAtime wrote:Im gonna test this :
I prefer your version, is more reaadable and probably faster but....anyhow....

Code: Select all

int b_ofs[] = { 11, -11, 13, -13, 0 };
int r_ofs[] = { 12, -12,  1,  -1, 0 };

int piece_mobility(int sq, int offsets[])
{
  int p, *d = offsets, s = 0;
 
  for ( ; *d; d++)
       for (p = sq + *d; board[p] == npiece; p += *d)
           s++;
  return s;
}

int bishop_mobility(int sq){ return piece_mobility(sq, b_ofs); }
int rook_mobility(int sq)  { return piece_mobility(sq, r_ofs); }
int queen_mobility(int sq) { return piece_mobility(sq, b_ofs)
                                  + piece_mobility(sq, r_ofs); }
so queen mobility is based on rooks and bishops? sorry, but it is confusing.
outAtime
outAtime
Posts: 226
Joined: Sun Mar 08, 2009 3:08 pm
Location: Canada

Re: Mobility

Post by outAtime »

That newest mobility code idea returns a different score than the previous bishop_mobility... maybe something is missing? It is certainly nice and compact.
outAtime
outAtime
Posts: 226
Joined: Sun Mar 08, 2009 3:08 pm
Location: Canada

Re: Mobility

Post by outAtime »

Harald wrote:
Loops and rays for a knight?
Try this:

Code: Select all

/* use piece offsets to get mobility score */

int offsets = [-10, 10, -14, 14, -23, 23, -25, 25];

int knight_mobility(int squaren)
{
  int n;
  int g = 0;

  for &#40;n = 0; n < 8; ++n&#41;
    if &#40;board&#91;squaren + offsets&#91;n&#93;&#93; == npiece&#41; // or == opponent
      g++;
  return g;
&#125; 
Harald
Any idea how to define "opponent" without being clunky (many lines of code) ? I'd like to try this but keep imagining I'll need to have a white_knight_mobility and a black_knight_mobility so I can use something like black_opponent and white_opponent where e.g.

Code: Select all

int white_opponent = bpawn || bknight || bbishop || brook || bqueen || bking; 
Id really rather not have to duplicate so much code....because Id need to also have white_bishop_mobility etc... rooks and queens.. ugh
outAtime
Harald
Posts: 318
Joined: Thu Mar 09, 2006 1:07 am

Re: Mobility

Post by Harald »

outAtime wrote:
Harald wrote:
Loops and rays for a knight?
Try this:

Code: Select all

/* use piece offsets to get mobility score */

int offsets = &#91;-10, 10, -14, 14, -23, 23, -25, 25&#93;;

int knight_mobility&#40;int squaren&#41;
&#123;
  int n;
  int g = 0;

  for &#40;n = 0; n < 8; ++n&#41;
    if &#40;board&#91;squaren + offsets&#91;n&#93;&#93; == npiece&#41; // or == opponent
      g++;
  return g;
&#125; 
Harald
Any idea how to define "opponent" without being clunky (many lines of code) ? I'd like to try this but keep imagining I'll need to have a white_knight_mobility and a black_knight_mobility so I can use something like black_opponent and white_opponent where e.g.

Code: Select all

int white_opponent = bpawn || bknight || bbishop || brook || bqueen || bking; 
Id really rather not have to duplicate so much code....because Id need to also have white_bishop_mobility etc... rooks and queens.. ugh
Oh my god! Are you sure you want to learn programming C with a chess program?
Perhaps learn programming C first, at least a little bit, and then return to chess.
On the other side I like to learn new programming languages with chess modules myself.

I have many comments:

Your code for white_opponewnt won't work. You are using the wrong or-operator or even a wrong data model.
If your chess board has its pieces as ints and each type (piece and color) has only one bit set, like
wpawn = 1, wknight = 2, wbishop = 4, ..., wking = 32, bpawn = 64, bknight = 128, ..., bking = 2048;
then this may work:

Code: Select all

int white_opponent = bpawn | bknight | bbishop | brook | bqueen | bking; 
But typically the representation on such a board is more like this
int noPiece=0, border = 99;
int wpawn = 1, wknight = 2, wbishop = 3, ..., wking = 6, bpawn = -1, bknight = -2, ..., bking = -6;
And then the white opponent can be found with
if (board[sq] < 0)
The black opponent can be found with
if (board[sq] > 0 && board[sq] != border)

|| is a logical operator for truth values with 0/false or 1/true as result.
It has a shortcut evaluation that stops when the result is clear, that is
after the first true value is found.

| is a binary operator that works on bits and the result collects all set bits
of the single parts. A result != 0 may be considered true in a second step.

If your bitwise definition of white opponent is working, then you only need
to calculate it once and make the variable global that will cost no time.
a)
#define WHITE_OPPONENT (BPAWN | BKNIGHT | ... | BKING)
or b)
const int white_opponent = (bpawn | ... | bking);
and then
if (board[sq] & WHITE_OPPONENT)

Some people prefer char board[] over int board[] because this will need less space.
Find out what is best for you. The bitwise approach won't work with this.

Think about a distinction between all opponents and valuable opponents (no pawns).

Other topic (seen in another posting):
If you change a line like
for (n = 0; n < 8; ++n)
to
for (n = 0; n < 8; n++)
that does no harm because most compilers will generate the same code.
But you should know the difference and know what you are doing.
In complex C++ classes with an own ++operator there may be a difference
and often ++X should be preferred over X++.

And last not least: Try to understand the code from helping people
before you copy and paste it.

Harald
outAtime
Posts: 226
Joined: Sun Mar 08, 2009 3:08 pm
Location: Canada

Re: Mobility

Post by outAtime »

Sorry I wasn't clear. I was trying to say that is what I _don't_ want to do. I'm looking for alternatives to this sort of definition.
outAtime
outAtime
Posts: 226
Joined: Sun Mar 08, 2009 3:08 pm
Location: Canada

Re: Mobility

Post by outAtime »

Looks like my only choice for now... I can't find anything else without doing a huge re-write...This is probably wrong... although it does compile, lol.

Code: Select all

#define frame   0
#define wpawn   1
#define bpawn   2
#define wknight 3
#define bknight 4
#define wking   5
#define bking   6
#define wrook   7
#define brook   8
#define wqueen  9
#define bqueen  10
#define wbishop 11
#define bbishop 12
#define npiece  13
#define black_piece bpawn | bknight | bbishop | brook | bqueen | bking 
#define white_piece wpawn | wknight | wbishop | wrook | wqueen | wking 
#define bmob &#40;npiece | white_piece&#41;
#define wmob &#40;npiece | black_piece&#41;
where...

Code: Select all

int white_bishop_mobility&#40;int squareb&#41;
&#123;
  int p, d, bfs, s = 0;
 
  for &#40;d = 0; d < 4; d++)
  &#123;
       bfs = b_offsets&#91;d&#93;;
       for &#40;p = squareb + bfs; board&#91;p&#93; == wmob; p += bfs&#41;
           s++;
  &#125;
  return s;
&#125; 

Code: Select all

int black_bishop_mobility&#40;int squareb&#41;
&#123;
  int p, d, bfs, s = 0;
 
  for &#40;d = 0; d < 4; d++)
  &#123;
       bfs = b_offsets&#91;d&#93;;
       for &#40;p = squareb + bfs; board&#91;p&#93; == bmob; p += bfs&#41;
           s++;
  &#125;
  return s;
&#125; 
hate having to repeat code like this, but it's all I can figure out :(
outAtime